Search Unity

Redirecting standard output using the -logFile parameter when in batchmode

Discussion in 'Windows' started by DanLevi, Apr 2, 2016.

  1. DanLevi

    DanLevi

    Joined:
    Nov 24, 2013
    Posts:
    9
    Hi,

    I am trying to redirect the standard output when running batch mode but it does not output, am i missing something out? I was under the impression that if i the executable was started with the -logFile parameter withput specifying a filename it would use StandardOutput. If that is the case i am doing something wrong, if not, how can i accomplish with i need?

    I have a simple project setup in Visual Studio where i spawn a process of a unity game called Rust, or rather, its dedicated server.

    As of now the only thing that shows up in the console (in VS) is these three lines:

    Code (Console Output):
    1. Mono path[0] = 'E:/SteamCMD/servers/rust/RustDedicated_Data/Managed'
    2. Mono path[1] = 'E:/SteamCMD/servers/rust/RustDedicated_Data/Mono'
    3. Mono config path = 'E:/SteamCMD/servers/rust/RustDedicated_Data/Mono/etc'
    Here are some code:

    Code (CSharp):
    1. Process serverProcess;
    2. private void StartServerThread()
    3. {
    4.     var serverArguments = GenerateServerArguments();
    5.     var serverExecutable = Properties.Settings.Default.Rustserverexecutable;
    6.     try
    7.     {
    8.         serverProcess = new Process {
    9.             StartInfo = new ProcessStartInfo
    10.             {
    11.                 WorkingDirectory = System.IO.Path.GetDirectoryName(serverExecutable),
    12.                 FileName = serverExecutable,
    13.                 Arguments = serverArguments,
    14.                 RedirectStandardInput = true,
    15.                 RedirectStandardOutput = true,
    16.                 RedirectStandardError = true,
    17.                 UseShellExecute = false
    18.             }
    19.         };
    20.         serverProcess.OutputDataReceived += ServerProcess_OutputDataReceived;
    21.         serverProcess.ErrorDataReceived += ServerProcess_ErrorDataReceived;
    22.         serverProcess.Start();
    23.         serverProcess.BeginOutputReadLine();
    24.         serverProcess.BeginErrorReadLine();
    25.         }
    26.     catch (Exception e)
    27.     {
    28.         Console.WriteLine("Exception: " + e.Message);
    29.     }
    30. }
    31.  
    32. private void ServerProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    33. {
    34.     OutputToConsole(e.Data);
    35. }
    36. private void ServerProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
    37. {
    38.     OutputToConsole(e.Data);
    39. }
    40. void OutputToConsole(string msg)
    41. {
    42.     if (!string.IsNullOrEmpty(msg))
    43.     {
    44.         Console.WriteLine(msg);
    45.     }  
    46. }
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,443
    Hi,

    passing "-logPath" argument without any path has no effect.

    There isn't a parameter that you can make Unity print everything to stdout by default, but you can subscribe to Application.logMessageReceivedThreaded callback and then print it anywhere you want.
     
  3. mdrotar

    mdrotar

    Joined:
    Aug 26, 2013
    Posts:
    377
    Can you confirm that attaching to Application.logMessageReceived (the non-threaded version) will NOT trigger for Debug.Log calls performed on a background thread? That seems to be what I'm observing. If this is intended behaviour, the documentation should make it clear that you will miss background thread messages using Application.logMessageReceived.
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,443
    Correct.
     
  5. TechCor

    TechCor

    Joined:
    Apr 3, 2015
    Posts:
    55
    I have this exact problem. Three mono lines and then nothing. What is the solution?

    When I use console.writeline it outputs to the log file and not the starting process.

    Setting the console to OpenStandardOutput does not appear to work either.
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,443
    There is no way to redirect output to actual stdout. But as I mentioned, you can subscribe to that event and then send the output to your parent process whatever way you want.
     
  7. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    Apart from the method that you suggested, is there going to be a way to output Unity's log to stdout by default with Unity? Because many different forum posts, answers and blogs say that adding -logFile with no path produces this. As we are running TeamCity under the SYSTEM account on our build server, this makes the log inaccessible. We've redirected the path to the checkout directory, but it would make more sense if we could output to stdout so that the log is collected in TeamCity's build log.
    Cheers
    Stormy
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,443
    You can submit a feature request if you wish, but I don't think we had anything planned.

    For a short term solution, you subscribe to Application.logMessageReceivedThreaded and write to stdout from the callback.
     
    Stormy102 likes this.
  9. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    Thanks for the suggestion @Tautvydas-Zilys
     
  10. rkoshak

    rkoshak

    Joined:
    Dec 1, 2014
    Posts:
    9
    Has anybody gotten
    to work?

    Ive tried putting the following code in my -executeMethod method:
    Code (CSharp):
    1. Application.logMessageReceivedThreaded += (logString, stackTrace, logType) =>
    2.         {
    3.             System.IO.TextWriter writer = Console.Out;
    4.             writer.WriteLine(logString);
    5.             writer.WriteLine(stackTrace);
    6.         };
    I get no build logs to print out in the TeamCity build log window.

    Passing "-logPath" in as an argument does work on Mac build machines. I get full logs in TC on mac with that.

    Passing "-logPath -" will print build logs within a command window or powerShell window on a Windows machine, but I cant get any output to TeamCity on those Windows machines.

    Anyone have any suggestions?

    Thanks
     
    saimsiddiqui and LouisHong like this.
  11. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    https://github.com/Facepunch/Facepunch.UnityBatch
    Try taking this and modifying it to your own needs. It means wrapping Unity's output in a custom executable, but tbh it's the easy way without having to do some seriously dodgy code within Unity :p
     
  12. rkoshak

    rkoshak

    Joined:
    Dec 1, 2014
    Posts:
    9
    Stormy102 likes this.
  13. yossi_horowitz_artie

    yossi_horowitz_artie

    Joined:
    Jan 30, 2019
    Posts:
    56
    I'm necroing this, now that the `-logfile` bug is reported fixed for 2019.1:

    https://issuetracker.unity3d.com/is...-outputs-to-screen-on-os-x-but-not-on-windows

    I've got some questions:

    1) They say "Logging now defaults to stdout on all platforms when running in batchmode. Note that on Windows, this still means output won't go to console by default as Windows applications don't have stdout handle by default. Output will, however, go to stdout if you launch Unity with a valid stdout handle. I.e. launching as a child process of a build system with redirected handles" Is there a way to launch Unity with a valid stdout handle from the Windows console or from a batch file, or is this feature only usable by writing a Windows application that spawns Unity as a child process?

    2) As implied by one of the comments on that page, is this feature only usable if the "-nographics" command line parameter is also used?
     
  14. nickrapp

    nickrapp

    Unity Technologies

    Joined:
    Oct 5, 2015
    Posts:
    47
    1. Depends if you mean editor or player? For editor, I don't know of an easy way to do this from Windows without spawning as a child process. We do have some ideas how to solve for this scenario in the future, so stay tuned on that. For player, if you build with "server build" option, the entry point executable used is built with /SUBSYSTEM:CONSOLE so Windows will create std handles for you by default, that direct to the console

    2. No, you can use logfile parameter on it's own.
     
  15. yossi_horowitz_artie

    yossi_horowitz_artie

    Joined:
    Jan 30, 2019
    Posts:
    56
    I did mean the editor. Thanks for the clarification!