Search Unity

Unity how to output to -batchmode console?

Discussion in 'Multiplayer' started by Slack43, Sep 8, 2018.

  1. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    Hi, recently i made a Dedicated server using -batchmode. I don't know how to output anything into it. I want to log some basic info like "Player joined the server" etc.
     
  2. LukeDawn

    LukeDawn

    Joined:
    Nov 10, 2016
    Posts:
    404
    Either write your output to a file of your own, or use debug.log() and view the output_log.txt file that unity writes. On windows that's C:\Users\username\AppData\LocalLow\companyname\productname\output_log.txt

    Make sure you're using -batchmode -nographics and that you've set application.runinbackground to run as a background service.
     
  3. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    It's not what i meant :)
    I want to do display some info like player joined the game on the console like this guy: https://garry.tv/2014/04/23/unity-batchmode-console/
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
  5. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    I think it should work. Im gonna check it later because i can't right now :) BTW. If someone needs to read input from user like me, check it out: https://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console
     
  6. Deleted User

    Deleted User

    Guest

  7. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    Deleted User likes this.
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'd run the server as a background task, then write to a file what you want to display, and tail that file to display its contents as they come in to the console.
     
  9. Deleted User

    Deleted User

    Guest

  10. marwi

    marwi

    Joined:
    Aug 13, 2014
    Posts:
    138
    You can also just open the unity console window!

    This is what I do now:

    Code (CSharp):
    1.  
    2.         private static EditorWindow TryOpenConsoleWindow()
    3.         {
    4.             try
    5.             {
    6.                 var consoleWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ConsoleWindow");
    7.                 var consoleWindow = ScriptableObject.CreateInstance(consoleWindowType) as EditorWindow;
    8.                 if (consoleWindow)
    9.                 {
    10.                     consoleWindow.Show(true);
    11.                     return consoleWindow;
    12.                 }
    13.              
    14.                 Debug.LogError("Could not open console window");
    15.             }
    16.             catch (Exception e)
    17.             {
    18.                 Debug.LogException(e);
    19.             }
    20.  
    21.             return null;
    22.         }

    and at the end of everything, before quitting the application (isDebugMode is set via a CLI arg)

    Code (CSharp):
    1.  
    2.             ........
    3.             finally
    4.             {
    5.                 if (isDebugMode && !success)
    6.                 {
    7.                     Debug.Log("Run failed - Close the console window to exit the Editor");
    8.                     // ReSharper disable once LoopVariableIsNeverChangedInsideLoop
    9.                     while (consoleWindow)
    10.                     {
    11.                         await Task.Delay(1000);
    12.                     }
    13.                 }
    14.                 Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "Build batch finished, closing Editor");
    15.                 EditorApplication.Exit(success ? 0 : 1);
    16.             }
     
    fherbst likes this.