// Kelsier project - Logging code (Logger.cs)
// Written by the Jobbig codeteam.
// 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 .
//
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; }
///
/// Construct a Logger, writing to the specified TextWriter objects. Use an empty array to disable an output.
///
/// Array of TextWriter's to use for debug output
/// ...for info output
/// ...for error output
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;
}
///
/// Debug logging.
///
/// Log message (with formatting placeholders, if desired)
/// Formatting replacements.
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());
}
}
///
/// Info logging.
///
/// Log message (with formatting placeholders, if desired)
/// Formatting replacements.
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());
}
}
///
/// Error logging.
///
/// Log message (with formatting placeholders, if desired)
/// Formatting replacements.
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;
}
}
}