Skip to content

Commit

Permalink
SteamController/PowerControl: Create Logs in Documents/SteamDeckTools…
Browse files Browse the repository at this point in the history
…/Logs
  • Loading branch information
ayufan committed May 21, 2023
1 parent 8327c0d commit 708aac3
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 13 deletions.
102 changes: 91 additions & 11 deletions CommonHelpers/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public static class Log
#endif

#if DEBUG
private static bool LogToTrace = true;
public static bool LogToTrace = true;
#else
private static bool LogToTrace = false;
public static bool LogToTrace = false;
#endif
private static bool LogToConsole = Environment.UserInteractive;
public static bool LogToConsole = Environment.UserInteractive;
public static bool LogToFile = false;
public static bool LogToFileDebug = false;

internal static void SentryOptions(Sentry.SentryOptions o)
{
Expand All @@ -43,18 +45,53 @@ internal static void SentryOptions(Sentry.SentryOptions o)

private static String? LogFileFolder;

private static void EnsureLogFileFolder()
{
if (LogFileFolder is not null)
return;

var documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var steamControllerDocumentsFolder = Path.Combine(documentsFolder, "SteamDeckTools", "Logs");
Directory.CreateDirectory(steamControllerDocumentsFolder);
LogFileFolder = steamControllerDocumentsFolder;
}

public static void CleanupLogFiles(DateTime beforeTime)
{
EnsureLogFileFolder();

if (LogFileFolder is null)
return;

var searchPattern = String.Format("{0}_*.log", Instance.ApplicationName);
string[] files = Directory.GetFiles(LogFileFolder, searchPattern);

foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
if (fi.LastAccessTime >= beforeTime)
continue;

try
{
fi.Delete();
}
catch (Exception ex)
{
TraceException("CleanupLog", fi.Name, ex);
}
}
}

private static SentryEvent? Sentry_BeforeSend(SentryEvent arg)
{
if (Instance.HasFile("DisableCheckForUpdates.txt") || Instance.HasFile("DisableSentryTracking.txt"))
return null;

if (LogFileFolder == null)
{
var documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var steamControllerDocumentsFolder = Path.Combine(documentsFolder, "SteamDeckTools", "Logs");
Directory.CreateDirectory(steamControllerDocumentsFolder);
LogFileFolder = steamControllerDocumentsFolder;
}
EnsureLogFileFolder();

if (LogFileFolder is null)
return null;

String logFile = Path.Combine(LogFileFolder, String.Format("SentryLog_{0}.json", arg.Timestamp.ToString("yyyy-MM-dd")));

Expand All @@ -67,16 +104,57 @@ internal static void SentryOptions(Sentry.SentryOptions o)
return arg;
}

private static void WriteToLogFile(String line)
{
EnsureLogFileFolder();

if (LogFileFolder is null)
return;

String logFile = Path.Combine(LogFileFolder, String.Format("{0}_{1}.json",
Instance.ApplicationName, DateTime.UtcNow.ToString("yyyy-MM-dd")));

for (int i = 0; i < 3; i++)
{
try
{
File.AppendAllText(logFile, String.Format("{0}: {1}: {2}\r\n",
DateTime.UtcNow, Process.GetCurrentProcess().Id, line));
return;
}
catch (IOException)
{
Thread.Sleep(0);
}
}
}

public static void TraceLine(string format, params object?[] arg)
{
if (!LogToTrace && !LogToConsole)
if (!LogToTrace && !LogToConsole && !LogToFile)
return;

String line = string.Format(format, arg);
if (LogToTrace)
Trace.WriteLine(line);
if (LogToConsole)
Console.WriteLine(line);
if (LogToFile)
WriteToLogFile(line);
}

public static void TraceDebug(string format, params object?[] arg)
{
if (!LogToTrace && !LogToConsole && !LogToFileDebug)
return;

String line = string.Format(format, arg);
if (LogToTrace)
Trace.WriteLine(line);
if (LogToConsole)
Console.WriteLine(line);
if (LogToFileDebug)
WriteToLogFile(line);
}

public static void TraceError(string format, params object?[] arg)
Expand All @@ -87,6 +165,8 @@ public static void TraceError(string format, params object?[] arg)
Trace.WriteLine(line);
if (LogToConsole)
Console.WriteLine(line);
if (LogToFile)
WriteToLogFile(line);
}

public static void TraceException(String type, Object? name, Exception e)
Expand Down
4 changes: 4 additions & 0 deletions PowerControl/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public Controller()
startupManager.Startup = false;
});

Log.CleanupLogFiles(DateTime.UtcNow.AddDays(-7));
Log.LogToFile = true;
Log.LogToFileDebug = true;

Instance.RunOnce(TitleWithVersion, "Global\\PowerControl");
Instance.RunUpdater(TitleWithVersion);

Expand Down
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

## 0.6.x

- SteamController/PowerControl: Create Logs in Documents/SteamDeckTools/Logs
- SteamController: Improve **Steam Input** support for **Steam Version 1684535786** WARNING: only English is supported!
- SteamController: Allow to configure DS4 back buttons
- SteamController: Allow to `EnableDS4Support=false` to hide DS4 controller
Expand Down
11 changes: 9 additions & 2 deletions SteamController/ContextDebug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace SteamController
{
public partial class Context
{
List<string> debugLastItems = new List<string>();
private List<string> debugLastItems = new List<string>();
private Profiles.Profile? debugLastProfile = null;

public void Debug()
{
Expand All @@ -16,6 +17,12 @@ public void Debug()
else
items.Add("[CONTROLLER]");

if (profile != debugLastProfile)
{
Log.TraceLine("ProfileChanged: {0} to {1}", debugLastProfile?.Name ?? "null", profile?.Name ?? "null");
debugLastProfile = profile;
}

if (Steam.LizardButtons)
items.Add("[LB]");
if (Steam.LizardMouse)
Expand Down Expand Up @@ -51,7 +58,7 @@ public void Debug()

if (!items.SequenceEqual(debugLastItems))
{
Log.TraceLine("DEBUG: {0}", String.Join(" ", items));
Log.TraceDebug("DEBUG: {0}", String.Join(" ", items));
debugLastItems = items;
}
}
Expand Down
4 changes: 4 additions & 0 deletions SteamController/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public Controller()
startupManager.Startup = false;
});

Log.CleanupLogFiles(DateTime.UtcNow.AddDays(-7));
Log.LogToFile = true;
Log.LogToFileDebug = Settings.Default.EnableDebugLogging;

Instance.RunOnce(TitleWithVersion, "Global\\SteamController");
Instance.RunUpdater(TitleWithVersion);

Expand Down
7 changes: 7 additions & 0 deletions SteamController/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public bool DetectRTSSForeground
set { Set("DetectRTSSForeground", value); }
}

[Description("Create a debug log in Documents/SteamDeckTools/Logs.")]
public bool EnableDebugLogging
{
get { return Get<bool>("EnableDebugLogging", false); }
set { Set("EnableDebugLogging", value); CommonHelpers.Log.LogToFileDebug = value; }
}

[Description("Default profile used when going back to Desktop mode")]
[Browsable(true)]
[TypeConverter(typeof(ProfilesSettings.Helpers.ProfileStringConverter))]
Expand Down

0 comments on commit 708aac3

Please sign in to comment.