Skip to content

Commit

Permalink
Merge pull request #24 from Elepover/pmcenter-lazer
Browse files Browse the repository at this point in the history
merge latest lazer branch into master
  • Loading branch information
Elepover authored Feb 20, 2020
2 parents edd4dcc + 7754cbf commit ee41696
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ A telegram bot helping you process private messages.
> - [⚙️ Prerequisites](#prerequisites)
> - [📥 Build `pmcenter` Yourself](#build-pmcenter-yourself)
> - [📩 Use Pre-compiled Binaries](#use-pre-compiled-binaries)
> - [🐋 Use Docker](#use-docker)
> - [🔧 Configuring](#configuring)
> - [⚒️ `pmcenter` Settings](#pmcenter-settings)
> - [📄 Note](#note)
Expand Down
1 change: 1 addition & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
> - [⚙️ 环境要求](#环境要求)
> - [📥 自行编译 `pmcenter`](#自行编译-pmcenter)
> - [📩 使用 CI 预编译二进制文件](#使用-ci-预编译二进制文件)
> - [🐋 使用 Docker](#使用-docker)
> - [🔧 配置](#配置)
> - [⚒️ `pmcenter` 设置](#pmcenter-设置)
> - [📄 注意事项](#注意事项)
Expand Down
22 changes: 21 additions & 1 deletion pmcenter/GlobalErrorHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using static pmcenter.Methods;

namespace pmcenter
{
Expand All @@ -11,9 +14,25 @@ private static void GlobalErrorHandler(object sender, UnhandledExceptionEventArg
L("pmcenter's global error handler has captured a critical error.");
L("If you believe that this is a bug, feel free to open an issue with the details below on pmcenter's GitHub repository at:");
L("https://github.com/Elepover/pmcenter");
L($"Is catastrophic? {(e.IsTerminating ? "yes" : "no")}.");
L(e.IsTerminating ? "" : "This is not a catastrophic. pmcenter will keep running.\nIf you encounter more errors, please consider restarting pmcenter.");
var exception = (Exception) e.ExceptionObject;
var fileName = Path.Combine(Environment.CurrentDirectory, $"pmcenter-error-{GetDateTimeString(true)}.log");
L($"Exception details: {exception.ToString()}");
L($"Attempting to write to {fileName}");
try
{
var writer = new StreamWriter(fileName, false, Encoding.UTF8) { AutoFlush = true };
writer.WriteLine($"pmcenter critical error log @{GetDateTimeString()} (local time)");
writer.WriteLine($"Framework: {RuntimeInformation.FrameworkDescription}\nSystem: {RuntimeInformation.OSDescription}");
writer.WriteLine("If you believe that this is a bug, feel free to open an issue with the details below on pmcenter's GitHub repository at https://github.com/Elepover/pmcenter");
writer.WriteLine($"==> HRESULT 0x{exception.HResult.ToString("x")} error details:");
writer.WriteLine(exception.ToString());
writer.Close();
}
catch (Exception ex)
{
L($"Unable to save error logs: {ex.ToString()}");
}
if (e.IsTerminating)
{
L("Since it's a catastrophic error, pmcenter will exit now.");
Expand All @@ -23,6 +42,7 @@ private static void GlobalErrorHandler(object sender, UnhandledExceptionEventArg

private static void L(string log)
{
if (string.IsNullOrEmpty(log)) return;
Console.Error.WriteLine(log);
}
}
Expand Down
19 changes: 19 additions & 0 deletions pmcenter/Methods/Methods.GetDateTimeString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.IO;

namespace pmcenter
{
public partial class Methods
{
public static string GetDateTimeString(bool removeInvalidChar = false)
{
var result = DateTime.Now.ToString("o");
if (!removeInvalidChar) return result;
foreach (var c in Path.GetInvalidFileNameChars())
{
result = result.Replace(c, '-');
}
return result;
}
}
}
1 change: 1 addition & 0 deletions pmcenter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static async Task MainAsync(string[] args)
Log("==> Running pre-start operations...");
// hook global errors (final failsafe)
AppDomain.CurrentDomain.UnhandledException += GlobalErrorHandler;
Log("Global error handler is armed and ready!");
// process commandlines
await CmdLineProcess.RunCommand(Environment.CommandLine).ConfigureAwait(false);
// everything (exits and/or errors) are handled above, please do not process.
Expand Down

0 comments on commit ee41696

Please sign in to comment.