Skip to content

Commit 708aac3

Browse files
committed
SteamController/PowerControl: Create Logs in Documents/SteamDeckTools/Logs
1 parent 8327c0d commit 708aac3

File tree

6 files changed

+116
-13
lines changed

6 files changed

+116
-13
lines changed

CommonHelpers/Log.cs

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ public static class Log
1313
#endif
1414

1515
#if DEBUG
16-
private static bool LogToTrace = true;
16+
public static bool LogToTrace = true;
1717
#else
18-
private static bool LogToTrace = false;
18+
public static bool LogToTrace = false;
1919
#endif
20-
private static bool LogToConsole = Environment.UserInteractive;
20+
public static bool LogToConsole = Environment.UserInteractive;
21+
public static bool LogToFile = false;
22+
public static bool LogToFileDebug = false;
2123

2224
internal static void SentryOptions(Sentry.SentryOptions o)
2325
{
@@ -43,18 +45,53 @@ internal static void SentryOptions(Sentry.SentryOptions o)
4345

4446
private static String? LogFileFolder;
4547

48+
private static void EnsureLogFileFolder()
49+
{
50+
if (LogFileFolder is not null)
51+
return;
52+
53+
var documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
54+
var steamControllerDocumentsFolder = Path.Combine(documentsFolder, "SteamDeckTools", "Logs");
55+
Directory.CreateDirectory(steamControllerDocumentsFolder);
56+
LogFileFolder = steamControllerDocumentsFolder;
57+
}
58+
59+
public static void CleanupLogFiles(DateTime beforeTime)
60+
{
61+
EnsureLogFileFolder();
62+
63+
if (LogFileFolder is null)
64+
return;
65+
66+
var searchPattern = String.Format("{0}_*.log", Instance.ApplicationName);
67+
string[] files = Directory.GetFiles(LogFileFolder, searchPattern);
68+
69+
foreach (string file in files)
70+
{
71+
FileInfo fi = new FileInfo(file);
72+
if (fi.LastAccessTime >= beforeTime)
73+
continue;
74+
75+
try
76+
{
77+
fi.Delete();
78+
}
79+
catch (Exception ex)
80+
{
81+
TraceException("CleanupLog", fi.Name, ex);
82+
}
83+
}
84+
}
85+
4686
private static SentryEvent? Sentry_BeforeSend(SentryEvent arg)
4787
{
4888
if (Instance.HasFile("DisableCheckForUpdates.txt") || Instance.HasFile("DisableSentryTracking.txt"))
4989
return null;
5090

51-
if (LogFileFolder == null)
52-
{
53-
var documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
54-
var steamControllerDocumentsFolder = Path.Combine(documentsFolder, "SteamDeckTools", "Logs");
55-
Directory.CreateDirectory(steamControllerDocumentsFolder);
56-
LogFileFolder = steamControllerDocumentsFolder;
57-
}
91+
EnsureLogFileFolder();
92+
93+
if (LogFileFolder is null)
94+
return null;
5895

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

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

107+
private static void WriteToLogFile(String line)
108+
{
109+
EnsureLogFileFolder();
110+
111+
if (LogFileFolder is null)
112+
return;
113+
114+
String logFile = Path.Combine(LogFileFolder, String.Format("{0}_{1}.json",
115+
Instance.ApplicationName, DateTime.UtcNow.ToString("yyyy-MM-dd")));
116+
117+
for (int i = 0; i < 3; i++)
118+
{
119+
try
120+
{
121+
File.AppendAllText(logFile, String.Format("{0}: {1}: {2}\r\n",
122+
DateTime.UtcNow, Process.GetCurrentProcess().Id, line));
123+
return;
124+
}
125+
catch (IOException)
126+
{
127+
Thread.Sleep(0);
128+
}
129+
}
130+
}
131+
70132
public static void TraceLine(string format, params object?[] arg)
71133
{
72-
if (!LogToTrace && !LogToConsole)
134+
if (!LogToTrace && !LogToConsole && !LogToFile)
135+
return;
136+
137+
String line = string.Format(format, arg);
138+
if (LogToTrace)
139+
Trace.WriteLine(line);
140+
if (LogToConsole)
141+
Console.WriteLine(line);
142+
if (LogToFile)
143+
WriteToLogFile(line);
144+
}
145+
146+
public static void TraceDebug(string format, params object?[] arg)
147+
{
148+
if (!LogToTrace && !LogToConsole && !LogToFileDebug)
73149
return;
74150

75151
String line = string.Format(format, arg);
76152
if (LogToTrace)
77153
Trace.WriteLine(line);
78154
if (LogToConsole)
79155
Console.WriteLine(line);
156+
if (LogToFileDebug)
157+
WriteToLogFile(line);
80158
}
81159

82160
public static void TraceError(string format, params object?[] arg)
@@ -87,6 +165,8 @@ public static void TraceError(string format, params object?[] arg)
87165
Trace.WriteLine(line);
88166
if (LogToConsole)
89167
Console.WriteLine(line);
168+
if (LogToFile)
169+
WriteToLogFile(line);
90170
}
91171

92172
public static void TraceException(String type, Object? name, Exception e)

PowerControl/Controller.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public Controller()
5050
startupManager.Startup = false;
5151
});
5252

