101 lines
4.1 KiB
C#
101 lines
4.1 KiB
C#
using NLog;
|
|
using SkiaSharp;
|
|
using Spectre.Console;
|
|
using Spine;
|
|
using Spine.Exporters;
|
|
using System.CommandLine;
|
|
using System.Globalization;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace SpineViewerCLI
|
|
{
|
|
public static class SpineViewerCLI
|
|
{
|
|
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
|
|
|
public static Option<bool> OptQuiet { get; } = new("--quiet", "-q")
|
|
{
|
|
Description = "Suppress console logging (quiet mode).",
|
|
Recursive = true,
|
|
};
|
|
|
|
public static int Main(string[] args)
|
|
{
|
|
InitializeFileLog();
|
|
|
|
var cmdRoot = new RootCommand("Root Command")
|
|
{
|
|
OptQuiet,
|
|
new QueryCommand(),
|
|
new PreviewCommand(),
|
|
new ExportCommand(),
|
|
};
|
|
|
|
var result = cmdRoot.Parse(args);
|
|
|
|
if (!result.GetValue(OptQuiet))
|
|
InitializeConsoleLog();
|
|
|
|
try
|
|
{
|
|
return result.Invoke();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Debug(ex.ToString());
|
|
_logger.Fatal("Failed to execute, {0}", ex.Message);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
private static void InitializeFileLog()
|
|
{
|
|
// XXX: 未知原因 linux 平台上无法正常生成日志文件
|
|
var config = new NLog.Config.LoggingConfiguration();
|
|
var fileTarget = new NLog.Targets.FileTarget("fileTarget")
|
|
{
|
|
Encoding = System.Text.Encoding.UTF8,
|
|
Layout = "${date:format=yyyy-MM-dd HH\\:mm\\:ss} - ${level:uppercase=true} - ${processid} - ${callsite-filename:includeSourcePath=false}:${callsite-linenumber} - ${message}",
|
|
AutoFlush = true,
|
|
CreateDirs = true,
|
|
FileName = "${basedir}/logs/cli.log",
|
|
ArchiveFileName = "${basedir}/logs/cli.{#}.log",
|
|
ArchiveNumbering = NLog.Targets.ArchiveNumberingMode.Rolling,
|
|
ArchiveAboveSize = 1048576,
|
|
MaxArchiveFiles = 5,
|
|
ConcurrentWrites = true,
|
|
KeepFileOpen = false,
|
|
};
|
|
|
|
config.AddTarget(fileTarget);
|
|
config.AddRule(LogLevel.Trace, LogLevel.Fatal, fileTarget);
|
|
LogManager.Configuration = config;
|
|
}
|
|
|
|
private static void InitializeConsoleLog()
|
|
{
|
|
var config = new NLog.Config.LoggingConfiguration();
|
|
var consoleTarget = new NLog.Targets.ColoredConsoleTarget("consoleTarget")
|
|
{
|
|
Encoding = System.Text.Encoding.UTF8,
|
|
Layout = "[${level:format=OneLetter}]${date:format=yyyy-MM-dd HH\\:mm\\:ss} - ${message}",
|
|
AutoFlush = true,
|
|
DetectConsoleAvailable = true,
|
|
StdErr = true,
|
|
DetectOutputRedirected = true,
|
|
};
|
|
|
|
consoleTarget.RowHighlightingRules.Add(new("level == LogLevel.Trace", NLog.Targets.ConsoleOutputColor.DarkGray, NLog.Targets.ConsoleOutputColor.NoChange));
|
|
consoleTarget.RowHighlightingRules.Add(new("level == LogLevel.Debug", NLog.Targets.ConsoleOutputColor.DarkGray, NLog.Targets.ConsoleOutputColor.NoChange));
|
|
consoleTarget.RowHighlightingRules.Add(new("level == LogLevel.Info", NLog.Targets.ConsoleOutputColor.DarkGray, NLog.Targets.ConsoleOutputColor.NoChange));
|
|
consoleTarget.RowHighlightingRules.Add(new("level == LogLevel.Warn", NLog.Targets.ConsoleOutputColor.DarkYellow, NLog.Targets.ConsoleOutputColor.NoChange));
|
|
consoleTarget.RowHighlightingRules.Add(new("level == LogLevel.Error", NLog.Targets.ConsoleOutputColor.Red, NLog.Targets.ConsoleOutputColor.NoChange));
|
|
consoleTarget.RowHighlightingRules.Add(new("level == LogLevel.Fatal", NLog.Targets.ConsoleOutputColor.White, NLog.Targets.ConsoleOutputColor.DarkRed));
|
|
|
|
config.AddTarget(consoleTarget);
|
|
config.AddRule(LogLevel.Trace, LogLevel.Fatal, consoleTarget);
|
|
LogManager.Configuration = config;
|
|
}
|
|
}
|
|
}
|