Skip to content

Commit

Permalink
Properly support log levels (#1019)
Browse files Browse the repository at this point in the history
* Working on WPF log levels

* ITwitchDownloaderArgs -> TwitchDownloaderArgs + initial CLI log level support

* Use the logger in the CLI instead of Console.WriteLine where applicable

* Update readme

* Add --silent deprecation message

* Missed a spot

* Add status log level to CLI

* Zero-alloc logging

* Add log levels to WPF
  • Loading branch information
ScrubN authored Mar 31, 2024
1 parent 55893dc commit 1669dfc
Show file tree
Hide file tree
Showing 38 changed files with 361 additions and 148 deletions.
16 changes: 16 additions & 0 deletions TwitchDownloaderCLI/Models/LogLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace TwitchDownloaderCLI.Models
{
[Flags]
internal enum LogLevel
{
None = 1 << 0,
Status = 1 << 1,
Verbose = 1 << 2,
Info = 1 << 3,
Warning = 1 << 4,
Error = 1 << 5,
Ffmpeg = 1 << 6,
}
}
5 changes: 1 addition & 4 deletions TwitchDownloaderCLI/Modes/Arguments/CacheArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
namespace TwitchDownloaderCLI.Modes.Arguments
{
[Verb("cache", HelpText = "Manage the working cache")]
public class CacheArgs : ITwitchDownloaderArgs
internal sealed class CacheArgs : TwitchDownloaderArgs
{
[Option('c', "clear", Default = false, Required = false, HelpText = "Clears the default cache folder.")]
public bool ClearCache { get; set; }

[Option("force-clear", Default = false, Required = false, HelpText = "Clears the default cache folder, bypassing the confirmation prompt")]
public bool ForceClearCache { get; set; }

[Option("banner", Default = true, HelpText = "Displays a banner containing version and copyright information.")]
public bool? ShowBanner { get; set; }
}
}
5 changes: 1 addition & 4 deletions TwitchDownloaderCLI/Modes/Arguments/ChatDownloadArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace TwitchDownloaderCLI.Modes.Arguments
{
[Verb("chatdownload", HelpText = "Downloads the chat from a VOD or clip")]
public class ChatDownloadArgs : ITwitchDownloaderArgs
internal sealed class ChatDownloadArgs : TwitchDownloaderArgs
{
[Option('u', "id", Required = true, HelpText = "The ID or URL of the VOD or clip to download that chat of.")]
public string Id { get; set; }
Expand Down Expand Up @@ -44,8 +44,5 @@ public class ChatDownloadArgs : ITwitchDownloaderArgs

[Option("temp-path", Default = "", HelpText = "Path to temporary folder to use for cache.")]
public string TempFolder { get; set; }

[Option("banner", Default = true, HelpText = "Displays a banner containing version and copyright information.")]
public bool? ShowBanner { get; set; }
}
}
5 changes: 1 addition & 4 deletions TwitchDownloaderCLI/Modes/Arguments/ChatRenderArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace TwitchDownloaderCLI.Modes.Arguments
{
[Verb("chatrender", HelpText = "Renders a chat JSON as a video")]
public class ChatRenderArgs : ITwitchDownloaderArgs
internal sealed class ChatRenderArgs : TwitchDownloaderArgs
{
[Option('i', "input", Required = true, HelpText = "Path to JSON chat file input.")]
public string InputFile { get; set; }
Expand Down Expand Up @@ -151,8 +151,5 @@ public class ChatRenderArgs : ITwitchDownloaderArgs

[Option("scale-highlight-indent", Default = 1.0, HelpText = "Number to scale highlight indent size (sub messages).")]
public double ScaleAccentIndent { get; set; }

[Option("banner", Default = true, HelpText = "Displays a banner containing version and copyright information.")]
public bool? ShowBanner { get; set; }
}
}
5 changes: 1 addition & 4 deletions TwitchDownloaderCLI/Modes/Arguments/ChatUpdateArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace TwitchDownloaderCLI.Modes.Arguments
{
[Verb("chatupdate", HelpText = "Updates the embedded emotes, badges, bits, and crops of a chat download and/or converts a JSON chat to another format.")]
public class ChatUpdateArgs : ITwitchDownloaderArgs
internal sealed class ChatUpdateArgs : TwitchDownloaderArgs
{
[Option('i', "input", Required = true, HelpText = "Path to input file. Valid extensions are: .json, .json.gz.")]
public string InputFile { get; set; }
Expand Down Expand Up @@ -41,8 +41,5 @@ public class ChatUpdateArgs : ITwitchDownloaderArgs

[Option("temp-path", Default = "", HelpText = "Path to temporary folder to use for cache.")]
public string TempFolder { get; set; }

[Option("banner", Default = true, HelpText = "Displays a banner containing version and copyright information.")]
public bool? ShowBanner { get; set; }
}
}
5 changes: 1 addition & 4 deletions TwitchDownloaderCLI/Modes/Arguments/ClipDownloadArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace TwitchDownloaderCLI.Modes.Arguments
{
[Verb("clipdownload", HelpText = "Downloads a clip from Twitch")]
public class ClipDownloadArgs : ITwitchDownloaderArgs
internal sealed class ClipDownloadArgs : TwitchDownloaderArgs
{
[Option('u', "id", Required = true, HelpText = "The ID or URL of the clip to download.")]
public string Id { get; set; }
Expand All @@ -25,8 +25,5 @@ public class ClipDownloadArgs : ITwitchDownloaderArgs

[Option("temp-path", Default = "", HelpText = "Path to temporary caching folder.")]
public string TempFolder { get; set; }

[Option("banner", Default = true, HelpText = "Displays a banner containing version and copyright information.")]
public bool? ShowBanner { get; set; }
}
}
5 changes: 1 addition & 4 deletions TwitchDownloaderCLI/Modes/Arguments/FfmpegArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
namespace TwitchDownloaderCLI.Modes.Arguments
{
[Verb("ffmpeg", HelpText = "Manage standalone ffmpeg")]
public class FfmpegArgs : ITwitchDownloaderArgs
internal sealed class FfmpegArgs : TwitchDownloaderArgs
{
[Option('d', "download", Default = false, Required = false, HelpText = "Downloads FFmpeg as a standalone file.")]
public bool DownloadFfmpeg { get; set; }

[Option("banner", Default = true, HelpText = "Displays a banner containing version and copyright information.")]
public bool? ShowBanner { get; set; }
}
}
8 changes: 0 additions & 8 deletions TwitchDownloaderCLI/Modes/Arguments/ITwitchDownloaderArgs.cs

This file was deleted.

5 changes: 1 addition & 4 deletions TwitchDownloaderCLI/Modes/Arguments/TsMergeArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
namespace TwitchDownloaderCLI.Modes.Arguments
{
[Verb("tsmerge", HelpText = "Concatenates multiple .ts/.tsv/.tsa/.m2t/.m2ts (MPEG Transport Stream) files into a single file")]
public class TsMergeArgs : ITwitchDownloaderArgs
internal sealed class TsMergeArgs : TwitchDownloaderArgs
{
[Option('i', "input", Required = true, HelpText = "Path a text file containing the absolute paths of the files to concatenate, separated by newlines. M3U/M3U8 is also supported.")]
public string InputList { get; set; }

[Option('o', "output", Required = true, HelpText = "Path to output file.")]
public string OutputFile { get; set; }

[Option("banner", Default = true, HelpText = "Displays a banner containing version and copyright information.")]
public bool? ShowBanner { get; set; }
}
}
14 changes: 14 additions & 0 deletions TwitchDownloaderCLI/Modes/Arguments/TwitchDownloaderArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using CommandLine;
using TwitchDownloaderCLI.Models;

namespace TwitchDownloaderCLI.Modes.Arguments
{
internal abstract class TwitchDownloaderArgs
{
[Option("banner", Default = true, HelpText = "Displays a banner containing version and copyright information.")]
public bool? ShowBanner { get; set; }

[Option("log-level", Default = Models.LogLevel.Status | LogLevel.Info | LogLevel.Warning | LogLevel.Error, HelpText = "Sets the log level flags. Applicable values are: None, Status, Verbose, Info, Warning, Error, Ffmpeg.")]
public LogLevel LogLevel { get; set; }
}
}
5 changes: 1 addition & 4 deletions TwitchDownloaderCLI/Modes/Arguments/VideoDownloadArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace TwitchDownloaderCLI.Modes.Arguments
{
[Verb("videodownload", HelpText = "Downloads a stream VOD from Twitch")]
public class VideoDownloadArgs : ITwitchDownloaderArgs
internal sealed class VideoDownloadArgs : TwitchDownloaderArgs
{
[Option('u', "id", Required = true, HelpText = "The ID or URL of the VOD to download.")]
public string Id { get; set; }
Expand Down Expand Up @@ -34,8 +34,5 @@ public class VideoDownloadArgs : ITwitchDownloaderArgs

[Option("temp-path", Default = "", HelpText = "Path to temporary caching folder.")]
public string TempFolder { get; set; }

[Option("banner", Default = true, HelpText = "Displays a banner containing version and copyright information.")]
public bool? ShowBanner { get; set; }
}
}
2 changes: 1 addition & 1 deletion TwitchDownloaderCLI/Modes/CacheHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace TwitchDownloaderCLI.Modes
{
public static class CacheHandler
internal static class CacheHandler
{
public static void ParseArgs(CacheArgs args)
{
Expand Down
11 changes: 6 additions & 5 deletions TwitchDownloaderCLI/Modes/DownloadChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using TwitchDownloaderCLI.Modes.Arguments;
using TwitchDownloaderCLI.Tools;
using TwitchDownloaderCore;
using TwitchDownloaderCore.Interfaces;
using TwitchDownloaderCore.Options;
using TwitchDownloaderCore.Tools;

Expand All @@ -13,26 +14,26 @@ internal static class DownloadChat
{
internal static void Download(ChatDownloadArgs inputOptions)
{
var downloadOptions = GetDownloadOptions(inputOptions);
var progress = new CliTaskProgress(inputOptions.LogLevel);

var progress = new CliTaskProgress();
var downloadOptions = GetDownloadOptions(inputOptions, progress);

var chatDownloader = new ChatDownloader(downloadOptions, progress);
chatDownloader.DownloadAsync(CancellationToken.None).Wait();
}

private static ChatDownloadOptions GetDownloadOptions(ChatDownloadArgs inputOptions)
private static ChatDownloadOptions GetDownloadOptions(ChatDownloadArgs inputOptions, ITaskLogger logger)
{
if (inputOptions.Id is null)
{
Console.WriteLine("[ERROR] - Vod/Clip ID/URL cannot be null!");
logger.LogError("Vod/Clip ID/URL cannot be null!");
Environment.Exit(1);
}

var vodClipIdMatch = TwitchRegex.MatchVideoOrClipId(inputOptions.Id);
if (vodClipIdMatch is not { Success: true })
{
Console.WriteLine("[ERROR] - Unable to parse Vod/Clip ID/URL.");
logger.LogError("Unable to parse Vod/Clip ID/URL.");
Environment.Exit(1);
}

Expand Down
11 changes: 6 additions & 5 deletions TwitchDownloaderCLI/Modes/DownloadClip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using TwitchDownloaderCLI.Modes.Arguments;
using TwitchDownloaderCLI.Tools;
using TwitchDownloaderCore;
using TwitchDownloaderCore.Interfaces;
using TwitchDownloaderCore.Options;
using TwitchDownloaderCore.Tools;

Expand All @@ -13,31 +14,31 @@ internal static class DownloadClip
{
internal static void Download(ClipDownloadArgs inputOptions)
{
var progress = new CliTaskProgress();
var progress = new CliTaskProgress(inputOptions.LogLevel);

if (inputOptions.EncodeMetadata == true)
{
FfmpegHandler.DetectFfmpeg(inputOptions.FfmpegPath, progress);
}

var downloadOptions = GetDownloadOptions(inputOptions);
var downloadOptions = GetDownloadOptions(inputOptions, progress);

var clipDownloader = new ClipDownloader(downloadOptions, progress);
clipDownloader.DownloadAsync(new CancellationToken()).Wait();
}

private static ClipDownloadOptions GetDownloadOptions(ClipDownloadArgs inputOptions)
private static ClipDownloadOptions GetDownloadOptions(ClipDownloadArgs inputOptions, ITaskLogger logger)
{
if (inputOptions.Id is null)
{
Console.WriteLine("[ERROR] - Clip ID/URL cannot be null!");
logger.LogError("Clip ID/URL cannot be null!");
Environment.Exit(1);
}

var clipIdMatch = TwitchRegex.MatchClipId(inputOptions.Id);
if (clipIdMatch is not { Success: true })
{
Console.WriteLine("[ERROR] - Unable to parse Clip ID/URL.");
logger.LogError("Unable to parse Clip ID/URL.");
Environment.Exit(1);
}

Expand Down
29 changes: 15 additions & 14 deletions TwitchDownloaderCLI/Modes/DownloadVideo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using TwitchDownloaderCLI.Modes.Arguments;
using TwitchDownloaderCLI.Tools;
using TwitchDownloaderCore;
using TwitchDownloaderCore.Interfaces;
using TwitchDownloaderCore.Options;
using TwitchDownloaderCore.Tools;

Expand All @@ -13,36 +14,28 @@ internal static class DownloadVideo
{
internal static void Download(VideoDownloadArgs inputOptions)
{
var progress = new CliTaskProgress();
var progress = new CliTaskProgress(inputOptions.LogLevel);

FfmpegHandler.DetectFfmpeg(inputOptions.FfmpegPath, progress);

var downloadOptions = GetDownloadOptions(inputOptions);
downloadOptions.CacheCleanerCallback = directoryInfos =>
{
progress.LogInfo(
$"{directoryInfos.Length} unmanaged video caches were found at '{downloadOptions.TempFolder}' and can be safely deleted. " +
"Run 'TwitchDownloaderCLI cache help' for more information.");

return Array.Empty<DirectoryInfo>();
};
var downloadOptions = GetDownloadOptions(inputOptions, progress);

var videoDownloader = new VideoDownloader(downloadOptions, progress);
videoDownloader.DownloadAsync(new CancellationToken()).Wait();
}

private static VideoDownloadOptions GetDownloadOptions(VideoDownloadArgs inputOptions)
private static VideoDownloadOptions GetDownloadOptions(VideoDownloadArgs inputOptions, ITaskLogger logger)
{
if (inputOptions.Id is null)
{
Console.WriteLine("[ERROR] - Vod ID/URL cannot be null!");
logger.LogError("Vod ID/URL cannot be null!");
Environment.Exit(1);
}

var vodIdMatch = TwitchRegex.MatchVideoId(inputOptions.Id);
if (vodIdMatch is not { Success: true })
{
Console.WriteLine("[ERROR] - Unable to parse Vod ID/URL.");
logger.LogError("Unable to parse Vod ID/URL.");
Environment.Exit(1);
}

Expand Down Expand Up @@ -74,7 +67,15 @@ private static VideoDownloadOptions GetDownloadOptions(VideoDownloadArgs inputOp
CropEnding = inputOptions.CropEndingTime > 0.0,
CropEndingTime = TimeSpan.FromSeconds(inputOptions.CropEndingTime),
FfmpegPath = string.IsNullOrWhiteSpace(inputOptions.FfmpegPath) ? FfmpegHandler.FfmpegExecutableName : Path.GetFullPath(inputOptions.FfmpegPath),
TempFolder = inputOptions.TempFolder
TempFolder = inputOptions.TempFolder,
CacheCleanerCallback = directoryInfos =>
{
logger.LogInfo(
$"{directoryInfos.Length} unmanaged video caches were found at '{inputOptions.TempFolder}' and can be safely deleted. " +
"Run 'TwitchDownloaderCLI cache help' for more information.");

return Array.Empty<DirectoryInfo>();
}
};

return downloadOptions;
Expand Down
Loading

0 comments on commit 1669dfc

Please sign in to comment.