forked from Unity-Technologies/ClusterDisplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogWriter.cs
53 lines (41 loc) · 1.66 KB
/
LogWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace Unity.ClusterDisplay.Editor.SourceGenerators
{
public class Debug
{
const string k_LogFolderPath = "C:/ClusterDisplay/";
const string k_LogFileName = "ClusterDisplay-SourceGenerator.txt";
readonly static SharedMutex k_Mutex = new SharedMutex("SourceGeneratorLogMutex");
static void Write(string msg)
{
k_Mutex.Lock();
try
{
if (!Directory.Exists(k_LogFolderPath))
{
Directory.CreateDirectory(k_LogFolderPath);
}
string logPath = $"{k_LogFolderPath}{k_LogFileName}";
var fileStream = new FileStream(logPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
fileStream.Seek(0, SeekOrigin.End);
var bytes = Encoding.UTF8.GetBytes(msg);
fileStream.Write(bytes, 0, bytes.Length);
fileStream.Write(Encoding.UTF8.GetBytes("\r\n"), 0, 2);
fileStream.Close();
fileStream.Dispose();
}
catch (System.Exception exception)
{
throw exception;
}
k_Mutex.Release();
}
public static void Log(string msg) => Write($"Log: {msg}");
public static void LogWarning(string msg) => Write($"Warning: {msg}");
public static void LogError(string msg) => Write($"Error: {msg}");
public static void LogException(Exception exception) => Write($"Exception: {exception.Message}\n{exception.StackTrace}");
}
}