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
15 changes: 14 additions & 1 deletion src/Configuration/PathProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,17 @@ public static string GetGlobalConfigPath()

return Path.Combine(configDir, "config.json");
}
}

/// <summary>
/// Gets the platform-specific log file path.
/// </summary>
public static string GetLogFilePath()
{
var logDir = OperatingSystem.IsWindows()
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "hypr", "logs")
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".local", "share", "hypr", "logs");

Directory.CreateDirectory(logDir);
return Path.Combine(logDir, "hypr.log");
}
}
4 changes: 3 additions & 1 deletion src/DebugOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
namespace Hypr;

public class DebugOption()
: Option<bool>("--debug", "Enable debug logging.");
: Option<bool>("--debug")
{
}
36 changes: 36 additions & 0 deletions src/Logging/LoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Hypr.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;

namespace Hypr.Logging;

public static class LoggingExtensions
{
public static IServiceCollection AddHyprLogging(this IServiceCollection services, string[] args)
{
// Check for debug flag in environment or args
var isDebug = args.Contains("--debug") ||
(Environment.GetEnvironmentVariable("HYPR_DEBUG")?.Equals("true", StringComparison.OrdinalIgnoreCase) ?? false);

// Configure Serilog
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Is(isDebug ? LogEventLevel.Debug : LogEventLevel.Information)
.WriteTo.File(PathProvider.GetLogFilePath(),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {SourceContext}: {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 7)
.WriteTo.Console(
outputTemplate: isDebug
? "{Message:lj}{NewLine}{Exception}"
: "[{Level:u3}] {Message:lj}{NewLine}{Exception}",
restrictedToMinimumLevel: isDebug ? LogEventLevel.Debug : LogEventLevel.Warning)
.CreateLogger();

// Use Serilog
services.AddLogging(x => x.ClearProviders().AddSerilog());

return services;
}
}
2 changes: 1 addition & 1 deletion src/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace hypr;
namespace Hypr;

internal static class Module
{
Expand Down
19 changes: 5 additions & 14 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.CommandLine;
using hypr;
using Hypr.Commands;
using Hypr.Configuration;
using Hypr.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Hypr;

// Build configuration with proper precedence order
var configBuilder = new ConfigurationBuilder()
Expand All @@ -21,17 +20,8 @@
var builder = Host.CreateApplicationBuilder(args);
builder.Configuration.AddConfiguration(configuration);

builder.Services.AddLogging(x =>
{
x.ClearProviders();
x.AddSimpleConsole(f =>
{
f.SingleLine = true;
f.TimestampFormat = "HH:mm:ss ";
f.IncludeScopes = true;
f.ColorBehavior = LoggerColorBehavior.Enabled;
});
});
// Add Hypr logging with debug flag support
builder.Services.AddHyprLogging(args);

// Register configuration
builder.Services.AddSingleton<IConfiguration>(configuration);
Expand All @@ -46,6 +36,7 @@

var host = builder.Build();
var rootCommand = new RootCommand("hypr - Git worktree manager");
rootCommand.Options.Add(new DebugOption());
var commands = host.Services.GetRequiredService<IEnumerable<Command>>();

foreach (var cmd in commands)
Expand Down
3 changes: 3 additions & 0 deletions src/hypr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

<ItemGroup>
<PackageReference Include="Scrutor" Version="6.1.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="10.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0" />

<PackageReference Include="Spectre.Console" Version="0.49.1" />
Expand Down
Loading