qa report v2
This commit is contained in:
@@ -6,6 +6,8 @@ public class Logger
|
||||
{
|
||||
public static bool Enabled = true;
|
||||
private static Logger m_instance = null;
|
||||
private static readonly object FileLock = new object();
|
||||
|
||||
private static string ApplicationDirectory
|
||||
{
|
||||
get
|
||||
@@ -18,6 +20,12 @@ public class Logger
|
||||
}
|
||||
|
||||
string path = Application.dataPath;
|
||||
#if UNITY_EDITOR
|
||||
// Never write under Assets/: the Editor importer locks those files, and
|
||||
// File.AppendAllText then throws Sharing violation. Mirror treats that as a
|
||||
// fatal RPC error and disconnects the client.
|
||||
path = Path.GetFullPath(Path.Combine(path, ".."));
|
||||
#else
|
||||
if (Application.platform == RuntimePlatform.OSXPlayer)
|
||||
{
|
||||
path = Path.Combine(path, "..", "..");
|
||||
@@ -26,6 +34,7 @@ public class Logger
|
||||
{
|
||||
path = Path.Combine(path, "..");
|
||||
}
|
||||
#endif
|
||||
return path;
|
||||
}
|
||||
}
|
||||
@@ -50,14 +59,14 @@ public class Logger
|
||||
if(LogFilePath == null){
|
||||
LogFilePath = Path.Combine(ApplicationDirectory, "Log.txt");
|
||||
}
|
||||
File.WriteAllText(LogFilePath, "Logger initiated at " + DateTime.Now + "\n\n");
|
||||
TryWriteFile(LogFilePath, "Logger initiated at " + DateTime.Now + "\n\n", append: false);
|
||||
}
|
||||
|
||||
public void log(string message){
|
||||
if(!Enabled){return;}
|
||||
|
||||
File.AppendAllText(LogFilePath,$"[{DateTime.Now}] {message}\n");
|
||||
Debug.Log(message);
|
||||
TryWriteFile(LogFilePath, $"[{DateTime.Now}] {message}\n", append: true);
|
||||
}
|
||||
|
||||
public static void Log(string message){
|
||||
@@ -86,4 +95,44 @@ public class Logger
|
||||
public static void SetFilePath(string path){
|
||||
instance.LogFilePath= path;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// File I/O must never throw: callers include Mirror RPCs, and an exception there
|
||||
/// disconnects the client. Sharing violations are expected if another process has
|
||||
/// the file open; FileShare.ReadWrite plus a swallow keeps gameplay alive.
|
||||
/// </summary>
|
||||
static void TryWriteFile(string path, string contents, bool append)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path) || contents == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
string dir = Path.GetDirectoryName(path);
|
||||
if (!string.IsNullOrEmpty(dir))
|
||||
EnsureDirectoryExists(dir);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning("Logger: could not create log directory: " + e.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
lock (FileLock)
|
||||
{
|
||||
try
|
||||
{
|
||||
FileMode mode = append ? FileMode.Append : FileMode.Create;
|
||||
using (var stream = new FileStream(path, mode, FileAccess.Write, FileShare.ReadWrite))
|
||||
using (var writer = new StreamWriter(stream))
|
||||
{
|
||||
writer.Write(contents);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning("Logger: file write failed (" + path + "): " + e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user