2020-12-16 19:12:36 +08:00
|
|
|
using System;
|
2021-02-23 18:03:37 +08:00
|
|
|
using System.CommandLine;
|
|
|
|
using System.CommandLine.Invocation;
|
2021-02-27 13:28:21 +08:00
|
|
|
using System.Diagnostics;
|
2020-12-16 19:12:36 +08:00
|
|
|
using System.IO;
|
|
|
|
using System.Runtime.ExceptionServices;
|
2021-04-14 23:46:24 +08:00
|
|
|
using System.Runtime.InteropServices;
|
2020-12-16 19:12:36 +08:00
|
|
|
using System.Security;
|
2021-02-23 18:03:37 +08:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-12-16 19:12:36 +08:00
|
|
|
using System.Windows.Threading;
|
2021-04-14 23:46:24 +08:00
|
|
|
using BililiveRecorder.ToolBox;
|
2020-12-16 19:12:36 +08:00
|
|
|
using Sentry;
|
2021-02-23 18:03:37 +08:00
|
|
|
using Serilog;
|
|
|
|
using Serilog.Core;
|
|
|
|
using Serilog.Exceptions;
|
|
|
|
using Serilog.Formatting.Compact;
|
2020-12-16 19:12:36 +08:00
|
|
|
|
2021-02-23 18:03:37 +08:00
|
|
|
#nullable enable
|
2020-12-16 19:12:36 +08:00
|
|
|
namespace BililiveRecorder.WPF
|
|
|
|
{
|
2021-02-23 18:03:37 +08:00
|
|
|
internal static class Program
|
2020-12-16 19:12:36 +08:00
|
|
|
{
|
2021-02-23 18:03:37 +08:00
|
|
|
private const int CODE__WPF = 0x5F_57_50_46;
|
2020-12-16 19:12:36 +08:00
|
|
|
|
2021-02-23 18:03:37 +08:00
|
|
|
internal static readonly LoggingLevelSwitch levelSwitchGlobal;
|
|
|
|
internal static readonly LoggingLevelSwitch levelSwitchConsole;
|
|
|
|
internal static readonly Logger logger;
|
|
|
|
internal static readonly Update update;
|
|
|
|
internal static Task? updateTask;
|
|
|
|
|
|
|
|
static Program()
|
2020-12-16 19:12:36 +08:00
|
|
|
{
|
2021-04-14 23:46:24 +08:00
|
|
|
AttachConsole(-1);
|
2021-02-23 18:03:37 +08:00
|
|
|
levelSwitchGlobal = new LoggingLevelSwitch(Serilog.Events.LogEventLevel.Debug);
|
2021-02-27 13:28:21 +08:00
|
|
|
if (Debugger.IsAttached)
|
|
|
|
levelSwitchGlobal.MinimumLevel = Serilog.Events.LogEventLevel.Verbose;
|
2021-02-23 18:03:37 +08:00
|
|
|
levelSwitchConsole = new LoggingLevelSwitch(Serilog.Events.LogEventLevel.Error);
|
|
|
|
logger = BuildLogger();
|
2020-12-16 19:12:36 +08:00
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
2021-02-23 18:03:37 +08:00
|
|
|
Log.Logger = logger;
|
|
|
|
SentrySdk.ConfigureScope(s =>
|
2020-12-16 19:12:36 +08:00
|
|
|
{
|
2021-03-01 21:38:13 +08:00
|
|
|
s.SetTag("fullsemver", GitVersionInformation.FullSemVer);
|
2021-02-23 18:03:37 +08:00
|
|
|
try
|
2020-12-16 19:12:36 +08:00
|
|
|
{
|
2021-02-23 18:03:37 +08:00
|
|
|
var path = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "..", "packages", ".betaId"));
|
|
|
|
if (!File.Exists(path)) return;
|
|
|
|
var content = File.ReadAllText(path);
|
2021-02-27 20:44:04 +08:00
|
|
|
if (Guid.TryParse(content, out var id))
|
|
|
|
s.User = new User { Id = id.ToString() };
|
2020-12-16 19:12:36 +08:00
|
|
|
}
|
2021-02-23 18:03:37 +08:00
|
|
|
catch (Exception) { }
|
|
|
|
});
|
|
|
|
update = new Update(logger);
|
|
|
|
}
|
2020-12-16 19:12:36 +08:00
|
|
|
|
2021-02-23 18:03:37 +08:00
|
|
|
[STAThread]
|
|
|
|
public static int Main(string[] args)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
logger.Debug("Starting, CurrentDirectory: {CurrentDirectory}, CommandLine: {CommandLine}", Environment.CurrentDirectory, Environment.CommandLine);
|
|
|
|
var code = BuildCommand().Invoke(args);
|
|
|
|
return code == CODE__WPF ? Commands.RunWpfReal() : code;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
logger.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static RootCommand BuildCommand()
|
|
|
|
{
|
|
|
|
var run = new Command("run", "Run BililiveRecorder at path")
|
|
|
|
{
|
|
|
|
new Argument<string>("path","Work directory")
|
|
|
|
|
|
|
|
};
|
|
|
|
run.Handler = CommandHandler.Create((string path) => Commands.RunWpfHandler(path, false));
|
|
|
|
|
|
|
|
var root = new RootCommand("")
|
|
|
|
{
|
|
|
|
run,
|
|
|
|
new Option<bool>("--squirrel-firstrun")
|
2020-12-16 19:12:36 +08:00
|
|
|
{
|
2021-02-23 18:03:37 +08:00
|
|
|
IsHidden = true
|
2021-04-14 23:46:24 +08:00
|
|
|
},
|
|
|
|
new ToolCommand(),
|
2021-02-23 18:03:37 +08:00
|
|
|
};
|
|
|
|
root.Handler = CommandHandler.Create((bool squirrelFirstrun) => Commands.RunWpfHandler(null, squirrelFirstrun));
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class Commands
|
|
|
|
{
|
|
|
|
internal static int RunWpfHandler(string? path, bool squirrelFirstrun)
|
|
|
|
{
|
|
|
|
Pages.RootPage.CommandArgumentRecorderPath = path;
|
|
|
|
Pages.RootPage.CommandArgumentFirstRun = squirrelFirstrun;
|
|
|
|
return CODE__WPF;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static int RunWpfReal()
|
2020-12-16 19:12:36 +08:00
|
|
|
{
|
2021-02-23 18:03:37 +08:00
|
|
|
var cancel = new CancellationTokenSource();
|
|
|
|
var token = cancel.Token;
|
|
|
|
try
|
2020-12-19 17:27:45 +08:00
|
|
|
{
|
2021-02-23 18:03:37 +08:00
|
|
|
var app = new App();
|
|
|
|
app.InitializeComponent();
|
|
|
|
app.DispatcherUnhandledException += App_DispatcherUnhandledException;
|
|
|
|
|
|
|
|
updateTask = Task.Run(async () =>
|
2020-12-19 17:27:45 +08:00
|
|
|
{
|
2021-02-23 18:03:37 +08:00
|
|
|
while (!token.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
await update.UpdateAsync().ConfigureAwait(false);
|
|
|
|
await Task.Delay(TimeSpan.FromDays(1), token).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return app.Run();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
cancel.Cancel();
|
|
|
|
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
|
|
|
|
update.WaitForUpdatesOnShutdownAsync().GetAwaiter().GetResult();
|
|
|
|
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
|
|
|
|
}
|
2020-12-16 19:12:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 18:03:37 +08:00
|
|
|
private static Logger BuildLogger() => new LoggerConfiguration()
|
|
|
|
.MinimumLevel.ControlledBy(levelSwitchGlobal)
|
|
|
|
.Enrich.WithProcessId()
|
|
|
|
.Enrich.WithThreadId()
|
|
|
|
.Enrich.WithThreadName()
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
.Enrich.WithExceptionDetails()
|
|
|
|
.WriteTo.Console(levelSwitch: levelSwitchConsole)
|
2021-02-27 13:28:21 +08:00
|
|
|
#if DEBUG
|
|
|
|
.WriteTo.Debug()
|
|
|
|
.WriteTo.Sink<WpfLogEventSink>(Serilog.Events.LogEventLevel.Debug)
|
|
|
|
#else
|
|
|
|
.WriteTo.Sink<WpfLogEventSink>(Serilog.Events.LogEventLevel.Information)
|
|
|
|
#endif
|
2021-04-14 23:46:24 +08:00
|
|
|
.WriteTo.File(new CompactJsonFormatter(), "./logs/bilirec.txt", shared: true, rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true)
|
2021-02-23 18:03:37 +08:00
|
|
|
.WriteTo.Sentry(o =>
|
|
|
|
{
|
2021-03-01 22:46:16 +08:00
|
|
|
o.Dsn = "https://7c6c5da3140543809661813aaa836207@o210546.ingest.sentry.io/5556540";
|
2021-02-23 18:03:37 +08:00
|
|
|
|
|
|
|
o.DisableAppDomainUnhandledExceptionCapture();
|
|
|
|
o.AddExceptionFilterForType<System.Net.Http.HttpRequestException>();
|
|
|
|
|
|
|
|
o.MinimumBreadcrumbLevel = Serilog.Events.LogEventLevel.Debug;
|
|
|
|
o.MinimumEventLevel = Serilog.Events.LogEventLevel.Error;
|
2021-03-01 22:46:16 +08:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
o.Environment = "debug-build";
|
|
|
|
#else
|
|
|
|
o.Environment = "release-build";
|
|
|
|
#endif
|
2021-02-23 18:03:37 +08:00
|
|
|
})
|
|
|
|
.CreateLogger();
|
|
|
|
|
2021-04-14 23:46:24 +08:00
|
|
|
[DllImport("kernel32")]
|
|
|
|
private static extern bool AttachConsole(int pid);
|
|
|
|
|
2020-12-16 19:12:36 +08:00
|
|
|
[HandleProcessCorruptedStateExceptions, SecurityCritical]
|
|
|
|
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.ExceptionObject is Exception ex)
|
|
|
|
logger.Fatal(ex, "Unhandled exception from Application.UnhandledException");
|
|
|
|
}
|
|
|
|
|
|
|
|
[HandleProcessCorruptedStateExceptions, SecurityCritical]
|
2021-02-23 18:03:37 +08:00
|
|
|
private static void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) =>
|
2020-12-16 19:12:36 +08:00
|
|
|
logger.Fatal(e.Exception, "Unhandled exception from AppDomain.DispatcherUnhandledException");
|
|
|
|
}
|
|
|
|
}
|