• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ How do I show datetime to my console outputs

Paulix

Active Member
Joined
Sep 13, 2012
Messages
129
Solutions
7
Reaction score
26
Title, my console is like this on TFS 1.4
1648501868651.png
I want to add date time to something like 0.4
1648501902225.png
 
Solution
Overriding cout like this:

or this (unlikely to work?):
C++ output with prefix padding (https://stackoverflow.com/questions/11916698/c-output-with-prefix-padding)

or replacing every output with your own functions like this and adding a prefix in a single place (not finished yet):
(check later commits for a more elegant solution for common warnings/errors)

You can also search "cout" in 0.4 sources to see if/where/how was it overridden or load the files in visual studio and go to sendMessage definition

Overall, if you need the date/time prefix in logs...

on all "couts" and similar funcs (.cpp files)
 
Last edited:

on all "couts" and similar funcs (.cpp files)
I was looking on 0.4 sources, and looks like thats not how it is done (can't find how it is), it must have a better way to do this
 
Overriding cout like this:

or this (unlikely to work?):
C++ output with prefix padding (https://stackoverflow.com/questions/11916698/c-output-with-prefix-padding)

or replacing every output with your own functions like this and adding a prefix in a single place (not finished yet):
(check later commits for a more elegant solution for common warnings/errors)

You can also search "cout" in 0.4 sources to see if/where/how was it overridden or load the files in visual studio and go to sendMessage definition

Overall, if you need the date/time prefix in logs, consider adding it to your console logging script rather than making heavy source edits (or just wait until I finish)
 
Last edited:
Solution
On linux you can use ts for this, it's in moreutils package:
Code:
sudo apt install moreutils

Example run:
Code:
./tfs 2>&1 | ts '[%Y-%m-%d %H:%M:%.S]'
Auto-restarter:
Code:
#!/bin/bash

ulimit -c unlimited

while true; do
 echo SERVER START
 ./tfs 2>&1 | ts '[%Y-%m-%d %H:%M:%.S]'
 echo RESTART, CTRL+C TO CLOSE RESTARTER
 sleep 3

done

How it looks in console:
Code:
[2022-03-29 11:04:03.823248] The Forgotten Server - Version v1.4.1-1-gc016ac44
[2022-03-29 11:04:03.823284] Git SHA1 c016ac44 dated 2022-01-25T18:15:10-05:00
[2022-03-29 11:04:03.823312] *** DIRTY - NOT OFFICIAL RELEASE ***
[2022-03-29 11:04:03.823320] 
[2022-03-29 11:04:03.823327] Compiled with GNU C++ version 8.4.0
[2022-03-29 11:04:03.823334] Compiled on Mar 28 2022 17:18:12 for platform x64
[2022-03-29 11:04:03.823340] Linked with LuaJIT 2.1.0-beta3 for Lua support
[2022-03-29 11:04:03.823347] 
[2022-03-29 11:04:03.823354] A server developed by Mark Samman
[2022-03-29 11:04:03.823361] Visit our forum for updates, support, and resources: https://otland.net/.
[2022-03-29 11:04:03.823368] 
[2022-03-29 11:04:03.823376] >> Loading config
[2022-03-29 11:04:03.823383] >> Establishing database connection... MySQL 8.0.28
[2022-03-29 11:04:03.823390] >> Running database manager
 
Back
Top