From 4071620361e32264c2581a77d104c12bfef62133 Mon Sep 17 00:00:00 2001
From: David Winter <david.winter@analog.com>
Date: Mon, 28 Jun 2021 09:46:48 +0200
Subject: gr: logger: Add logger_get_configured_logger()

A helper method is added to gr/lib/logger.cc to facilitate the
allocation of loggers which are automatically configured according to the
local GNURadio configuration.

Signed-off-by: David Winter <david.winter@analog.com>
---
 gnuradio-runtime/lib/logger.cc | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

(limited to 'gnuradio-runtime/lib/logger.cc')

diff --git a/gnuradio-runtime/lib/logger.cc b/gnuradio-runtime/lib/logger.cc
index ea3febfc34..eebc4a68e2 100644
--- a/gnuradio-runtime/lib/logger.cc
+++ b/gnuradio-runtime/lib/logger.cc
@@ -153,6 +153,31 @@ logger_ptr logger_get_logger(std::string name)
     }
 }
 
+logger_ptr logger_get_configured_logger(const std::string& name)
+{
+    if (log4cpp::Category::exists(name))
+        return &log4cpp::Category::getInstance(name);
+
+    prefs* p = prefs::singleton();
+    std::string config_file = p->get_string("LOG", "log_config", "");
+    std::string log_level = p->get_string("LOG", "log_level", "off");
+    std::string log_file = p->get_string("LOG", "log_file", "");
+
+    GR_LOG_GETLOGGER(LOG, "gr_log." + name);
+    GR_LOG_SET_LEVEL(LOG, log_level);
+
+    if (!log_file.empty()) {
+        if (log_file == "stdout") {
+            GR_LOG_SET_CONSOLE_APPENDER(LOG, "stdout", "gr::log :%p: %c{1} - %m%n");
+        } else if (log_file == "stderr") {
+            GR_LOG_SET_CONSOLE_APPENDER(LOG, "stderr", "gr::log :%p: %c{1} - %m%n");
+        } else {
+            GR_LOG_SET_FILE_APPENDER(LOG, log_file, true, "%r :%p: %c{1} - %m%n");
+        }
+    }
+    return LOG;
+}
+
 bool logger_load_config(const std::string& config_filename)
 {
     if (!config_filename.empty()) {
-- 
cgit v1.2.3