Last active
March 8, 2019 16:11
-
-
Save MaximovInk/690c88289ba495485ad62bcb9944884f to your computer and use it in GitHub Desktop.
Debug log in console like unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public static class Debug | |
{ | |
//private static string LogHistory { get; set; } = string.Empty; | |
public static List<Message> messages { get; set; } = new List<Message>(); | |
public static int Limit = 20; | |
public static void LogError<T>(T message) | |
{ | |
if (message == null) | |
return; | |
var messageValue = message.ToString(); | |
var match = messages | |
.FirstOrDefault(stringToCheck => stringToCheck.Value == messageValue); | |
if (match != null && match.Type == Message.TYPE.ErrorMessage) | |
{ | |
match.Count++; | |
} | |
else | |
{ | |
if (messages.Count >= Limit) | |
{ | |
messages.RemoveAt(0); | |
} | |
messages.Add(new Message() { Value = messageValue, Count = 1, Type = Message.TYPE.ErrorMessage }); | |
} | |
UpdateDisplay(); | |
} | |
public static void LogWarning<T>(T message) | |
{ | |
if (message == null) | |
return; | |
var messageValue = message.ToString(); | |
var match = messages | |
.FirstOrDefault(stringToCheck => stringToCheck.Value == messageValue); | |
if (match != null && match.Type == Message.TYPE.WarningMessage) | |
{ | |
match.Count++; | |
} | |
else | |
{ | |
if (messages.Count >= Limit) | |
{ | |
messages.RemoveAt(0); | |
} | |
messages.Add(new Message() { Value = messageValue, Count = 1, Type = Message.TYPE.WarningMessage }); | |
} | |
UpdateDisplay(); | |
} | |
public static void Log<T>(T message) | |
{ | |
if (message == null) | |
return; | |
var messageValue = message.ToString(); | |
var match = messages | |
.FirstOrDefault(stringToCheck => stringToCheck.Value == messageValue); | |
if (match != null) | |
{ | |
match.Count++; | |
} | |
else | |
{ | |
if (messages.Count >= Limit) | |
{ | |
messages.RemoveAt(0); | |
} | |
messages.Add(new Message() { Value = messageValue, Count = 1 }); | |
} | |
UpdateDisplay(); | |
} | |
private static void UpdateDisplay() | |
{ | |
Console.Clear(); | |
for (int i = 0; i < messages.Count; i++) | |
{ | |
switch (messages[i].Type) | |
{ | |
case Message.TYPE.WarningMessage: | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
break; | |
case Message.TYPE.ErrorMessage: | |
Console.ForegroundColor = ConsoleColor.Red; | |
break; | |
} | |
Console.WriteLine(messages[i].Value + "(" + messages[i].Count + ")"); | |
} | |
Console.ResetColor(); | |
} | |
public class Message | |
{ | |
public string Value; | |
public int Count; | |
public TYPE Type = TYPE.Message; | |
public bool Equals(string obj) | |
{ | |
return obj == Value; | |
} | |
public enum TYPE | |
{ | |
Message, | |
WarningMessage, | |
ErrorMessage | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment