Skip to content
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
22 changes: 18 additions & 4 deletions BrowseRouter/Config/ConfigService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using BrowseRouter.Infrastructure;
using BrowseRouter.Model;

namespace BrowseRouter.Config;
Expand All @@ -14,9 +15,15 @@ internal class ConfigService(Config config) : IConfigService
public LogPreference GetLogPreference() => new()
{
IsEnabled = config.Log.Enabled,
File = string.IsNullOrWhiteSpace(config.Log.File)
? LogPreference.DefaultLogFile
: config.Log.File!
File = string.IsNullOrWhiteSpace(config.Log.File) switch
{
true => LogPreference.DefaultLogFile,
false => Path.IsPathFullyQualified(config.Log.File) switch
{
true => config.Log.File,
false => Path.Combine(App.ExePath, config.Log.File)
}
}
};

public IEnumerable<UrlPreference> GetUrlPreferences(ConfigType configType)
Expand Down Expand Up @@ -53,7 +60,14 @@ public async Task<List<FilterPreference>> GetFiltersAsync()
if (string.IsNullOrWhiteSpace(config.FiltersFile))
return [];

var json = await File.ReadAllTextAsync(config.FiltersFile);
var dir = Path.GetDirectoryName(App.ExePath)!;
string path = Path.IsPathFullyQualified(config.FiltersFile) switch
{
true => config.FiltersFile,
false => Path.Combine(dir, config.FiltersFile)
};

var json = await File.ReadAllTextAsync(path);
return JsonSerializer.Deserialize(json, SourceGenerationContext.Default.FilterPreferenceList) ?? [];
}
}
12 changes: 10 additions & 2 deletions BrowseRouter/Infrastructure/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public static class Actions
{
public static bool TryRun(Action a)
public static bool TryRun(Action a, bool logOnlyToConsole = false)
{
try
{
Expand All @@ -11,7 +11,15 @@ public static bool TryRun(Action a)
}
catch (Exception e)
{
Log.Write($"{e}");
if (logOnlyToConsole)
{
Console.WriteLine(e);
}
else
{
Log.Write($"{e}");
}

return false;
}
}
Expand Down
4 changes: 1 addition & 3 deletions BrowseRouter/Infrastructure/App.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Reflection;

namespace BrowseRouter.Infrastructure;
namespace BrowseRouter.Infrastructure;

public static class App
{
Expand Down
6 changes: 4 additions & 2 deletions BrowseRouter/Infrastructure/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private static void EnsureLogDirExists()
string? parent = Path.GetDirectoryName(Preference.File);
if (parent is not null)
{
Directory.CreateDirectory(parent);
Actions.TryRun(() => Directory.CreateDirectory(parent), logOnlyToConsole: true);
}
}

Expand All @@ -36,9 +36,11 @@ private static void TryWrite(string message)
writer.WriteLine(message);
return;
}
catch (Exception)
catch (Exception e)
{
Console.WriteLine($"Failed to write log file {Preference.File} on attempt {i + 1}: {e}");
}
Thread.Sleep(100);
}
}
}
3 changes: 1 addition & 2 deletions BrowseRouter/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"silent": false
},
"log": {
"enabled": true,
"file": "logs/app.log"
"enabled": true
},
"urls": {
"*.google.com": "chrome",
Expand Down
3 changes: 1 addition & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<Project>
<PropertyGroup>
<WarningLevel>4</WarningLevel>
<!-- TODO enable when all warnings are resolved -->
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Version>0.15.0</Version>
<Version>0.15.1</Version>
<Product>BrowseRouter</Product>
<Copyright>EnduraByte LLC 2025</Copyright>
<!-- Reduces flagging as malware -->
Expand Down