]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
sys: make stdout/stderr configurable during runtime
authorbones_was_here <bones_was_here@xonotic.au>
Wed, 3 Jan 2024 16:58:15 +0000 (02:58 +1000)
committerbones_was_here <bones_was_here@xonotic.au>
Fri, 5 Jan 2024 05:31:06 +0000 (15:31 +1000)
Adds cvar sys_stdout which offers the same behaviours as the -nostdout
and -stderr cmdline options and is initialised by them if they're passed.
Makes -noterminal cmdline option a synonym for -nostdout, previously it
was effectively the same but less efficient.

Platforms with the fcntl() syscall (ie non-Windows):
Changes to non-blocking stdout/stderr by default to prevent stutters and
halts in certain scenarios.
Adds cvar sys_stdout_blocks for enabling blocking stdout/stderr if
required.

Retrieves stdio stream FDs instead of assuming they're always 0,1,2.

Signed-off-by: bones_was_here <bones_was_here@xonotic.au>
console.c
host.c
sys_shared.c

index f35645cc8ba034ddab9447929d62b95c2ab5fc82..fd2df3e0532aba93aeae25a16ce66b4ff46b1d96 100644 (file)
--- a/console.c
+++ b/console.c
@@ -1150,7 +1150,6 @@ Con_MaskPrint
 */
 extern cvar_t timestamps;
 extern cvar_t timeformat;
-extern qbool sys_nostdout;
 void Con_MaskPrint(int additionalmask, const char *msg)
 {
        static int mask = 0;
@@ -1219,7 +1218,7 @@ void Con_MaskPrint(int additionalmask, const char *msg)
                                Con_PrintToHistory(line, mask);
                        }
                        // send to terminal or dedicated server window
-                       if (!sys_nostdout)
+                       if (sys.outfd >= 0)
                        if (developer.integer || !(mask & CON_MASK_DEVELOPER))
                        {
                                if(sys_specialcharactertranslation.integer)
diff --git a/host.c b/host.c
index 4d272e365f52aa42352b6a525e6c9d79fb698a40..424b0729c56f8307ccf2c784cd7e0d53a0c7ee29 100644 (file)
--- a/host.c
+++ b/host.c
@@ -293,7 +293,6 @@ static void Host_InitLocal (void)
 
 char engineversion[128];
 
-qbool sys_nostdout = false;
 
 static qfile_t *locksession_fh = NULL;
 static qbool locksession_run = false;
@@ -424,10 +423,6 @@ static void Host_Init (void)
                gl_printcheckerror.integer = 1;gl_printcheckerror.string = "1";
        }
 
-// COMMANDLINEOPTION: Console: -nostdout disables text output to the terminal the game was launched from
-       if (Sys_CheckParm("-nostdout"))
-               sys_nostdout = 1;
-
        // -dedicated is checked in SV_ServerOptions() but that's too late for Cvar_RegisterVariable() to skip all the client-only cvars
        if (Sys_CheckParm ("-dedicated") || !cl_available)
                cls.state = ca_dedicated;
