using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace StardewModdingAPI
{
public static class Log
{
private static readonly LogWriter _writer;
static Log()
{
_writer = LogWriter.Instance;
}
private static void PrintLog(LogInfo li)
{
_writer.WriteToLog(li);
}
#region Exception Logging
///
/// Catch unhandled exception from the application
///
/// Should be moved out of here if we do more than just log the exception.
public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("An exception has been caught");
File.WriteAllText(Path.Combine(Constants.LogDir, $"MODDED_ErrorLog.Log_{DateTime.UtcNow.Ticks}.txt"), e.ExceptionObject.ToString());
}
///
/// Catch thread exception from the application
///
/// Should be moved out of here if we do more than just log the exception.
public static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
Console.WriteLine("A thread exception has been caught");
File.WriteAllText(Path.Combine(Constants.LogDir, $"MODDED_ErrorLog.Log_{Extensions.Random.Next(100000000, 999999999)}.txt"), e.Exception.ToString());
}
#endregion
#region Sync Logging
///
/// NOTICE: Sync logging is discouraged. Please use Async instead.
///
/// Message to log
/// Colour of message
public static void SyncColour(object message, ConsoleColor colour)
{
PrintLog(new LogInfo(message?.ToString(), colour));
}
#endregion
#region Async Logging
public static void AsyncColour(object message, ConsoleColor colour)
{
Task.Run(() => { PrintLog(new LogInfo(message?.ToString(), colour)); });
}
public static void Async(object message)
{
AsyncColour(message?.ToString(), ConsoleColor.Gray);
}
public static void AsyncR(object message)
{
AsyncColour(message?.ToString(), ConsoleColor.Red);
}
public static void AsyncO(object message)
{
AsyncColour(message.ToString(), ConsoleColor.DarkYellow);
}
public static void AsyncY(object message)
{
AsyncColour(message?.ToString(), ConsoleColor.Yellow);
}
public static void AsyncG(object message)
{
AsyncColour(message?.ToString(), ConsoleColor.Green);
}
public static void AsyncC(object message)
{
AsyncColour(message?.ToString(), ConsoleColor.Cyan);
}
public static void AsyncM(object message)
{
AsyncColour(message?.ToString(), ConsoleColor.Magenta);
}
public static void Error(object message)
{
AsyncR("[ERROR] " + message);
}
public static void Success(object message)
{
AsyncG("[SUCCESS] " + message);
}
public static void Info(object message)
{
AsyncY("[INFO] " + message);
}
public static void Out(object message)
{
Async("[OUT] " + message);
}
public static void Debug(object message)
{
AsyncO("[DEBUG] " + message);
}
#endregion
#region ToRemove
public static void LogValueNotSpecified()
{
AsyncR(" must be specified");
}
public static void LogObjectValueNotSpecified()
{
AsyncR("