61 lines
2.0 KiB
C#
Raw Normal View History

2020-08-21 14:04:13 +01:00
using OWML.Common;
2020-09-16 17:51:58 +01:00
using OWML.Logging;
using System.Diagnostics;
using System.Linq;
namespace QSB.Utility
2020-02-15 20:48:02 +01:00
{
2020-08-20 14:10:37 +01:00
public static class DebugLog
2020-02-15 20:48:02 +01:00
{
public static void ToConsole(string message, MessageType type = MessageType.Message)
{
2020-09-16 17:51:58 +01:00
var console = (ModSocketOutput)QSB.Helper.Console;
var method = console.GetType()
.GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.Last(x => x.Name == "WriteLine");
var callingType = GetCallingType(new StackTrace());
method.Invoke(console, new object[] { type, message, callingType });
}
public static void ToHud(string message)
2020-02-15 20:48:02 +01:00
{
if (Locator.GetPlayerBody() == null)
{
return;
}
2020-08-24 16:01:33 +01:00
var data = new NotificationData(NotificationTarget.Player, message.ToUpper());
2020-07-28 15:59:24 +02:00
NotificationManager.SharedInstance.PostNotification(data);
}
2020-02-15 20:48:02 +01:00
public static void ToAll(string message, MessageType type = MessageType.Message)
2020-03-04 21:46:16 +01:00
{
ToConsole(message, type);
ToHud(message);
2020-03-04 21:46:16 +01:00
}
2020-08-10 18:17:54 +02:00
2020-08-22 20:21:13 +01:00
public static void DebugWrite(string message, MessageType type = MessageType.Message)
{
if (QSB.DebugMode)
{
ToConsole(message, type);
}
}
public static void LogState(string name, bool state)
2020-08-20 14:47:44 +01:00
{
var status = state ? "OK" : "FAIL";
var messageType = state ? MessageType.Success : MessageType.Error;
2020-10-22 21:21:41 +01:00
if (!state) // to stop "OK" spam
{
DebugWrite($"* {name} {status}", messageType);
}
2020-08-20 14:47:44 +01:00
}
2020-09-22 21:00:56 +01:00
private static string GetCallingType(StackTrace frame)
2020-09-16 17:51:58 +01:00
{
2020-09-22 21:00:56 +01:00
var stackFrame = frame.GetFrames().First(x => x.GetMethod().DeclaringType.Name != "DebugLog");
return stackFrame.GetMethod().DeclaringType.Name;
2020-09-16 17:51:58 +01:00
}
}
}