53+
Log.CleanupLogFiles(DateTime.UtcNow.AddDays(-7));
54+
Log.LogToFile = true;
55+
Log.LogToFileDebug = true;
56+
5357
Instance.RunOnce(TitleWithVersion, "Global\\PowerControl");
5458
Instance.RunUpdater(TitleWithVersion);
5559

RELEASE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
## 0.6.x
1111

12+
- SteamController/PowerControl: Create Logs in Documents/SteamDeckTools/Logs
1213
- SteamController: Improve **Steam Input** support for **Steam Version 1684535786** WARNING: only English is supported!
1314
- SteamController: Allow to configure DS4 back buttons
1415
- SteamController: Allow to `EnableDS4Support=false` to hide DS4 controller

SteamController/ContextDebug.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ namespace SteamController
44
{
55
public partial class Context
66
{
7-
List<string> debugLastItems = new List<string>();
7+
private List<string> debugLastItems = new List<string>();
8+
private Profiles.Profile? debugLastProfile = null;
89

910
public void Debug()
1011
{
@@ -16,6 +17,12 @@ public void Debug()
1617
else
1718
items.Add("[CONTROLLER]");
1819

20+
if (profile != debugLastProfile)
21+
{
22+
Log.TraceLine("ProfileChanged: {0} to {1}", debugLastProfile?.Name ?? "null", profile?.Name ?? "null");
23+
debugLastProfile = profile;
24+
}
25+
1926
if (Steam.LizardButtons)
2027
items.Add("[LB]");
2128
if (Steam.LizardMouse)
@@ -51,7 +58,7 @@ public void Debug()
5158

5259
if (!items.SequenceEqual(debugLastItems))
5360
{
54-
Log.TraceLine("DEBUG: {0}", String.Join(" ", items));
61+
Log.TraceDebug("DEBUG: {0}", String.Join(" ", items));
5562
debugLastItems = items;
5663
}
5764
}

SteamController/Controller.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public Controller()
6666
startupManager.Startup = false;
6767
});
6868

69+
Log.CleanupLogFiles(DateTime.UtcNow.AddDays(-7));
70+
Log.LogToFile = true;
71+
Log.LogToFileDebug = Settings.Default.EnableDebugLogging;
72+
6973
Instance.RunOnce(TitleWithVersion, "Global\\SteamController");
7074
Instance.RunUpdater(TitleWithVersion);
7175

SteamController/Settings.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public bool DetectRTSSForeground
4141
set { Set("DetectRTSSForeground", value); }
4242
}
4343

44+
[Description("Create a debug log in Documents/SteamDeckTools/Logs.")]
45+
public bool EnableDebugLogging
46+
{
47+
get { return Get<bool>("EnableDebugLogging", false); }
48+
set { Set("EnableDebugLogging", value); CommonHelpers.Log.LogToFileDebug = value; }
49+
}
50+
4451
[Description("Default profile used when going back to Desktop mode")]
4552
[Browsable(true)]
4653
[TypeConverter(typeof(ProfilesSettings.Helpers.ProfileStringConverter))]

0 commit comments

Comments
 (0)