First commit

This commit is contained in:
EnderIce2
2020-10-25 16:19:43 +02:00
parent 9d78d84acb
commit b91db81473
73 changed files with 7634 additions and 0 deletions

View File

@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DiscordRPC.Logging
{
/// <summary>
/// Logs the outputs to the console using <see cref="Console.WriteLine()"/>
/// </summary>
public class ConsoleLogger : ILogger
{
/// <summary>
/// The level of logging to apply to this logger.
/// </summary>
public LogLevel Level { get; set; }
/// <summary>
/// Should the output be coloured?
/// </summary>
public bool Coloured { get; set; }
/// <summary>
/// A alias too <see cref="Coloured"/>
/// </summary>
public bool Colored { get { return Coloured; } set { Coloured = value; } }
/// <summary>
/// Creates a new instance of a Console Logger.
/// </summary>
public ConsoleLogger()
{
this.Level = LogLevel.Info;
Coloured = false;
}
/// <summary>
/// Creates a new instance of a Console Logger with a set log level
/// </summary>
/// <param name="level"></param>
/// <param name="coloured"></param>
public ConsoleLogger(LogLevel level, bool coloured = false)
{
Level = level;
Coloured = coloured;
}
/// <summary>
/// Informative log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Trace(string message, params object[] args)
{
if (Level > LogLevel.Trace) return;
if (Coloured) Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("TRACE: " + message, args);
}
/// <summary>
/// Informative log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Info(string message, params object[] args)
{
if (Level > LogLevel.Info) return;
if (Coloured) Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("INFO: " + message, args);
}
/// <summary>
/// Warning log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Warning(string message, params object[] args)
{
if (Level > LogLevel.Warning) return;
if (Coloured) Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("WARN: " + message, args);
}
/// <summary>
/// Error log messsages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Error(string message, params object[] args)
{
if (Level > LogLevel.Error) return;
if (Coloured) Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("ERR : " + message, args);
}
}
}

View File

@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DiscordRPC.Logging
{
/// <summary>
/// Logs the outputs to a file
/// </summary>
public class FileLogger : ILogger
{
/// <summary>
/// The level of logging to apply to this logger.
/// </summary>
public LogLevel Level { get; set; }
/// <summary>
/// Should the output be coloured?
/// </summary>
public string File { get; set; }
private object filelock;
/// <summary>
/// Creates a new instance of the file logger
/// </summary>
/// <param name="path">The path of the log file.</param>
public FileLogger(string path)
: this(path, LogLevel.Info) { }
/// <summary>
/// Creates a new instance of the file logger
/// </summary>
/// <param name="path">The path of the log file.</param>
/// <param name="level">The level to assign to the logger.</param>
public FileLogger(string path, LogLevel level)
{
Level = level;
File = path;
filelock = new object();
}
/// <summary>
/// Informative log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Trace(string message, params object[] args)
{
if (Level > LogLevel.Trace) return;
lock (filelock) System.IO.File.AppendAllText(File, "\r\nTRCE: " + (args.Length > 0 ? string.Format(message, args) : message));
}
/// <summary>
/// Informative log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Info(string message, params object[] args)
{
if (Level > LogLevel.Info) return;
lock(filelock) System.IO.File.AppendAllText(File, "\r\nINFO: " + (args.Length > 0 ? string.Format(message, args) : message));
}
/// <summary>
/// Warning log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Warning(string message, params object[] args)
{
if (Level > LogLevel.Warning) return;
lock (filelock)
System.IO.File.AppendAllText(File, "\r\nWARN: " + (args.Length > 0 ? string.Format(message, args) : message));
}
/// <summary>
/// Error log messsages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Error(string message, params object[] args)
{
if (Level > LogLevel.Error) return;
lock (filelock)
System.IO.File.AppendAllText(File, "\r\nERR : " + (args.Length > 0 ? string.Format(message, args) : message));
}
}
}

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DiscordRPC.Logging
{
/// <summary>
/// Logging interface to log the internal states of the pipe. Logs are sent in a NON thread safe way. They can come from multiple threads and it is upto the ILogger to account for it.
/// </summary>
public interface ILogger
{
/// <summary>
/// The level of logging to apply to this logger.
/// </summary>
LogLevel Level { get; set; }
/// <summary>
/// Debug trace messeages used for debugging internal elements.
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
void Trace(string message, params object[] args);
/// <summary>
/// Informative log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
void Info(string message, params object[] args);
/// <summary>
/// Warning log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
void Warning(string message, params object[] args);
/// <summary>
/// Error log messsages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
void Error(string message, params object[] args);
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DiscordRPC.Logging
{
/// <summary>
/// Level of logging to use.
/// </summary>
public enum LogLevel
{
/// <summary>
/// Trace, Info, Warning and Errors are logged
/// </summary>
Trace = 1,
/// <summary>
/// Info, Warning and Errors are logged
/// </summary>
Info = 2,
/// <summary>
/// Warning and Errors are logged
/// </summary>
Warning = 3,
/// <summary>
/// Only Errors are logged
/// </summary>
Error = 4,
/// <summary>
/// Nothing is logged
/// </summary>
None = 256
}
}

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DiscordRPC.Logging
{
/// <summary>
/// Ignores all log events
/// </summary>
public class NullLogger : ILogger
{
/// <summary>
/// The level of logging to apply to this logger.
/// </summary>
public LogLevel Level { get; set; }
/// <summary>
/// Informative log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Trace(string message, params object[] args)
{
//Null Logger, so no messages are acutally sent
}
/// <summary>
/// Informative log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Info(string message, params object[] args)
{
//Null Logger, so no messages are acutally sent
}
/// <summary>
/// Warning log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Warning(string message, params object[] args)
{
//Null Logger, so no messages are acutally sent
}
/// <summary>
/// Error log messsages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Error(string message, params object[] args)
{
//Null Logger, so no messages are acutally sent
}
}
}