using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BlubbFish.Utils
{
abstract public class OwnObject
{
public struct LogObject {
public LogObject(DateTime date, String location, String message, LogLevel level) {
this.Date = date;
this.Location = location;
this.Message = message;
this.Level = level;
}
public DateTime Date { get; set; }
public String Location { get; set; }
public String Message { get; set; }
public LogLevel Level { get; set; }
///
/// Formates a LogMessage to a String
///
/// Formated String
public override String ToString() {
return "[" + this.Date.ToString("R") + "]: " + this.Level.ToString() + " "+ this.Location + ", " + this.Message;
}
///
/// Formates a LogMessage to a String
///
/// Enables the output of the location
/// Enables the output of the date
/// Formated String
public String ToString(Boolean classNames, Boolean timeStamps) {
return (timeStamps ? "[" + this.Date.ToString("R") + "]: " + this.Level.ToString() + " " : "") + (classNames ? this.Location + ", " : "") + this.Message;
}
}
private List loglist = new List();
public delegate void LogEvent(Object sender, LogEventArgs e);
public enum LogLevel : Int32 {
Debug = 1,
Notice = 2,
Info = 4,
Warn = 8,
Error = 16
}
public event LogEvent EventDebug;
public event LogEvent EventNotice;
public event LogEvent EventInfo;
public event LogEvent EventWarn;
public event LogEvent EventError;
public event LogEvent EventLog;
///
/// Get the Complete Log
///
public List GetLog(LogLevel level, Boolean classNames, Boolean timeStamps) {
List ret = new List();
foreach (LogObject t in this.loglist) {
if (t.Level >= level) {
ret.Add(t.ToString(classNames, timeStamps));
}
}
return ret;
}
///
/// Put a message in the log
///
/// Where the event arrives
/// The logmessage itselfs
/// Level of the message
protected void AddLog(String location, String message, LogLevel level)
{
this.AddLog(location, message, level, DateTime.Now);
}
///
/// Put a message in the log
///
/// Where the event arrives
/// The logmessage itselfs
/// Level of the message
/// Date of the message
protected void AddLog(String location, String message, LogLevel level, DateTime date)
{
LogEventArgs e = new LogEventArgs(location, message, level, date);
if (EventDebug != null && level >= LogLevel.Debug) {
EventDebug(this, e);
}
if (EventNotice != null && level >= LogLevel.Notice) {
EventNotice(this, e);
}
if (EventInfo != null && level >= LogLevel.Info) {
EventInfo(this, e);
}
if (EventWarn != null && level >= LogLevel.Warn) {
EventWarn(this, e);
}
if (EventError != null && level >= LogLevel.Error) {
EventError(this, e);
}
EventLog?.Invoke(this, e);
this.loglist.Add(new LogObject(date, location, message, level));
}
}
}