index e92eb94f71296215231e8fa58e41199245f51a08..78bb56e3914eaf38cbbe5ae016b6c53f02e4d06d 100644 (file)
@@ -329,6 +329,11 @@ static cvar_t sys_usequeryperformancecounter = {CF_SHARED | CF_ARCHIVE, "sys_use
 static cvar_t sys_useclockgettime = {CF_SHARED | CF_ARCHIVE, "sys_useclockgettime", "1", "use POSIX clock_gettime function (not adjusted by NTP on some older Linux kernels) for timing rather than gettimeofday (which has issues if the system time is stepped by ntpdate, or apparently on some Xen installations)"};
 #endif
 
+static cvar_t sys_stdout = {CF_SHARED, "sys_stdout", "1", "0: nothing is written to stdout (-nostdout cmdline option sets this), 1: normal messages are written to stdout, 2: normal messages are written to stderr (-stderr cmdline option sets this)"};
+#ifndef WIN32
+static cvar_t sys_stdout_blocks = {CF_SHARED, "sys_stdout_blocks", "0", "1: writes to stdout and stderr streams will block (causing a stutter or complete halt) if the buffer is full, ensuring no messages are lost at a price"};
+#endif
+
 static double benchmark_time; // actually always contains an integer amount of milliseconds, will eventually "overflow"
 
 /*
@@ -354,6 +359,17 @@ int Sys_CheckParm (const char *parm)
        return 0;
 }
 
+static void Sys_UpdateOutFD_c(cvar_t *var)
+{
+       switch (sys_stdout.integer)
+       {
+               case 0: sys.outfd = -1; break;
+               default:
+               case 1: sys.outfd = fileno(stdout); break;
+               case 2: sys.outfd = fileno(stderr); break;
+       }
+}
+
 void Sys_Init_Commands (void)
 {
        Cvar_RegisterVariable(&sys_debugsleep);
@@ -371,6 +387,11 @@ void Sys_Init_Commands (void)
 #endif
 #if HAVE_CLOCKGETTIME
        Cvar_RegisterVariable(&sys_useclockgettime);
+#endif
+       Cvar_RegisterVariable(&sys_stdout);
+       Cvar_RegisterCallback(&sys_stdout, Sys_UpdateOutFD_c);
+#ifndef WIN32
+       Cvar_RegisterVariable(&sys_stdout_blocks);
 #endif
 }
 
@@ -581,8 +602,9 @@ void Sys_Print(const char *text)
        // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
        // this is because both go to /dev/tty by default!
        {
-               int origflags = fcntl (sys.outfd, F_GETFL, 0);
-               fcntl (sys.outfd, F_SETFL, origflags & ~O_NONBLOCK);
+               int origflags = fcntl(sys.outfd, F_GETFL, 0);
+               if (sys_stdout_blocks.integer)
+                       fcntl(sys.outfd, F_SETFL, origflags & ~O_NONBLOCK);
   #else
     #define write _write
   #endif
@@ -594,7 +616,8 @@ void Sys_Print(const char *text)
                        text += written;
                }
   #ifndef WIN32
-               fcntl (sys.outfd, F_SETFL, origflags);
+               if (sys_stdout_blocks.integer)
+                       fcntl(sys.outfd, F_SETFL, origflags);
        }
   #endif
        //fprintf(stdout, "%s", text);
@@ -970,22 +993,26 @@ int main (int argc, char **argv)
        sys.argc = argc;
        sys.argv = (const char **)argv;
 
+       // COMMANDLINEOPTION: Console: -nostdout disables text output to the terminal the game was launched from
        // COMMANDLINEOPTION: -noterminal disables console output on stdout
-       if(Sys_CheckParm("-noterminal"))
-               sys.outfd = -1;
+       if(Sys_CheckParm("-noterminal") || Sys_CheckParm("-nostdout"))
+               sys_stdout.string = "0";
        // COMMANDLINEOPTION: -stderr moves console output to stderr
        else if(Sys_CheckParm("-stderr"))
-               sys.outfd = 2;
-       else
-               sys.outfd = 1;
+               sys_stdout.string = "2";
+       // too early for Cvar_SetQuick
+       sys_stdout.value = sys_stdout.integer = atoi(sys_stdout.string);
+       Sys_UpdateOutFD_c(&sys_stdout);
+#ifndef WIN32
+       fcntl(fileno(stdin), F_SETFL, fcntl(fileno(stdin), F_GETFL, 0) | O_NONBLOCK);
+       // stdout/stderr will be set to blocking in Sys_Print() if so configured, or during a fatal error.
+       fcntl(fileno(stdout), F_SETFL, fcntl(fileno(stdout), F_GETFL, 0) | O_NONBLOCK);
+       fcntl(fileno(stderr), F_SETFL, fcntl(fileno(stderr), F_GETFL, 0) | O_NONBLOCK);
+#endif
 
        sys.selffd = -1;
        Sys_ProvideSelfFD(); // may call Con_Printf() so must be after sys.outfd is set
 
-#ifndef WIN32
-       fcntl(fileno(stdin), F_SETFL, fcntl (fileno(stdin), F_GETFL, 0) | O_NONBLOCK);
-#endif
-
 #ifdef __ANDROID__
        Sys_AllowProfiling(true);
 #endif