]>
Commit | Line | Data |
---|---|---|
7ed73705 JR |
1 | // Kelsier project - Logging code (Logger.cs) |
2 | // Written by the Jobbig codeteam. <http://jobbig.eu/code/> | |
3 | // Copyright 2013 John Runyon. | |
4 | // | |
5 | // This file is part of the Kelsier project. | |
6 | // | |
7 | // Kelsier is free software: you can redistribute it and/or modify | |
8 | // it under the terms of the GNU Affero General Public License as published by | |
9 | // the Free Software Foundation, either version 3 of the License, or | |
10 | // (at your option) any later version. | |
11 | // | |
12 | // This program is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU Affero General Public License for more details. | |
16 | // | |
17 | // You should have received a copy of the GNU Affero General Public License | |
18 | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
169f10ae | 19 | // |
7ed73705 JR |
20 | |
21 | using System; | |
88ae06a4 JR |
22 | using System.Collections.Generic; |
23 | using System.Linq; | |
24 | using System.Text; | |
25 | using System.IO; | |
26 | ||
e5993b94 JR |
27 | namespace Kelsier.Common { |
28 | public class Logger { | |
29 | public LogOutput[] debugs { get; private set; } | |
12e0e204 JR |
30 | public LogOutput[] infos { get; private set; } |
31 | public LogOutput[] errors { get; private set; } | |
32 | public string from { get; private set; } | |
a3c84b90 JR |
33 | |
34 | /// <summary> | |
35 | /// Construct a Logger, writing to the specified TextWriter objects. Use an empty array to disable an output. | |
36 | /// </summary> | |
37 | /// <param name="debugs">Array of TextWriter's to use for debug output</param> | |
38 | /// <param name="infos">...for info output</param> | |
39 | /// <param name="errors">...for error output</param> | |
12e0e204 JR |
40 | public Logger(string from, LogOutput[] debugs, LogOutput[] infos, LogOutput[] errors) { |
41 | int fromid; | |
42 | if (Int32.TryParse(from, out fromid)) { | |
43 | this.from = fromid.ToString("0000"); | |
44 | } else { | |
45 | this.from = from; | |
46 | } | |
47 | ||
88ae06a4 JR |
48 | this.debugs = debugs; |
49 | this.infos = infos; | |
50 | this.errors = errors; | |
51 | ||
12e0e204 JR |
52 | if (from == "Root") { |
53 | this.debug("Starting Root logging (debug) here at "+DateTime.Now.ToString("F")); | |
54 | this.info("Starting Root logging (info) here at "+DateTime.Now.ToString("F")); | |
55 | this.error("Starting Root logging (error) here at "+DateTime.Now.ToString("F")); | |
56 | } | |
88ae06a4 | 57 | } |
12e0e204 JR |
58 | public Logger(string from, Logger copy) { |
59 | this.from = from; | |
88ae06a4 | 60 | |
12e0e204 JR |
61 | this.debugs = copy.debugs; |
62 | this.infos = copy.infos; | |
63 | this.errors = copy.infos; | |
88ae06a4 JR |
64 | } |
65 | ||
a3c84b90 JR |
66 | /// <summary> |
67 | /// Debug logging. | |
68 | /// </summary> | |
69 | /// <param name="msg">Log message (with formatting placeholders, if desired)</param> | |
70 | /// <param name="args">Formatting replacements.</param> | |
12e0e204 JR |
71 | public void debug(object msg) { |
72 | foreach (LogOutput outw in debugs) { | |
73 | if (outw.open) | |
74 | outw.writer.WriteLine("(D@" + DateTime.Now.ToString("HH:mm:ss") + ")[" + from + "] " + msg.ToString()); | |
88ae06a4 JR |
75 | } |
76 | } | |
77 | ||
a3c84b90 JR |
78 | /// <summary> |
79 | /// Info logging. | |
80 | /// </summary> | |
81 | /// <param name="msg">Log message (with formatting placeholders, if desired)</param> | |
82 | /// <param name="args">Formatting replacements.</param> | |
12e0e204 JR |
83 | public void info(object msg) { |
84 | foreach (LogOutput outw in infos) { | |
85 | if (outw.open) | |
86 | outw.writer.WriteLine("(I@" + DateTime.Now.ToString("HH:mm:ss") + ")[" + from + "] " + msg.ToString()); | |
88ae06a4 JR |
87 | } |
88 | } | |
89 | ||
a3c84b90 JR |
90 | /// <summary> |
91 | /// Error logging. | |
92 | /// </summary> | |
93 | /// <param name="msg">Log message (with formatting placeholders, if desired)</param> | |
94 | /// <param name="args">Formatting replacements.</param> | |
12e0e204 JR |
95 | public void error(object msg) { |
96 | foreach (LogOutput outw in errors) { | |
97 | if (outw.open) | |
98 | outw.writer.WriteLine("(E@" + DateTime.Now.ToString("HH:mm:ss") + ")[" + from + "] " + msg.ToString()); | |
88ae06a4 JR |
99 | } |
100 | } | |
101 | } | |
12e0e204 | 102 | |
e5993b94 | 103 | public class LogOutput { |
12e0e204 JR |
104 | public TextWriter writer { get; private set; } |
105 | public bool open; | |
106 | public LogOutput(TextWriter writer) { | |
107 | this.writer = writer; | |
108 | this.open = true; | |
109 | } | |
110 | } | |
88ae06a4 | 111 | } |