Skip to content

Commit

Permalink
Fix for Linux' logging thread-safe solution being run under Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed Dec 7, 2024
1 parent 1446b13 commit da0c4c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
30 changes: 13 additions & 17 deletions ModTek.Common/Utils/LogStream.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;

namespace ModTek.Common.Utils;

Expand All @@ -9,22 +8,19 @@ internal class LogStream
private readonly ILogStream _impl;
internal LogStream(string path)
{
try
{
if (typeof(string).Assembly.GetTypes().Any(x => x.FullName == "System.IO.MonoIO"))
{
_impl = new MonoIoFileStreamImpl(path);
}
else
{
// win32 improvements
throw new NotImplementedException();
}
}
catch
_impl = GetPlatformDependentLogStream(path);
}

private static ILogStream GetPlatformDependentLogStream(string path)
{
var os = Environment.OSVersion;

if (os.Platform is PlatformID.Unix)
{
_impl = new ThreadSafeFileStreamImpl(path);
return new MonoIoFileStreamImpl(path);
}

return new SimpleThreadSafeFileStreamImpl(path);
}

public void Append(byte[] bytes, int srcOffset, int count)
Expand Down Expand Up @@ -112,10 +108,10 @@ public void Dispose()
}
}

private class ThreadSafeFileStreamImpl : ILogStream
private class SimpleThreadSafeFileStreamImpl : ILogStream
{
private readonly FileStream _stream;
internal ThreadSafeFileStreamImpl(string path)
internal SimpleThreadSafeFileStreamImpl(string path)
{
_stream = new FileStream(
path,
Expand Down
3 changes: 3 additions & 0 deletions ModTek/Features/Logging/AppenderFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Globalization;
using ModTek.Common.Utils;
using UnityEngine;

namespace ModTek.Features.Logging;

Expand All @@ -23,12 +24,14 @@ internal AppenderFile(string path, AppenderSettings settings)
Write(System.Text.Encoding.UTF8.GetBytes(
$"""
ModTek v{GitVersionInformation.InformationalVersion} ({GitVersionInformation.CommitDate})
{Environment.OSVersion} ; BattleTech {Application.version} ; Unity {Application.unityVersion} ; CLR {Environment.Version} ; {System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription}"
{dateTime.ToLocalTime().ToString("o", CultureInfo.InvariantCulture)} {nameof(unityStartupTime)}={unityStartupTime.ToString(null, CultureInfo.InvariantCulture)} {nameof(stopwatchTimestamp)}={stopwatchTimestamp}
{new string('-', 80)}
{VersionInfo.GetFormattedInfo()}
"""
));
}

private void Write(byte[] bytes)
{
Write(bytes, bytes.Length);
Expand Down

0 comments on commit da0c4c7

Please sign in to comment.