Skip to content

Commit cd65c09

Browse files
committed
switch to rolling logfiles (2x 64kb per log type)
1 parent fb23fa8 commit cd65c09

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

Signal-Windows.Lib/SignalLogging.cs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,41 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
9393
}
9494
}
9595

96-
class SignalFileLoggerProvider : ILoggerProvider
96+
public class SignalFileLoggerProvider : ILoggerProvider
9797
{
98+
private static string UILog = ApplicationData.Current.LocalCacheFolder.Path + @"\Signal-Windows.ui.log";
9899
private readonly string Filename;
100+
private readonly string OldFilename;
99101
private readonly string Prefix;
100-
private StreamWriter Writer;
101-
private object Lock = new object();
102+
private const int MaxLogSize = 64 * 1024;
103+
private static object Lock = new object();
102104

103105
public SignalFileLoggerProvider(string filename, string prefix)
104106
{
105107
Filename = filename;
108+
OldFilename = Filename + ".old";
106109
Prefix = prefix;
107-
Open();
108110
}
109111

110-
public void Open()
112+
public void TruncateLog()
111113
{
112-
Writer = File.CreateText(Filename);
114+
try
115+
{
116+
var length = new FileInfo(Filename).Length;
117+
if (length > MaxLogSize)
118+
{
119+
if (File.Exists(OldFilename))
120+
{
121+
File.Delete(OldFilename);
122+
}
123+
File.Move(Filename, OldFilename);
124+
File.AppendAllText(Filename, $"{DateTime.UtcNow.ToString("s")} [SignalFileLoggerProvider] truncated log file\n");
125+
}
126+
}
127+
catch (Exception e)
128+
{
129+
Debug.WriteLine(string.Format("SignalFileLoggerProvider failed to truncate file: {0}", e));
130+
}
113131
}
114132

115133
public void Log(string line)
@@ -118,10 +136,10 @@ public void Log(string line)
118136
{
119137
try
120138
{
121-
Writer.WriteLine($"{DateTime.UtcNow.ToString("s")} [{Prefix}] {line}");
122-
Writer.Flush();
139+
TruncateLog();
140+
File.AppendAllText(Filename, $"{DateTime.UtcNow.ToString("s")} [{Prefix}] {line}\n");
123141
}
124-
catch(Exception e)
142+
catch (Exception e)
125143
{
126144
Debug.WriteLine(string.Format("SignalFileLoggerProvider failed to write: {0}", e));
127145
}
@@ -135,10 +153,20 @@ public ILogger CreateLogger(string categoryName)
135153

136154
public void Dispose()
137155
{
138-
lock(Lock)
156+
}
157+
158+
public static void ForceAddUILog(string msg)
159+
{
160+
lock (Lock)
139161
{
140-
Writer?.Dispose();
141-
Writer = null;
162+
try
163+
{
164+
File.AppendAllText(UILog, msg);
165+
}
166+
catch (Exception e)
167+
{
168+
Debug.WriteLine(string.Format("SignalFileLoggerProvider failed to write: {0}", e));
169+
}
142170
}
143171
}
144172
}

Signal-Windows/App.xaml.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace Signal_Windows
3333
sealed partial class App : Application
3434
{
3535
private static App Instance;
36-
private readonly ILogger Logger = LibsignalLogging.CreateLogger<App>();
36+
private static ILogger Logger = LibsignalLogging.CreateLogger<App>();
3737
public static string URL = "https://textsecure-service.whispersystems.org";
3838
public static SignalServiceUrl[] ServiceUrls = new SignalServiceUrl[] { new SignalServiceUrl(URL, null) };
3939
public static StorageFolder LocalCacheFolder = ApplicationData.Current.LocalCacheFolder;
@@ -47,7 +47,7 @@ sealed partial class App : Application
4747

4848
static App()
4949
{
50-
// TODO enforce these have begun before initializing
50+
// TODO enforce these have begun before initializing and ensure the logger is working
5151
Task.Run(() => { SignalDBContext.Migrate(); });
5252
Task.Run(() => { LibsignalDBContext.Migrate(); });
5353
}
@@ -57,6 +57,7 @@ static App()
5757
/// </summary>
5858
public App()
5959
{
60+
SignalFileLoggerProvider.ForceAddUILog(Utils.GetAppStartMessage());
6061
Instance = this;
6162
SignalLogging.SetupLogging(true);
6263
this.InitializeComponent();

Signal-Windows/Utils.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics;
77
using System.Globalization;
88
using System.Threading.Tasks;
9+
using Windows.ApplicationModel;
910
using Windows.Foundation;
1011
using Windows.Foundation.Metadata;
1112
using Windows.UI;
@@ -263,6 +264,16 @@ public static String BytesToString(long byteCount) //https://stackoverflow.com/a
263264
return (Math.Sign(byteCount) * num).ToString() + suf[place];
264265
}
265266

267+
268+
public static string GetAppStartMessage()
269+
{
270+
var version = Package.Current.Id.Version;
271+
return
272+
"-------------------------------------------------\n" +
273+
String.Format(" Signal-Windows {0}.{1}.{2}.{3} starting\n", version.Major, version.Minor, version.Build, version.Revision) +
274+
"-------------------------------------------------\n";
275+
}
276+
266277
public static string GetCountryCode(string ISO3166) //https://stackoverflow.com/questions/34837436/uwp-get-country-phone-number-prefix
267278
{
268279
var dictionary = new Dictionary<string, string>();

0 commit comments

Comments
 (0)