]> jfr.im git - z_archive/kelsier.git/blob - Logger.cs
bbe14d183cf8b3b5908c54a0044ee2bf680a509f
[z_archive/kelsier.git] / Logger.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6
7 namespace Kelsier {
8 class Logger {
9 protected TextWriter[] debugs, infos, errors;
10
11 /// <summary>
12 /// Construct a Logger, writing to the specified TextWriter objects. Use an empty array to disable an output.
13 /// </summary>
14 /// <param name="debugs">Array of TextWriter's to use for debug output</param>
15 /// <param name="infos">...for info output</param>
16 /// <param name="errors">...for error output</param>
17 public Logger(TextWriter[] debugs, TextWriter[] infos, TextWriter[] errors) {
18 this.debugs = debugs;
19 this.infos = infos;
20 this.errors = errors;
21
22 this.debug("Starting logging (debug) here at {0}", DateTime.Now.ToString("F"));
23 this.info("Starting logging (info) here at {0}", DateTime.Now.ToString("F"));
24 this.error("Starting logging (error) here at {0}", DateTime.Now.ToString("F"));
25 }
26
27 ~Logger() {
28 this.debug("Stopping logging (debug) here at {0}", DateTime.Now.ToString("F"));
29 this.info("Stopping logging (info) here at {0}", DateTime.Now.ToString("F"));
30 this.error("Stopping logging (error) here at {0}", DateTime.Now.ToString("F"));
31 foreach (TextWriter[] group in new TextWriter[][] { this.debugs, this.infos, this.errors }) {
32 foreach (TextWriter outw in group) {
33 outw.Close();
34 }
35 }
36 }
37
38 /// <summary>
39 /// Debug logging.
40 /// </summary>
41 /// <param name="msg">Log message (with formatting placeholders, if desired)</param>
42 /// <param name="args">Formatting replacements.</param>
43 public void debug(string msg, params object[] args) {
44 foreach (TextWriter outw in this.debugs) {
45 outw.WriteLine("(D@" + DateTime.Now.ToString("HH:mm:ss") + ")[MAIN] " + msg, args);
46 }
47 }
48
49 /// <summary>
50 /// Info logging.
51 /// </summary>
52 /// <param name="msg">Log message (with formatting placeholders, if desired)</param>
53 /// <param name="args">Formatting replacements.</param>
54 public void info(string msg, params object[] args) {
55 foreach (TextWriter outw in this.infos) {
56 outw.WriteLine("(I@" + DateTime.Now.ToString("HH:mm:ss") + ")[MAIN] " + msg, args);
57 }
58 }
59
60 /// <summary>
61 /// Error logging.
62 /// </summary>
63 /// <param name="msg">Log message (with formatting placeholders, if desired)</param>
64 /// <param name="args">Formatting replacements.</param>
65 public void error(string msg, params object[] args) {
66 foreach (TextWriter outw in this.errors) {
67 outw.WriteLine("(E@" + DateTime.Now.ToString("HH:mm:ss") + ")[MAIN] " + msg, args);
68 }
69 }
70 }
71 }