diff options
author | Jef Driesen <jefdriesen@telenet.be> | 2013-12-23 21:07:13 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-12-24 09:57:10 -0800 |
commit | 60d85de292e0d0b86a897f32334657346173c170 (patch) | |
tree | 50b5d3021b5b9bd9ac999fd021b5b325d653acaf | |
parent | badce21b242affd8585cfc112290a445c3142315 (diff) | |
download | subsurface-60d85de292e0d0b86a897f32334657346173c170.tar.gz |
Enable diagnostic logging from libdivecomputer.
Signed-off-by: Jef Driesen <jefdriesen@telenet.be>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | libdivecomputer.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/libdivecomputer.c b/libdivecomputer.c index bfa223731..37f6546f2 100644 --- a/libdivecomputer.c +++ b/libdivecomputer.c @@ -679,26 +679,56 @@ static const char *do_device_import(device_data_t *data) return NULL; } +static void +logfunc(dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *msg, void *userdata) +{ + const char *loglevels[] = {"NONE", "ERROR", "WARNING", "INFO", "DEBUG", "ALL"}; + + FILE *fp = (FILE *) userdata; + + if (loglevel == DC_LOGLEVEL_ERROR || loglevel == DC_LOGLEVEL_WARNING) { + fprintf(fp, "%s: %s [in %s:%d (%s)]\n", loglevels[loglevel], msg, file, line, function); + } else { + fprintf(fp, "%s: %s\n", loglevels[loglevel], msg); + } +} + const char *do_libdivecomputer_import(device_data_t *data) { dc_status_t rc; const char *err; + FILE *fp = NULL; import_dive_number = 0; first_temp_is_air = 0; data->device = NULL; data->context = NULL; + /* TODO: Enable logging from the UI somehow. */ + /* TODO: Should the filename (and directory) be configurable? */ + fp = fopen("subsurface.log", "w"); + rc = dc_context_new(&data->context); if (rc != DC_STATUS_SUCCESS) return translate("gettextFromC","Unable to create libdivecomputer context"); + if (fp) { + dc_context_set_loglevel(data->context, DC_LOGLEVEL_ALL); + dc_context_set_logfunc(data->context, logfunc, fp); + } + err = translate("gettextFromC","Unable to open %s %s (%s)"); rc = dc_device_open(&data->device, data->context, data->descriptor, data->devname); if (rc == DC_STATUS_SUCCESS) { err = do_device_import(data); + /* TODO: Show the logfile to the user on error. */ dc_device_close(data->device); } dc_context_free(data->context); + + if (fp) { + fclose(fp); + } + return err; } |