Skip to content

Commit 4a2f53d

Browse files
committed
Use Logger.Log* everywhere
fixes #29
1 parent 437bcb6 commit 4a2f53d

File tree

2 files changed

+18
-32
lines changed

2 files changed

+18
-32
lines changed

libsignal-service-dotnet/SignalServiceMessageSender.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using libsignalservice.push.exceptions;
1111
using libsignalservice.push.http;
1212
using libsignalservice.util;
13+
using Microsoft.Extensions.Logging;
1314
using Strilanc.Value;
1415
using System;
1516
using System.Collections.Generic;
@@ -28,7 +29,7 @@ namespace libsignalservice
2829
/// </summary>
2930
public class SignalServiceMessageSender
3031
{
31-
private static readonly string TAG = "SignalServiceMessageSender";
32+
private readonly ILogger Logger = LibsignalLogging.CreateLogger<SignalServiceMessageSender>();
3233

3334
private readonly PushServiceSocket socket;
3435
private readonly SignalProtocolStore store;
@@ -577,17 +578,17 @@ private async Task<SendMessageResponseList> SendMessage(CancellationToken token,
577578
}
578579
catch (UntrustedIdentityException e)
579580
{
580-
Debug.WriteLine("untrusted identity: " + recipient, TAG);
581+
Logger.LogError("SendMessage() untrusted identity");
581582
responseList.UntrustedIdentities.Add(e);
582583
}
583584
catch (UnregisteredUserException e)
584585
{
585-
Debug.WriteLine("unregistered user: " + recipient, TAG);
586+
Logger.LogError("SendMessage() unregistered user");
586587
responseList.UnregisteredUsers.Add(e);
587588
}
588589
catch (PushNetworkException e)
589590
{
590-
Debug.WriteLine("PushNetWorkException for:" + recipient, TAG);
591+
Logger.LogError("SendMessage() failed: {0}\n{1}", e.Message, e.StackTrace);
591592
responseList.NetworkExceptions.Add(new NetworkFailureException(recipient.E164number, e));
592593
}
593594
}
@@ -605,16 +606,16 @@ private async Task<SendMessageResponse> SendMessage(CancellationToken token, Sig
605606
{
606607
try
607608
{
608-
Debug.WriteLine("Transmitting over pipe...");
609+
Logger.LogTrace("Transmitting over pipe...");
609610
return await pipe.Send(messages);
610611
}
611612
catch (Exception e)
612613
{
613-
Debug.WriteLine(e.Message + " - falling back to new connection...");
614+
Logger.LogWarning(e.Message + " - falling back to new connection...");
614615
}
615616
}
616617

617-
Debug.WriteLine("Not transmitting over pipe...");
618+
Logger.LogTrace("Not transmitting over pipe...");
618619
return await socket.SendMessage(token, messages);
619620
}
620621
catch (MismatchedDevicesException mde)
@@ -626,7 +627,7 @@ private async Task<SendMessageResponse> SendMessage(CancellationToken token, Sig
626627
HandleStaleDevices(recipient, ste.StaleDevices);
627628
}
628629
}
629-
Debug.WriteLine("Failed to resolve conflicts after 3 attempts!");
630+
Logger.LogError("Failed to resolve conflicts after 3 attempts!");
630631
throw new Exception("Failed to resolve conflicts after 3 attempts!");
631632
}
632633

