]> jfr.im git - z_archive/kelsier.git/blobdiff - Kelsier.Common/Logger.cs
preparing for modules!
[z_archive/kelsier.git] / Kelsier.Common / Logger.cs
diff --git a/Kelsier.Common/Logger.cs b/Kelsier.Common/Logger.cs
new file mode 100644 (file)
index 0000000..1478662
--- /dev/null
@@ -0,0 +1,111 @@
+// Kelsier project - Logging code (Logger.cs)
+// Written by the Jobbig codeteam. <http://jobbig.eu/code/>
+// Copyright 2013 John Runyon.
+// 
+// This file is part of the Kelsier project.
+// 
+// Kelsier is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+// 
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+// 
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+
+namespace Kelsier.Common {
+    public class Logger {
+        public LogOutput[] debugs { get; private set; }
+        public LogOutput[] infos { get; private set; }
+        public LogOutput[] errors { get; private set; }
+        public string from { get; private set; }
+
+        /// <summary>
+        /// Construct a Logger, writing to the specified TextWriter objects. Use an empty array to disable an output.
+        /// </summary>
+        /// <param name="debugs">Array of TextWriter's to use for debug output</param>
+        /// <param name="infos">...for info output</param>
+        /// <param name="errors">...for error output</param>
+        public Logger(string from, LogOutput[] debugs, LogOutput[] infos, LogOutput[] errors) {
+            int fromid;
+            if (Int32.TryParse(from, out fromid)) {
+                this.from = fromid.ToString("0000");
+            } else {
+                this.from = from;
+            }
+
+            this.debugs = debugs;
+            this.infos = infos;
+            this.errors = errors;
+
+            if (from == "Root") {
+                this.debug("Starting Root logging (debug) here at "+DateTime.Now.ToString("F"));
+                this.info("Starting Root logging (info) here at "+DateTime.Now.ToString("F"));
+                this.error("Starting Root logging (error) here at "+DateTime.Now.ToString("F"));
+            }
+        }
+        public Logger(string from, Logger copy) {
+            this.from = from;
+
+            this.debugs = copy.debugs;
+            this.infos = copy.infos;
+            this.errors = copy.infos;
+        }
+
+        /// <summary>
+        /// Debug logging.
+        /// </summary>
+        /// <param name="msg">Log message (with formatting placeholders, if desired)</param>
+        /// <param name="args">Formatting replacements.</param>
+        public void debug(object msg) {
+            foreach (LogOutput outw in debugs) {
+                if (outw.open)
+                    outw.writer.WriteLine("(D@" + DateTime.Now.ToString("HH:mm:ss") + ")[" + from + "] " + msg.ToString());
+            }
+        }
+
+        /// <summary>
+        /// Info logging.
+        /// </summary>
+        /// <param name="msg">Log message (with formatting placeholders, if desired)</param>
+        /// <param name="args">Formatting replacements.</param>
+        public void info(object msg) {
+            foreach (LogOutput outw in infos) {
+                if (outw.open)
+                    outw.writer.WriteLine("(I@" + DateTime.Now.ToString("HH:mm:ss") + ")[" + from + "] " + msg.ToString());
+            }
+        }
+
+        /// <summary>
+        /// Error logging.
+        /// </summary>
+        /// <param name="msg">Log message (with formatting placeholders, if desired)</param>
+        /// <param name="args">Formatting replacements.</param>
+        public void error(object msg) {
+            foreach (LogOutput outw in errors) {
+                if (outw.open)
+                    outw.writer.WriteLine("(E@" + DateTime.Now.ToString("HH:mm:ss") + ")[" + from + "] " + msg.ToString());
+            }
+        }
+    }
+
+    public class LogOutput {
+        public TextWriter writer { get; private set; }
+        public bool open;
+        public LogOutput(TextWriter writer) {
+            this.writer = writer;
+            this.open = true;
+        }
+    }
+}