This repository has been archived by the owner on Apr 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
VRCModLogger.cs
85 lines (73 loc) · 2.98 KB
/
VRCModLogger.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.IO;
using UnityEngine;
namespace VRCModLoader
{
public class VRCModLogger
{
internal static bool consoleEnabled = false;
private static StreamWriter log;
internal static void Init()
{
string logFilePath = Path.Combine(Environment.CurrentDirectory, ("Logs/VRCModLoader_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff") + ".log"));
FileInfo logFileInfo = new FileInfo(logFilePath);
DirectoryInfo logDirInfo = new DirectoryInfo(logFileInfo.DirectoryName);
if (!logDirInfo.Exists)
logDirInfo.Create();
else
CleanOld(logDirInfo);
FileStream fileStream = null;
if (!logFileInfo.Exists)
fileStream = logFileInfo.Create();
else
fileStream = new FileStream(logFilePath, FileMode.Open, FileAccess.Write, FileShare.Read);
log = new StreamWriter(fileStream);
log.AutoFlush = true;
Log("Logger Initialized");
}
internal static void CleanOld(DirectoryInfo logDirInfo)
{
FileInfo[] filetbl = logDirInfo.GetFiles("VRCModLoader_*");
if (filetbl.Length > 0)
{
List<FileInfo> filelist = filetbl.ToList().OrderBy(x => x.LastWriteTime).ToList();
for (int i = (filelist.Count - 10); i > -1; i--)
{
FileInfo file = filelist[i];
file.Delete();
}
}
}
internal static void Stop()
{
log.Close();
}
internal static string GetTimestamp() { return DateTime.Now.ToString("HH:mm:ss.fff"); }
public static void Log(string s)
{
var timestamp = GetTimestamp();
if(consoleEnabled) Console.WriteLine("[" + timestamp + "] [VRCMod] " + s);
if(log != null) log.WriteLine("[" + timestamp + "] " + s);
}
public static void Log(string s, params object[] args)
{
var timestamp = GetTimestamp();
var formatted = string.Format(s, args);
if (consoleEnabled) Console.WriteLine("[" + timestamp + "] [VRCMod] " + s, args);
if (log != null) log.WriteLine("[" + timestamp + "] " + formatted);
}
public static void LogError(string s)
{
var timestamp = GetTimestamp();
if (consoleEnabled) Console.WriteLine("[" + timestamp + "] [VRCMod] [Error] " + s);
if (log != null) log.WriteLine("[" + timestamp + "] [Error] " + s);
}
public static void LogError(string s, params object[] args)
{
var timestamp = GetTimestamp();
var formatted = string.Format(s, args);
if (consoleEnabled) Console.WriteLine("[" + timestamp + "] [VRCMod] [Error] " + formatted);
if (log != null) log.WriteLine("[" + timestamp + "] [Error] " + formatted);
}
}
}