From 17da360d6daf2dad86fa675800178e0b30da8c0d Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Tue, 12 Jul 2022 19:03:50 +0800 Subject: [PATCH] update: 'libsigrokdecode4DS' project used new log lib --- DSView/main.cpp | 6 +- DSView/pv/appcontrol.cpp | 5 +- DSView/pv/data/decoderstack.cpp | 6 +- DSView/pv/device/devinst.cpp | 4 +- DSView/pv/dock/protocoldock.cpp | 4 +- DSView/pv/log.cpp | 4 +- DSView/pv/mainwindow.cpp | 6 +- DSView/pv/sigsession.cpp | 24 +- libsigrok4DSL/hardware/DSL/dscope.c | 2 +- libsigrok4DSL/hardware/DSL/dslogic.c | 2 +- libsigrok4DSL/libsigrok.h | 10 - libsigrok4DSL/log.c | 4 +- libsigrok4DSL/session.c | 4 +- libsigrok4DSL/session_file.c | 2 +- libsigrokdecode4DSL/decoder.c | 1 + libsigrokdecode4DSL/exception.c | 5 +- libsigrokdecode4DSL/instance.c | 4 +- .../libsigrokdecode-internal.h | 16 +- libsigrokdecode4DSL/libsigrokdecode.h | 27 +- libsigrokdecode4DSL/log.c | 238 ++++-------------- libsigrokdecode4DSL/log.h | 57 +++++ libsigrokdecode4DSL/session.c | 8 +- libsigrokdecode4DSL/srd.c | 5 + libsigrokdecode4DSL/type_decoder.c | 7 +- libsigrokdecode4DSL/util.c | 5 +- 25 files changed, 181 insertions(+), 275 deletions(-) create mode 100644 libsigrokdecode4DSL/log.h diff --git a/DSView/main.cpp b/DSView/main.cpp index 96eec1e5..a5fada35 100644 --- a/DSView/main.cpp +++ b/DSView/main.cpp @@ -150,8 +150,8 @@ int main(int argc, char *argv[]) } #ifdef DEBUG_INFO - if (XLOG_LEVEL_DBG > logLevel){ - dsv_log_level(XLOG_LEVEL_DBG); // on develop mode, set the default log ldevel + if (XLOG_LEVEL_INFO > logLevel){ + dsv_log_level(XLOG_LEVEL_INFO); // on develop mode, set the default log ldevel } #endif @@ -195,7 +195,7 @@ bool bHighScale = true; //----------------------run dsv_info("----------------- version: %s-----------------", DS_VERSION_STRING); - dsv_dbg("Qt:%s", QT_VERSION_STR); + dsv_info("Qt:%s", QT_VERSION_STR); AppControl *control = AppControl::Instance(); AppConfig::Instance().LoadAll(); //load app config diff --git a/DSView/pv/appcontrol.cpp b/DSView/pv/appcontrol.cpp index cc899683..de318ee1 100644 --- a/DSView/pv/appcontrol.cpp +++ b/DSView/pv/appcontrol.cpp @@ -69,6 +69,7 @@ void AppControl::Destroy(){ bool AppControl::Init() { sr_log_set_context(dsv_log_context()); + srd_log_set_context(dsv_log_context()); // Initialise libsigrok if (sr_init(&sr_ctx) != SR_OK) @@ -81,8 +82,6 @@ bool AppControl::Init() QString resdir = GetResourceDir(); sr_set_firmware_resource_dir(resdir.toUtf8().data()); - - #ifdef _WIN32 //able run debug with qtcreator QString pythonHome = "c:/python"; @@ -99,7 +98,7 @@ bool AppControl::Init() QString dir = GetDecodeScriptDir(); strcpy(path, dir.toUtf8().data()); - dsv_dbg("decode script path: \"%s\"", dir.toUtf8().data()); + dsv_info("decode script path: \"%s\"", dir.toUtf8().data()); // Initialise libsigrokdecode if (srd_init(path) != SRD_OK) diff --git a/DSView/pv/data/decoderstack.cpp b/DSView/pv/data/decoderstack.cpp index 0ae0c7d9..5643fc5f 100644 --- a/DSView/pv/data/decoderstack.cpp +++ b/DSView/pv/data/decoderstack.cpp @@ -508,7 +508,7 @@ void DecoderStack::decode_data(const uint64_t decode_start, const uint64_t decod //struct srd_push_param push_param; if( i >= decode_end){ - dsv_dbg("%s", "decode data index have been end"); + dsv_info("%s", "decode data index have been end"); } while(i < decode_end && !_no_memory && !status->_bStop) @@ -580,7 +580,7 @@ void DecoderStack::decode_data(const uint64_t decode_start, const uint64_t decod _error_message = QString::fromLocal8Bit(error); } - dsv_dbg("%s%llu", "send to decoder times:", entry_cnt); + dsv_info("%s%llu", "send to decoder times: ", entry_cnt); if (error) g_free(error); @@ -608,7 +608,7 @@ void DecoderStack::execute_decode_stack() // Get the intial sample count _sample_count = _snapshot->get_sample_count(); - dsv_dbg("%s%llu", "decoder sample count:", _sample_count); + dsv_info("%s%llu", "decoder sample count: ", _sample_count); // Create the decoders for(auto &dec : _stack) diff --git a/DSView/pv/device/devinst.cpp b/DSView/pv/device/devinst.cpp index 19f5371c..1545cc32 100644 --- a/DSView/pv/device/devinst.cpp +++ b/DSView/pv/device/devinst.cpp @@ -199,12 +199,12 @@ void DevInst::start() void DevInst::run() { - dsv_dbg("%s", "session run loop start"); + dsv_info("%s", "session run loop start"); int ret = sr_session_run(); if (ret != SR_OK){ dsv_err("%s", "start session error!"); } - dsv_dbg("%s", "session run loop end"); + dsv_info("%s", "session run loop end"); } void DevInst::stop() diff --git a/DSView/pv/dock/protocoldock.cpp b/DSView/pv/dock/protocoldock.cpp index a6e37c6d..37510258 100644 --- a/DSView/pv/dock/protocoldock.cpp +++ b/DSView/pv/dock/protocoldock.cpp @@ -362,12 +362,12 @@ void ProtocolDock::on_add_protocol() bool ProtocolDock::add_protocol_by_id(QString id, bool silent, std::list &sub_decoders) { if (_session->is_device_re_attach() == true){ - dsv_dbg("%s", "Keep current decoders, cancel add new."); + dsv_info("%s", "Keep current decoders, cancel add new."); return true; } if (_session->get_device()->dev_inst()->mode != LOGIC) { - dsv_dbg("%s", "Protocol Analyzer\nProtocol Analyzer is only valid in Digital Mode!"); + dsv_info("%s", "Protocol Analyzer\nProtocol Analyzer is only valid in Digital Mode!"); return false; } diff --git a/DSView/pv/log.cpp b/DSView/pv/log.cpp index e01aca39..21dc3af5 100644 --- a/DSView/pv/log.cpp +++ b/DSView/pv/log.cpp @@ -52,7 +52,7 @@ xlog_context* dsv_log_context() void dsv_log_level(int l) { xlog_set_level(log_ctx, l); - dsv_dbg("%s%d", "set log level: ", l); + dsv_info("%s%d", "set log level: ", l); } void dsv_log_enalbe_logfile() @@ -70,6 +70,6 @@ void dsv_log_enalbe_logfile() xlog_add_receiver_from_file(log_ctx, lf.toUtf8().data(), 0); - dsv_dbg("%s\"%s\"", "store log to file: ", lf.toUtf8().data()); + dsv_info("%s\"%s\"", "store log to file: ", lf.toUtf8().data()); } } \ No newline at end of file diff --git a/DSView/pv/mainwindow.cpp b/DSView/pv/mainwindow.cpp index 8361883e..7d939f0a 100644 --- a/DSView/pv/mainwindow.cpp +++ b/DSView/pv/mainwindow.cpp @@ -445,7 +445,7 @@ void MainWindow::update_device_list() if (ldFileName != ""){ if (QFile::exists(ldFileName)){ - dsv_dbg("auto load file:%s", ldFileName.toUtf8().data()); + dsv_info("auto load file:%s", ldFileName.toUtf8().data()); on_load_file(ldFileName); } else{ @@ -976,7 +976,7 @@ bool MainWindow::load_session_json(QJsonDocument json, bool file_dev, bool bDeco } if (sessionObj["Version"].toInt() < BASE_SESSION_VERSION){ - dsv_dbg("%s", "session file version is error!"); + dsv_err("%s", "session file version is error!"); return false; } @@ -1459,7 +1459,7 @@ void MainWindow::switchLanguage(int language) retranslateUi(); } else{ - dsv_dbg("%s%d", "Unknown language code:", language); + dsv_err("%s%d", "Unknown language code:", language); } } diff --git a/DSView/pv/sigsession.cpp b/DSView/pv/sigsession.cpp index 8463cde2..35d65f2f 100644 --- a/DSView/pv/sigsession.cpp +++ b/DSView/pv/sigsession.cpp @@ -168,9 +168,9 @@ void SigSession::set_device(DevInst *dev_inst) } if (_dev_inst->is_file()) - dsv_dbg("%s\"%s\"", "Switch to file: ", dev_name.toUtf8().data()); + dsv_info("%s\"%s\"", "Switch to file: ", dev_name.toUtf8().data()); else - dsv_dbg("%s\"%s\"", "Switch to device: ", dev_name.toUtf8().data()); + dsv_info("%s\"%s\"", "Switch to device: ", dev_name.toUtf8().data()); try { _dev_inst->use(this); @@ -439,7 +439,7 @@ void SigSession::start_capture(bool instant) { // Check that a device instance has been selected. if (!_dev_inst) { - dsv_dbg("%s", "No device selected"); + dsv_err("%s", "No device selected"); capture_state_changed(SigSession::Stopped); return; } @@ -709,7 +709,7 @@ void SigSession::init_signals() clear_all_decoder(); } else{ - dsv_dbg("%s", "Device loose contact"); + dsv_err("%s", "Device loose contact"); } // Detect what data types we will receive @@ -1265,7 +1265,7 @@ void SigSession::hotplug_proc() if (!_dev_inst) return; - dsv_dbg("%s", "Hotplug thread start!"); + dsv_info("%s", "Hotplug thread start!"); try { while(_session && !_bHotplugStop) { @@ -1273,12 +1273,12 @@ void SigSession::hotplug_proc() sr_hotplug_wait_timout(_sr_ctx); if (_hot_attach) { - dsv_dbg("%s", "process event: DreamSourceLab hardware attached!"); + dsv_info("%s", "process event: DreamSourceLab hardware attached!"); _callback->device_attach(); _hot_attach = false; } if (_hot_detach) { - dsv_dbg("%s", "process event: DreamSourceLab hardware detached!"); + dsv_info("%s", "process event: DreamSourceLab hardware detached!"); _callback->device_detach(); _hot_detach = false; } @@ -1300,7 +1300,7 @@ void SigSession::hotplug_proc() dsv_err("%s", "Interrupt exception for hotplug thread was thrown."); } - dsv_dbg("%s", "Hotplug thread exit!"); + dsv_info("%s", "Hotplug thread exit!"); } void SigSession::register_hotplug_callback() @@ -1884,7 +1884,7 @@ void SigSession::set_stop_scale(float scale) if ((*it) == trace){ (*it)->decoder()->stop_decode_work(); _decode_tasks.erase(it); - dsv_dbg("%s", "remove a waiting decode task"); + dsv_info("%s", "remove a waiting decode task"); return; } } @@ -1973,7 +1973,7 @@ void SigSession::set_stop_scale(float scale) //the decode task thread proc void SigSession::decode_task_proc() { - dsv_dbg("%s", "------->decode thread start"); + dsv_info("%s", "------->decode thread start"); auto task = get_top_decode_task(); while (task != NULL) @@ -1983,7 +1983,7 @@ void SigSession::set_stop_scale(float scale) } if (task->_delete_flag){ - dsv_dbg("%s", "destroy a decoder in task thread"); + dsv_info("%s", "destroy a decoder in task thread"); DESTROY_QT_LATER(task); std::this_thread::sleep_for(std::chrono::milliseconds(100)); @@ -1995,7 +1995,7 @@ void SigSession::set_stop_scale(float scale) task = get_top_decode_task(); } - dsv_dbg("%s", "------->decode thread end"); + dsv_info("%s", "------->decode thread end"); _bDecodeRunning = false; } diff --git a/libsigrok4DSL/hardware/DSL/dscope.c b/libsigrok4DSL/hardware/DSL/dscope.c index ce142bc6..40b184dc 100644 --- a/libsigrok4DSL/hardware/DSL/dscope.c +++ b/libsigrok4DSL/hardware/DSL/dscope.c @@ -283,7 +283,7 @@ static GSList *scan(GSList *options) if (dsl_check_conf_profile(devlist[i])) { /* Already has the firmware, so fix the new address. */ - sr_dbg("Found an DSCope device."); + sr_info("Found an DSCope device."); sdi->status = SR_ST_INACTIVE; sdi->inst_type = SR_INST_USB; sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), diff --git a/libsigrok4DSL/hardware/DSL/dslogic.c b/libsigrok4DSL/hardware/DSL/dslogic.c index 1dd6538c..b2955fca 100644 --- a/libsigrok4DSL/hardware/DSL/dslogic.c +++ b/libsigrok4DSL/hardware/DSL/dslogic.c @@ -352,7 +352,7 @@ static GSList *scan(GSList *options) if (dsl_check_conf_profile(devlist[i])) { /* Already has the firmware, so fix the new address. */ - sr_dbg("Found an DSLogic device."); + sr_info("Found an DSLogic device."); sdi->status = SR_ST_INACTIVE; sdi->inst_type = SR_INST_USB; sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), diff --git a/libsigrok4DSL/libsigrok.h b/libsigrok4DSL/libsigrok.h index b41c03cf..14c120a8 100644 --- a/libsigrok4DSL/libsigrok.h +++ b/libsigrok4DSL/libsigrok.h @@ -133,16 +133,6 @@ enum { #define MAX_TIMEBASE SR_SEC(10) #define MIN_TIMEBASE SR_NS(10) -/** libsigrok loglevels. */ -enum { - SR_LOG_NONE = 0, /**< Output no messages at all. */ - SR_LOG_ERR = 1, /**< Output error messages. */ - SR_LOG_WARN = 2, /**< Output warnings. */ - SR_LOG_INFO = 3, /**< Output informational messages. */ - SR_LOG_DBG = 4, /**< Output debug messages. */ - SR_LOG_SPEW = 5, /**< Output very noisy debug messages. */ -}; - /* * Use SR_API to mark public API symbols, and SR_PRIV for private symbols. * diff --git a/libsigrok4DSL/log.c b/libsigrok4DSL/log.c index 9bc1627e..f11421cd 100644 --- a/libsigrok4DSL/log.c +++ b/libsigrok4DSL/log.c @@ -25,8 +25,8 @@ xlog_writer *sr_log = 0; static xlog_context *log_ctx = 0; //private log context -int is_private_log = 0; -int log_level_value = -1; +static int is_private_log = 0; +static int log_level_value = -1; /** * Init a private log context diff --git a/libsigrok4DSL/session.c b/libsigrok4DSL/session.c index 3ecc1407..831867f9 100644 --- a/libsigrok4DSL/session.c +++ b/libsigrok4DSL/session.c @@ -362,7 +362,7 @@ SR_API int sr_session_start(void) return SR_ERR_BUG; } - sr_dbg("Starting..."); + sr_info("Starting..."); ret = SR_OK; for (l = session->devs; l; l = l->next) { @@ -461,7 +461,7 @@ SR_PRIV int sr_session_stop_sync(void) return SR_ERR_BUG; } - sr_dbg("Stopping."); + sr_info("Stopping."); for (l = session->devs; l; l = l->next) { sdi = l->data; diff --git a/libsigrok4DSL/session_file.c b/libsigrok4DSL/session_file.c index 8afdb9ce..906ab8f5 100644 --- a/libsigrok4DSL/session_file.c +++ b/libsigrok4DSL/session_file.c @@ -124,7 +124,7 @@ SR_API int sr_session_load(const char *filename) kf = g_key_file_new(); if (!g_key_file_load_from_data(kf, metafile, fileInfo.uncompressed_size, 0, NULL)) { - sr_dbg("Failed to parse metadata."); + sr_err("Failed to parse metadata."); return SR_ERR; } diff --git a/libsigrokdecode4DSL/decoder.c b/libsigrokdecode4DSL/decoder.c index 1019c814..7d15723c 100644 --- a/libsigrokdecode4DSL/decoder.c +++ b/libsigrokdecode4DSL/decoder.c @@ -23,6 +23,7 @@ #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ #include "libsigrokdecode.h" #include +#include "log.h" /** * @file diff --git a/libsigrokdecode4DSL/exception.c b/libsigrokdecode4DSL/exception.c index e8c20633..67a9be86 100644 --- a/libsigrokdecode4DSL/exception.c +++ b/libsigrokdecode4DSL/exception.c @@ -22,6 +22,7 @@ #include "libsigrokdecode.h" #include #include +#include "log.h" static char *py_stringify(PyObject *py_obj) { @@ -48,7 +49,7 @@ cleanup: Py_XDECREF(py_str); if (!str) { PyErr_Clear(); - srd_dbg("Failed to stringify object."); + srd_err("Failed to stringify object."); } return str; @@ -79,7 +80,7 @@ cleanup: Py_XDECREF(py_str); if (!str) { PyErr_Clear(); - srd_dbg("Failed to get object attribute %s.", attr); + srd_err("Failed to get object attribute %s.", attr); } return str; diff --git a/libsigrokdecode4DSL/instance.c b/libsigrokdecode4DSL/instance.c index 72fde30f..2ffa2127 100644 --- a/libsigrokdecode4DSL/instance.c +++ b/libsigrokdecode4DSL/instance.c @@ -27,6 +27,7 @@ #include #include #include +#include "log.h" /** @cond PRIVATE */ @@ -1130,8 +1131,7 @@ static gpointer di_thread(gpointer data) srd_dbg("%s: decode() terminated (req %d).", di->inst_id, wanted_term); PyErr_Clear(); - PyGILState_Release(gstate); - //srd_dbg("%s: Thread done (with res).", di->inst_id); + PyGILState_Release(gstate); return NULL; } diff --git a/libsigrokdecode4DSL/libsigrokdecode-internal.h b/libsigrokdecode4DSL/libsigrokdecode-internal.h index 08b76245..f023360c 100644 --- a/libsigrokdecode4DSL/libsigrokdecode-internal.h +++ b/libsigrokdecode4DSL/libsigrokdecode-internal.h @@ -82,19 +82,11 @@ SRD_PRIV void srd_inst_free_all(struct srd_session *sess); * On MinGW, we need to specify the gnu_printf format flavor or GCC * will assume non-standard Microsoft printf syntax. */ -SRD_PRIV int srd_log(int loglevel, const char *format, ...) - __attribute__((__format__ (__gnu_printf__, 2, 3))); +//SRD_PRIV int srd_log(int loglevel, const char *format, ...) +// __attribute__((__format__ (__gnu_printf__, 2, 3))); #else -SRD_PRIV int srd_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3); -#endif - -#define srd_spew(...) srd_log(SRD_LOG_SPEW, __VA_ARGS__) -#define srd_dbg(...) srd_log(SRD_LOG_DBG, __VA_ARGS__) -#define srd_info(...) srd_log(SRD_LOG_INFO, __VA_ARGS__) -#define srd_warn(...) srd_log(SRD_LOG_WARN, __VA_ARGS__) -#define srd_err(...) srd_log(SRD_LOG_ERR, __VA_ARGS__) - -SRD_PRIV void debug_log(const char *str); +//SRD_PRIV int srd_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3); +#endif /* decoder.c */ SRD_PRIV long srd_decoder_apiver(const struct srd_decoder *d); diff --git a/libsigrokdecode4DSL/libsigrokdecode.h b/libsigrokdecode4DSL/libsigrokdecode.h index 1129d5d8..b4e31a9d 100644 --- a/libsigrokdecode4DSL/libsigrokdecode.h +++ b/libsigrokdecode4DSL/libsigrokdecode.h @@ -24,6 +24,7 @@ #include #include +#include #define DECODE_NUM_HEX_MAX_LEN 35 @@ -93,16 +94,6 @@ enum srd_error_code { */ }; -/* libsigrokdecode loglevels. */ -enum srd_loglevel { - SRD_LOG_NONE = 0, /**< Output no messages at all. */ - SRD_LOG_ERR = 1, /**< Output error messages. */ - SRD_LOG_WARN = 2, /**< Output warnings. */ - SRD_LOG_INFO = 3, /**< Output informational messages. */ - SRD_LOG_DBG = 4, /**< Output debug messages. */ - SRD_LOG_SPEW = 5, /**< Output very noisy debug messages. */ -}; - /* * Use SRD_API to mark public API symbols, and SRD_PRIV for private symbols. * @@ -418,13 +409,15 @@ SRD_API int srd_inst_initial_pins_set_all(struct srd_decoder_inst *di, GArray *initial_pins); /* log.c */ -typedef int (*srd_log_callback)(void *cb_data, int loglevel, - const char *format, va_list args); -SRD_API int srd_log_loglevel_set(int loglevel); -SRD_API int srd_log_loglevel_get(void); -SRD_API int srd_log_callback_get(srd_log_callback *cb, void **cb_data); -SRD_API int srd_log_callback_set(srd_log_callback cb, void *cb_data); -SRD_API int srd_log_callback_set_default(void); +/** + * Use a shared context, and drop the private log context + */ +SRD_API void srd_log_set_context(xlog_context *ctx); + +/** + * Set the private log context level + */ +SRD_API void srd_log_level(int level); /* error.c */ SRD_API const char *srd_strerror(int error_code); diff --git a/libsigrokdecode4DSL/log.c b/libsigrokdecode4DSL/log.c index e8be57a2..effea531 100644 --- a/libsigrokdecode4DSL/log.c +++ b/libsigrokdecode4DSL/log.c @@ -1,7 +1,8 @@ /* - * This file is part of the libsigrokdecode project. - * - * Copyright (C) 2011-2012 Uwe Hermann + * This file is part of the DSView project. + * DSView is based on PulseView. + * + * Copyright (C) 2022 DreamSourceLab * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,207 +15,72 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, see . + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ + + #include "log.h" -#include "config.h" -#include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ -#include "libsigrokdecode.h" -#include -#include -#include + #define LOG_DOMAIN "srd" + +xlog_writer *srd_log = 0; +static xlog_context *log_ctx = 0; //private log context +static int is_private_log = 0; +static int log_level_value = -1; /** - * @file - * - * Controlling the libsigrokdecode message logging functionality. + * Init a private log context */ - -/** - * @defgroup grp_logging Logging - * - * Controlling the libsigrokdecode message logging functionality. - * - * @{ - */ - -/* Currently selected libsigrokdecode loglevel. Default: SRD_LOG_WARN. */ -static int cur_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */ - -/* Function prototype. */ -static int srd_logv(void *cb_data, int loglevel, const char *format, - va_list args); - -/* Pointer to the currently selected log callback. Default: srd_logv(). */ -static srd_log_callback srd_log_cb = srd_logv; - -/* - * Pointer to private data that can be passed to the log callback. - * This can be used (for example) by C++ GUIs to pass a "this" pointer. - */ -static void *srd_log_cb_data = NULL; - -/** - * Set the libsigrokdecode loglevel. - * - * This influences the amount of log messages (debug messages, error messages, - * and so on) libsigrokdecode will output. Using SRD_LOG_NONE disables all - * messages. - * - * Note that this function itself will also output log messages. After the - * loglevel has changed, it will output a debug message with SRD_LOG_DBG for - * example. Whether this message is shown depends on the (new) loglevel. - * - * @param loglevel The loglevel to set (SRD_LOG_NONE, SRD_LOG_ERR, - * SRD_LOG_WARN, SRD_LOG_INFO, SRD_LOG_DBG, or SRD_LOG_SPEW). - * - * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel. - * - * @since 0.1.0 - */ -SRD_API int srd_log_loglevel_set(int loglevel) +SRD_PRIV void srd_log_init() { - if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) { - srd_err("Invalid loglevel %d.", loglevel); - return SRD_ERR_ARG; - } + if (!log_ctx && !srd_log){ + log_ctx = xlog_new(); + srd_log = xlog_create_writer(log_ctx, LOG_DOMAIN); + is_private_log = 1; - cur_loglevel = loglevel; - - srd_dbg("libsigrokdecode loglevel set to %d.", loglevel); - - return SRD_OK; + if (log_level_value != -1){ + xlog_set_level(log_ctx, log_level_value); + } + } } /** - * Get the libsigrokdecode loglevel. - * - * @return The currently configured libsigrokdecode loglevel. - * - * @since 0.1.0 + * Destroy the private log context */ -SRD_API int srd_log_loglevel_get(void) +SRD_PRIV void srd_log_uninit() { - return cur_loglevel; + if (is_private_log && log_ctx){ + xlog_free(log_ctx); + log_ctx = 0; + xlog_free_writer(srd_log); + srd_log = 0; + is_private_log = 0; + } } /** - * Set the libsigrokdecode log callback to the specified function. - * - * @param cb Function pointer to the log callback function to use. - * Must not be NULL. - * @param cb_data Pointer to private data to be passed on. This can be used - * by the caller to pass arbitrary data to the log functions. - * This pointer is only stored or passed on by libsigrokdecode, - * and is never used or interpreted in any way. The pointer - * is allowed to be NULL if the caller doesn't need/want to - * pass any data. - * - * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments. - * - * @since 0.3.0 + * Use a shared context, and drop the private log context */ -SRD_API int srd_log_callback_set(srd_log_callback cb, void *cb_data) -{ - if (!cb) { - srd_err("log: %s: cb was NULL", __func__); - return SRD_ERR_ARG; - } +SRD_API void srd_log_set_context(xlog_context *ctx) +{ + if (ctx){ + srd_log_uninit(); + srd_log = xlog_create_writer(ctx, LOG_DOMAIN); - /* Note: 'cb_data' is allowed to be NULL. */ - - srd_log_cb = cb; - srd_log_cb_data = cb_data; - - return SRD_OK; + if (log_level_value != -1){ + xlog_set_level(ctx, log_level_value); + } + } } /** - * Get the libsigrokdecode log callback routine and callback data. - * - * @param[out] cb Pointer to a function pointer to receive the log callback - * function. Optional, can be NULL. - * @param[out] cb_data Pointer to a void pointer to receive the log callback's - * additional arguments. Optional, can be NULL. - * - * @return SRD_OK upon success. - * - * @since 0.5.2 + * Set the private log context level */ -SRD_API int srd_log_callback_get(srd_log_callback *cb, void **cb_data) -{ - if (cb) - *cb = srd_log_cb; - if (cb_data) - *cb_data = srd_log_cb_data; - - return SRD_OK; -} - -/** - * Set the libsigrokdecode log callback to the default built-in one. - * - * Additionally, the internal 'srd_log_cb_data' pointer is set to NULL. - * - * @return SRD_OK upon success, a (negative) error code otherwise. - * - * @since 0.1.0 - */ -SRD_API int srd_log_callback_set_default(void) -{ - /* - * Note: No log output in this function, as it should safely work - * even if the currently set log callback is buggy/broken. - */ - srd_log_cb = srd_logv; - srd_log_cb_data = NULL; - - return SRD_OK; -} - -static int srd_logv(void *cb_data, int loglevel, const char *format, - va_list args) -{ - /* This specific log callback doesn't need the void pointer data. */ - (void)cb_data; - - (void)loglevel; - - if (fputs("srd: ", stderr) < 0 - || vfprintf(stderr, format, args) < 0 - || putc('\n', stderr) < 0) - return SRD_ERR; - - fflush(stderr); - - return SRD_OK; -} - -/** @private */ -SRD_PRIV int srd_log(int loglevel, const char *format, ...) -{ - int ret; - va_list args; - - /* Only output messages of at least the selected loglevel(s). */ - if (loglevel > cur_loglevel) - return SRD_OK; - - va_start(args, format); - ret = srd_log_cb(srd_log_cb_data, loglevel, format, args); - va_end(args); - - return ret; -} - -/* -*/ -void debug_log(const char *str) -{ - if (str && *str){ - fputs(str, stdout); - fflush(stdout); - } -} - -/** @} */ +SRD_API void srd_log_level(int level) +{ + log_level_value = level; + if (log_ctx) + { + xlog_set_level(log_ctx, level); + } +} \ No newline at end of file diff --git a/libsigrokdecode4DSL/log.h b/libsigrokdecode4DSL/log.h new file mode 100644 index 00000000..6be28bf4 --- /dev/null +++ b/libsigrokdecode4DSL/log.h @@ -0,0 +1,57 @@ +/* + * This file is part of the DSView project. + * DSView is based on PulseView. + * + * Copyright (C) 2022 DreamSourceLab + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _SRD_LOG_H_ +#define _SRD_LOG_H_ + +#include +#include "libsigrokdecode.h" + +extern xlog_writer *srd_log; + +/** + * Init a private log context + */ +SRD_PRIV void srd_log_init(); + +/** + * Destroy the private log context + */ +SRD_PRIV void srd_log_uninit(); + +/** + * Use a shared context, and drop the private log context + */ +SRD_API void srd_log_set_context(xlog_context *ctx); + +/** + * Set the private log context level + */ +SRD_API void srd_log_level(int level); + +#define LOG_PREFIX "" +#define srd_err(fmt, args...) xlog_err(srd_log, LOG_PREFIX fmt, ## args) +#define srd_warn(fmt, args...) xlog_warn(srd_log, LOG_PREFIX fmt, ## args) +#define srd_info(fmt, args...) xlog_info(srd_log, LOG_PREFIX fmt, ## args) +#define srd_dbg(fmt, args...) xlog_dbg(srd_log, LOG_PREFIX fmt, ## args) +#define srd_detail(fmt, args...) xlog_detail(srd_log, LOG_PREFIX fmt, ## args) + +#endif \ No newline at end of file diff --git a/libsigrokdecode4DSL/session.c b/libsigrokdecode4DSL/session.c index 7f116b5a..f22974c5 100644 --- a/libsigrokdecode4DSL/session.c +++ b/libsigrokdecode4DSL/session.c @@ -23,7 +23,7 @@ #include "libsigrokdecode.h" #include #include - +#include "log.h" SRD_PRIV int srd_call_sub_decoder_end(struct srd_decoder_inst *di, char **error); @@ -73,7 +73,7 @@ SRD_API int srd_session_new(struct srd_session **sess) /* Keep a list of all sessions, so we can clean up as needed. */ sessions = g_slist_append(sessions, *sess); - srd_dbg("Creating session %d.", (*sess)->session_id); + srd_info("Creating session %d.", (*sess)->session_id); return SRD_OK; } @@ -99,7 +99,7 @@ SRD_API int srd_session_start(struct srd_session *sess, char **error) if (!sess) return SRD_ERR_ARG; - srd_dbg("Calling start() of all instances in session %d.", sess->session_id); + srd_info("Calling start() of all instances in session %d.", sess->session_id); /* Run the start() method of all decoders receiving frontend data. */ ret = SRD_OK; @@ -347,7 +347,7 @@ SRD_API int srd_session_destroy(struct srd_session *sess) sessions = g_slist_remove(sessions, sess); g_free(sess); - srd_dbg("Destroyed session %d.", session_id); + srd_info("Destroyed session %d.", session_id); return SRD_OK; } diff --git a/libsigrokdecode4DSL/srd.c b/libsigrokdecode4DSL/srd.c index 5a43b98e..826c76e7 100644 --- a/libsigrokdecode4DSL/srd.c +++ b/libsigrokdecode4DSL/srd.c @@ -22,6 +22,7 @@ #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ #include "libsigrokdecode.h" #include +#include "log.h" /** @cond PRIVATE */ @@ -190,6 +191,8 @@ SRD_API int srd_init(const char *path) size_t i; int ret; const char *env_path; + + srd_log_init(); //init log if (max_session_id != -1) { srd_err("libsigrokdecode is already initialized."); @@ -310,6 +313,8 @@ SRD_API int srd_exit(void) max_session_id = -1; + srd_log_uninit(); //uninit log + return SRD_OK; } diff --git a/libsigrokdecode4DSL/type_decoder.c b/libsigrokdecode4DSL/type_decoder.c index ea74cca8..3b019e1e 100644 --- a/libsigrokdecode4DSL/type_decoder.c +++ b/libsigrokdecode4DSL/type_decoder.c @@ -23,6 +23,7 @@ #include "libsigrokdecode.h" #include #include +#include "log.h" /** @cond PRIVATE */ extern SRD_PRIV GSList *sessions; @@ -494,7 +495,7 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args) /* Upon SRD_OUTPUT_PYTHON for stacked PDs, we have a nicer log message later. */ if (pdo->output_type != SRD_OUTPUT_PYTHON && di->next_di != NULL) { - srd_spew("Instance %s put %"PRIu64 "-%" PRIu64 " %s on " + srd_detail("Instance %s put %"PRIu64 "-%" PRIu64 " %s on " "oid %d (%s).", di->inst_id, start_sample, end_sample, output_type_name(pdo->output_type), output_id, pdo->proto_id); @@ -526,7 +527,7 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args) for (l = di->next_di; l; l = l->next) { next_di = l->data; - srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s " + srd_detail("Instance %s put %" PRIu64 "-%" PRIu64 " %s " "on oid %d (%s) to instance %s.", di->inst_id, start_sample, end_sample, output_type_name(pdo->output_type), @@ -1224,7 +1225,7 @@ static PyObject *Decoder_printlog(PyObject *self, PyObject *args) } str = PyBytes_AsString(py_bytes); - debug_log(str); + srd_err("%s", str); //print string from python to console Py_DECREF(py_bytes); PyGILState_Release(gstate); diff --git a/libsigrokdecode4DSL/util.c b/libsigrokdecode4DSL/util.c index defe7c66..a1dfe1ae 100644 --- a/libsigrokdecode4DSL/util.c +++ b/libsigrokdecode4DSL/util.c @@ -20,6 +20,7 @@ #include "config.h" #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ +#include "log.h" /** * Import a Python module by name. @@ -187,7 +188,7 @@ SRD_PRIV int py_dictitem_as_str(PyObject *py_obj, const char *key, } if (!(py_value = PyDict_GetItemString(py_obj, key))) { - srd_dbg("Dictionary has no attribute '%s'.", key); + srd_detail("Dictionary has no attribute '%s'.", key); goto err; } @@ -225,7 +226,7 @@ SRD_PRIV int py_dictitem_to_int(PyObject *py_obj, const char *key) } if (!(py_value = PyDict_GetItemString(py_obj, key))) { - srd_dbg("Dictionary has no attribute '%s'.", key); + srd_detail("Dictionary has no attribute '%s'.", key); goto err; }