Add save log item in window ctx

This commit is contained in:
mashed-potatoes 2020-07-12 21:19:59 +05:00
parent 712cbb7ae5
commit c7cdf3d4df
3 changed files with 96 additions and 14 deletions

View file

@ -1,15 +1,16 @@
<Window x:Class="PotatoNV_next.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:PotatoNV_next.Controls"
mc:Ignorable="d"
Title="PotatoNV Next"
Height="416"
Width="720"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize">
<Window
x:Class="PotatoNV_next.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:PotatoNV_next.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="PotatoNV Next"
Width="720"
Height="416"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
<Window.Resources>
<Style TargetType="StackPanel">
<Setter Property="Margin" Value="4" />
@ -25,7 +26,7 @@
<Setter Property="Padding" Value="2" />
</Style>
</Window.Resources>
<UniformGrid Columns="2">
<TabControl Margin="3">
<TabItem Header="NVME Update">

View file

@ -1,11 +1,55 @@
using PotatoNV_next.Utils;
using Microsoft.Win32;
using PotatoNV_next.Utils;
using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace PotatoNV_next
{
public partial class MainWindow : Window
{
#region Win32 interop handle
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
private static extern bool InsertMenu(IntPtr hMenu, int wPosition, int wFlags, int wIDNewItem, string lpNewItem);
public const int WM_SYSCOMMAND = 0x112;
public const int MF_SEPARATOR = 0x800;
public const int MF_BYPOSITION = 0x400;
public const int MF_STRING = 0x0;
public const int TB_SAVE_LOGS = 1000;
public IntPtr Handle
{
get
{
return new WindowInteropHelper(this).Handle;
}
}
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_SYSCOMMAND)
{
switch (wParam.ToInt32())
{
case TB_SAVE_LOGS:
SaveLogs();
handled = true;
break;
}
}
return IntPtr.Zero;
}
#endregion
readonly Core core = new Core();
public MainWindow()
@ -15,10 +59,38 @@ namespace PotatoNV_next
nvFrom.OnFormSubmit += NvFrom_OnFormSubmit;
core.RunWorkerCompleted += Core_RunWorkerCompleted;
Loaded += MainWindow_Loaded;
SetupLog();
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
IntPtr systemMenuHandle = GetSystemMenu(Handle, false);
InsertMenu(systemMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty);
InsertMenu(systemMenuHandle, 6, MF_BYPOSITION, TB_SAVE_LOGS, "Save log to file");
HwndSource source = HwndSource.FromHwnd(Handle);
source.AddHook(new HwndSourceHook(WndProc));
}
private static void SaveLogs()
{
var dialog = new SaveFileDialog
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
CheckPathExists = true,
DefaultExt = "log",
Filter = "Log file (*.log)|*.log"
};
if (dialog.ShowDialog() == true)
{
File.WriteAllText(dialog.FileName, Log.GetLog());
}
}
private void NvFrom_OnFormSubmit(Controls.NVForm.FormEventArgs formEventArgs)
{
SetupLog();

View file

@ -1,10 +1,12 @@
using System;
using System.Text;
namespace PotatoNV_next.Utils
{
public class Log
{
public static bool PrintDebug { get; set; } = false;
private static StringBuilder builder = new StringBuilder();
public delegate void LogHandler(LogEventArgs logEventArgs);
public static event LogHandler Notify;
@ -28,6 +30,8 @@ namespace PotatoNV_next.Utils
var buffer = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} | {status.ToString().ToUpper(),-8} | {message}";
builder.AppendLine(buffer);
if (status == Status.Debug)
{
if (!PrintDebug)
@ -86,6 +90,11 @@ namespace PotatoNV_next.Utils
Value = value
});
}
public static string GetLog()
{
return builder.ToString();
}
}
public class LogEventArgs : EventArgs