52 lines
1.3 KiB
C#
Raw Normal View History

using OWML.Common;
2020-09-16 17:51:58 +01:00
using System.Diagnostics;
using System.Linq;
2022-03-02 19:46:33 -08:00
namespace QSB.Utility;
public static class DebugLog
2020-02-15 20:48:02 +01:00
{
2022-06-06 23:35:13 -07:00
public static readonly int ProcessInstanceId = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)
.IndexOf(x => x.Id == Process.GetCurrentProcess().Id);
2022-03-02 19:46:33 -08:00
public static void ToConsole(string message, MessageType type = MessageType.Message)
2020-12-02 21:23:01 +00:00
{
if (QSBCore.DebugSettings.InstanceIdInLogs)
2022-01-18 01:46:41 -08:00
{
2022-06-06 23:35:13 -07:00
message = $"[{ProcessInstanceId}] " + message;
2020-12-02 21:23:01 +00:00
}
2020-08-10 18:17:54 +02:00
2022-03-02 19:46:33 -08:00
QSBCore.Helper.Console.WriteLine(message, type, GetCallingType(new StackTrace()));
}
2022-03-02 19:46:33 -08:00
public static void ToHud(string message)
{
if (Locator.GetPlayerBody() == null)
2020-12-02 21:23:01 +00:00
{
2022-03-02 19:46:33 -08:00
return;
2020-12-02 21:23:01 +00:00
}
2022-03-02 19:46:33 -08:00
var data = new NotificationData(NotificationTarget.Player, message.ToUpper());
NotificationManager.SharedInstance.PostNotification(data);
}
public static void ToAll(string message, MessageType type = MessageType.Message)
{
ToConsole(message, type);
ToHud(message);
}
public static void DebugWrite(string message, MessageType type = MessageType.Message)
{
if (QSBCore.DebugSettings.DebugMode)
{
2022-03-02 19:46:33 -08:00
ToConsole(message, type);
}
}
2022-03-02 19:46:33 -08:00
private static string GetCallingType(StackTrace frame) =>
frame.GetFrames()!
.Select(x => x.GetMethod().DeclaringType!.Name)
.First(x => x != nameof(DebugLog));
}