diff options
author | Christof Arnosti <charno@charno.ch> | 2020-03-10 22:58:24 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-03-10 17:42:54 -0700 |
commit | cb28158b9abe10f08142f12f11ddbb5d23686fd5 (patch) | |
tree | 1d3aec604dfe116accf10326a3f4fe2cca3bf975 /core/libdivecomputer.c | |
parent | b6163804fd56acf23bf6156a84cad02bc6f6eb08 (diff) | |
download | subsurface-cb28158b9abe10f08142f12f11ddbb5d23686fd5.tar.gz |
Add timestamps to libdivecomputer.log
Since I learned while trying to implement this that getting sub-second
resolution time in portable C99 is hard (especially for someone who is
used to the comfort of std::chrono and Howard Hinnants date library) the
timer-implemetation from libdivecomputer is now copied to the subsurface
source.
Signed-off-by: Christof Arnosti <charno@charno.ch>
Diffstat (limited to 'core/libdivecomputer.c')
-rw-r--r-- | core/libdivecomputer.c | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/core/libdivecomputer.c b/core/libdivecomputer.c index 2728cd8ab..475a78ef0 100644 --- a/core/libdivecomputer.c +++ b/core/libdivecomputer.c @@ -21,6 +21,7 @@ #include "display.h" #include "errorhelper.h" #include "sha1.h" +#include "timer.h" #include <libdivecomputer/version.h> #include <libdivecomputer/usbhid.h> @@ -1188,17 +1189,29 @@ static const char *do_device_import(device_data_t *data) return NULL; } +static dc_timer_t *logfunc_timer = NULL; void logfunc(dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *msg, void *userdata) { UNUSED(context); const char *loglevels[] = { "NONE", "ERROR", "WARNING", "INFO", "DEBUG", "ALL" }; + if (logfunc_timer == NULL) + dc_timer_new(&logfunc_timer); + FILE *fp = (FILE *)userdata; + dc_usecs_t now = 0; + dc_timer_now(logfunc_timer, &now); + + unsigned long seconds = now / 1000000; + unsigned long microseconds = now % 1000000; + if (loglevel == DC_LOGLEVEL_ERROR || loglevel == DC_LOGLEVEL_WARNING) { - fprintf(fp, "%s: %s [in %s:%d (%s)]\n", loglevels[loglevel], msg, file, line, function); + fprintf(fp, "[%li.%06li] %s: %s [in %s:%d (%s)]\n", + seconds, microseconds, + loglevels[loglevel], msg, file, line, function); } else { - fprintf(fp, "%s: %s\n", loglevels[loglevel], msg); + fprintf(fp, "[%li.%06li] %s: %s\n", seconds, microseconds, loglevels[loglevel], msg); } } |