# HG changeset patch # User Adam Kaminski # Date 1613496690 18000 # Tue Feb 16 12:31:30 2021 -0500 # Node ID d05cc188c8307ad2f360976c58b714bdb13cfb1e # Parent d115be44635a07f622ce55ec67cc7e1acaaefb3d Added the CVar "con_showtimestamps" which adds a timestamp to the beginning of each line on the console, based on patches made by Leonard. diff -r d115be44635a -r d05cc188c830 src/c_console.cpp --- a/src/c_console.cpp Sun Feb 07 11:48:59 2021 -0500 +++ b/src/c_console.cpp Tue Feb 16 12:31:30 2021 -0500 @@ -117,6 +117,7 @@ static char ConsoleBuffer[CONSOLESIZE]; static char *Lines[CONSOLELINES]; +static char *TimeStamps[CONSOLELINES]; // [Leo] static bool LineJoins[CONSOLELINES]; static int TopLine, InsertLine; @@ -201,6 +202,9 @@ // [RC] Now a three-level setting. No/Yes/Not in chat. CVAR( Int, con_colorinmessages, 1, CVAR_ARCHIVE ) +// [AK] Add a timestamp to every line printed to the console. +CVAR (Bool, con_showtimestamps, true, CVAR_ARCHIVE) + // [BB] Add a timestamp to every string printed to the logfile. CVAR (Bool, sv_logfiletimestamp, true, CVAR_ARCHIVE) @@ -604,6 +608,17 @@ BufferRover = ConsoleBuffer; memset (ConsoleBuffer, 0, CONSOLESIZE); memset (Lines, 0, sizeof(Lines)); + + // [AK] Delete all the timestamps. + for (int i = 0; i < CONSOLELINES; i++) + { + if (TimeStamps[i] != NULL) + { + delete[] TimeStamps[i]; + } + } + + memset (TimeStamps, 0, sizeof(TimeStamps)); memset (LineJoins, 0, sizeof(LineJoins)); } @@ -708,6 +723,13 @@ if (Lines[i] < stop && Lines[i] + strlen (Lines[i]) > start) { Lines[i] = NULL; + + // [AK] Delete this timestamp if used. + if (TimeStamps[i] != NULL) + { + delete[] TimeStamps[i]; + TimeStamps[i] = NULL; + } } else { @@ -717,7 +739,8 @@ return i; } -static void AddLine (const char *text, bool more, size_t len) +// [Leo] Added an argument for adding timestamps to line entries. +static void AddLine (const char *text, bool more, size_t len, char *timestamp) { if (BufferRover + len + 1 - ConsoleBuffer > CONSOLESIZE) { @@ -735,6 +758,14 @@ Lines[InsertLine] = BufferRover; BufferRover += len + 1; LineJoins[InsertLine] = more; + + // [AK] Delete the old timestamp if used, then add the new one if it exists. + if (TimeStamps[InsertLine] != NULL) + { + delete[] TimeStamps[InsertLine]; + } + + TimeStamps[InsertLine] = (timestamp == NULL) ? NULL : copystring(timestamp); InsertLine = (InsertLine + 1) & LINEMASK; if (InsertLine == TopLine) { @@ -753,6 +784,7 @@ char *work_p; char *linestart; + char *timestamp = NULL; // [Leo] FString cc('A' + char(CR_TAN)); int size, len; int x; @@ -764,6 +796,17 @@ return; } + // [AK] Generate the timestamp "[HH:MM:SS] " if we want to show it in the console. + if (con_showtimestamps) + { + time_t clock; + time(&clock); + struct tm *lt = localtime (&clock); + char timestring[14]; + sprintf(timestring, "\034i[%02d:%02d:%02d] ", lt->tm_hour, lt->tm_min, lt->tm_sec); + timestamp = timestring; + } + len = (int)strlen (text); size = len + 20; @@ -864,9 +907,10 @@ continue; } int w = ConFont->GetCharWidth (*work_p); - if (*work_p == '\n' || x + w > maxwidth) + // [AK] Also take into account the width of the timestamp string, if there is one. + if (*work_p == '\n' || x + w + (timestamp != NULL ? ConFont->StringWidth(timestamp) : 0) > maxwidth) { - AddLine (linestart, *work_p != '\n', work_p - linestart); + AddLine (linestart, *work_p != '\n', work_p - linestart, timestamp); if (*work_p == '\n') { x = 0; @@ -905,7 +949,7 @@ if (*linestart) { - AddLine (linestart, true, work_p - linestart); + AddLine (linestart, true, work_p - linestart, timestamp); } } else @@ -914,13 +958,13 @@ { if (*work_p++ == '\n') { - AddLine (linestart, false, work_p - linestart - 1); + AddLine (linestart, false, work_p - linestart - 1, timestamp); linestart = work_p; } } if (*linestart) { - AddLine (linestart, true, work_p - linestart); + AddLine (linestart, true, work_p - linestart, timestamp); } } @@ -1486,7 +1530,18 @@ pos = (pos - 1) & LINEMASK; if (Lines[pos] != NULL) { - screen->DrawText (ConFont, CR_TAN, LEFTMARGIN, offset + lines * ConFont->GetHeight(), + // [AK] Add the timestamp to the beginning of this line, if there is one. + int lineoffset = LEFTMARGIN; + if (TimeStamps[pos] != NULL) + { + screen->DrawText (ConFont, CR_TAN, lineoffset, offset + lines * ConFont->GetHeight(), + TimeStamps[pos], TAG_DONE); + + lineoffset += ConFont->StringWidth(TimeStamps[pos]); + } + + // [AK] Offset the rest of the line by on the width of the timestamp. + screen->DrawText (ConFont, CR_TAN, lineoffset, offset + lines * ConFont->GetHeight(), Lines[pos], TAG_DONE); } lines--; diff -r d115be44635a -r d05cc188c830 wadsrc/static/menudef.txt --- a/wadsrc/static/menudef.txt Sun Feb 07 11:48:59 2021 -0500 +++ b/wadsrc/static/menudef.txt Tue Feb 16 12:31:30 2021 -0500 @@ -1187,6 +1187,7 @@ StaticText " " Option "Screenshot messages", "screenshot_quiet", "OffOn" Option "Detailed save messages", "longsavemessages", "OnOff" + Option "Display Console Timestamps", "con_showtimestamps", "YesNo" // [AK] } //-------------------------------------------------------------------------------------------