[PATCH 4/5] Enable diagnostic logs from dc configuration

Anton Lundin glance at acc.umu.se
Wed May 27 12:19:12 PDT 2015


This implements support for writing debug/diagnostic logs when
configuring dive computers and upgrading the firmware.

Signed-off-by: Anton Lundin <glance at acc.umu.se>
---
 configuredivecomputerthreads.cpp | 90 +++++++++++++++++++++++++++++++++++++++-
 libdivecomputer.c                |  2 +-
 libdivecomputer.h                |  1 +
 3 files changed, 91 insertions(+), 2 deletions(-)

diff --git a/configuredivecomputerthreads.cpp b/configuredivecomputerthreads.cpp
index 72915ea..9939fd1 100644
--- a/configuredivecomputerthreads.cpp
+++ b/configuredivecomputerthreads.cpp
@@ -1,5 +1,6 @@
 #include "configuredivecomputerthreads.h"
 #include "libdivecomputer/hw.h"
+#include "libdivecomputer.h"
 #include <QDateTime>
 #include <QStringList>
 
@@ -1463,9 +1464,26 @@ ReadSettingsThread::ReadSettingsThread(QObject *parent, device_data_t *data) : D
 
 void ReadSettingsThread::run()
 {
+	FILE *fp = NULL;
 	bool supported = false;
 	dc_status_t rc;
 
+	if (m_data->libdc_log)
+		fp = subsurface_fopen(logfile_name, "w");
+
+	m_data->libdc_logfile = fp;
+
+	rc = dc_context_new(&m_data->context);
+	if (rc != DC_STATUS_SUCCESS) {
+		emit error(tr("Unable to create libdivecomputer context"));
+		return;
+	}
+
+	if (fp) {
+		dc_context_set_loglevel(m_data->context, DC_LOGLEVEL_ALL);
+		dc_context_set_logfunc(m_data->context, logfunc, fp);
+	}
+
 	rc = dc_device_open(&m_data->device, m_data->context, m_data->descriptor, m_data->devname);
 	if (rc == DC_STATUS_SUCCESS) {
 		DeviceDetails *m_deviceDetails = new DeviceDetails(0);
@@ -1516,6 +1534,10 @@ void ReadSettingsThread::run()
 	} else {
 		emit error(tr("Could not a establish connection to the dive computer."));
 	}
+	dc_context_free(m_data->context);
+
+	if (fp)
+		fclose(fp);
 }
 
 WriteSettingsThread::WriteSettingsThread(QObject *parent, device_data_t *data) : DeviceThread(parent, data)
