Skip to content

Instantly share code, notes, and snippets.

@roydejong
Last active January 7, 2019 05:47
Show Gist options
  • Save roydejong/9681cc6f27f2d354be1b111178ac22ce to your computer and use it in GitHub Desktop.
Save roydejong/9681cc6f27f2d354be1b111178ac22ce to your computer and use it in GitHub Desktop.
Unity: Debug logger class for the DiscordRPC C# Wrapper Library
using DiscordRPC.Logging;
using System;
using UnityEngine;
public class DiscordDebugLogger : DiscordRPC.Logging.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>
/// 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;
Debug.LogFormat("[DRP] (Logger) " + 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.Info && Level != LogLevel.Warning) return;
if (Coloured) Console.ForegroundColor = ConsoleColor.Yellow;
Debug.LogWarningFormat("[DRP] (Logger) " + 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.Info && Level != LogLevel.Warning && Level != LogLevel.Error) return;
if (Coloured) Console.ForegroundColor = ConsoleColor.Red;
Debug.LogErrorFormat("[DRP] (Logger) " + message, args);
}
}
@roydejong
Copy link
Author

Example use:

client = new DiscordRpcClient(DiscordClientId, SteamAppId, true, (int)DiscordPipe.FirstAvailable, pipe);
client.Logger = new DiscordDebugLogger() { Level = EnableDebugLogging ? LogLevel.Info : LogLevel.Warning };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment