Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log verbose progress messages in S/R at debug level #1202

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/FwHeadless/FwHeadlessKernel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FwDataMiniLcmBridge;
using FwHeadless.Services;
using FwLiteProjectSync;
using LcmCrdt;

Expand All @@ -16,6 +17,8 @@ public static void AddFwHeadless(this IServiceCollection services)
.ValidateOnStart();
services.AddScoped<SendReceiveService>();
services.AddScoped<ProjectLookupService>();
services.AddScoped<LogSanitizerService>();
services.AddScoped<SafeLoggingProgress>();
services
.AddLcmCrdtClient()
.AddFwDataBridge()
Expand Down
16 changes: 8 additions & 8 deletions backend/FwHeadless/SendReceiveHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public SendReceiveAuth(FwHeadlessConfig config) : this(config.LexboxUsername, co

public record LfMergeBridgeResult(string Output, string ProgressMessages);

private static LfMergeBridgeResult CallLfMergeBridge(string method, IDictionary<string, string> flexBridgeOptions)
private static LfMergeBridgeResult CallLfMergeBridge(string method, IDictionary<string, string> flexBridgeOptions, IProgress? progress = null)
{
var progress = new StringBuilderProgress();
LfMergeBridge.LfMergeBridge.Execute(method, progress, flexBridgeOptions.ToDictionary(), out var lfMergeBridgeOutputForClient);
return new LfMergeBridgeResult(lfMergeBridgeOutputForClient, progress.ToString());
var sbProgress = new StringBuilderProgress();
LfMergeBridge.LfMergeBridge.Execute(method, progress ?? sbProgress, flexBridgeOptions.ToDictionary(), out var lfMergeBridgeOutputForClient);
return new LfMergeBridgeResult(lfMergeBridgeOutputForClient, progress == null ? sbProgress.ToString() : "");
}

private static Uri BuildSendReceiveUrl(string baseUrl, string projectCode, SendReceiveAuth? auth)
Expand All @@ -45,7 +45,7 @@ private static Uri BuildSendReceiveUrl(string baseUrl, string projectCode, SendR
return builder.Uri;
}

public static LfMergeBridgeResult SendReceive(FwDataProject project, string? projectCode = null, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072", string? commitMessage = null)
public static LfMergeBridgeResult SendReceive(FwDataProject project, string? projectCode = null, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072", string? commitMessage = null, IProgress? progress = null)
{
projectCode ??= project.Name;
var fwdataInfo = new FileInfo(project.FilePath);
Expand All @@ -65,10 +65,10 @@ public static LfMergeBridgeResult SendReceive(FwDataProject project, string? pro
{ "user", "LexBox" },
};
if (commitMessage is not null) flexBridgeOptions["commitMessage"] = commitMessage;
return CallLfMergeBridge("Language_Forge_Send_Receive", flexBridgeOptions);
return CallLfMergeBridge("Language_Forge_Send_Receive", flexBridgeOptions, progress);
}

public static LfMergeBridgeResult CloneProject(FwDataProject project, string? projectCode = null, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072")
public static LfMergeBridgeResult CloneProject(FwDataProject project, string? projectCode = null, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072", IProgress? progress = null)
{
projectCode ??= project.Name;
var fwdataInfo = new FileInfo(project.FilePath);
Expand All @@ -84,6 +84,6 @@ public static LfMergeBridgeResult CloneProject(FwDataProject project, string? pr
{ "languageDepotRepoUri", repoUrl.ToString() },
{ "deleteRepoIfNoSuchBranch", "false" },
};
return CallLfMergeBridge("Language_Forge_Clone", flexBridgeOptions);
return CallLfMergeBridge("Language_Forge_Clone", flexBridgeOptions, progress);
}
}
9 changes: 6 additions & 3 deletions backend/FwHeadless/SendReceiveService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using FwDataMiniLcmBridge;
using FwHeadless.Services;
using Microsoft.Extensions.Options;

namespace FwHeadless;

public class SendReceiveService(IOptions<FwHeadlessConfig> config)
public class SendReceiveService(IOptions<FwHeadlessConfig> config, SafeLoggingProgress progress)
{
public SendReceiveHelpers.LfMergeBridgeResult SendReceive(FwDataProject project, string? projectCode, string? commitMessage = null)
{
Expand All @@ -13,7 +14,8 @@ public SendReceiveHelpers.LfMergeBridgeResult SendReceive(FwDataProject project,
baseUrl: config.Value.HgWebUrl,
auth: new SendReceiveHelpers.SendReceiveAuth(config.Value),
fdoDataModelVersion: config.Value.FdoDataModelVersion,
commitMessage: commitMessage
commitMessage: commitMessage,
progress: progress
);
}

Expand All @@ -24,7 +26,8 @@ public SendReceiveHelpers.LfMergeBridgeResult Clone(FwDataProject project, strin
projectCode: projectCode,
baseUrl: config.Value.HgWebUrl,
auth: new SendReceiveHelpers.SendReceiveAuth(config.Value),
fdoDataModelVersion: config.Value.FdoDataModelVersion
fdoDataModelVersion: config.Value.FdoDataModelVersion,
progress: progress
);
}
}
29 changes: 29 additions & 0 deletions backend/FwHeadless/Services/LogSanitizerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text;
using Microsoft.Extensions.Options;

namespace FwHeadless.Services;

public class LogSanitizerService
{
private string Password { get; init; }
private string Base64Password { get; init; }
private string Base64UrlPassword { get; init; }
private bool ShouldSanitize { get; init; }

public LogSanitizerService(IOptions<FwHeadlessConfig> config)
{
Password = config.Value.LexboxPassword;
Base64Password = Convert.ToBase64String(Encoding.UTF8.GetBytes(Password));
Base64UrlPassword = Convert.ToBase64String(Encoding.UTF8.GetBytes(Password)).Replace('+', '-').Replace('/', '_').TrimEnd('=');
ShouldSanitize = Password != "pass";
}

public string SanitizeLogMessage(string original)
{
if (!ShouldSanitize) return original;
return original
.Replace(Password, "***")
.Replace(Base64Password, "***")
.Replace(Base64UrlPassword, "***");
}
}
53 changes: 53 additions & 0 deletions backend/FwHeadless/Services/SafeLoggingProgress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using SIL.Progress;

namespace FwHeadless.Services;

public class SafeLoggingProgress(ILoggerFactory loggerFactory, LogSanitizerService sanitizer) : IProgress
{
private readonly ILogger logger = loggerFactory.CreateLogger("SendReceive");
public bool ShowVerbose { get; set; }
public bool CancelRequested { get; set; }
public bool ErrorEncountered { get; set; }
public IProgressIndicator ProgressIndicator { get; set; } = null!;
public SynchronizationContext SyncContext { get; set; } = null!;

private string Sanitize(string message, params object[] args)
{
return sanitizer.SanitizeLogMessage(GenericProgress.SafeFormat(message, args));
}

public void WriteError(string message, params object[] args)
{
logger.LogError(Sanitize(message, args));
}

public void WriteException(Exception error)
{
WriteError(error.ToString());
}

public void WriteMessage(string message, params object[] args)
{
logger.LogInformation(Sanitize(message, args));
}

public void WriteMessageWithColor(string colorName, string message, params object[] args)
{
WriteMessage(message, args);
}

public void WriteStatus(string message, params object[] args)
{
WriteMessage(message, args);
}

public void WriteVerbose(string message, params object[] args)
{
logger.LogDebug(Sanitize(message, args));
}

public void WriteWarning(string message, params object[] args)
{
logger.LogWarning(Sanitize(message, args));
}
}
3 changes: 1 addition & 2 deletions backend/FwHeadless/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Default": "Information"
}
}
}
3 changes: 2 additions & 1 deletion backend/FwHeadless/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Microsoft.AspNetCore": "Information",
"SendReceive": "Debug"
}
},
"AllowedHosts": "*"
Expand Down
Loading