@@ -1529,9 +1551,26 @@ void WriteSettingsThread::setDeviceDetails(DeviceDetails *details)
 
 void WriteSettingsThread::run()
 {
+	FILE *fp = NULL;
 	bool supported = false;
 	dc_status_t rc;
 
+	if (m_data->libdc_log)
+		fp = subsurface_fopen(logfile_name, "w");
+
+	m_data->libdc_logfile = fp;
+
+	rc = dc_context_new(&m_data->context);
+	if (rc != DC_STATUS_SUCCESS) {
+		emit error(tr("Unable to create libdivecomputer context"));
+		return;
+	}
+
+	if (fp) {
+		dc_context_set_loglevel(m_data->context, DC_LOGLEVEL_ALL);
+		dc_context_set_logfunc(m_data->context, logfunc, fp);
+	}
+
 	rc = dc_device_open(&m_data->device, m_data->context, m_data->descriptor, m_data->devname);
 	if (rc == DC_STATUS_SUCCESS) {
 		switch (dc_device_get_type(m_data->device)) {
@@ -1576,6 +1615,11 @@ void WriteSettingsThread::run()
 	} else {
 		emit error(tr("Could not a establish connection to the dive computer."));
 	}
+
+	dc_context_free(m_data->context);
+
+	if (fp)
+		fclose(fp);
 }
 
 
@@ -1585,16 +1629,33 @@ FirmwareUpdateThread::FirmwareUpdateThread(QObject *parent, device_data_t *data,
 
 void FirmwareUpdateThread::run()
 {
+	FILE *fp = NULL;
 	bool supported = false;
 	dc_status_t rc;
 
+	if (m_data->libdc_log)
+		fp = subsurface_fopen(logfile_name, "w");
+
+	m_data->libdc_logfile = fp;
+
+	rc = dc_context_new(&m_data->context);
+	if (rc != DC_STATUS_SUCCESS) {
+		emit error(tr("Unable to create libdivecomputer context"));
+		return;
+	}
+
+	if (fp) {
+		dc_context_set_loglevel(m_data->context, DC_LOGLEVEL_ALL);
+		dc_context_set_logfunc(m_data->context, logfunc, fp);
+	}
+
 	rc = dc_device_open(&m_data->device, m_data->context, m_data->descriptor, m_data->devname);
 	if (rc == DC_STATUS_SUCCESS) {
 		rc = dc_device_set_events(m_data->device, DC_EVENT_PROGRESS, DeviceThread::event_cb, this);
 		if (rc != DC_STATUS_SUCCESS) {
 			emit error("Error registering the event handler.");
 			dc_device_close(m_data->device);
-			return;
+			goto firmware_run_out;
 		}
 		switch (dc_device_get_type(m_data->device)) {
 #if DC_VERSION_CHECK(0, 5, 0)
@@ -1621,6 +1682,11 @@ void FirmwareUpdateThread::run()
 	} else {
 		emit error(tr("Could not a establish connection to the dive computer."));
 	}
+firmware_run_out:
+	dc_context_free(m_data->context);
+
+	if (fp)
+		fclose(fp);
 }
 
 
@@ -1630,8 +1696,26 @@ ResetSettingsThread::ResetSettingsThread(QObject *parent, device_data_t *data) :
 
 void ResetSettingsThread::run()
 {
+	FILE *fp = NULL;
 	bool supported = false;
 	dc_status_t rc;
+
+	if (m_data->libdc_log)
+		fp = subsurface_fopen(logfile_name, "w");
+
+	m_data->libdc_logfile = fp;
+
+	rc = dc_context_new(&m_data->context);
+	if (rc != DC_STATUS_SUCCESS) {
+		emit error(tr("Unable to create libdivecomputer context"));
+		return;
+	}
+
+	if (fp) {
+		dc_context_set_loglevel(m_data->context, DC_LOGLEVEL_ALL);
+		dc_context_set_logfunc(m_data->context, logfunc, fp);
+	}
+
 	rc = dc_device_open(&m_data->device, m_data->context, m_data->descriptor, m_data->devname);
 	if (rc == DC_STATUS_SUCCESS) {
 #if DC_VERSION_CHECK(0, 5, 0)
@@ -1649,4 +1733,8 @@ void ResetSettingsThread::run()
 	} else {
 		emit error(tr("Could not a establish connection to the dive computer."));
 	}
+	dc_context_free(m_data->context);
+
+	if (fp)
+		fclose(fp);
 }
diff --git a/libdivecomputer.c b/libdivecomputer.c
index f07023f..2522a92 100644
--- a/libdivecomputer.c
+++ b/libdivecomputer.c
@@ -874,7 +874,7 @@ static const char *do_device_import(device_data_t *data)
 	return NULL;
 }
 
-static void
+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" };
diff --git a/libdivecomputer.h b/libdivecomputer.h
index 2e4fb9a..dfb6267 100644
--- a/libdivecomputer.h
+++ b/libdivecomputer.h
@@ -45,6 +45,7 @@ const char *errmsg (dc_status_t rc);
 const char *do_libdivecomputer_import(device_data_t *data);
 const char *do_uemis_import(device_data_t *data);
 dc_status_t libdc_buffer_parser(struct dive *dive, device_data_t *data, unsigned char *buffer, int size);
+void logfunc(dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *msg, void *userdata);
 
 extern int import_thread_cancelled;
 extern const char *progress_bar_text;
-- 
2.1.4



More information about the subsurface mailing list