1 (edited by floyd 2013-11-13 20:12:16)

Topic: How to turn the hexa date in the log to something human readable(bash)

hellu all,

in the log written by the server, you get the date in an hexadecimal format, which is quite annoying for certains purposes. Working on some bash scripts, I got to find out a way to made it human readable. Here is one solution.

Example (don't pay attention to the error tongue) :
[52814340][server]: couldn't open socket. port 8303 might already be in use

We want to turn 52814340 to something, we, human can understand.
Let's pretend you have a log file nammed server.log

# this is just to grab the first line of the log and then the hexa date of this line, don't pay attention to this
HEXA_DATE=$(tail server.log | head -1 | awk -F ']' '{ print $1}' | tr -d '[')

# or (same result)
HEXA_DATE=52814340

# here is how to convert
date -d @$(echo $((0x${HEXA_DATE}))) +'%F %T'

which gives => 2013-11-11 21:51:12


P.S: i'm not so sure if I posted this in the good section nor if this post is interesting, but well enjoy if it helps you smile
edit: forgot to write the result

Need a tool to manage your servers? teeman
<3

2

Re: How to turn the hexa date in the log to something human readable(bash)

Wouldn't be a bad idea to at least turn it into h:m:s imo. I personally just disabled it.

Not Luck, Just Magic.

3

Re: How to turn the hexa date in the log to something human readable(bash)

Well to be true I don't know why it's an hexa timestamp in the log, this should be something more readable. Logs are there to understand what happens, so if you have to convert all the time the timestamp to understand at what time something happened ...
Btw I disagree this should be date AND time, only time wouldn't be a good help if you have few days of log.
What do you mean by "I personally just disabled it." ? You don't log or you set an option to not display the timestamp in the log? (I've never heard or seen that)
Thanks

Need a tool to manage your servers? teeman
<3

4

Re: How to turn the hexa date in the log to something human readable(bash)

As Python (3, should work in 2 too, but hangs for some reason) oneliner:

teeworlds_srv | python -uc 'for l in __import__("sys").stdin: print("["+__import__("datetime").datetime.fromtimestamp(int(l[1:9], 16)).strftime("%H:%M:%S")+l[9:].strip())'