Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Includes
- using System;
- using System.Collections.Generic;
- using System.Threading;
- #endregion
- namespace Minerva
- {
- public class Log
- {
- #region Private Fields
- static Queue<LogMessage> messages = new Queue<LogMessage>();
- static bool stopped = false;
- #endregion
- #region Private Methods
- static void Listen()
- {
- while (!stopped)
- {
- if (messages.Count > 0)
- Message(messages.Dequeue());
- }
- else
- Thread.Sleep(150);
- }
- }
- #endregion
- #region Private Methods
- static void Message(LogMessage m)
- {
- string message = m.Message;
- ConsoleColor colour = m.Colour;
- string tag = m.Tag;
- bool bright = !((int)colour < 8);
- Console.BackgroundColor = !bright ? (ConsoleColor)((int)colour | 8) : (ConsoleColor)((int)colour ^ 8);
- Console.ForegroundColor = !bright ? ConsoleColor.Black : ConsoleColor.White;
- if (tag != "") Console.Write(" ##{0}## ", tag);
- Console.BackgroundColor = Configuration.Colours.DefaultBG;
- Console.ForegroundColor = colour;
- Console.Write((tag != "") ? " {0}\n" : "{0}\n", message);
- Console.ForegroundColor = Configuration.Colours.DefaultFG;
- }
- #endregion
- #region Public Methods
- public static void Received(string type, int opcode, int size)
- { Console.WriteLine(String.Format("Received packet: CSC_{0} (opcode: {1}, size: {2})", type, opcode, size)); }
- public static void Sent(string type, int opcode, int size)
- { Console.WriteLine(String.Format("Sent packet: SSC_{0} (opcode: {1}, size: {2})", type, opcode, size)); }
- public static void Error(string message)
- { Message(message, Configuration.Colours.Error, "ERROR"); }
- public static void Error(string format, params object[] arg)
- { Error(String.Format(format, arg)); }
- public static void Notice(string message)
- { Message(message, Configuration.Colours.Notice, "NOTICE"); }
- public static void Notice(string format, params object[] arg)
- { Notice(String.Format(format, arg)); }
- public static void Warning(string message)
- { Message(message, Configuration.Colours.Warning, "WARNING"); }
- public static void Warning(string format, params object[] arg)
- { Warning(String.Format(format, arg)); }
- public static void Message(string message, ConsoleColor colour, string tag = "")
- {
- lock (messages)
- messages.Enqueue(new LogMessage(message, colour, tag));
- }
- public static void Start()
- {
- var t = new Thread(new ThreadStart(Listen));
- t.Start();
- }
- public static void Stop()
- { stopped = true; }
- #endregion
- }
- public struct LogMessage
- {
- #region Public Fields
- public string Message;
- public ConsoleColor Colour;
- public string Tag;
- #endregion
- #region Constructor
- public LogMessage(string message, ConsoleColor colour)
- {
- Message = message;
- Colour = colour;
- Tag = "";
- }
- public LogMessage(string message, ConsoleColor colour, string tag)
- {
- Message = message;
- Colour = colour;
- Tag = tag;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement