rmsoft1

LogWriter.cs

Apr 29th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. using Discord;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace RMSoftware.ModularBot
  10. {
  11.     public class ConsoleLogWriter
  12.     {
  13.         bool WriterBusy = false;
  14.  
  15.        
  16.  
  17.         Stack<LogMessage> log_stack = new Stack<LogMessage>();
  18.  
  19.         public async Task WriteEntry(LogMessage message)
  20.         {
  21.             string category = message.Source;
  22.             string logText = message.Message;
  23.             if (WriterBusy)//If busy, queue it up.
  24.             {
  25.                 //check for item in queue.
  26.                 if (log_stack.Contains(message))
  27.                 {
  28.                     //don't queue.
  29.                     return;
  30.                 }
  31.                 else
  32.                 {
  33.                     //re-queue.
  34.                     log_stack.Push(message);
  35.                 }
  36.                 return;
  37.             }
  38.             else //Otherwise, process it.
  39.             {
  40.                 WriterBusy = true;
  41.                 ConsoleColor BG = Console.BackgroundColor;
  42.                 ConsoleColor FG = Console.ForegroundColor;
  43.                 switch (message.Severity)
  44.                 {
  45.                     case LogSeverity.Critical:
  46.                         BG = ConsoleColor.DarkRed;
  47.                         break;
  48.                     case LogSeverity.Error:
  49.                         BG = ConsoleColor.Red;
  50.                         break;
  51.                     case LogSeverity.Warning:
  52.                         BG = ConsoleColor.Yellow; FG = ConsoleColor.Black;
  53.                         break;
  54.                     case LogSeverity.Info:
  55.                         BG = ConsoleColor.DarkBlue;
  56.                         break;
  57.                     case LogSeverity.Verbose:
  58.                         BG = ConsoleColor.Black;
  59.                         break;
  60.                     case LogSeverity.Debug:
  61.                         BG = ConsoleColor.Black;
  62.                         break;
  63.                     default:
  64.                         break;
  65.                 }
  66.                
  67.                 Console.Write(DateTime.Now.ToString("HH:mm:ss").PadRight(9));
  68.                 ConsoleColor last = Console.BackgroundColor;
  69.                 ConsoleColor f_last = Console.ForegroundColor;
  70.                 Console.BackgroundColor = BG;
  71.                 Console.ForegroundColor = FG;
  72.                 if (message.Source.Length > 11)
  73.                 {
  74.  
  75.                     Console.Write(message.Source.Substring(0, 11));
  76.                 }
  77.                 else
  78.                 {
  79.  
  80.                     Console.Write(message.Source.PadRight(11));
  81.                 }
  82.                 Console.BackgroundColor = last;
  83.                 Console.ForegroundColor = f_last;
  84.                 Console.Write(" ");
  85.                 Console.WriteLine(message.Message);
  86.                 await Task.Delay(3);
  87.                 WriterBusy = false;//after the writer is finished writing this entry, it won't be busy.
  88.             }
  89.            
  90.         }
  91.  
  92.         public void push(LogMessage logmessage)
  93.         {
  94.             log_stack.Push(logmessage);
  95.         }
  96.  
  97.         public async Task ProcessStack()
  98.         {
  99.             while (true)
  100.             {
  101.                 if(!Program.discon)
  102.                 {
  103.                     SpinWait.SpinUntil(() => log_stack.Count > 0);
  104.                 }
  105.                 //once the log stack has shit inside, process it if writer isn't busy.
  106.                 if(WriterBusy)
  107.                 {
  108.                     return;//Do not write an entry if the writer is busy.
  109.                 }
  110.                 await WriteEntry(log_stack.Pop());
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment