From 01642af37128bffc1accbf2e92cb2428ea570720 Mon Sep 17 00:00:00 2001 From: bones_was_here Date: Tue, 9 Jan 2024 15:46:37 +1000 Subject: [PATCH] sys: optimise printing to stdout Signed-off-by: bones_was_here --- cl_screen.c | 18 ++++++------------ console.c | 10 +++++----- console.h | 2 +- sys.h | 5 +++-- sys_shared.c | 11 ++++++----- 5 files changed, 21 insertions(+), 25 deletions(-) diff --git a/cl_screen.c b/cl_screen.c index 202a520e..1d1280e9 100644 --- a/cl_screen.c +++ b/cl_screen.c @@ -1224,19 +1224,13 @@ static void SCR_CaptureVideo_VideoFrame(int newframestepframenum) cls.capturevideo.videoframes(newframestepframenum - cls.capturevideo.framestepframe); cls.capturevideo.framestepframe = newframestepframenum; - if(cl_capturevideo_printfps.integer) + if(cl_capturevideo_printfps.integer && host.realtime > cls.capturevideo.lastfpstime + 1) { - char buf[80]; - double t = host.realtime; - if(t > cls.capturevideo.lastfpstime + 1) - { - double fps1 = (cls.capturevideo.frame - cls.capturevideo.lastfpsframe) / (t - cls.capturevideo.lastfpstime + 0.0000001); - double fps = (cls.capturevideo.frame ) / (t - cls.capturevideo.starttime + 0.0000001); - dpsnprintf(buf, sizeof(buf), "capturevideo: (%.1fs) last second %.3ffps, total %.3ffps\n", cls.capturevideo.frame / cls.capturevideo.framerate, fps1, fps); - Sys_Print(buf); - cls.capturevideo.lastfpstime = t; - cls.capturevideo.lastfpsframe = cls.capturevideo.frame; - } + double fps1 = (cls.capturevideo.frame - cls.capturevideo.lastfpsframe) / (host.realtime - cls.capturevideo.lastfpstime + 0.0000001); + double fps = (cls.capturevideo.frame ) / (host.realtime - cls.capturevideo.starttime + 0.0000001); + Sys_Printf("capturevideo: (%.1fs) last second %.3ffps, total %.3ffps\n", cls.capturevideo.frame / cls.capturevideo.framerate, fps1, fps); + cls.capturevideo.lastfpstime = host.realtime; + cls.capturevideo.lastfpsframe = cls.capturevideo.frame; } } diff --git a/console.c b/console.c index 99414a21..01c371af 100644 --- a/console.c +++ b/console.c @@ -1356,12 +1356,12 @@ void Con_MaskPrint(int additionalmask, const char *msg) *out++ = '['; *out++ = 'm'; } - *out++ = 0; - Sys_Print(printline); + *out = '\0'; + Sys_Print(printline, out - printline); } else if(sys_colortranslation.integer == 2) // Quake { - Sys_Print(line); + Sys_Print(line, index); } else // strip { @@ -1411,8 +1411,8 @@ void Con_MaskPrint(int additionalmask, const char *msg) break; } } - *out++ = 0; - Sys_Print(printline); + *out = '\0'; + Sys_Print(printline, out - printline); } } // empty the line buffer diff --git a/console.h b/console.h index 8ce07935..093fd8c6 100644 --- a/console.h +++ b/console.h @@ -46,7 +46,7 @@ void Con_Shutdown (void); void Con_DrawConsole (int lines); /// Prints to a chosen console target -void Con_MaskPrint(int mask, const char *msg); +void Con_MaskPrint(int additionalmask, const char *msg); // Prints to a chosen console target void Con_MaskPrintf(int mask, const char *fmt, ...) DP_FUNC_PRINTF(2); diff --git a/sys.h b/sys.h index 119748ed..2255a6a3 100644 --- a/sys.h +++ b/sys.h @@ -208,8 +208,9 @@ char *Sys_TimeString(const char *timeformat); void Sys_Error (const char *error, ...) DP_FUNC_PRINTF(1) DP_FUNC_NORETURN; /// (may) output text to terminal which launched program -void Sys_Print(const char *text); -/// for the console to report failures inside Con_Printf() +/// textlen excludes any (optional) \0 terminator +void Sys_Print(const char *text, size_t textlen); +/// used to report failures inside Con_Printf() void Sys_Printf(const char *fmt, ...); /// INFO: This is only called by Host_Shutdown so we dont need testing for recursion diff --git a/sys_shared.c b/sys_shared.c index f9f118bf..26bd27c2 100644 --- a/sys_shared.c +++ b/sys_shared.c @@ -596,7 +596,7 @@ STDIO =============================================================================== */ -void Sys_Print(const char *text) +void Sys_Print(const char *text, size_t textlen) { #ifdef __ANDROID__ if (developer.integer > 0) @@ -618,7 +618,7 @@ void Sys_Print(const char *text) #endif while(*text) { - fs_offset_t written = (fs_offset_t)write(sys.outfd, text, (int)strlen(text)); + fs_offset_t written = (fs_offset_t)write(sys.outfd, text, textlen); if(written <= 0) break; // sorry, I cannot do anything about this error - without an output text += written; @@ -632,17 +632,18 @@ void Sys_Print(const char *text) #endif } -/// for the console to report failures inside Con_Printf() void Sys_Printf(const char *fmt, ...) { va_list argptr; char msg[MAX_INPUTLINE]; + int msglen; va_start(argptr,fmt); - dpvsnprintf(msg,sizeof(msg),fmt,argptr); + msglen = dpvsnprintf(msg, sizeof(msg), fmt, argptr); va_end(argptr); - Sys_Print(msg); + if (msglen >= 0) + Sys_Print(msg, msglen); } /// Reads a line from POSIX stdin or the Windows console -- 2.39.2