-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ca1d17
commit 1196db9
Showing
4 changed files
with
109 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,142 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace TaskbarIconHost | ||
{ | ||
public class PluginLogger : IPluginLogger | ||
{ | ||
public PluginLogger() | ||
{ | ||
#if DEBUG | ||
IsLogOn = true; | ||
#endif | ||
|
||
try | ||
{ | ||
string Location = Assembly.GetExecutingAssembly().Location; | ||
string SettingFilePath = Path.Combine(Path.GetDirectoryName(Location), "settings.txt"); | ||
|
||
if (File.Exists(SettingFilePath)) | ||
{ | ||
using (FileStream fs = new FileStream(SettingFilePath, FileMode.Open, FileAccess.Read)) | ||
{ | ||
using (StreamReader sr = new StreamReader(fs)) | ||
{ | ||
TraceFilePath = sr.ReadLine(); | ||
} | ||
} | ||
} | ||
|
||
if (!string.IsNullOrEmpty(TraceFilePath)) | ||
{ | ||
bool IsFirstTraceWritten = false; | ||
|
||
using (FileStream fs = new FileStream(TraceFilePath, FileMode.Append, FileAccess.Write)) | ||
{ | ||
using (StreamWriter sw = new StreamWriter(fs)) | ||
{ | ||
sw.WriteLine("** Log started **"); | ||
IsFirstTraceWritten = true; | ||
} | ||
} | ||
|
||
if (IsFirstTraceWritten) | ||
{ | ||
IsLogOn = true; | ||
IsFileLogOn = true; | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
PrintLine("Unable to start logging traces."); | ||
PrintLine(e.Message); | ||
} | ||
} | ||
|
||
public void AddLog(string text) | ||
{ | ||
AddLog(text, false); | ||
} | ||
|
||
public void AddLog(string text, bool showNow) | ||
{ | ||
#if DEBUG | ||
lock (GlobalLock) | ||
if (IsLogOn) | ||
{ | ||
DateTime UtcNow = DateTime.UtcNow; | ||
string TimeLog = UtcNow.ToString(CultureInfo.InvariantCulture) + UtcNow.Millisecond.ToString("D3"); | ||
lock (GlobalLock) | ||
{ | ||
DateTime UtcNow = DateTime.UtcNow; | ||
string TimeLog = UtcNow.ToString(CultureInfo.InvariantCulture) + UtcNow.Millisecond.ToString("D3"); | ||
|
||
string Line = $"TaskbarIconHost - {TimeLog}: {text}\n"; | ||
string Line = $"TaskbarIconHost - {TimeLog}: {text}\n"; | ||
|
||
if (LogLines == null) | ||
LogLines = Line; | ||
else | ||
LogLines += Line; | ||
if (LogLines == null) | ||
LogLines = Line; | ||
else | ||
LogLines += Line; | ||
} | ||
} | ||
#endif | ||
|
||
if (showNow) | ||
PrintLog(); | ||
} | ||
|
||
public void PrintLog() | ||
{ | ||
#if DEBUG | ||
lock (GlobalLock) | ||
if (IsLogOn) | ||
{ | ||
if (LogLines != null) | ||
lock (GlobalLock) | ||
{ | ||
string[] Lines = LogLines.Split('\n'); | ||
foreach (string Line in Lines) | ||
OutputDebugString(Line); | ||
if (LogLines != null) | ||
{ | ||
string[] Lines = LogLines.Split('\n'); | ||
foreach (string Line in Lines) | ||
PrintLine(Line); | ||
|
||
LogLines = null; | ||
LogLines = null; | ||
} | ||
} | ||
} | ||
#endif | ||
} | ||
|
||
private void PrintLine(string line) | ||
{ | ||
OutputDebugString(line); | ||
|
||
if (IsFileLogOn) | ||
WriteLineToTraceFile(line); | ||
} | ||
|
||
private void WriteLineToTraceFile(string line) | ||
{ | ||
try | ||
{ | ||
if (line.Length == 0) | ||
return; | ||
|
||
using (FileStream fs = new FileStream(TraceFilePath, FileMode.Append, FileAccess.Write)) | ||
{ | ||
using (StreamWriter sw = new StreamWriter(fs)) | ||
{ | ||
sw.WriteLine(line); | ||
} | ||
} | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
|
||
[DllImport("kernel32", CharSet = CharSet.Unicode)] | ||
public static extern void OutputDebugString([In][MarshalAs(UnmanagedType.LPWStr)] string message); | ||
|
||
#if DEBUG | ||
private string LogLines = null; | ||
#endif | ||
private object GlobalLock = ""; | ||
private bool IsLogOn; | ||
private bool IsFileLogOn; | ||
private string TraceFilePath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters