]>
Commit | Line | Data |
---|---|---|
88ae06a4 JR |
1 | using System; |
2 | using System.Collections.Generic; | |
3 | using System.Linq; | |
4 | using System.Text; | |
5 | using System.IO; | |
6 | ||
a3c84b90 JR |
7 | namespace Kelsier { |
8 | class Logger { | |
12e0e204 JR |
9 | public LogOutput[] debugs {get; private set; } |
10 | public LogOutput[] infos { get; private set; } | |
11 | public LogOutput[] errors { get; private set; } | |
12 | public string from { get; private set; } | |
a3c84b90 JR |
13 | |
14 | /// <summary> | |
15 | /// Construct a Logger, writing to the specified TextWriter objects. Use an empty array to disable an output. | |
16 | /// </summary> | |
17 | /// <param name="debugs">Array of TextWriter's to use for debug output</param> | |
18 | /// <param name="infos">...for info output</param> | |
19 | /// <param name="errors">...for error output</param> | |
12e0e204 JR |
20 | public Logger(string from, LogOutput[] debugs, LogOutput[] infos, LogOutput[] errors) { |
21 | int fromid; | |
22 | if (Int32.TryParse(from, out fromid)) { | |
23 | this.from = fromid.ToString("0000"); | |
24 | } else { | |
25 | this.from = from; | |
26 | } | |
27 | ||
88ae06a4 JR |
28 | this.debugs = debugs; |
29 | this.infos = infos; | |
30 | this.errors = errors; | |
31 | ||
12e0e204 JR |
32 | if (from == "Root") { |
33 | this.debug("Starting Root logging (debug) here at "+DateTime.Now.ToString("F")); | |
34 | this.info("Starting Root logging (info) here at "+DateTime.Now.ToString("F")); | |
35 | this.error("Starting Root logging (error) here at "+DateTime.Now.ToString("F")); | |
36 | } | |
88ae06a4 | 37 | } |
12e0e204 JR |
38 | public Logger(string from, Logger copy) { |
39 | this.from = from; | |
88ae06a4 | 40 | |
12e0e204 JR |
41 | this.debugs = copy.debugs; |
42 | this.infos = copy.infos; | |
43 | this.errors = copy.infos; | |
88ae06a4 JR |
44 | } |
45 | ||
a3c84b90 JR |
46 | /// <summary> |
47 | /// Debug logging. | |
48 | /// </summary> | |
49 | /// <param name="msg">Log message (with formatting placeholders, if desired)</param> | |
50 | /// <param name="args">Formatting replacements.</param> | |
12e0e204 JR |
51 | public void debug(object msg) { |
52 | foreach (LogOutput outw in debugs) { | |
53 | if (outw.open) | |
54 | outw.writer.WriteLine("(D@" + DateTime.Now.ToString("HH:mm:ss") + ")[" + from + "] " + msg.ToString()); | |
88ae06a4 JR |
55 | } |
56 | } | |
57 | ||
a3c84b90 JR |
58 | /// <summary> |
59 | /// Info logging. | |
60 | /// </summary> | |
61 | /// <param name="msg">Log message (with formatting placeholders, if desired)</param> | |
62 | /// <param name="args">Formatting replacements.</param> | |
12e0e204 JR |
63 | public void info(object msg) { |
64 | foreach (LogOutput outw in infos) { | |
65 | if (outw.open) | |
66 | outw.writer.WriteLine("(I@" + DateTime.Now.ToString("HH:mm:ss") + ")[" + from + "] " + msg.ToString()); | |
88ae06a4 JR |
67 | } |
68 | } | |
69 | ||
a3c84b90 JR |
70 | /// <summary> |
71 | /// Error logging. | |
72 | /// </summary> | |
73 | /// <param name="msg">Log message (with formatting placeholders, if desired)</param> | |
74 | /// <param name="args">Formatting replacements.</param> | |
12e0e204 JR |
75 | public void error(object msg) { |
76 | foreach (LogOutput outw in errors) { | |
77 | if (outw.open) | |
78 | outw.writer.WriteLine("(E@" + DateTime.Now.ToString("HH:mm:ss") + ")[" + from + "] " + msg.ToString()); | |
88ae06a4 JR |
79 | } |
80 | } | |
81 | } | |
12e0e204 JR |
82 | |
83 | class LogOutput { | |
84 | public TextWriter writer { get; private set; } | |
85 | public bool open; | |
86 | public LogOutput(TextWriter writer) { | |
87 | this.writer = writer; | |
88 | this.open = true; | |
89 | } | |
90 | } | |
88ae06a4 | 91 | } |