Skip to content

Commit

Permalink
Remove logging to stdout as it's being swallowed by something anyway,…
Browse files Browse the repository at this point in the history
… log to file instead if DAYZLAUNCHER_UNIX_LOGS is set
  • Loading branch information
valters-tomsons committed May 17, 2024
1 parent 0725812 commit 4ddd298
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/DayZLauncher.UnixPatcher.Utils/UnixJunctions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -26,13 +26,14 @@ static UnixJunctions()
{
if (IsRunningOnMono)
{
Log("UnixJunctions: running on Mono runtime!");
Directory.CreateDirectory(LinuxLauncherDataPath);
}
}

public static void Create(string junctionPoint, string targetDir, bool overwrite)
{
Console.WriteLine("UnixJunctions: Create() called junctionPoint='" + junctionPoint + "' ; targetDir='" + targetDir + "'");
Log("UnixJunctions: Create() called junctionPoint='" + junctionPoint + "' ; targetDir='" + targetDir + "'");

targetDir = Path.GetFullPath(targetDir);

Expand All @@ -49,7 +50,7 @@ public static void Create(string junctionPoint, string targetDir, bool overwrite

public static void Delete(string junctionPoint)
{
Console.WriteLine("UnixJunctions: Delete() called junctionPoint='" + junctionPoint + "'");
Log("UnixJunctions: Delete() called junctionPoint='" + junctionPoint + "'");

if (!Directory.Exists(junctionPoint))
{
Expand All @@ -69,7 +70,7 @@ public static void Delete(string junctionPoint)

public static bool Exists(string path)
{
Console.WriteLine("UnixJunctions: Exists() called path='" + path + "'");
Log("UnixJunctions: Exists() called path='" + path + "'");

if (!Directory.Exists(path))
{
Expand All @@ -90,7 +91,7 @@ public static bool Exists(string path)

public static string GetTarget(string junctionPoint)
{
Console.WriteLine("UnixJunctions: GetTarget() called junctionPoint='" + junctionPoint + "'");
Log("UnixJunctions: GetTarget() called junctionPoint='" + junctionPoint + "'");

junctionPoint = ToUnixPath(junctionPoint);
string output = RunShellCommand("readlink", $"'{junctionPoint}'");
Expand All @@ -99,17 +100,13 @@ public static string GetTarget(string junctionPoint)

private static string RunShellCommand(string command, string arguments)
{
Console.WriteLine("UnixJunctions.RunShellCommand: command= " + command + " ;arguments= " + arguments);

var gameLocation = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var basePath = gameLocation + @"\LinuxLauncherData";
Directory.CreateDirectory(basePath);
Log("UnixJunctions.RunShellCommand: command= " + command + " ;arguments= " + arguments);

string uniqueId = Guid.NewGuid().ToString("N");
string tempOutputPath = LinuxLauncherDataPath + @$"\tmp_output_{uniqueId}.txt";
string lockFilePath = LinuxLauncherDataPath + @$"\{uniqueId}.lock";

Console.WriteLine($"UnixJunctions.RunShellCommand: tempOutputPath='{tempOutputPath}'");
Log($"UnixJunctions.RunShellCommand: tempOutputPath='{tempOutputPath}'");

var script = $"""
#!/bin/sh
Expand All @@ -128,7 +125,7 @@ private static string RunShellCommand(string command, string arguments)
CreateNoWindow = true
};

Console.WriteLine("UnixJunctions.RunShellCommand: about to execute script " + uniqueId);
Log("UnixJunctions.RunShellCommand: about to execute script " + uniqueId);

using (Process process = new() { StartInfo = processStartInfo })
{
Expand All @@ -137,25 +134,25 @@ private static string RunShellCommand(string command, string arguments)

if (process.ExitCode != 0)
{
Console.WriteLine($"UnixJunctions: Error executing script '{uniqueId}'. Exit code: {process.ExitCode}");
Log($"UnixJunctions: Error executing script '{uniqueId}'. Exit code: {process.ExitCode}");
}
}

while (!File.Exists(tempOutputPath))
{
Console.WriteLine("UnixJunctions.RunShellCommand: waiting for output file " + uniqueId);
Log("UnixJunctions.RunShellCommand: waiting for output file " + uniqueId);
Thread.Sleep(50);
}

while (File.Exists(lockFilePath))
{
Console.WriteLine("UnixJunctions.RunShellCommand: waiting for unix write unlock " + uniqueId);
Log("UnixJunctions.RunShellCommand: waiting for unix write unlock " + uniqueId);
Thread.Sleep(50);
}

// Read the output file
string scriptOutput = File.ReadAllText(tempOutputPath);
Console.WriteLine($"UnixJunctions.RunShellCommand: {uniqueId} output= {scriptOutput}");
Log($"UnixJunctions.RunShellCommand: {uniqueId} output= {scriptOutput}");
File.Delete(tempOutputPath);

return scriptOutput;
Expand All @@ -165,7 +162,7 @@ private static string ToUnixPath(string windowsPath)
{
// Skip drive letter (e.g. 'Z:')
var result = windowsPath.Substring(2).Replace("\\", "/");
Console.WriteLine($"UnixJunctions.ToUnixPath: windowsPath='{windowsPath}', result='{result}'");
Log($"UnixJunctions.ToUnixPath: windowsPath='{windowsPath}', result='{result}'");
return EscapeSymbols(result);
}

Expand All @@ -174,7 +171,15 @@ private static string EscapeSymbols(string path)
{
var buffer = path.Split(BadSymbols, StringSplitOptions.RemoveEmptyEntries);
var result = string.Join(string.Empty, buffer);
Console.WriteLine($"UnixJunctions.EscapeSymbols: path='{path}', result='{result}'");
Log($"UnixJunctions.EscapeSymbols: path='{path}', result='{result}'");
return result;
}

private static void Log(string message)
{
if (EnableDebugLogging)
{
File.AppendAllText(DebugLogFilePath, $"{DateTime.Now}: {message}{Environment.NewLine}");
}
}
}

0 comments on commit 4ddd298

Please sign in to comment.