Skip to content

Commit

Permalink
Working on WPF log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Mar 30, 2024
1 parent ccf2abb commit 817752d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
15 changes: 15 additions & 0 deletions TwitchDownloaderWPF/Models/LogLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace TwitchDownloaderWPF.Models
{
[Flags]
internal enum LogLevel
{
None = 0,
Verbose = 1 << 0,
Info = 1 << 1,
Warning = 1 << 2,
Error = 1 << 3,
Ffmpeg = 1 << 4,
}
}
37 changes: 32 additions & 5 deletions TwitchDownloaderWPF/Utils/WpfTaskProgress.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using TwitchDownloaderCore.Interfaces;
using TwitchDownloaderWPF.Models;

namespace TwitchDownloaderWPF.Utils
{
Expand All @@ -13,6 +14,8 @@ internal class WpfTaskProgress : ITaskProgress
private TimeSpan _lastTime1 = new(-1);
private TimeSpan _lastTime2 = new(-1);

private readonly LogLevel _logLevel;

private readonly Action<int> _handlePercent;
private readonly Action<string> _handleStatus;
private readonly Action<string> _handleLog;
Expand All @@ -24,6 +27,8 @@ public WpfTaskProgress(Action<int> handlePercent)
_handleStatus = null;
_handleLog = null;
_handleFfmpegLog = null;

_logLevel = LogLevel.None;
}

public WpfTaskProgress(Action<int> handlePercent, Action<string> handleStatus, Action<string> handleLog, Action<string> handleFfmpegLog = null)
Expand All @@ -32,6 +37,13 @@ public WpfTaskProgress(Action<int> handlePercent, Action<string> handleStatus, A
_handleStatus = handleStatus;
_handleLog = handleLog;
_handleFfmpegLog = handleFfmpegLog;

// TODO: Make this user configurable
_logLevel = LogLevel.Info | LogLevel.Warning | LogLevel.Error;
if (handleFfmpegLog is not null)
{
_logLevel |= LogLevel.Ffmpeg;
}
}

public void SetStatus(string status)
Expand Down Expand Up @@ -117,27 +129,42 @@ public void ReportProgress(int percent, TimeSpan time1, TimeSpan time2)

public void LogVerbose(string logMessage)
{
//_handleLog?.Invoke(logMessage);
if ((_logLevel & LogLevel.Verbose) != 0)
{
_handleLog?.Invoke(logMessage);
}
}

public void LogInfo(string logMessage)
{
_handleLog?.Invoke(logMessage);
if ((_logLevel & LogLevel.Info) != 0)
{
_handleLog.Invoke(logMessage);
}
}

public void LogWarning(string logMessage)
{
_handleLog?.Invoke(logMessage);
if ((_logLevel & LogLevel.Warning) != 0)
{
_handleLog?.Invoke(logMessage);
}
}

public void LogError(string logMessage)
{
_handleLog?.Invoke(Translations.Strings.ErrorLog + logMessage);
if ((_logLevel & LogLevel.Error) != 0)
{
_handleLog?.Invoke(Translations.Strings.ErrorLog + logMessage);
}
}

public void LogFfmpeg(string logMessage)
{
_handleFfmpegLog?.Invoke(logMessage);
if ((_logLevel & LogLevel.Ffmpeg) != 0)
{
_handleFfmpegLog?.Invoke(logMessage);
}
}
}
}

0 comments on commit 817752d

Please sign in to comment.