Skip to content

Commit faa4fca

Browse files
committed
make logger write and flush asynchronously just in case to avoid I/O ever blocking main thread
1 parent 68d34fa commit faa4fca

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

CodeStats/Logger.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System;
33
using System.IO;
44
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57
using System.Windows.Forms;
68

79
namespace CodeStats
@@ -18,7 +20,8 @@ static class Logger
1820
{
1921
internal static string configDir;
2022
internal static bool hasAlreadyShownErrorBox = false;
21-
internal static StreamWriter writer;
23+
private static StreamWriter writer;
24+
private static SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
2225

2326
internal static void Debug(string msg)
2427
{
@@ -47,30 +50,36 @@ internal static void Error(string msg, Exception ex = null)
4750

4851
internal static void Log(LogLevel level, string msg)
4952
{
50-
try
51-
{
52-
//var writer = Setup();
53-
if (writer == null) writer = Setup(); // we'll try to keep the file opened all the time and see how bad we end up with it
54-
if (writer == null) return;
55-
56-
writer.WriteLine("[Code::Stats {0} {1}] {2}", Enum.GetName(level.GetType(), level), DateTime.Now.ToString("HH:mm:ss"), msg);
57-
writer.Flush();
58-
//writer.Close();
59-
}
60-
catch (Exception ex)
53+
Task.Run(async () =>
6154
{
62-
if (!hasAlreadyShownErrorBox)
55+
try
6356
{
64-
hasAlreadyShownErrorBox = true;
65-
MessageBox.Show(ex.ToString() + "\n\nNo further log writing errors will be shown in this session to avoid interrupting your work.", "Error writing to log file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
66-
//MessageBox.Show(string.Format("{0}\\{1}.log", configDir, Constants.PluginName), "Error writing to log file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
57+
await semaphore.WaitAsync();
58+
59+
if (writer == null) writer = Setup(); // we'll try to keep the file opened all the time and see how bad we end up with it
60+
if (writer == null) return;
61+
62+
await writer.WriteLineAsync(String.Format("[Code::Stats {0} {1}] {2}", Enum.GetName(level.GetType(), level), DateTime.Now.ToString("HH:mm:ss"), msg));
63+
await writer.FlushAsync();
6764
}
68-
}
65+
catch (Exception ex)
66+
{
67+
if (!hasAlreadyShownErrorBox)
68+
{
69+
hasAlreadyShownErrorBox = true;
70+
MessageBox.Show(ex.ToString() + "\n\nNo further log writing errors will be shown in this session to avoid interrupting your work.", "Error writing to log file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
71+
//MessageBox.Show(string.Format("{0}\\{1}.log", configDir, Constants.PluginName), "Error writing to log file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
72+
}
73+
}
74+
finally
75+
{
76+
semaphore.Release();
77+
}
78+
});
6979
}
7080

7181
private static StreamWriter Setup()
7282
{
73-
//var configDir = Dependencies.AppDataDirectory;
7483
if (String.IsNullOrWhiteSpace(configDir))
7584
{
7685
// get path of plugin configuration
@@ -87,7 +96,6 @@ private static StreamWriter Setup()
8796

8897
public static void Delete()
8998
{
90-
//var configDir = Dependencies.AppDataDirectory;
9199
if (String.IsNullOrWhiteSpace(configDir))
92100
{
93101
// get path of plugin configuration

0 commit comments

Comments
 (0)