Add log-file-path option

This commit is contained in:
qwer-lives 2025-01-28 18:31:35 +01:00
parent cd4dfb5e75
commit 575d6bc581
6 changed files with 48 additions and 16 deletions

View File

@ -22,7 +22,7 @@ public static partial class Logger
/// <summary>
/// 本次运行日志文件所在位置
/// </summary>
private static string? LogFilePath { get; set; }
public static string? LogFilePath { get; set; }
// 读写锁
static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
@ -33,26 +33,30 @@ public static partial class Logger
try
{
var logDir = Path.GetDirectoryName(Environment.ProcessPath) + "/Logs";
var logDir = Path.GetDirectoryName(LogFilePath) ?? (Path.GetDirectoryName(Environment.ProcessPath) + "/Logs");
if (!Directory.Exists(logDir))
{
Directory.CreateDirectory(logDir);
}
var now = DateTime.Now;
if (string.IsNullOrEmpty(LogFilePath))
{
LogFilePath = Path.Combine(logDir, now.ToString("yyyy-MM-dd_HH-mm-ss-fff") + ".log");
int index = 1;
var fileName = Path.GetFileNameWithoutExtension(LogFilePath);
string init = "LOG " + now.ToString("yyyy/MM/dd") + Environment.NewLine
+ "Save Path: " + Path.GetDirectoryName(LogFilePath) + Environment.NewLine
+ "Task Start: " + now.ToString("yyyy/MM/dd HH:mm:ss") + Environment.NewLine
+ "Task CommandLine: " + Environment.CommandLine;
init += $"{Environment.NewLine}{Environment.NewLine}";
// 若文件存在则加序号
while (File.Exists(LogFilePath))
{
LogFilePath = Path.Combine(Path.GetDirectoryName(LogFilePath)!, $"{fileName}-{index++}.log");
}
}
string init = "LOG " + now.ToString("yyyy/MM/dd") + Environment.NewLine
+ "Save Path: " + Path.GetDirectoryName(LogFilePath) + Environment.NewLine
+ "Task Start: " + now.ToString("yyyy/MM/dd HH:mm:ss") + Environment.NewLine
+ "Task CommandLine: " + Environment.CommandLine;
init += $"{Environment.NewLine}{Environment.NewLine}";
File.WriteAllText(LogFilePath, init, Encoding.UTF8);
}
catch (Exception ex)

View File

@ -62,6 +62,7 @@ public static class ResString
public static string cmd_saveDir => GetText("cmd_saveDir");
public static string cmd_saveName => GetText("cmd_saveName");
public static string cmd_savePattern => GetText("cmd_savePattern");
public static string cmd_logFilePath => GetText("cmd_logFilePath");
public static string cmd_skipDownload => GetText("cmd_skipDownload");
public static string cmd_noDateInfo => GetText("cmd_noDateInfo");
public static string cmd_noLog => GetText("cmd_noLog");

View File

@ -310,6 +310,12 @@ internal static class StaticText
zhTW: "",
enUS: ""
),
["cmd_logFilePath"] = new TextContainer
(
zhCN: "设置日志文件路径",
zhTW: "設定日誌檔案路徑",
enUS: "Set log file path"
),
["cmd_skipDownload"] = new TextContainer
(
zhCN: "跳过下载",

View File

@ -31,6 +31,7 @@ internal static partial class CommandInvoker
private static readonly Option<string?> SaveDir = new(["--save-dir"], description: ResString.cmd_saveDir);
private static readonly Option<string?> SaveName = new(["--save-name"], description: ResString.cmd_saveName, parseArgument: ParseSaveName);
private static readonly Option<string?> SavePattern = new(["--save-pattern"], description: ResString.cmd_savePattern, getDefaultValue: () => "<SaveName>_<Id>_<Codecs>_<Language>_<Ext>");
private static readonly Option<string?> LogFilePath = new(["--log-file-path"], description: ResString.cmd_logFilePath, parseArgument: ParseFilePath);
private static readonly Option<string?> UILanguage = new Option<string?>(["--ui-language"], description: ResString.cmd_uiLanguage).FromAmong("en-US", "zh-CN", "zh-TW");
private static readonly Option<string?> UrlProcessorArgs = new(["--urlprocessor-args"], description: ResString.cmd_urlProcessorArgs);
private static readonly Option<string[]?> Keys = new(["--key"], description: ResString.cmd_keys) { Arity = ArgumentArity.OneOrMore, AllowMultipleArgumentsPerToken = false };
@ -292,6 +293,20 @@ internal static partial class CommandInvoker
return newName;
}
private static string? ParseFilePath(ArgumentResult result)
{
var input = result.Tokens[0].Value;
var dir = Path.GetDirectoryName(input);
var filename = Path.GetFileName(input);
var newName = OtherUtil.GetValidFileName(filename);
if (string.IsNullOrEmpty(newName))
{
result.ErrorMessage = "Invalid log file name!";
return null;
}
return Path.Combine(dir!, newName);
}
/// <summary>
/// 流过滤器
/// </summary>
@ -515,6 +530,7 @@ internal static partial class CommandInvoker
TmpDir = bindingContext.ParseResult.GetValueForOption(TmpDir),
SaveDir = bindingContext.ParseResult.GetValueForOption(SaveDir),
SaveName = bindingContext.ParseResult.GetValueForOption(SaveName),
LogFilePath = bindingContext.ParseResult.GetValueForOption(LogFilePath),
ThreadCount = bindingContext.ParseResult.GetValueForOption(ThreadCount),
UILanguage = bindingContext.ParseResult.GetValueForOption(UILanguage),
SkipDownload = bindingContext.ParseResult.GetValueForOption(SkipDownload),
@ -613,7 +629,7 @@ internal static partial class CommandInvoker
var rootCommand = new RootCommand(VERSION_INFO)
{
Input, TmpDir, SaveDir, SaveName, BaseUrl, ThreadCount, DownloadRetryCount, HttpRequestTimeout, ForceAnsiConsole, NoAnsiColor,AutoSelect, SkipMerge, SkipDownload, CheckSegmentsCount,
Input, TmpDir, SaveDir, SaveName, LogFilePath, BaseUrl, ThreadCount, DownloadRetryCount, HttpRequestTimeout, ForceAnsiConsole, NoAnsiColor,AutoSelect, SkipMerge, SkipDownload, CheckSegmentsCount,
BinaryMerge, UseFFmpegConcatDemuxer, DelAfterDone, NoDateInfo, NoLog, WriteMetaJson, AppendUrlParams, ConcurrentDownload, Headers, /**SavePattern,**/ SubOnly, SubtitleFormat, AutoSubtitleFix,
FFmpegBinaryPath,
LogLevel, UILanguage, UrlProcessorArgs, Keys, KeyTextFile, DecryptionEngine, DecryptionBinaryPath, UseShakaPackager, MP4RealTimeDecryption,

View File

@ -190,6 +190,10 @@ internal class MyOption
/// </summary>
public string? SavePattern { get; set; }
/// <summary>
/// See: <see cref="CommandInvoker.LogFilePath"/>.
/// </summary>
public string? LogFilePath { get; set; }
/// <summary>
/// See: <see cref="CommandInvoker.UILanguage"/>.
/// </summary>
public string? UILanguage { get; set; }

View File

@ -101,6 +101,7 @@ internal class Program
_ = CheckUpdateAsync();
Logger.IsWriteFile = !option.NoLog;
Logger.LogFilePath = option.LogFilePath;
Logger.InitLogFile();
Logger.LogLevel = option.LogLevel;
Logger.Info(CommandInvoker.VERSION_INFO);