Add program log

This commit is contained in:
2022-12-02 20:50:12 +08:00
parent 7ec81ab146
commit 2300b8d5db
4 changed files with 93 additions and 24 deletions

View File

@ -34,9 +34,11 @@ namespace GrasscutterTools.Forms
{
#region - Init -
private const string TAG = "FormMain";
public FormMain()
{
Console.WriteLine("FormMain ctor enter");
Logger.I(TAG, "FormMain ctor enter");
InitializeComponent();
Icon = Resources.IconGrasscutter;
@ -49,14 +51,14 @@ namespace GrasscutterTools.Forms
{
StartPosition = FormStartPosition.Manual;
Location = Settings.Default.MainFormLocation;
Console.WriteLine("Restore window location: " + Location.ToString());
Logger.I(TAG, "Restore window location: " + Location.ToString());
}
// 还原窗体大小
if (Settings.Default.MainFormSize != default)
{
Size = Settings.Default.MainFormSize;
Console.WriteLine("Restore window size: " + Size.ToString());
Logger.I(TAG, "Restore window size: " + Size.ToString());
}
// 初始化页面
@ -66,9 +68,10 @@ namespace GrasscutterTools.Forms
}
catch (Exception ex)
{
Logger.E(TAG, "Loading settings error", ex);
MessageBox.Show(Resources.SettingLoadError + ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Console.WriteLine("FormMain ctor completed");
Logger.I(TAG, "FormMain ctor completed");
}
/// <summary>
@ -76,7 +79,7 @@ namespace GrasscutterTools.Forms
/// </summary>
private void InitPages()
{
Console.WriteLine("InitPages enter");
Logger.I(TAG, "InitPages enter");
TCMain.SuspendLayout();
var ph = CreatePage<PageHome>();
ph.OnLanguageChanged = () => FormMain_Load(this, EventArgs.Empty);
@ -96,7 +99,7 @@ namespace GrasscutterTools.Forms
TPScene.Controls.Add(CreatePage<PageScene>());
TPAbout.Controls.Add(CreatePage<PageAbout>());
TCMain.ResumeLayout();
Console.WriteLine("InitPages completed");
Logger.I(TAG, "InitPages completed");
}
/// <summary>
@ -112,7 +115,7 @@ namespace GrasscutterTools.Forms
RunCommands = RunCommands,
GetCommand = () => CmbCommand.Text,
Dock = DockStyle.Fill,
Name = nameof(T)
Name = typeof(T).Name,
};
return page;
}
@ -122,7 +125,7 @@ namespace GrasscutterTools.Forms
/// </summary>
private void FormMain_Load(object sender, EventArgs e)
{
Console.WriteLine("FormMain_Load enter");
Logger.I(TAG, "FormMain_Load enter");
Text += " - by jie65535 - v" + Common.AppVersion.ToString(3);
#if DEBUG
Text += "-debug";
@ -137,12 +140,12 @@ namespace GrasscutterTools.Forms
{
if (tp.Controls.Count > 0 && tp.Controls[0] is BasePage page)
{
Console.WriteLine($"{page.Name} OnLoad enter");
Logger.I(TAG, $"{page.Name} OnLoad enter");
page.OnLoad();
Console.WriteLine($"{page.Name} OnLoad completed");
Logger.I(TAG, $"{page.Name} OnLoad completed");
}
}
Console.WriteLine("FormMain_Load completed");
Logger.I(TAG, "FormMain_Load completed");
}
/// <summary>
@ -150,7 +153,7 @@ namespace GrasscutterTools.Forms
/// </summary>
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
Console.WriteLine("FormMain FormClosed enter");
Logger.I(TAG, "FormMain FormClosed enter");
// 遍历每一个页面,通知关闭
foreach (TabPage tp in TCMain.Controls)
{
@ -160,7 +163,7 @@ namespace GrasscutterTools.Forms
// 保存当前设置
SaveSettings();
Console.WriteLine("FormMain FormClosed completed");
Logger.I(TAG, "FormMain FormClosed completed");
}
/// <summary>
@ -198,7 +201,7 @@ namespace GrasscutterTools.Forms
/// <param name="command">命令</param>
private void SetCommand(string command)
{
Console.WriteLine($"SetCommand(\"{command}\")");
Logger.I(TAG, $"SetCommand(\"{command}\")");
var oldCommand = CmbCommand.Text;
CmbCommand.Text = (ModifierKeys == Keys.Shift) ? $"{oldCommand} | {command}" : command;
if (ChkAutoCopy.Checked)
@ -337,14 +340,14 @@ namespace GrasscutterTools.Forms
var cmd = command.TrimStart('/');
try
{
Console.WriteLine("RunCommand:" + cmd);
Logger.I(TAG, "RunCommand:" + cmd);
var msg = await Common.OC.Invoke(cmd);
TxtCommandRunLog.AppendText(string.IsNullOrEmpty(msg) ? "OK" : msg);
TxtCommandRunLog.AppendText(Environment.NewLine);
}
catch (Exception ex)
{
Console.WriteLine("RunCommand Error:" + ex.ToString());
Logger.W(TAG, "RunCommand Error:", ex);
TxtCommandRunLog.AppendText("Error: ");
TxtCommandRunLog.AppendText(ex.Message);
TxtCommandRunLog.AppendText(Environment.NewLine);

View File

@ -248,6 +248,7 @@
<Compile Include="Utils\ArtifactUtils.cs" />
<Compile Include="Utils\Common.cs" />
<Compile Include="Utils\HttpHelper.cs" />
<Compile Include="Utils\Logger.cs" />
<Compile Include="Utils\ReleaseAPI.cs" />
<Compile Include="Utils\UIUtil.cs" />
<EmbeddedResource Include="Forms\FormDropEditor.en-US.resx">

View File

@ -24,11 +24,14 @@ using System.Threading;
using System.Windows.Forms;
using GrasscutterTools.Properties;
using GrasscutterTools.Utils;
namespace GrasscutterTools
{
internal static class Program
{
private const string TAG = "Program";
static Program()
{
AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
@ -72,12 +75,22 @@ namespace GrasscutterTools
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
// 初始化语言环境
if (!string.IsNullOrEmpty(Settings.Default.DefaultLanguage))
MultiLanguage.SetDefaultLanguage(Settings.Default.DefaultLanguage);
Logger.I(TAG, "Program startup");
try
{
// 初始化语言环境
if (!string.IsNullOrEmpty(Settings.Default.DefaultLanguage))
MultiLanguage.SetDefaultLanguage(Settings.Default.DefaultLanguage);
}
catch (Exception ex)
{
Logger.W(TAG, "Loading language error", ex);
MessageBox.Show(Resources.SettingLoadError + ex.ToString(), Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Application.Run(new Forms.FormMain());
Console.WriteLine("Program end.");
Logger.I(TAG, "Program end.");
}
#region - -
@ -85,16 +98,16 @@ namespace GrasscutterTools
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
Console.WriteLine("Application_ThreadException");
Console.WriteLine(str);
Logger.E(TAG, "Application_ThreadException");
Logger.E(TAG, str);
MessageBox.Show(str, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
Console.WriteLine("CurrentDomain_UnhandledException");
Console.WriteLine(str);
Logger.E(TAG, "CurrentDomain_UnhandledException");
Logger.E(TAG, str);
MessageBox.Show(str, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

View File

@ -0,0 +1,52 @@
/**
* Grasscutter Tools
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
**/
using System;
using System.IO;
using System.Windows.Forms;
namespace GrasscutterTools.Utils
{
public static class Logger
{
private static readonly string LogPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), $"GcTools-{DateTime.Now:MMdd}.log");
private static void Write(string message)
{
#if DEBUG
Console.WriteLine($"{DateTime.Now:mm:ss.fff} {message}");
#else
// Test log
//File.AppendAllText(LogPath, $"{DateTime.Now:mm:ss.fff} {message}{Environment.NewLine}");
#endif
}
private static void Write(string level, string tag, string message) => Write($"<{level}:{tag}> {message}");
public static void I(string tag, string info) => Write("INFO", tag, info);
public static void W(string tag, string message) => Write("WARR", tag, message);
public static void W(string tag, string message, Exception ex) => Write("WARR", tag, $"{message} {ex}");
public static void E(string tag, string message) => Write("ERROR", tag, message);
public static void E(string tag, string message, Exception ex) => Write("ERROR", tag, $"{message} {ex}");
}
}