Skip to content

Commit b2649b8

Browse files
committed
Ditch longrunning tasks (they don't work properly in the bg task), add more logging
1 parent 8bb58fc commit b2649b8

File tree

6 files changed

+43
-17
lines changed

6 files changed

+43
-17
lines changed

Signal-Windows.Lib/SignalLibHandle.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -890,15 +890,17 @@ private void InitNetwork()
890890
{
891891
try
892892
{
893+
Logger.LogTrace("InitNetwork() sync context = {0}", SynchronizationContext.Current);
893894
MessageReceiver = new SignalServiceMessageReceiver(LibUtils.ServiceConfiguration, new StaticCredentialsProvider(Store.Username, Store.Password, Store.SignalingKey, (int)Store.DeviceId), LibUtils.USER_AGENT);
894895
var pipeTask = Task.Run(async () =>
895896
{
896897
try
897898
{
898899
var pipe = await MessageReceiver.CreateMessagePipe(CancelSource.Token, new SignalWebSocketFactory());
899-
Logger.LogTrace("Messagepipe created");
900-
IncomingMessagesTask = await Task.Factory.StartNew(async () => await new IncomingMessages(CancelSource.Token, pipe, MessageReceiver).HandleIncomingMessages(), TaskCreationOptions.LongRunning);
901-
OutgoingMessagesTask = await Task.Factory.StartNew(async () => await new OutgoingMessages(CancelSource.Token, pipe, Store, this).HandleOutgoingMessages(), TaskCreationOptions.LongRunning);
900+
Logger.LogTrace("Starting IncomingMessagesTask");
901+
IncomingMessagesTask = Task.Run(() => new IncomingMessages(CancelSource.Token, pipe, MessageReceiver).HandleIncomingMessages());
902+
Logger.LogTrace("Starting OutgoingMessagesTask");
903+
OutgoingMessagesTask = Task.Run(() => new OutgoingMessages(CancelSource.Token, pipe, Store, this).HandleOutgoingMessages());
902904
return pipe;
903905
}
904906
catch(Exception e)

Signal-Windows.Lib/SignalLogging.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
9696
public class SignalFileLoggerProvider : ILoggerProvider
9797
{
9898
private static readonly string UILog = ApplicationData.Current.LocalCacheFolder.Path + @"\Signal-Windows.ui.log";
99+
private static readonly string BGLog = ApplicationData.Current.LocalCacheFolder.Path + @"\Signal-Windows.bg.log";
99100
private readonly string Filename;
100101
private readonly string OldFilename;
101102
private readonly string Prefix;
@@ -170,6 +171,21 @@ public static void ForceAddUILog(string msg)
170171
}
171172
}
172173

174+
public static void ForceAddBGLog(string msg)
175+
{
176+
lock (Lock)
177+
{
178+
try
179+
{
180+
File.AppendAllText(BGLog, msg);
181+
}
182+
catch (Exception e)
183+
{
184+
Debug.WriteLine(string.Format("SignalFileLoggerProvider failed to write: {0}", e));
185+
}
186+
}
187+
}
188+
173189
public static void ExportUILog(StorageFile file)
174190
{
175191
lock(Lock)

Signal-Windows.Lib/Util/LibUtils.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Text;
1111
using System.Threading;
1212
using System.Threading.Tasks;
13+
using Windows.ApplicationModel;
1314
using Windows.Foundation.Metadata;
1415
using Windows.Networking.BackgroundTransfer;
1516
using Windows.Storage;
@@ -114,6 +115,24 @@ public static FileStream CreateTmpFile(string name)
114115
{
115116
return File.Open(ApplicationData.Current.LocalCacheFolder.Path + Path.AltDirectorySeparatorChar + name, FileMode.Create, FileAccess.ReadWrite);
116117
}
118+
119+
public static string GetAppStartMessage()
120+
{
121+
var version = Package.Current.Id.Version;
122+
return
123+
"-------------------------------------------------\n" +
124+
String.Format(" Signal-Windows {0}.{1}.{2}.{3} starting\n", version.Major, version.Minor, version.Build, version.Revision) +
125+
"-------------------------------------------------\n";
126+
}
127+
128+
public static string GetBGStartMessage()
129+
{
130+
var version = Package.Current.Id.Version;
131+
return
132+
"-------------------------------------------------\n" +
133+
String.Format(" Signal-Windows BG {0}.{1}.{2}.{3} starting\n", version.Major, version.Minor, version.Build, version.Revision) +
134+
"-------------------------------------------------\n";
135+
}
117136
}
118137

119138
public static class StringExt

Signal-Windows.RC/SignalBackgroundTask.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ public sealed class SignalBackgroundTask : IBackgroundTask
3030

3131
public void Run(IBackgroundTaskInstance taskInstance)
3232
{
33-
34-
Logger.LogInformation("Background task starting");
35-
Deferral = taskInstance.GetDeferral();
33+
SignalFileLoggerProvider.ForceAddBGLog(LibUtils.GetBGStartMessage());
3634
SignalLogging.SetupLogging(false);
35+
Deferral = taskInstance.GetDeferral();
3736
ToastNotifier = ToastNotificationManager.CreateToastNotifier();
3837
taskInstance.Canceled += OnCanceled;
3938
bool locked = LibUtils.Lock(5000);

Signal-Windows/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static App()
5858
/// </summary>
5959
public App()
6060
{
61-
SignalFileLoggerProvider.ForceAddUILog(Utils.GetAppStartMessage());
61+
SignalFileLoggerProvider.ForceAddUILog(LibUtils.GetAppStartMessage());
6262
Instance = this;
6363
SignalLogging.SetupLogging(true);
6464
this.InitializeComponent();

Signal-Windows/Utils.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,6 @@ public static String BytesToString(long byteCount) //https://stackoverflow.com/a
273273
return (Math.Sign(byteCount) * num).ToString() + suf[place];
274274
}
275275

276-
277-
public static string GetAppStartMessage()
278-
{
279-
var version = Package.Current.Id.Version;
280-
return
281-
"-------------------------------------------------\n" +
282-
String.Format(" Signal-Windows {0}.{1}.{2}.{3} starting\n", version.Major, version.Minor, version.Build, version.Revision) +
283-
"-------------------------------------------------\n";
284-
}
285-
286276
public static string GetCountryCode(string ISO3166) //https://stackoverflow.com/questions/34837436/uwp-get-country-phone-number-prefix
287277
{
288278
var dictionary = new Dictionary<string, string>();

0 commit comments

Comments
 (0)