@@ -636,15 +637,15 @@ private async Task<IList<AttachmentPointer>> CreateAttachmentPointers(Cancellati
636637

637638
if (attachments == null || attachments.Count == 0)
638639
{
639-
Debug.WriteLine("No attachments present...", TAG);
640+
Logger.LogTrace("No attachments present...");
640641
return pointers;
641642
}
642643

643644
foreach (SignalServiceAttachment attachment in attachments)
644645
{
645646
if (attachment.IsStream())
646647
{
647-
Debug.WriteLine("Found attachment, creating pointer...", TAG);
648+
Logger.LogTrace("Found attachment, creating pointer...");
648649
pointers.Add(await CreateAttachmentPointer(token, attachment.AsStream()));
649650
}
650651
else if (attachment.IsPointer())

libsignal-service-dotnet/push/PushServiceSocket.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ namespace libsignalservice.push
3333
{
3434
internal class PushServiceSocket
3535
{
36-
private static readonly string TAG = "PushServiceSocket";
3736
private static readonly string CREATE_ACCOUNT_SMS_PATH = "/v1/accounts/sms/code/{0}";
3837
private static readonly string CREATE_ACCOUNT_VOICE_PATH = "/v1/accounts/voice/code/{0}";
3938
private static readonly string VERIFY_ACCOUNT_CODE_PATH = "/v1/accounts/code/{0}";
@@ -321,7 +320,7 @@ public async Task<SignedPreKeyEntity> GetCurrentSignedPreKey(CancellationToken t
321320
}
322321
catch (/*NotFound*/Exception e)
323322
{
324-
Debug.WriteLine(e.Message, TAG);
323+
Logger.LogError("GetCurrentSignedPreKey() failed: {0}\n{1}", e.Message, e.StackTrace);
325324
return null;
326325
}
327326
}
@@ -358,8 +357,6 @@ public async Task<bool> SetCurrentSignedPreKey(CancellationToken token, SignedPr
358357
{
359358
throw new Exception("Server failed to allocate an attachment key!");
360359
}
361-
362-
Debug.WriteLine("Got attachment content location: " + attachmentKey.Location, TAG);
363360
return (attachmentKey.Id, attachmentKey.Location);
364361
}
365362

@@ -382,9 +379,7 @@ public async Task<string> RetrieveAttachmentDownloadUrl(CancellationToken token,
382379
}
383380

384381
string response = await MakeServiceRequestAsync(token, path, "GET", null);
385-
Debug.WriteLine("PushServiceSocket: Received resp " + response);
386382
AttachmentDescriptor descriptor = JsonUtil.FromJson<AttachmentDescriptor>(response);
387-
Debug.WriteLine("PushServiceSocket: Attachment: " + attachmentId + " is at: " + descriptor.Location);
388383
return descriptor.Location;
389384
}
390385

@@ -498,10 +493,9 @@ private async Task DownloadAttachment(CancellationToken token, string url, Strea
498493
{
499494
HttpClient connection = Util.CreateHttpClient();
500495
var headers = connection.DefaultRequestHeaders;
501-
Debug.WriteLine("downloading " + url);
502496
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, url);
503497
req.Content = new StringContent("");
504-
req.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
498+
req.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
505499
using (var resp = await connection.SendAsync(req, token))
506500
{
507501
Stream input = await resp.Content.ReadAsStreamAsync();
@@ -512,7 +506,6 @@ private async Task DownloadAttachment(CancellationToken token, string url, Strea
512506
read = input.Read(buffer, 0, 4096);
513507
if (read == 0)
514508
{
515-
Debug.WriteLine("PushServiceSocket Downloaded: " + url + " to: " + localDestination);
516509
localDestination.Flush();
517510
return;
518511
}
@@ -522,8 +515,7 @@ private async Task DownloadAttachment(CancellationToken token, string url, Strea
522515
}
523516
catch (Exception ioe)
524517
{
525-
Debug.WriteLine(ioe.Message);
526-
Debug.WriteLine(ioe.StackTrace);
518+
Logger.LogError("DownloadAttachment() failed: {0}\n{1}", ioe.Message, ioe.StackTrace);
527519
throw new PushNetworkException(ioe);
528520
}
529521
}
@@ -632,8 +624,7 @@ private async Task<string> MakeServiceRequestAsync(CancellationToken token, stri
632624
}
633625
catch (Exception ioe)
634626
{
635-
Debug.WriteLine(ioe.Message);
636-
Debug.WriteLine(ioe.StackTrace);
627+
Logger.LogError("MakeServiceRequestAsync failed: {0}\n{1}", ioe.Message, ioe.StackTrace);
637628
throw new PushNetworkException(ioe);
638629
}
639630

@@ -654,8 +645,7 @@ private async Task<string> MakeServiceRequestAsync(CancellationToken token, stri
654645
}
655646
catch (Exception e)
656647
{
657-
Debug.WriteLine(e);
658-
Debug.WriteLine(e.StackTrace);
648+
Logger.LogError("MakeServiceRequestAsync() failed: {0}\n{1}", e.Message, e.StackTrace);
659649
throw new PushNetworkException(e);
660650
}
661651
throw new MismatchedDevicesException(mismatchedDevices);
@@ -667,8 +657,7 @@ private async Task<string> MakeServiceRequestAsync(CancellationToken token, stri
667657
}
668658
catch (Exception e)
669659
{
670-
Debug.WriteLine(e);
671-
Debug.WriteLine(e.StackTrace);
660+
Logger.LogError("MakeServiceRequestAsync() failed: {0}\n{1}", e.Message, e.StackTrace);
672661
throw new PushNetworkException(e);
673662
}
674663
throw new StaleDevicesException(staleDevices);
@@ -717,15 +706,13 @@ private async Task<HttpResponseMessage> GetServiceConnectionAsync(CancellationTo
717706
string url = signalUrl.Url;
718707
string hostHeader = signalUrl.HostHeader;
719708
Uri uri = new Uri(string.Format("{0}{1}", url, urlFragment));
720-
Debug.WriteLine("{0}: Uri {1}", TAG, uri);
721709
HttpClient connection = Util.CreateHttpClient();
722710

723711
var headers = connection.DefaultRequestHeaders;
724712

725713
if (CredentialsProvider.Password != null)
726714
{
727715
string authHeader = GetAuthorizationHeader(CredentialsProvider);
728-
Debug.WriteLine(String.Format("Authorization: {0}", authHeader), TAG);
729716
headers.Add("Authorization", authHeader);
730717
}
731718

@@ -768,9 +755,7 @@ private async Task<HttpResponseMessage> GetServiceConnectionAsync(CancellationTo
768755
}
769756
catch (Exception e)
770757
{
771-
Debug.WriteLine("getConnection() failed:", TAG);
772-
Debug.WriteLine(e.Message);
773-
Debug.WriteLine(e.StackTrace);
758+
Logger.LogError("GetServiceConnectionAsync() failed: {0}\n{1}", e.Message, e.StackTrace);
774759
throw new PushNetworkException(e);
775760
}
776761
}

0 commit comments

Comments
 (0)