]> sigrok.org Git - pulseview.git/commitdiff
Logging: obsolete -s, log to internal buffer as well as stdout
authorGerhard Sittig <redacted>
Wed, 15 Aug 2018 21:48:52 +0000 (23:48 +0200)
committerUwe Hermann <redacted>
Thu, 30 Aug 2018 17:17:20 +0000 (19:17 +0200)
Query the sr and srd libraries' current log routines before registering
PV's own log callback which queues messages in an internal buffer. This
allows duplication of messages to the internal buffer _and_ stdout, and
thus obsoletes the -s option.

main.cpp
pv/logging.cpp

index 780371b9945b396514ffeac40b0678ebca946a33..ce1257300383e30fe1b6daa8893820737699cac4 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -155,7 +155,6 @@ void usage()
                "  -i, --input-file                Load input from file\n"
                "  -I, --input-format              Input format\n"
                "  -c, --clean                     Don't restore previous sessions on startup\n"
-               "  -s, --log-to-stdout             Don't use logging, output to stdout instead\n"
                "\n", PV_BIN_NAME);
 }
 
@@ -167,7 +166,6 @@ int main(int argc, char *argv[])
        vector<string> open_files;
        bool restore_sessions = true;
        bool do_scan = true;
-       bool do_logging = true;
 
        Application a(argc, argv);
 
@@ -193,7 +191,7 @@ int main(int argc, char *argv[])
                };
 
                const int c = getopt_long(argc, argv,
-                       "h?VDcsl:d:i:I:", long_options, nullptr);
+                       "h?VDcl:d:i:I:", long_options, nullptr);
                if (c == -1)
                        break;
 
@@ -248,10 +246,6 @@ int main(int argc, char *argv[])
                case 'c':
                        restore_sessions = false;
                        break;
-
-               case 's':
-                       do_logging = false;
-                       break;
                }
        }
        argc -= optind;
@@ -264,8 +258,7 @@ int main(int argc, char *argv[])
        pv::GlobalSettings settings;
        settings.set_defaults_where_needed();
 
-       if (do_logging)
-               pv::logging.init();
+       pv::logging.init();
 
        // Initialise libsigrok
        context = sigrok::Context::create();
index dea333e534489b78bae632da28ccad90c56e35bd..83577af200cee7afff6b0cb384a485d63883256a 100644 (file)
@@ -37,12 +37,24 @@ Logging logging;
 const int Logging::MIN_BUFFER_SIZE = 10;
 const int Logging::MAX_BUFFER_SIZE = 50000;
 
+static sr_log_callback prev_sr_log_cb;
+static void *prev_sr_log_cb_data;
+
+#ifdef ENABLE_DECODE
+static srd_log_callback prev_srd_log_cb;
+static void *prev_srd_log_cb_data;
+#endif
+
 Logging::~Logging()
 {
        qInstallMessageHandler(nullptr);
-       sr_log_callback_set_default();
+       sr_log_callback_set(prev_sr_log_cb, prev_sr_log_cb_data);
+       prev_sr_log_cb = NULL;
+       prev_sr_log_cb_data = NULL;
 #ifdef ENABLE_DECODE
-       srd_log_callback_set_default();
+       srd_log_callback_set(prev_srd_log_cb, prev_srd_log_cb_data);
+       prev_srd_log_cb = NULL;
+       prev_srd_log_cb_data = NULL;
 #endif
 
        GlobalSettings::remove_change_handler(this);
@@ -58,8 +70,10 @@ void Logging::init()
        buffer_.reserve(buffer_size_);
 
        qInstallMessageHandler(log_pv);
+       sr_log_callback_get(&prev_sr_log_cb, &prev_sr_log_cb_data);
        sr_log_callback_set(log_sr, nullptr);
 #ifdef ENABLE_DECODE
+       srd_log_callback_get(&prev_srd_log_cb, &prev_srd_log_cb_data);
        srd_log_callback_set(log_srd, nullptr);
 #endif
 
@@ -138,8 +152,14 @@ void Logging::log_pv(QtMsgType type, const QMessageLogContext &context, const QS
 
 int Logging::log_sr(void *cb_data, int loglevel, const char *format, va_list args)
 {
+       va_list args2;
+
        (void)cb_data;
-       (void)loglevel;
+
+       va_copy(args2, args);
+       if (prev_sr_log_cb)
+               prev_sr_log_cb(prev_sr_log_cb_data, loglevel, format, args2);
+       va_end(args2);
 
        char *text = g_strdup_vprintf(format, args);
        logging.log(QString::fromUtf8(text), LogSource_sr);
@@ -151,8 +171,14 @@ int Logging::log_sr(void *cb_data, int loglevel, const char *format, va_list arg
 #ifdef ENABLE_DECODE
 int Logging::log_srd(void *cb_data, int loglevel, const char *format, va_list args)
 {
+       va_list args2;
+
        (void)cb_data;
-       (void)loglevel;
+
+       va_copy(args2, args);
+       if (prev_srd_log_cb)
+               prev_srd_log_cb(prev_srd_log_cb_data, loglevel, format, args2);
+       va_end(args2);
 
        char *text = g_strdup_vprintf(format, args);
        logging.log(QString::fromUtf8(text), LogSource_srd);