@@ -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 )
0 commit comments