c++ - Cross-platform microsecond precise timestamp -


i writing logging class should provide current date , time @ microsecond precision level. not want format log messages directly using snprintf (so can filtered efficiently) rather have struct this:

struct log_record {     timestamp_t time;     uint32_t    msg_len;     ... } 

this should able shared other machines (using tcp, example) may have cpu, os (linux , windows), etc. there existing timestamp format / library (maybe in database system?) work out requirements?

you can achieve microsecond granularity on platforms, won't microsecond precision, since system clock not accurate. expect offsets of seconds or minutes on typical desktop machines, down milliseconds on machines synchronised using ntp.

on unix systems known me (including linux), old gettimeofday yields granularity somewhere between 10ms , 1µs, depending on os , hardware:

rc = gettimeofday(&tv, null); 

on linux platforms, clock_gettime gives better granularity, down hundreds of nanoseconds:

rc = clock_gettime(clock_realtime, &ts); 

on windows, you'll want use getsystemtimeasfiletime function, results of can convert useful using filetimetosystemtime function. however, granularity of few dozen milliseconds, unless play dirty tricks performance counters.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -