commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 01/01: runtime: fix a memory leak in the lo


From: git
Subject: [Commit-gnuradio] [gnuradio] 01/01: runtime: fix a memory leak in the logger when restarting a flowgraph due to adding new appenders to the logger. Now we clear them and set a
Date: Mon, 30 Jun 2014 02:29:21 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

trondeau pushed a commit to branch maint
in repository gnuradio.

commit 0ae33fc67a7655b63a6384a1898319b7cc2c5bf6
Author: Tom Rondeau <address@hidden>
Date:   Sun Jun 29 22:01:02 2014 -0400

    runtime: fix a memory leak in the logger when restarting a flowgraph due to 
adding new appenders to the logger. Now we clear them and set a
    
    This adds a set of "set" functions to set a new appender. We can always add 
additional loggers using the old "add" functions.
---
 gnuradio-runtime/include/gnuradio/logger.h.in | 86 +++++++++++++++++++++++++++
 gnuradio-runtime/lib/block.cc                 | 12 ++--
 gnuradio-runtime/lib/logger.cc                | 15 +++++
 gnuradio-runtime/lib/tpb_thread_body.cc       | 40 ++++++-------
 4 files changed, 127 insertions(+), 26 deletions(-)

diff --git a/gnuradio-runtime/include/gnuradio/logger.h.in 
b/gnuradio-runtime/include/gnuradio/logger.h.in
index 7fcc519..17afa7e 100644
--- a/gnuradio-runtime/include/gnuradio/logger.h.in
+++ b/gnuradio-runtime/include/gnuradio/logger.h.in
@@ -76,10 +76,16 @@ namespace gr {
 #define GR_LOG_GET_LEVEL(logger, level)
 #define GR_ADD_APPENDER(name,appender)
 #define GR_LOG_ADD_APPENDER(logger,appender)
+#define GR_SET_APPENDER(name,appender)
+#define GR_LOG_SET_APPENDER(logger,appender)
 #define GR_ADD_CONSOLE_APPENDER(logger,target,pattern)
 #define GR_LOG_ADD_CONSOLE_APPENDER(logger,target,pattern)
+#define GR_SET_CONSOLE_APPENDER(logger,target,pattern)
+#define GR_LOG_SET_CONSOLE_APPENDER(logger,target,pattern)
 #define GR_ADD_FILE_APPENDER(name,filename,append,pattern)
 #define GR_LOG_ADD_FILE_APPENDER(logger,filename,append,pattern)
+#define GR_SET_FILE_APPENDER(name,filename,append,pattern)
+#define GR_LOG_SET_FILE_APPENDER(logger,filename,append,pattern)
 #define 
GR_ADD_ROLLINGFILE_APPENDER(name,filename,filesize,bkup_index,append,mode,pattern)
 #define 
GR_LOG_ADD_ROLLINGFILE_APPENDER(logger,filename,filesize,bkup_index,append,mode,pattern)
 #define GR_GET_LOGGER_NAMES(names)
@@ -180,6 +186,13 @@ namespace gr {
 #define GR_LOG_ADD_APPENDER(logger, appender) { \
     gr::logger_add_appender(logger, appender);}
 
+#define GR_SET_APPENDER(name, appender) {                       \
+    gr::logger_ptr logger = gr::logger_get_logger(name);        \
+    gr::logger_set_appender(logger,appender);}
+
+#define GR_LOG_SET_APPENDER(logger, appender) { \
+    gr::logger_set_appender(logger, appender);}
+
 #define GR_ADD_CONSOLE_APPENDER(name, target, pattern) {        \
     gr::logger_ptr logger = gr::logger_get_logger(name);        \
     gr::logger_add_console_appender(logger,target,pattern);}
@@ -187,6 +200,13 @@ namespace gr {
 #define GR_LOG_ADD_CONSOLE_APPENDER(logger, target, pattern) {  \
     gr::logger_add_console_appender(logger,target,pattern);}
 
+#define GR_SET_CONSOLE_APPENDER(name, target, pattern) {        \
+    gr::logger_ptr logger = gr::logger_get_logger(name);        \
+    gr::logger_set_console_appender(logger,target,pattern);}
+
+#define GR_LOG_SET_CONSOLE_APPENDER(logger, target, pattern) {  \
+    gr::logger_set_console_appender(logger,target,pattern);}
+
 #define GR_ADD_FILE_APPENDER(name, filename, append, pattern) {         \
     gr::logger_ptr logger = gr::logger_get_logger(name);                \
     gr::logger_add_file_appender(logger,filename,append,pattern);}
@@ -194,6 +214,13 @@ namespace gr {
 #define GR_LOG_ADD_FILE_APPENDER(logger, filename, append, pattern) {   \
     gr::logger_add_file_appender(logger,filename,append,pattern);}
 
+#define GR_SET_FILE_APPENDER(name, filename, append, pattern) {         \
+    gr::logger_ptr logger = gr::logger_get_logger(name);                \
+    gr::logger_set_file_appender(logger,filename,append,pattern);}
+
+#define GR_LOG_SET_FILE_APPENDER(logger, filename, append, pattern) {   \
+    gr::logger_set_file_appender(logger,filename,append,pattern);}
+
 #define GR_ADD_ROLLINGFILE_APPENDER(name, filename, filesize, bkup_index, 
append, mode, pattern) { \
     gr::logger_ptr logger = gr::logger_get_logger(name);                    \
     
gr::logger_add_rollingfile_appender(logger,filename,filesize,bkup_index,append,mode,pattern);}
@@ -520,6 +547,17 @@ namespace gr {
                                           std::string appender);
 
   /*!
+   * \brief Sets a console appender to a given logger. Deletes any
+   * existing appenders and adds a new one. To add an additional
+   * appender, use logger_add_appender.
+   *
+   * \param logger Logger to which appender will be added
+   * \param appender Name of appender to add to logger
+   */
+  GR_RUNTIME_API void logger_set_appender(logger_ptr logger,
+                                          std::string appender);
+
+  /*!
    * \brief Add console appender to a given logger
    *
    * Add console appender to a given logger
@@ -533,6 +571,19 @@ namespace gr {
                                                   std::string pattern);
 
   /*!
+   * \brief Sets a new console appender to a given logger after
+   * removing all others. Use logger_add_console_appender to add
+   * another.
+   *
+   * \param logger Logger to which appender will be added
+   * \param target Std target to write 'cout' or 'cerr' (default is cout)
+   * \param pattern Formating pattern for log messages
+   */
+  GR_RUNTIME_API void logger_set_console_appender(logger_ptr logger,
+                                                  std::string target,
+                                                  std::string pattern);
+
+  /*!
    * \brief Add file appender to a given logger
    *
    * Add file appender to a given logger
@@ -547,6 +598,19 @@ namespace gr {
                                                bool append, std::string 
pattern);
 
   /*!
+   * \brief Set a file appender to a given logger. To add another file
+   * appender, use logger_add_file_appender.
+   *
+   * \param logger Logger to which appender will be added
+   * \param filename File to which log will be written
+   * \param append Overwrite or append to log file
+   * \param pattern Formating pattern for log messages
+   */
+  GR_RUNTIME_API void logger_set_file_appender(logger_ptr logger,
+                                               std::string filename,
+                                               bool append, std::string 
pattern);
+
+  /*!
    * \brief Add rolling file appender to a given logger
    *
    * Add rolling file appender to a given logger
@@ -594,10 +658,16 @@ namespace gr {
 #define GR_LOG_GET_LEVEL(logger, level)
 #define GR_ADD_APPENDER(name,appender)
 #define GR_LOG_ADD_APPENDER(logger,appender)
+#define GR_SET_APPENDER(name,appender)
+#define GR_LOG_SET_APPENDER(logger,appender)
 #define GR_ADD_CONSOLE_APPENDER(logger,target,pattern)
 #define GR_LOG_ADD_CONSOLE_APPENDER(logger,target,pattern)
+#define GR_SET_CONSOLE_APPENDER(logger,target,pattern)
+#define GR_LOG_SET_CONSOLE_APPENDER(logger,target,pattern)
 #define GR_ADD_FILE_APPENDER(name,filename,append,pattern)
 #define GR_LOG_ADD_FILE_APPENDER(logger,filename,append,pattern)
+#define GR_SET_FILE_APPENDER(name,filename,append,pattern)
+#define GR_LOG_SET_FILE_APPENDER(logger,filename,append,pattern)
 #define 
GR_ADD_ROLLINGFILE_APPENDER(name,filename,filesize,bkup_index,append,mode,pattern)
 #define 
GR_LOG_ADD_ROLLINGFILE_APPENDER(logger,filename,filesize,bkup_index,append,mode,pattern)
 #define GR_GET_LOGGER_NAMES(names)
@@ -703,16 +773,32 @@ namespace gr {
       GR_LOG_ADD_APPENDER(d_logger, appender);
     }
 
+    /*! \brief inline function, Method to set an appender to logger by
+        name (define appender in conf file) */
+    void set_appender(std::string appender) {
+      GR_LOG_SET_APPENDER(d_logger, appender);
+    }
+
     /*! \brief inline function, Method to add console appender to logger */
     void add_console_appender(std::string target,std::string pattern) {
       GR_LOG_ADD_CONSOLE_APPENDER(d_logger, target, pattern);
     }
 
+    /*! \brief inline function, Method to set a console appender to logger */
+    void set_console_appender(std::string target,std::string pattern) {
+      GR_LOG_SET_CONSOLE_APPENDER(d_logger, target, pattern);
+    }
+
     /*! \brief inline function, Method to add file appender to logger */
     void add_file_appender(std::string filename, bool append, std::string 
pattern) {
       GR_LOG_ADD_FILE_APPENDER(d_logger, filename, append, pattern);
     }
 
+    /*! \brief inline function, Method to set a file appender to logger */
+    void set_file_appender(std::string filename, bool append, std::string 
pattern) {
+      GR_LOG_SET_FILE_APPENDER(d_logger, filename, append, pattern);
+    }
+
     /*! \brief inline function, Method to add rolling file appender to logger 
*/
     void add_rollingfile_appender(std::string filename, size_t filesize,
                                   int bkup_index, bool append, mode_t mode,
diff --git a/gnuradio-runtime/lib/block.cc b/gnuradio-runtime/lib/block.cc
index 46cbc98..9e4fcf5 100644
--- a/gnuradio-runtime/lib/block.cc
+++ b/gnuradio-runtime/lib/block.cc
@@ -77,13 +77,13 @@ namespace gr {
     GR_LOG_SET_LEVEL(LOG, log_level);
     if(log_file.size() > 0) {
       if(log_file == "stdout") {
-        GR_LOG_ADD_CONSOLE_APPENDER(LOG, "cout","gr::log :%p: %c{1} - %m%n");
+        GR_LOG_SET_CONSOLE_APPENDER(LOG, "cout","gr::log :%p: %c{1} - %m%n");
       }
       else if(log_file == "stderr") {
-        GR_LOG_ADD_CONSOLE_APPENDER(LOG, "cerr","gr::log :%p: %c{1} - %m%n");
+        GR_LOG_SET_CONSOLE_APPENDER(LOG, "cerr","gr::log :%p: %c{1} - %m%n");
       }
       else {
-        GR_LOG_ADD_FILE_APPENDER(LOG, log_file , true,"%r :%p: %c{1} - %m%n");
+        GR_LOG_SET_FILE_APPENDER(LOG, log_file , true,"%r :%p: %c{1} - %m%n");
       }
     }
     d_logger = LOG;
@@ -92,13 +92,13 @@ namespace gr {
     GR_LOG_SET_LEVEL(DLOG, debug_level);
     if(debug_file.size() > 0) {
       if(debug_file == "stdout") {
-        GR_LOG_ADD_CONSOLE_APPENDER(DLOG, "cout","gr::debug :%p: %c{1} - 
%m%n");
+        GR_LOG_SET_CONSOLE_APPENDER(DLOG, "cout","gr::debug :%p: %c{1} - 
%m%n");
       }
       else if(debug_file == "stderr") {
-        GR_LOG_ADD_CONSOLE_APPENDER(DLOG, "cerr", "gr::debug :%p: %c{1} - 
%m%n");
+        GR_LOG_SET_CONSOLE_APPENDER(DLOG, "cerr", "gr::debug :%p: %c{1} - 
%m%n");
       }
       else {
-        GR_LOG_ADD_FILE_APPENDER(DLOG, debug_file, true, "%r :%p: %c{1} - 
%m%n");
+        GR_LOG_SET_FILE_APPENDER(DLOG, debug_file, true, "%r :%p: %c{1} - 
%m%n");
       }
     }
     d_debug_logger = DLOG;
diff --git a/gnuradio-runtime/lib/logger.cc b/gnuradio-runtime/lib/logger.cc
index a7e4a25..c11f384 100644
--- a/gnuradio-runtime/lib/logger.cc
+++ b/gnuradio-runtime/lib/logger.cc
@@ -254,6 +254,13 @@ namespace gr {
   }
 
   void
+  logger_set_console_appender(logger_ptr logger, std::string target, 
std::string pattern)
+  {
+    logger->removeAllAppenders();
+    logger_add_console_appender(logger, target, pattern);
+  }
+
+  void
   logger_add_file_appender(logger_ptr logger, std::string filename,
                            bool append, std::string pattern)
   {
@@ -267,6 +274,14 @@ namespace gr {
   }
 
   void
+  logger_set_file_appender(logger_ptr logger, std::string filename,
+                           bool append, std::string pattern)
+  {
+    logger->removeAllAppenders();
+    logger_add_file_appender(logger, filename, append, pattern);
+  }
+
+  void
   logger_add_rollingfile_appender(logger_ptr logger, std::string filename,
                                   size_t filesize, int bkup_index, bool append,
                                   mode_t mode, std::string pattern)
diff --git a/gnuradio-runtime/lib/tpb_thread_body.cc 
b/gnuradio-runtime/lib/tpb_thread_body.cc
index eb47a43..d2f0fce 100644
--- a/gnuradio-runtime/lib/tpb_thread_body.cc
+++ b/gnuradio-runtime/lib/tpb_thread_body.cc
@@ -36,7 +36,7 @@ namespace gr {
     : d_exec(block, max_noutput_items)
   {
     //std::cerr << "tpb_thread_body: " << block << std::endl;
-    
+
     thread::set_thread_name(pthread_self(), boost::str(boost::format("%s%d") % 
block->name() % block->unique_id()));
 
     block_detail *d = block->detail().get();
@@ -61,13 +61,13 @@ namespace gr {
     GR_CONFIG_LOGGER(config_file);
     if(log_file.size() > 0) {
       if(log_file == "stdout") {
-        GR_LOG_ADD_CONSOLE_APPENDER(LOG, "cout","gr::log :%p: %c{1} - %m%n");
+        GR_LOG_SET_CONSOLE_APPENDER(LOG, "cout","gr::log :%p: %c{1} - %m%n");
       }
       else if(log_file == "stderr") {
-        GR_LOG_ADD_CONSOLE_APPENDER(LOG, "cerr","gr::log :%p: %c{1} - %m%n");
+        GR_LOG_SET_CONSOLE_APPENDER(LOG, "cerr","gr::log :%p: %c{1} - %m%n");
       }
       else {
-        GR_LOG_ADD_FILE_APPENDER(LOG, log_file , true,"%r :%p: %c{1} - %m%n");
+        GR_LOG_SET_FILE_APPENDER(LOG, log_file , true,"%r :%p: %c{1} - %m%n");
       }
     }
 #endif /* HAVE_LOG4CPP */
@@ -83,7 +83,7 @@ namespace gr {
     if(block->thread_priority() > 0) {
       gr::thread::set_thread_priority(d->thread, block->thread_priority());
     }
-    
+
     // make sure our block isnt finished
     block->clear_finished();
 
@@ -107,7 +107,7 @@ namespace gr {
           if(block->nmsgs(i.first) > max_nmsgs){
             GR_LOG_WARN(LOG,"asynchronous message buffer overflowing, dropping 
message");
             msg = block->delete_head_nowait(i.first);
-            }
+          }
         }
       }
 
@@ -154,19 +154,19 @@ namespace gr {
           // handle all pending messages
           BOOST_FOREACH(basic_block::msg_queue_map_t::value_type &i, 
block->msg_queue) {
             if(block->has_msg_handler(i.first)) {
-                while((msg = block->delete_head_nowait(i.first))) {
-                  guard.unlock();                      // release lock while 
processing msg
-                  block->dispatch_msg(i.first, msg);
-                  guard.lock();
-                }
+              while((msg = block->delete_head_nowait(i.first))) {
+                guard.unlock();                        // release lock while 
processing msg
+                block->dispatch_msg(i.first, msg);
+                guard.lock();
+              }
             }
             else {
-                // leave msg in queue if no handler is defined
-                // start dropping if we have too many
-                if(block->nmsgs(i.first) > max_nmsgs){
-                    GR_LOG_WARN(LOG,"asynchronous message buffer overflowing, 
dropping message");
-                    msg = block->delete_head_nowait(i.first);
-                }
+              // leave msg in queue if no handler is defined
+              // start dropping if we have too many
+              if(block->nmsgs(i.first) > max_nmsgs){
+                GR_LOG_WARN(LOG,"asynchronous message buffer overflowing, 
dropping message");
+                msg = block->delete_head_nowait(i.first);
+              }
             }
           }
          if (d->done()) {
@@ -197,11 +197,11 @@ namespace gr {
                 // leave msg in queue if no handler is defined
                 // start dropping if we have too many
                 if(block->nmsgs(i.first) > max_nmsgs){
-                    GR_LOG_WARN(LOG,"asynchronous message buffer overflowing, 
dropping message");
-                    msg = block->delete_head_nowait(i.first);
-                    }
+                  GR_LOG_WARN(LOG,"asynchronous message buffer overflowing, 
dropping message");
+                  msg = block->delete_head_nowait(i.first);
                 }
             }
+          }
         }
       }
       break;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]