Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1. #region Includes
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6.  
  7. #endregion
  8.  
  9. namespace Minerva
  10. {
  11.     public class Log
  12.     {
  13.         #region Private Fields
  14.  
  15.         static Queue<LogMessage> messages = new Queue<LogMessage>();
  16.         static bool stopped = false;
  17.  
  18.         #endregion
  19.  
  20.         #region Private Methods
  21.  
  22.         static void Listen()
  23.         {
  24.             while (!stopped)
  25.             {
  26.                 if (messages.Count > 0)
  27.                     Message(messages.Dequeue());
  28.                 }
  29.                 else
  30.                     Thread.Sleep(150);
  31.             }
  32.         }
  33.  
  34.         #endregion
  35.  
  36.         #region Private Methods
  37.  
  38.         static void Message(LogMessage m)
  39.         {
  40.             string message = m.Message;
  41.             ConsoleColor colour = m.Colour;
  42.             string tag = m.Tag;
  43.  
  44.             bool bright = !((int)colour < 8);
  45.             Console.BackgroundColor = !bright ? (ConsoleColor)((int)colour | 8) : (ConsoleColor)((int)colour ^ 8);
  46.             Console.ForegroundColor = !bright ? ConsoleColor.Black : ConsoleColor.White;
  47.             if (tag != "") Console.Write(" ##{0}## ", tag);
  48.             Console.BackgroundColor = Configuration.Colours.DefaultBG;
  49.             Console.ForegroundColor = colour;
  50.             Console.Write((tag != "") ? " {0}\n" : "{0}\n", message);
  51.             Console.ForegroundColor = Configuration.Colours.DefaultFG;
  52.         }
  53.  
  54.         #endregion
  55.  
  56.         #region Public Methods
  57.  
  58.         public static void Received(string type, int opcode, int size)
  59.             { Console.WriteLine(String.Format("Received packet: CSC_{0} (opcode: {1}, size: {2})", type, opcode, size)); }
  60.  
  61.         public static void Sent(string type, int opcode, int size)
  62.             { Console.WriteLine(String.Format("Sent packet: SSC_{0} (opcode: {1}, size: {2})", type, opcode, size)); }
  63.  
  64.         public static void Error(string message)
  65.             { Message(message, Configuration.Colours.Error, "ERROR"); }
  66.         public static void Error(string format, params object[] arg)
  67.             { Error(String.Format(format, arg)); }
  68.  
  69.         public static void Notice(string message)
  70.             { Message(message, Configuration.Colours.Notice, "NOTICE"); }
  71.         public static void Notice(string format, params object[] arg)
  72.             { Notice(String.Format(format, arg)); }
  73.  
  74.         public static void Warning(string message)
  75.             { Message(message, Configuration.Colours.Warning, "WARNING"); }
  76.         public static void Warning(string format, params object[] arg)
  77.             { Warning(String.Format(format, arg)); }
  78.  
  79.         public static void Message(string message, ConsoleColor colour, string tag = "")
  80.         {
  81.             lock (messages)
  82.                 messages.Enqueue(new LogMessage(message, colour, tag));
  83.         }
  84.  
  85.         public static void Start()
  86.         {
  87.             var t = new Thread(new ThreadStart(Listen));
  88.             t.Start();
  89.         }
  90.  
  91.         public static void Stop()
  92.             { stopped = true; }
  93.  
  94.         #endregion
  95.     }
  96.  
  97.     public struct LogMessage
  98.     {
  99.         #region Public Fields
  100.  
  101.         public string Message;
  102.         public ConsoleColor Colour;
  103.         public string Tag;
  104.  
  105.         #endregion
  106.  
  107.         #region Constructor
  108.  
  109.         public LogMessage(string message, ConsoleColor colour)
  110.         {
  111.             Message = message;
  112.             Colour = colour;
  113.             Tag = "";
  114.         }
  115.  
  116.         public LogMessage(string message, ConsoleColor colour, string tag)
  117.         {
  118.             Message = message;
  119.             Colour = colour;
  120.             Tag = tag;
  121.         }
  122.  
  123.         #endregion
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement