Skip to content

Commit

Permalink
#115 feat: add datetime format for logging
Browse files Browse the repository at this point in the history
+ fixed grid for settings in smaller devices
  • Loading branch information
joseantmazonsb committed Mar 13, 2022
1 parent 9d3ef58 commit e898915
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 52 deletions.
2 changes: 2 additions & 0 deletions Linguard/Core.Test/YamlConfigurationSerializerShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class YamlConfigurationSerializerShould {
Endpoint: ''
Logging:
Level: Debug
DateTimeFormat: yyyy-MM-dd
Web:
LoginAttempts: 10
SecretKey: ''
Expand All @@ -106,6 +107,7 @@ public void Serialize() {
var config = new Configuration {
Logging = new LoggingConfiguration {
Level = LogLevel.Debug,
DateTimeFormat = "yyyy-MM-dd"
},
Traffic = new TrafficConfiguration {
Enabled = false,
Expand Down
1 change: 1 addition & 0 deletions Linguard/Core/Configuration/ILoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ namespace Linguard.Core.Configuration;

public interface ILoggingConfiguration : ICloneable {
public LogLevel Level { get; set; }
public string DateTimeFormat { get; set; }
}
2 changes: 2 additions & 0 deletions Linguard/Core/Configuration/LoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Linguard.Core.Configuration;

public class LoggingConfiguration : ILoggingConfiguration {
public LogLevel Level { get; set; }
public string DateTimeFormat { get; set; }

public object Clone() {
return MemberwiseClone();
}
Expand Down
29 changes: 20 additions & 9 deletions Linguard/Core/Managers/ConfigurationManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ namespace Linguard.Core.Managers;
public abstract class ConfigurationManagerBase : IConfigurationManager {

private readonly ISystemWrapper _systemWrapper;
private readonly ILinguardLogger _logger;

protected ConfigurationManagerBase(IConfiguration configuration, IWorkingDirectory workingDirectory,
ISystemWrapper systemWrapper) {
ISystemWrapper systemWrapper, ILinguardLogger logger) {
Configuration = configuration;
WorkingDirectory = workingDirectory;
_systemWrapper = systemWrapper;
_logger = logger;
}

public IConfiguration Configuration { get; set; }
public IWorkingDirectory WorkingDirectory { get; set; }

public ILogTarget LoggingTarget { get; set; }
public bool IsSetupNeeded { get; set; } = true;

public void LoadDefaults() {
Expand All @@ -40,6 +40,7 @@ private void LoadWebDefaults() {
private void LoadLoggingDefaults() {
Configuration.Logging = new LoggingConfiguration();
Configuration.Logging.Level = LogLevel.Information;
Configuration.Logging.DateTimeFormat = _logger.DateTimeFormat;
}
private void LoadTrafficDefaults() {
Configuration.Traffic = new TrafficConfiguration();
Expand All @@ -49,28 +50,38 @@ private void LoadTrafficDefaults() {
private void LoadWireguardDefaults() {
Configuration.Wireguard = new WireguardConfiguration();
Configuration.Wireguard.Interfaces = new HashSet<Interface>();
Configuration.Wireguard.IptablesBin = _systemWrapper
.RunCommand("whereis iptables | tr ' ' '\n' | grep bin").Stdout;
Configuration.Wireguard.WireguardBin = _systemWrapper
.RunCommand("whereis wg | tr ' ' '\n' | grep bin").Stdout;
Configuration.Wireguard.WireguardQuickBin = _systemWrapper
.RunCommand("whereis wg-quick | tr ' ' '\n' | grep bin").Stdout;
Configuration.Wireguard.Interfaces = new HashSet<Interface>();
Configuration.Wireguard.PrimaryDns = new("8.8.8.8", UriKind.RelativeOrAbsolute);
Configuration.Wireguard.SecondaryDns = new("8.8.4.4", UriKind.RelativeOrAbsolute);
var publicIp = Network.GetPublicIPAddress();
Configuration.Wireguard.Endpoint = publicIp == default
? default
: new(publicIp.ToString(), UriKind.RelativeOrAbsolute);
Configuration.Wireguard.IptablesBin = _systemWrapper
.RunCommand("whereis iptables | tr ' ' '\n' | grep bin").Stdout;
Configuration.Wireguard.WireguardBin = _systemWrapper
.RunCommand("whereis wg | tr ' ' '\n' | grep bin").Stdout;
Configuration.Wireguard.WireguardQuickBin = _systemWrapper
.RunCommand("whereis wg-quick | tr ' ' '\n' | grep bin").Stdout;
}

public abstract void Load();

public void Save() {
IsSetupNeeded = false;
ApplyChanges();
DoSave();
}

private void ApplyChanges() {
ApplyLogChanges();

void ApplyLogChanges() {
_logger.LogLevel = Configuration.Logging.Level;
_logger.DateTimeFormat = Configuration.Logging.DateTimeFormat;
}
}

protected abstract void DoSave();
public abstract string Export();
}
5 changes: 3 additions & 2 deletions Linguard/Core/Managers/DefaultFileConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using Linguard.Core.Configuration.Serialization;
using Linguard.Core.OS;
using Linguard.Core.Utils;
using Linguard.Log;

namespace Linguard.Core.Managers;

public abstract class DefaultFileConfigurationManager : FileConfigurationManager {

protected DefaultFileConfigurationManager(IConfiguration configuration, IWorkingDirectory workingDirectory,
ISystemWrapper systemWrapper, IConfigurationSerializer serializer)
: base(configuration, workingDirectory, systemWrapper, serializer) {
ISystemWrapper systemWrapper, IConfigurationSerializer serializer, ILinguardLogger logger)
: base(configuration, workingDirectory, systemWrapper, serializer, logger) {
}

private FileInfo? _configurationFile;
Expand Down
4 changes: 3 additions & 1 deletion Linguard/Core/Managers/FileConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using Linguard.Core.Configuration.Exceptions;
using Linguard.Core.Configuration.Serialization;
using Linguard.Core.OS;
using Linguard.Log;

namespace Linguard.Core.Managers;

public abstract class FileConfigurationManager : ConfigurationManagerBase {

protected FileConfigurationManager(IConfiguration configuration, IWorkingDirectory workingDirectory,
ISystemWrapper systemWrapper, IConfigurationSerializer serializer) : base(configuration, workingDirectory, systemWrapper) {
ISystemWrapper systemWrapper, IConfigurationSerializer serializer, ILinguardLogger logger)
: base(configuration, workingDirectory, systemWrapper, logger) {
Configuration = configuration;
WorkingDirectory = workingDirectory;
Serializer = serializer;
Expand Down
7 changes: 4 additions & 3 deletions Linguard/Core/Managers/IConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public interface IConfigurationManager {
/// Configuration of the application.
/// </summary>
IConfiguration Configuration { get; set; }
// Working directory of the application.
/// <summary>
/// Working directory of the application.
/// </summary>
IWorkingDirectory WorkingDirectory { get; set; }
/// <summary>
/// Logging target of the application.
/// Flag used to tell whether the initial setup has been completed.
/// </summary>
ILogTarget LoggingTarget { get; set; }
bool IsSetupNeeded { get; set; }
/// <summary>
/// Load default options.
Expand Down
5 changes: 3 additions & 2 deletions Linguard/Core/Managers/YamlConfigurationManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Linguard.Core.Configuration;
using Linguard.Core.Configuration.Serialization;
using Linguard.Core.OS;
using Linguard.Log;

namespace Linguard.Core.Managers;

Expand All @@ -11,7 +12,7 @@ public class YamlConfigurationManager : DefaultFileConfigurationManager {
};

public YamlConfigurationManager(IConfiguration configuration, IWorkingDirectory workingDirectory,
ISystemWrapper systemWrapper, IConfigurationSerializer serializer)
: base(configuration, workingDirectory, systemWrapper, serializer) {
ISystemWrapper systemWrapper, IConfigurationSerializer serializer, ILinguardLogger logger)
: base(configuration, workingDirectory, systemWrapper, serializer, logger) {
}
}
1 change: 1 addition & 0 deletions Linguard/Log/ILinguardLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace Linguard.Log;
public interface ILinguardLogger : ILogger {
LogLevel LogLevel { get; set; }
ILogTarget Target { get; set; }
string DateTimeFormat { get; set; }
}
17 changes: 4 additions & 13 deletions Linguard/Log/SimpleFileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@
namespace Linguard.Log;

public class SimpleFileLogger : ILinguardLogger {

public SimpleFileLogger() {
LogLevel = LogLevel.Information;
}

public SimpleFileLogger(FileLogTarget target, LogLevel logLevel = LogLevel.Information) {
Target = target;
LogLevel = logLevel;
}

public LogLevel LogLevel { get; set; }
public ILogTarget? Target { get; set; }
public LogLevel LogLevel { get; set; } = LogLevel.Information;
public ILogTarget Target { get; set; }
public string DateTimeFormat { get; set; } = "yyyy-MM-dd HH:mm:ss.fff";

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
Exception? exception, Func<TState, Exception?, string> formatter) {
if (!IsEnabled(logLevel)) return;
Target?.WriteLine($"{DateTime.Now:u} [{logLevel.ToString().ToUpper()}] " +
Target?.WriteLine($"{DateTime.Now.ToString(DateTimeFormat)} [{logLevel.ToString().ToUpper()}] " +
$"{formatter(state, exception)}");
}

Expand Down
3 changes: 1 addition & 2 deletions Linguard/Web/Services/LifetimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void LoadConfiguration() {
_logger.LogInformation("Configuration loaded.");
}
catch (ConfigurationNotLoadedError e) {
_logger.LogWarning(e, "Unable to load configuration. Using defaults.");
_logger.LogWarning(e, "Unable to load configuration. Using defaults");
_configurationManager.LoadDefaults();
}
}
Expand All @@ -75,7 +75,6 @@ private DirectoryInfo GetWorkingDirectory() {
var logFilename = Path.ChangeExtension(AssemblyInfo.Product.ToLower(), "log");
var logfile = new FileInfo(Path.Combine(workingDirectory.FullName, logFilename));
_logger.Target = new FileLogTarget(logfile);
_configurationManager.LoggingTarget = _logger.Target;
if (useCurrentDirectory) {
_logger.LogWarning("No working directory specified through environment variable " +
$"'{WorkingDirectoryEnvironmentVariable}'.");
Expand Down
45 changes: 31 additions & 14 deletions Linguard/Web/Shared/LoggingSettings.razor
Original file line number Diff line number Diff line change
@@ -1,37 +1,54 @@
@using Linguard.Core.Configuration
@using Linguard.Core.Managers
@using Linguard.Log
@namespace Linguard.Web.Shared

<div class="row">
<div class="col-md-6">
<div class="col-xxl-8">
<div class="row mb-2">
<div class="col">
<RadzenLabel Text="Target"/>
</div>
</div>
<div class="row mb-2">
<div class="col-xxl-10">
<RadzenTextBox class="w-100" Disabled="true" Value="@_configurationManager.LoggingTarget.ToString()"/>
</div>
</div>
<div class="row mb-2">
<div class="col">
<RadzenLabel Text="Level"/>
<RadzenTextBox class="w-100" Disabled="true" Value="@_logger.Target.ToString()"/>
</div>
</div>
<div class="row">
<div class="col-xxl-10">
<RadzenDropDown class="w-100" TValue="string" AllowClear="false" Placeholder="Select a log level"
Data="@Enum.GetNames(typeof(LogLevel)).Select(s => s.ToString())"
Value="@Configuration.Level.ToString()"
ValueChanged="l => Configuration.Level = Enum.Parse<LogLevel>(l)"
/>
<div class="col">
<div class="row mb-2">
<div class="col">
<RadzenLabel Text="Level"/>
</div>
</div>
<div class="row">
<div class="col">
<RadzenDropDown class="w-100" TValue="string" AllowClear="false" Placeholder="Select a log level"
Data="@Enum.GetNames(typeof(LogLevel)).Select(s => s.ToString())"
Value="@Configuration.Level.ToString()"
ValueChanged="l => Configuration.Level = Enum.Parse<LogLevel>(l)"
/>
</div>
</div>
</div>
<div class="col-xxl">
<div class="row mb-2">
<div class="col">
<RadzenLabel Text="Datetime format"/>
</div>
</div>
<div class="row mb-2">
<div class="col">
<RadzenTextBox class="w-100" @bind-Value="@Configuration.DateTimeFormat"/>
</div>
</div>
</div>
</div>
</div>
</div>

@inject IConfigurationManager _configurationManager
@inject ILinguardLogger _logger

@code {
[Parameter]
Expand Down
12 changes: 6 additions & 6 deletions Linguard/Web/Shared/WireguardSettings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div>
<!-- Default endpoint and dns -->
<div class="row mb-2">
<div class="col">
<div class="col-xxl">
<div class="row">
<div class="col">
<RadzenLabel class="mb-2" Text="Endpoint"/>
Expand All @@ -18,7 +18,7 @@
</div>
</div>
</div>
<div class="col mb-2">
<div class="col-xxl mb-2">
<div class="row">
<div class="col">
<RadzenLabel class="mb-2" Text="Primary DNS"/>
Expand All @@ -32,7 +32,7 @@
</div>
</div>
</div>
<div class="col">
<div class="col-xxl">
<div class="row">
<div class="col">
<RadzenLabel class="mb-2" Text="Secondary DNS"/>
Expand All @@ -49,7 +49,7 @@
</div>
<!-- Binaries -->
<div class="row">
<div class="col">
<div class="col-xxl">
<div class="row">
<div class="col">
<RadzenLabel class="mb-2" Text="Path to wg"/>
Expand All @@ -62,7 +62,7 @@
</div>
</div>
</div>
<div class="col mb-2">
<div class="col-xxl mb-2">
<div class="row">
<div class="col">
<RadzenLabel class="mb-2" Text="Path to wg-quick"/>
Expand All @@ -75,7 +75,7 @@
</div>
</div>
</div>
<div class="col mb-2">
<div class="col-xxl mb-2">
<div class="row">
<div class="col">
<RadzenLabel class="mb-2" Text="Path to iptables"/>
Expand Down

0 comments on commit e898915

Please sign in to comment.