Skip to content

Commit 4a79d18

Browse files
committed
added filelog
close #9
1 parent 9018b7c commit 4a79d18

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

LighthouseV2PowerControl/Program.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Drawing.Text;
34
using System.IO;
45
using System.Text.RegularExpressions;
56
using System.Threading.Tasks;
@@ -22,7 +23,7 @@ static class Program
2223
private static readonly byte deactivateByte = 0x00;
2324
private static List<GattCharacteristic> listGattCharacteristics = new List<GattCharacteristic>();
2425

25-
26+
private static readonly string logFileName = Path.Combine(Directory.GetCurrentDirectory(), "Logs");
2627
public delegate void LogHandler(object msg, LogType type);
2728
public static event LogHandler OnLog;
2829
private static Form1 app = null;
@@ -38,6 +39,7 @@ static void Main(string[] args)
3839
Application.ApplicationExit += (obj, e) => cancellationToken.Cancel();
3940
app = new Form1();
4041
OnLog += (msg, type) => app.Log(msg, type);
42+
LogFileStart();
4143
if (args.Length > 0)
4244
{
4345
app.ShowInTaskbar = false;
@@ -232,16 +234,31 @@ private static async void UseArgumentsAsync(string[] args)
232234

233235
private static void Log(object msg)
234236
{
237+
WriteToLogFile(msg, LogType.log);
235238
if (app == null) return;
236239
OnLog.Invoke(msg, LogType.log);
237240
}
238241

239242
private static void LogError(object msg)
240243
{
244+
WriteToLogFile(msg, LogType.error);
241245
if (app == null) return;
242246
OnLog.Invoke(msg, LogType.error);
243247
}
244248

249+
private static void LogFileStart()
250+
{
251+
File.WriteAllText(logFileName, null);
252+
}
253+
254+
private async static Task WriteToLogFile(object msg, LogType type)
255+
{
256+
string res = $"[{DateTime.Now}]";
257+
res += "[" + ((type == LogType.log)? "Log" : "ERROR") + "] ";
258+
res += msg.ToString();
259+
await File.AppendAllTextAsync(logFileName, res + "\n");
260+
}
261+
245262
private static void AppManifest(WithManifestTask task)
246263
{
247264
EVRInitError evrInitError = EVRInitError.None;

0 commit comments

Comments
 (0)