Skip to content

Commit 6b1de43

Browse files
authored
feat: FileLogger. A small utility to log to a file identified by proc… (#481)
* feat: FileLogger. A small utility to log to a file identified by process ID. Useful when running multiple instances locally
1 parent 27263d3 commit 6b1de43

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.IO;
3+
4+
#if DEVELOPMENT_BUILD || UNITY_EDITOR
5+
6+
namespace MLAPI.Logging
7+
{
8+
/// <summary>
9+
/// Helper class for logging
10+
/// Saves to a file identified by the current UTC epoch time. This is useful
11+
/// when debugging what happens on multiple processes on the same machine
12+
/// Only available for debugging on DEVELOPMENT_BUILD or UNITY_EDITOR
13+
/// </summary>
14+
public class FileLogger:IDisposable
15+
{
16+
private static FileLogger m_Instance = null;
17+
public static FileLogger Instance
18+
{
19+
get
20+
{
21+
if (m_Instance == null)
22+
{
23+
m_Instance = new FileLogger();
24+
}
25+
return m_Instance;
26+
}
27+
}
28+
29+
private StreamWriter m_Writer;
30+
31+
private FileLogger()
32+
{
33+
m_Writer = new StreamWriter("log." + DateTimeOffset.UtcNow.ToUnixTimeSeconds() + ".txt");
34+
m_Writer.AutoFlush = true;
35+
}
36+
37+
public void Dispose()
38+
{
39+
m_Writer.Dispose();
40+
}
41+
42+
/// <summary>
43+
/// Logs a line
44+
/// <param name="line">The line to log to the file for this process</param>
45+
/// </summary>
46+
public void Log(string line)
47+
{
48+
m_Writer.WriteLine(line);
49+
}
50+
}
51+
}
52+
53+
#endif

com.unity.multiplayer.mlapi/Runtime/Logging/FileLogger.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)