GNU Radio Manual and C++ API Reference  3.7.13.4
The Free & Open Software Radio Ecosystem
logger.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012-2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 /*******************************************************************************
24 * Author: Mark Plett
25 * Description:
26 * The gr::logger module wraps the log4cpp library for logging in gnuradio
27 *******************************************************************************/
28 
29 #ifndef INCLUDED_GR_LOGGER_H
30 #define INCLUDED_GR_LOGGER_H
31 
32 /*!
33 * \ingroup logging
34 * \brief GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
35 *
36 */
37 
38 // handle current status of GR_LOG
39 #ifdef EXT_ENABLE_GR_LOG
40 #undef EXT_ENABLE_GR_LOG
41 #endif
42 #ifdef ENABLE_GR_LOG
43 #define EXT_ENABLE_GR_LOG
44 #undef ENABLE_GR_LOG
45 #endif
46 
47 // did GR enable LOG?
48 #define ENABLE_GR_LOG
49 
50 // if GR does not provide logging and the current module is requesting
51 // it, disable it.
52 #if defined(EXT_ENABLE_GR_LOG) && !defined(ENABLE_GR_LOG)
53 #warning "Logging was requested but is not enabled in GNU Radio, so disabling."
54 #undef EXT_ENABLE_GR_LOG
55 #endif
56 
57 // if GR provides logging but the current module is not requesting it,
58 // disable it.
59 #if !defined(EXT_ENABLE_GR_LOG) && defined(ENABLE_GR_LOG)
60 #undef ENABLE_GR_LOG
61 #endif
62 
63 // the other 2 cases work; no need to check them!
64 
65 // handle current status of LOG4CPP
66 #ifdef EXT_HAVE_LOG4CPP
67 #undef EXT_HAVE_LOG4CPP
68 #endif
69 #ifdef HAVE_LOG4CPP
70 #define EXT_HAVE_LOG4CPP
71 #undef HAVE_LOG4CPP
72 #endif
73 
74 // did GR use log4cpp?
75 #define HAVE_LOG4CPP
76 
77 // if GR does not use log4cpp and the current module is requesting it,
78 // disable it & print a warning.
79 #if defined(EXT_HAVE_LOG4CPP) && !defined(HAVE_LOG4CPP)
80 #warning "Log4Cpp use was requested but was not in GNU Radio, so disabling."
81 #undef EXT_HAVE_LOG4CPP
82 #endif
83 
84 // if GR provides for using log4cpp but the current module is not
85 // requesting it, disable it quietly.
86 #if !defined(EXT_HAVE_LOG4CPP) && defined(HAVE_LOG4CPP)
87 #undef HAVE_LOG4CPP
88 #endif
89 
90 // the other 2 cases work; no need to check them!
91 
92 #ifdef _MSC_VER
93 typedef unsigned short mode_t;
94 #else
95 #include <sys/types.h>
96 #endif
97 
98 #include <gnuradio/api.h>
99 #include <assert.h>
100 #include <iostream>
101 #include <time.h>
102 #include <boost/filesystem.hpp>
103 #include <boost/thread.hpp>
104 #include <boost/format.hpp>
105 #include <pmt/pmt.h>
106 
107 #ifdef ENABLE_GR_LOG
108 
109 // We have three configurations... first logging to stdout/stderr
110 #ifndef HAVE_LOG4CPP
111 
112 namespace gr {
113  //#warning GR logging Enabled and using std::cout
114  typedef void* logger_ptr;
115 } /* namespace gr */
116 
117 #define GR_LOG_DECLARE_LOGPTR(logger)
118 #define GR_LOG_ASSIGN_LOGPTR(logger,name)
119 #define GR_CONFIG_LOGGER(config)
120 #define GR_CONFIG_AND_WATCH_LOGGER(config,period)
121 #define GR_LOG_GETLOGGER(logger, name)
122 #define GR_SET_LEVEL(name, level)
123 #define GR_LOG_SET_LEVEL(logger, level)
124 #define GR_GET_LEVEL(name, level)
125 #define GR_LOG_GET_LEVEL(logger, level)
126 #define GR_ADD_APPENDER(name,appender)
127 #define GR_LOG_ADD_APPENDER(logger,appender)
128 #define GR_SET_APPENDER(name,appender)
129 #define GR_LOG_SET_APPENDER(logger,appender)
130 #define GR_ADD_CONSOLE_APPENDER(logger,target,pattern)
131 #define GR_LOG_ADD_CONSOLE_APPENDER(logger,target,pattern)
132 #define GR_SET_CONSOLE_APPENDER(logger,target,pattern)
133 #define GR_LOG_SET_CONSOLE_APPENDER(logger,target,pattern)
134 #define GR_ADD_FILE_APPENDER(name,filename,append,pattern)
135 #define GR_LOG_ADD_FILE_APPENDER(logger,filename,append,pattern)
136 #define GR_SET_FILE_APPENDER(name,filename,append,pattern)
137 #define GR_LOG_SET_FILE_APPENDER(logger,filename,append,pattern)
138 #define GR_ADD_ROLLINGFILE_APPENDER(name,filename,filesize,bkup_index,append,mode,pattern)
139 #define GR_LOG_ADD_ROLLINGFILE_APPENDER(logger,filename,filesize,bkup_index,append,mode,pattern)
140 #define GR_GET_LOGGER_NAMES(names)
141 #define GR_RESET_CONFIGURATION()
142 #define GR_DEBUG(name, msg) std::cout<<"DEBUG: "<<msg<<std::endl
143 #define GR_INFO(name, msg) std::cout<<"INFO: "<<msg<<std::endl
144 #define GR_NOTICE(name, msg) std::cout<<"NOTICE: "<<msg<<std::endl
145 #define GR_WARN(name, msg) std::cerr<<"WARN: "<<msg<<std::endl
146 #define GR_ERROR(name, msg) std::cerr<<"ERROR: "<<msg<<std::endl
147 #define GR_ALERT(name, msg) std::cerr<<"ERROR: "<<msg<<std::endl
148 #define GR_CRIT(name, msg) std::cerr<<"ERROR: "<<msg<<std::endl
149 #define GR_FATAL(name, msg) std::cerr<<"FATAL: "<<msg<<std::endl
150 #define GR_EMERG(name, msg) std::cerr<<"EMERG: "<<msg<<std::endl
151 #define GR_ERRORIF(name, cond, msg) {if((cond)) std::cerr<<"ERROR: "<<msg<<std::endl;}
152 #define GR_ASSERT(name, cond, msg) {if(!(cond)) std::cerr<<"FATAL: "<<msg<<std::endl; assert(cond);}
153 #define GR_LOG_DEBUG(logger, msg) std::cout<<"DEBUG: "<<msg<<std::endl
154 #define GR_LOG_INFO(logger, msg) std::cout<<"INFO: "<<msg<<std::endl
155 #define GR_LOG_NOTICE(logger, msg) std::cout<<"NOTICE: "<<msg<<std::endl
156 #define GR_LOG_WARN(logger, msg) std::cerr<<"WARN: "<<msg<<std::endl
157 #define GR_LOG_ERROR(logger, msg) std::cerr<<"ERROR: "<<msg<<std::endl
158 #define GR_LOG_ALERT(logger, msg) std::cerr<<"ALERT: "<<msg<<std::endl
159 #define GR_LOG_CRIT(logger, msg) std::cerr<<"CRIT: "<<msg<<std::endl
160 #define GR_LOG_FATAL(logger, msg) std::cerr<<"FATAL: "<<msg<<std::endl
161 #define GR_LOG_EMERG(logger, msg) std::cerr<<"EMERG: "<<msg<<std::endl
162 #define GR_LOG_ERRORIF(logger, cond, msg) { \
163  if((cond)) std::cerr<<"ERROR: "<<msg<<std::endl;}
164 #define GR_LOG_ASSERT(logger, cond, msg) { \
165  if(!(cond)) {std::cerr<<"FATAL: "<<msg<<std::endl; assert(cond);};}
166 
167 
168 #else /* HAVE_LOG4CPP */
169 
170 // Second configuration...logging to log4cpp
171 #include <log4cpp/Category.hh>
172 #include <log4cpp/PropertyConfigurator.hh>
173 #include <log4cpp/FileAppender.hh>
174 #include <log4cpp/RollingFileAppender.hh>
175 #include <log4cpp/OstreamAppender.hh>
176 #include <log4cpp/PatternLayout.hh>
177 
178 namespace gr {
179 
180  /*!
181  * \brief GR_LOG macros
182  * \ingroup logging
183  *
184  * These macros wrap the standard LOG4CPP_LEVEL macros. The availablie macros
185  * are:
186  * LOG_DEBUG
187  * LOG_INFO
188  * LOG_WARN
189  * LOG_TRACE
190  * LOG_ERROR
191  * LOG_ALERT
192  * LOG_CRIT
193  * LOG_FATAL
194  * LOG_EMERG
195  */
196  typedef log4cpp::Category* logger_ptr;
197 
198 } /* namespace gr */
199 
200 
201  /* Macros for Programmatic Configuration */
202 #define GR_LOG_DECLARE_LOGPTR(logger) \
203  gr::logger_ptr logger;
204 
205 #define GR_LOG_ASSIGN_LOGPTR(logger,name) \
206  logger = gr::logger_get_logger(name);
207 
208 #define GR_CONFIG_LOGGER(config) \
209  gr::logger_config::load_config(config)
210 
211 #define GR_CONFIG_AND_WATCH_LOGGER(config,period) \
212  gr::logger_config::load_config(config,period)
213 
214 #define GR_LOG_GETLOGGER(logger, name) \
215  gr::logger_ptr logger = gr::logger_get_logger(name);
216 
217 #define GR_SET_LEVEL(name, level) { \
218  gr::logger_ptr logger = gr::logger_get_logger(name); \
219  gr::logger_set_level(logger,level);}
220 
221 #define GR_LOG_SET_LEVEL(logger, level) \
222  gr::logger_set_level(logger, level);
223 
224 #define GR_GET_LEVEL(name, level) { \
225  gr::logger_ptr logger = gr::logger_get_logger(name); \
226  gr::logger_get_level(logger,level);}
227 
228 #define GR_LOG_GET_LEVEL(logger, level) \
229  gr::logger_get_level(logger,level);
230 
231 #define GR_ADD_APPENDER(name, appender) { \
232  gr::logger_ptr logger = gr::logger_get_logger(name); \
233  gr::logger_add_appender(logger,appender);}
234 
235 #define GR_LOG_ADD_APPENDER(logger, appender) { \
236  gr::logger_add_appender(logger, appender);}
237 
238 #define GR_SET_APPENDER(name, appender) { \
239  gr::logger_ptr logger = gr::logger_get_logger(name); \
240  gr::logger_set_appender(logger,appender);}
241 
242 #define GR_LOG_SET_APPENDER(logger, appender) { \
243  gr::logger_set_appender(logger, appender);}
244 
245 #define GR_ADD_CONSOLE_APPENDER(name, target, pattern) { \
246  gr::logger_ptr logger = gr::logger_get_logger(name); \
247  gr::logger_add_console_appender(logger,target,pattern);}
248 
249 #define GR_LOG_ADD_CONSOLE_APPENDER(logger, target, pattern) { \
250  gr::logger_add_console_appender(logger,target,pattern);}
251 
252 #define GR_SET_CONSOLE_APPENDER(name, target, pattern) { \
253  gr::logger_ptr logger = gr::logger_get_logger(name); \
254  gr::logger_set_console_appender(logger,target,pattern);}
255 
256 #define GR_LOG_SET_CONSOLE_APPENDER(logger, target, pattern) { \
257  gr::logger_set_console_appender(logger,target,pattern);}
258 
259 #define GR_ADD_FILE_APPENDER(name, filename, append, pattern) { \
260  gr::logger_ptr logger = gr::logger_get_logger(name); \
261  gr::logger_add_file_appender(logger,filename,append,pattern);}
262 
263 #define GR_LOG_ADD_FILE_APPENDER(logger, filename, append, pattern) { \
264  gr::logger_add_file_appender(logger,filename,append,pattern);}
265 
266 #define GR_SET_FILE_APPENDER(name, filename, append, pattern) { \
267  gr::logger_ptr logger = gr::logger_get_logger(name); \
268  gr::logger_set_file_appender(logger,filename,append,pattern);}
269 
270 #define GR_LOG_SET_FILE_APPENDER(logger, filename, append, pattern) { \
271  gr::logger_set_file_appender(logger,filename,append,pattern);}
272 
273 #define GR_ADD_ROLLINGFILE_APPENDER(name, filename, filesize, bkup_index, append, mode, pattern) { \
274  gr::logger_ptr logger = gr::logger_get_logger(name); \
275  gr::logger_add_rollingfile_appender(logger,filename,filesize,bkup_index,append,mode,pattern);}
276 
277 #define GR_LOG_ADD_ROLLINGFILE_APPENDER(logger, filename, filesize, bkup_index, append, mode, pattern) { \
278  gr::logger_add_rollingfile_appender(logger,filename,filesize,bkup_index,append,mode,pattern);}
279 
280 #define GR_GET_LOGGER_NAMES(names) { \
281  names = gr::logger_get_logger_names();}
282 
283 #define GR_RESET_CONFIGURATION() \
284  gr::logger_config::reset_config();
285 
286  /* Logger name referenced macros */
287 #define GR_DEBUG(name, msg) { \
288  gr::logger_ptr logger = gr::logger_get_logger(name); \
289  *logger<< log4cpp::Priority::DEBUG << msg << log4cpp::eol;}
290 
291 #define GR_INFO(name, msg) { \
292  gr::logger_ptr logger = gr::logger_get_logger(name); \
293  *logger<< log4cpp::Priority::INFO << msg << log4cpp::eol;}
294 
295 #define GR_NOTICE(name, msg) { \
296  gr::logger_ptr logger = gr::logger_get_logger(name); \
297  *logger << log4cpp::Priority::NOTICE << msg;}
298 
299 #define GR_WARN(name, msg) { \
300  gr::logger_ptr logger = gr::logger_get_logger(name); \
301  *logger<< log4cpp::Priority::WARN << msg << log4cpp::eol;}
302 
303 #define GR_ERROR(name, msg) { \
304  gr::logger_ptr logger = gr::logger_get_logger(name); \
305  *logger<< log4cpp::Priority::ERROR << msg << log4cpp::eol;}
306 
307 #define GR_CRIT(name, msg) { \
308  gr::logger_ptr logger = gr::logger_get_logger(name); \
309  *logger<< log4cpp::Priority::CRIT << msg << log4cpp::eol;}
310 
311 #define GR_ALERT(name, msg) { \
312  gr::logger_ptr logger = gr::logger_get_logger(name); \
313  *logger<< log4cpp::Priority::ALERT << msg << log4cpp::eol;}
314 
315 #define GR_FATAL(name, msg) { \
316  gr::logger_ptr logger = gr::logger_get_logger(name); \
317  *logger<< log4cpp::Priority::FATAL << msg << log4cpp::eol;}
318 
319 #define GR_EMERG(name, msg) { \
320  gr::logger_ptr logger = gr::logger_get_logger(name); \
321  *logger<< log4cpp::Priority::EMERG << msg << log4cpp::eol;}
322 
323 #define GR_ERRORIF(name, cond, msg) { \
324  if((cond)) { \
325  gr::logger_ptr logger = gr::logger_get_logger(name); \
326  *logger<< log4cpp::Priority::ERROR << msg << log4cpp::eol;} \
327  }
328 
329 #define GR_ASSERT(name, cond, msg) { \
330  if(!(cond)) { \
331  gr::logger_ptr logger = gr::logger_get_logger(name); \
332  *logger<< log4cpp::Priority::EMERG << msg << log4cpp::eol;} \
333  assert(0); \
334  }
335 
336  /* LoggerPtr Referenced Macros */
337 #define GR_LOG_DEBUG(logger, msg) { \
338  *logger << log4cpp::Priority::DEBUG << msg << log4cpp::eol;}
339 
340 #define GR_LOG_INFO(logger, msg) { \
341  *logger << log4cpp::Priority::INFO << msg << log4cpp::eol;}
342 
343 #define GR_LOG_NOTICE(logger, msg) { \
344  *logger << log4cpp::Priority::NOTICE << msg << log4cpp::eol;}
345 
346 #define GR_LOG_WARN(logger, msg) { \
347  *logger << log4cpp::Priority::WARN << msg << log4cpp::eol;}
348 
349 #define GR_LOG_ERROR(logger, msg) { \
350  *logger << log4cpp::Priority::ERROR << msg << log4cpp::eol;}
351 
352 #define GR_LOG_CRIT(logger, msg) { \
353  *logger << log4cpp::Priority::CRIT << msg << log4cpp::eol;}
354 
355 #define GR_LOG_ALERT(logger, msg) { \
356  *logger << log4cpp::Priority::ALERT << msg << log4cpp::eol;}
357 
358 #define GR_LOG_FATAL(logger, msg) { \
359  *logger << log4cpp::Priority::FATAL << msg << log4cpp::eol;}
360 
361 #define GR_LOG_EMERG(logger, msg) { \
362  *logger << log4cpp::Priority::EMERG << msg << log4cpp::eol;}
363 
364 #define GR_LOG_ERRORIF(logger,cond, msg) { \
365  if((cond)) { \
366  *logger<< log4cpp::Priority::ERROR << msg << log4cpp::eol;} \
367  }
368 
369 #define GR_LOG_ASSERT(logger, cond, msg) { \
370  if(!(cond)) { \
371  *logger<< log4cpp::Priority::EMERG << msg << log4cpp::eol; \
372  assert(0);} \
373  }
374 
375 namespace gr {
376 
377  /*!
378  * \brief Class to control configuration of logger.
379  * This is a singleton that can launch a thread to watch a config file for changes
380  * \ingroup logging
381  */
382  class GR_RUNTIME_API logger_config
383  {
384  private:
385  /*! \brief filename of logger config file */
386  std::string filename;
387  /*! \brief Period (seconds) over which watcher thread checks config file for changes */
388  unsigned int watch_period;
389  /*! \brief Pointer to watch thread for config file changes */
390  boost::thread *watch_thread;
391 
392  /*! \brief Watcher thread method
393  * /param filename Name of configuration file
394  * /param watch_period Seconds between checks for changes in config file
395  */
396  static void watch_file(std::string filename,unsigned int watch_period);
397 
398  static bool logger_configured;
399 
400  logger_config()/*:
401  rpc_get_filename("logger_config", "filename", &logger_config::get_filename4rpc,
402  pmt::mp(""), pmt::mp(""), pmt::mp(""),
403  "", "filename", RPC_PRIVLVL_MIN,
404  DISPTIME | DISPOPTSTRIP),
405  rpc_get_watchperiod("logger_config", "watch_period", &logger_config::get_watchperiod4rpc,
406  pmt::mp(0), pmt::mp(32768), pmt::mp(0),
407  "", "watch_period", RPC_PRIVLVL_MIN,
408  DISPTIME | DISPOPTSTRIP),
409  rpc_get_config("logger_config", "config", &logger_config::get_config4rpc,
410  pmt::mp(""), pmt::mp(""), pmt::mp(""),
411  "", "filename", RPC_PRIVLVL_MIN,
412  DISPTIME | DISPOPTSTRIP),
413  rpc_set_config("logger_config","config", &logger_config::set_config4rpc,
414  pmt::mp(""), pmt::mp(""), pmt::mp(""),
415  "", "filename", RPC_PRIVLVL_MIN,
416  DISPTIME | DISPOPTSTRIP)
417  */
418  {
419  } //!< Constructor
420 
421  /*
422  rpcbasic_register_get<logger_config,std::string> rpc_get_filename;
423  rpcbasic_register_get<logger_config,int> rpc_get_watchperiod;
424  rpcbasic_register_get<logger_config,std::string> rpc_get_config;
425  rpcbasic_register_set<logger_config,std::string> rpc_set_config;
426  */
427 
428  logger_config(logger_config const&); //!<Copy constructor
429  void operator=(logger_config const&); //!<Assignment Operator
430 
431  std::string get_filename4rpc() {
432  return filename;
433  }
434  int get_watchperiod4rpc(){return watch_period;};
435 
436  std::string get_config4rpc() {
437  return filename;
438  }
439 
440  void set_config4rpc(std::string set) {
441  printf("Set string was:%s\n", set.c_str());
442  }
443 
444  /*! \brief destrcutor stops watch thread before exits */
445  ~logger_config() {
446  stop_watch();
447  }
448 
449  /*! \brief Instance getter for singleton. Only used by class. */
450  static logger_config& get_instance(void);
451 
452  public:
453  /*! \brief Getter for config filename */
454  static std::string get_filename();
455  /*! \brief Getter for watch period */
456  static unsigned int get_watch_period();
457  /*! \brief Method to load configuration
458  * /param filename Name of configuration file
459  * /param watch_period Seconds between checks for changes in config file
460  */
461  static void load_config(std::string filename,unsigned int watch_period=0);
462  /*! \brief Method to stop watcher thread */
463  static void stop_watch();
464  /*! \brief method to reset logger configuration */
465  static void reset_config(void);
466  };
467 
468  /*!
469  * \brief Retrieve a pointer to a logger by name
470  *
471  * Retrieves a logger pointer
472  * \p name.
473  *
474  * \param name Name of the logger for which a pointer is requested
475  */
476  GR_RUNTIME_API logger_ptr logger_get_logger(std::string name);
477 
478  /*!
479  * \brief Load logger's configuration file.
480  *
481  * Initialize the GNU Radio logger by loading the configuration file
482  * \p config_filename.
483  *
484  * \param config_filename The configuration file. Set to "" for the
485  * basic logger that outputs to the console.
486  */
487  GR_RUNTIME_API bool logger_load_config(const std::string &config_filename="");
488 
489  /*!
490  * \brief Reset logger's configuration file.
491  *
492  * Remove all appenders from loggers
493  */
494  GR_RUNTIME_API void logger_reset_config(void);
495 
496  /*!
497  * \brief Set the logger's output level.
498  *
499  * Sets the level of the logger. This takes a string that is
500  * translated to the standard levels and can be (case insensitive):
501  *
502  * \li off , notset
503  * \li debug
504  * \li info
505  * \li notice
506  * \li warn
507  * \li error
508  * \li crit
509  * \li alert
510  * \li fatal
511  * \li emerg
512  *
513  * \param logger the logger to set the level of.
514  * \param level string to set the level to.
515  */
516  GR_RUNTIME_API void logger_set_level(logger_ptr logger,
517  const std::string &level);
518 
519  /*!
520  * \brief Set the logger's output level.
521  *
522  * Sets the level of the logger. This takes the actual Log4cpp::Priority
523  * data type, which can be:
524  *
525  * \li log4cpp::Priority::NOTSET
526  * \li log4cpp::Priority::DEBUG
527  * \li log4cpp::Priority::INFO
528  * \li log4cpp::Priority::NOTICE
529  * \li log4cpp::Priority::WARN
530  * \li log4cpp::Priority::ERROR
531  * \li log4cpp::Priority::CRIT
532  * \li log4cpp::Priority::ALERT
533  * \li log4cpp::Priority::FATAL
534  * \li log4cpp::Priority::EMERG
535  *
536  * \param logger the logger to set the level of.
537  * \param level new logger level of type Log4cpp::Priority
538  */
539  GR_RUNTIME_API void logger_set_level(logger_ptr logger,
540  log4cpp::Priority::Value level);
541 
542  /*!
543  * \brief Get the logger's output level.
544  *
545  * Gets the level of the logger. This returns a string that
546  * corresponds to the standard levels and can be (case insensitive):
547  *
548  * \li notset
549  * \li debug
550  * \li info
551  * \li notice
552  * \li warn
553  * \li error
554  * \li crit
555  * \li alert
556  * \li fatal
557  * \li emerg
558  *
559  * \param logger the logger to get the level of.
560  * \param level string to get the level into.
561  */
562  GR_RUNTIME_API void logger_get_level(logger_ptr logger, std::string &level);
563 
564  /*!
565  * \brief Get the logger's output level.
566  *
567  * Gets the level of the logger. This returns the actual Log4cpp::Level
568  * data type, which can be:
569  *
570  * \li log4cpp::Priority::NOTSET
571  * \li log4cpp::Priority::DEBUG
572  * \li log4cpp::Priority::INFO
573  * \li log4cpp::Priority::NOTICE
574  * \li log4cpp::Priority::WARN
575  * \li log4cpp::Priority::ERROR
576  * \li log4cpp::Priority::CRIT
577  * \li log4cpp::Priority::ALERT
578  * \li log4cpp::Priority::FATAL
579  * \li log4cpp::Priority::EMERG
580  *
581  * \param logger the logger to get the level of.
582  * \param level of the logger.
583  */
584  GR_RUNTIME_API void logger_get_level(logger_ptr logger,
585  log4cpp::Priority::Value &level);
586 
587  /*!
588  * \brief Add console appender to a given logger
589  *
590  * Add console appender to a given logger
591  *
592  * \param logger Logger to which appender will be added
593  * \param appender Name of appender to add to logger
594  */
595  GR_RUNTIME_API void logger_add_appender(logger_ptr logger,
596  std::string appender);
597 
598  /*!
599  * \brief Sets a console appender to a given logger. Deletes any
600  * existing appenders and adds a new one. To add an additional
601  * appender, use logger_add_appender.
602  *
603  * \param logger Logger to which appender will be added
604  * \param appender Name of appender to add to logger
605  */
606  GR_RUNTIME_API void logger_set_appender(logger_ptr logger,
607  std::string appender);
608 
609  /*!
610  * \brief Add console appender to a given logger
611  *
612  * Add console appender to a given logger
613  *
614  * \param logger Logger to which appender will be added
615  * \param target Std target to write 'cout' or 'cerr' (default is cout)
616  * \param pattern Formatting pattern for log messages
617  */
618  GR_RUNTIME_API void logger_add_console_appender(logger_ptr logger,
619  std::string target,
620  std::string pattern);
621 
622  /*!
623  * \brief Sets a new console appender to a given logger after
624  * removing all others. Use logger_add_console_appender to add
625  * another.
626  *
627  * \param logger Logger to which appender will be added
628  * \param target Std target to write 'cout' or 'cerr' (default is cout)
629  * \param pattern Formatting pattern for log messages
630  */
631  GR_RUNTIME_API void logger_set_console_appender(logger_ptr logger,
632  std::string target,
633  std::string pattern);
634 
635  /*!
636  * \brief Add file appender to a given logger
637  *
638  * Add file appender to a given logger
639  *
640  * \param logger Logger to which appender will be added
641  * \param filename File to which log will be written
642  * \param append Overwrite or append to log file
643  * \param pattern Formatting pattern for log messages
644  */
645  GR_RUNTIME_API void logger_add_file_appender(logger_ptr logger,
646  std::string filename,
647  bool append, std::string pattern);
648 
649  /*!
650  * \brief Set a file appender to a given logger. To add another file
651  * appender, use logger_add_file_appender.
652  *
653  * \param logger Logger to which appender will be added
654  * \param filename File to which log will be written
655  * \param append Overwrite or append to log file
656  * \param pattern Formatting pattern for log messages
657  */
658  GR_RUNTIME_API void logger_set_file_appender(logger_ptr logger,
659  std::string filename,
660  bool append, std::string pattern);
661 
662  /*!
663  * \brief Add rolling file appender to a given logger
664  *
665  * Add rolling file appender to a given logger
666  *
667  * \param logger Logger to which appender will be added
668  * \param filename File to which log will be written
669  * \param filesize Sizez of files to write
670  * \param bkup_index Number of files to write
671  * \param append Overwrite or append to log file
672  * \param mode Permissions to set on log file
673  * \param pattern Formatting pattern for log messages
674  */
675  GR_RUNTIME_API void logger_add_rollingfile_appender(logger_ptr logger, std::string filename,
676  size_t filesize, int bkup_index, bool append,
677  mode_t mode,std::string pattern);
678 
679  /*!
680  * \brief Add rolling file appender to a given logger
681  *
682  * Add rolling file appender to a given logger
683  *
684  * \return vector of string names of loggers
685  */
686  GR_RUNTIME_API std::vector<std::string> logger_get_logger_names(void);
687 
688 } /* namespace gr */
689 
690 #endif /* HAVE_LOG4CPP */
691 
692  // If Logger disable do nothing
693 #else /* ENABLE_GR_LOG */
694 
695 namespace gr {
696  typedef void* logger_ptr;
697 } /* namespace gr */
698 
699 #define GR_LOG_DECLARE_LOGPTR(logger)
700 #define GR_LOG_ASSIGN_LOGPTR(logger,name)
701 #define GR_CONFIG_LOGGER(config)
702 #define GR_CONFIG_AND_WATCH_LOGGER(config,period)
703 #define GR_LOG_GETLOGGER(logger, name)
704 #define GR_SET_LEVEL(name, level)
705 #define GR_LOG_SET_LEVEL(logger, level)
706 #define GR_GET_LEVEL(name, level)
707 #define GR_LOG_GET_LEVEL(logger, level)
708 #define GR_ADD_APPENDER(name,appender)
709 #define GR_LOG_ADD_APPENDER(logger,appender)
710 #define GR_SET_APPENDER(name,appender)
711 #define GR_LOG_SET_APPENDER(logger,appender)
712 #define GR_ADD_CONSOLE_APPENDER(logger,target,pattern)
713 #define GR_LOG_ADD_CONSOLE_APPENDER(logger,target,pattern)
714 #define GR_SET_CONSOLE_APPENDER(logger,target,pattern)
715 #define GR_LOG_SET_CONSOLE_APPENDER(logger,target,pattern)
716 #define GR_ADD_FILE_APPENDER(name,filename,append,pattern)
717 #define GR_LOG_ADD_FILE_APPENDER(logger,filename,append,pattern)
718 #define GR_SET_FILE_APPENDER(name,filename,append,pattern)
719 #define GR_LOG_SET_FILE_APPENDER(logger,filename,append,pattern)
720 #define GR_ADD_ROLLINGFILE_APPENDER(name,filename,filesize,bkup_index,append,mode,pattern)
721 #define GR_LOG_ADD_ROLLINGFILE_APPENDER(logger,filename,filesize,bkup_index,append,mode,pattern)
722 #define GR_GET_LOGGER_NAMES(names)
723 #define GR_RESET_CONFIGURATION()
724 #define GR_DEBUG(name, msg)
725 #define GR_INFO(name, msg)
726 #define GR_NOTICE(name, msg)
727 #define GR_WARN(name, msg)
728 #define GR_ERROR(name, msg)
729 #define GR_ALERT(name, msg)
730 #define GR_CRIT(name, msg)
731 #define GR_FATAL(name, msg)
732 #define GR_EMERG(name, msg)
733 #define GR_ERRORIF(name, cond, msg)
734 #define GR_ASSERT(name, cond, msg)
735 #define GR_LOG_DEBUG(logger, msg)
736 #define GR_LOG_INFO(logger, msg)
737 #define GR_LOG_NOTICE(logger, msg)
738 #define GR_LOG_WARN(logger, msg)
739 #define GR_LOG_ERROR(logger, msg)
740 #define GR_LOG_ALERT(logger, msg)
741 #define GR_LOG_CRIT(logger, msg)
742 #define GR_LOG_FATAL(logger, msg)
743 #define GR_LOG_EMERG(logger, msg)
744 #define GR_LOG_ERRORIF(logger, cond, msg)
745 #define GR_LOG_ASSERT(logger, cond, msg)
746 
747 #endif /* ENABLE_GR_LOG */
748 
749 namespace gr {
750 
751  // Even if logger is disabled we'll need for methods below to exist in python.
752  // The macros these call will be disabled if ENABLE_GR_LOG is undefined
753 
754  /********************* Start Classes and Methods for Python ******************/
755  /*!
756  * \brief Logger class for referencing loggers in python. Not
757  * needed in C++ (use macros) Wraps and manipulates loggers for
758  * python as python has no macros
759  * \ingroup logging
760  *
761  */
763  {
764  private:
765  /*! \brief logger pointer to logger associated wiith this wrapper class */
766  logger_ptr d_logger;
767  public:
768  /*!
769  * \brief constructor Provide name of logger to associate with this class
770  * \param logger_name Name of logger associated with class
771  */
772  logger(std::string logger_name) {
773  GR_LOG_ASSIGN_LOGPTR(d_logger,logger_name);
774  };
775 
776  /*! \brief Destructor */
777  ~logger(){;}
778 
779  // Wrappers for logging macros
780  /*! \brief inline function, wrapper to set the logger level */
781  void set_level(std::string level){GR_LOG_SET_LEVEL(d_logger,level);}
782 
783  /*! \brief inline function, wrapper to get the logger level */
784  void get_level(std::string &level){GR_LOG_GET_LEVEL(d_logger,level);}
785 
786  /*! \brief inline function, wrapper for LOG4CPP_DEBUG for DEBUG message */
787  void debug(std::string msg){GR_LOG_DEBUG(d_logger,msg);};
788 
789  /*! \brief inline function, wrapper for LOG4CPP_INFO for INFO message */
790  void info(std::string msg){GR_LOG_INFO(d_logger,msg);}
791 
792  /*! \brief inline function, wrapper for NOTICE message */
793  void notice(std::string msg){GR_LOG_NOTICE(d_logger,msg);}
794 
795  /*! \brief inline function, wrapper for LOG4CPP_WARN for WARN message */
796  void warn(std::string msg){GR_LOG_WARN(d_logger,msg);}
797 
798  /*! \brief inline function, wrapper for LOG4CPP_ERROR for ERROR message */
799  void error(std::string msg){GR_LOG_ERROR(d_logger,msg);}
800 
801  /*! \brief inline function, wrapper for NOTICE message */
802  void crit(std::string msg){GR_LOG_CRIT(d_logger,msg);}
803 
804  /*! \brief inline function, wrapper for ALERT message */
805  void alert(std::string msg){GR_LOG_ALERT(d_logger,msg);}
806 
807  /*! \brief inline function, wrapper for FATAL message */
808  void fatal(std::string msg){GR_LOG_FATAL(d_logger,msg);}
809 
810  /*! \brief inline function, wrapper for EMERG message */
811  void emerg(std::string msg){GR_LOG_EMERG(d_logger,msg);}
812 
813  /*! \brief inline function, wrapper for LOG4CPP_ASSERT for conditional ERROR message */
814  void errorIF(bool cond,std::string msg){GR_LOG_ERRORIF(d_logger,cond,msg);}
815 
816  /*! \brief inline function, wrapper for LOG4CPP_ASSERT for conditional ERROR message */
817  void log_assert(bool cond,std::string msg){GR_LOG_ASSERT(d_logger,cond,msg);}
818 
819  /*! \brief inline function, Method to add appender to logger by
820  name (define appender in conf file) */
821  void add_appender(std::string appender) {
822  GR_LOG_ADD_APPENDER(d_logger, appender);
823  }
824 
825  /*! \brief inline function, Method to set an appender to logger by
826  name (define appender in conf file) */
827  void set_appender(std::string appender) {
828  GR_LOG_SET_APPENDER(d_logger, appender);
829  }
830 
831  /*! \brief inline function, Method to add console appender to logger */
832  void add_console_appender(std::string target,std::string pattern) {
833  GR_LOG_ADD_CONSOLE_APPENDER(d_logger, target, pattern);
834  }
835 
836  /*! \brief inline function, Method to set a console appender to logger */
837  void set_console_appender(std::string target,std::string pattern) {
838  GR_LOG_SET_CONSOLE_APPENDER(d_logger, target, pattern);
839  }
840 
841  /*! \brief inline function, Method to add file appender to logger */
842  void add_file_appender(std::string filename, bool append, std::string pattern) {
843  GR_LOG_ADD_FILE_APPENDER(d_logger, filename, append, pattern);
844  }
845 
846  /*! \brief inline function, Method to set a file appender to logger */
847  void set_file_appender(std::string filename, bool append, std::string pattern) {
848  GR_LOG_SET_FILE_APPENDER(d_logger, filename, append, pattern);
849  }
850 
851  /*! \brief inline function, Method to add rolling file appender to logger */
852  void add_rollingfile_appender(std::string filename, size_t filesize,
853  int bkup_index, bool append, mode_t mode,
854  std::string pattern) {
855  GR_LOG_ADD_ROLLINGFILE_APPENDER(d_logger,filename,filesize,
856  bkup_index,append,mode,pattern);
857  }
858  };
859 
860 } /* namespace gr */
861 
862 /**************** Start Configuration Class and Methods for Python ************/
863 /*!
864  * \brief Function to call configuration macro from python.
865  * Note: Configuration is only updated if filename or watch_period has changed.
866  * \param config_filename Name of configuration file
867  * \param watch_period Seconds to wait between checking for changes in conf file.
868  * Watch_period defaults to 0 in which case the file is not watched for changes
869  */
870 GR_RUNTIME_API void gr_logger_config(const std::string config_filename,
871  unsigned int watch_period = 0);
872 
873 /*!
874  * \brief Function to return logger names to python
875  * \return Vector of name strings
876  *
877  */
878 GR_RUNTIME_API std::vector<std::string> gr_logger_get_logger_names(void);
879 
880 /*!
881  * \brief Function to reset logger configuration from python
882  *
883  */
885 
886 
887 namespace gr {
888  /*!
889  * Function to use the GR prefs files to get and setup the two
890  * default loggers defined there. The loggers are unique to the
891  * class in which they are called, and we pass it the \p name to
892  * identify where the log message originates from. For a GNU Radio
893  * block, we use 'alias()' for this value, and this is set up for us
894  * automatically in gr::block.
895  */
897  const std::string name);
898 
899  GR_RUNTIME_API bool update_logger_alias(const std::string &name, const std::string &alias);
900 
901 } /* namespace gr */
902 
903 
904 #endif /* INCLUDED_GR_LOGGER_H */
#define GR_LOG_SET_FILE_APPENDER(logger, filename, append, pattern)
Definition: logger.h:719
void add_rollingfile_appender(std::string filename, size_t filesize, int bkup_index, bool append, mode_t mode, std::string pattern)
inline function, Method to add rolling file appender to logger
Definition: logger.h:852
void set_level(std::string level)
inline function, wrapper to set the logger level
Definition: logger.h:781
#define GR_LOG_NOTICE(logger, msg)
Definition: logger.h:737
#define GR_LOG_ERROR(logger, msg)
Definition: logger.h:739
#define GR_LOG_CRIT(logger, msg)
Definition: logger.h:741
GR_RUNTIME_API void gr_logger_reset_config(void)
Function to reset logger configuration from python.
void error(std::string msg)
inline function, wrapper for LOG4CPP_ERROR for ERROR message
Definition: logger.h:799
GR_RUNTIME_API std::vector< std::string > gr_logger_get_logger_names(void)
Function to return logger names to python.
GR_RUNTIME_API bool update_logger_alias(const std::string &name, const std::string &alias)
#define GR_LOG_WARN(logger, msg)
Definition: logger.h:738
#define GR_LOG_SET_LEVEL(logger, level)
Definition: logger.h:705
void set_appender(std::string appender)
inline function, Method to set an appender to logger by name (define appender in conf file) ...
Definition: logger.h:827
#define GR_LOG_ADD_ROLLINGFILE_APPENDER(logger, filename, filesize, bkup_index, append, mode, pattern)
Definition: logger.h:721
void fatal(std::string msg)
inline function, wrapper for FATAL message
Definition: logger.h:808
#define GR_LOG_SET_CONSOLE_APPENDER(logger, target, pattern)
Definition: logger.h:715
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
logger(std::string logger_name)
constructor Provide name of logger to associate with this class
Definition: logger.h:772
void alert(std::string msg)
inline function, wrapper for ALERT message
Definition: logger.h:805
void warn(std::string msg)
inline function, wrapper for LOG4CPP_WARN for WARN message
Definition: logger.h:796
void add_file_appender(std::string filename, bool append, std::string pattern)
inline function, Method to add file appender to logger
Definition: logger.h:842
void get_level(std::string &level)
inline function, wrapper to get the logger level
Definition: logger.h:784
#define GR_LOG_DEBUG(logger, msg)
Definition: logger.h:735
void emerg(std::string msg)
inline function, wrapper for EMERG message
Definition: logger.h:811
#define GR_LOG_EMERG(logger, msg)
Definition: logger.h:743
boost::thread thread
Definition: thread.h:47
#define GR_LOG_ADD_FILE_APPENDER(logger, filename, append, pattern)
Definition: logger.h:717
void info(std::string msg)
inline function, wrapper for LOG4CPP_INFO for INFO message
Definition: logger.h:790
Include this header to use the message passing features.
Definition: logger.h:695
void * logger_ptr
Definition: logger.h:696
void add_appender(std::string appender)
inline function, Method to add appender to logger by name (define appender in conf file) ...
Definition: logger.h:821
void log_assert(bool cond, std::string msg)
inline function, wrapper for LOG4CPP_ASSERT for conditional ERROR message
Definition: logger.h:817
#define GR_LOG_GET_LEVEL(logger, level)
Definition: logger.h:707
void add_console_appender(std::string target, std::string pattern)
inline function, Method to add console appender to logger
Definition: logger.h:832
~logger()
Destructor.
Definition: logger.h:777
void debug(std::string msg)
inline function, wrapper for LOG4CPP_DEBUG for DEBUG message
Definition: logger.h:787
void crit(std::string msg)
inline function, wrapper for NOTICE message
Definition: logger.h:802
GR_RUNTIME_API bool configure_default_loggers(gr::logger_ptr &l, gr::logger_ptr &d, const std::string name)
#define GR_LOG_ALERT(logger, msg)
Definition: logger.h:740
#define GR_LOG_ADD_CONSOLE_APPENDER(logger, target, pattern)
Definition: logger.h:713
void errorIF(bool cond, std::string msg)
inline function, wrapper for LOG4CPP_ASSERT for conditional ERROR message
Definition: logger.h:814
#define GR_LOG_SET_APPENDER(logger, appender)
Definition: logger.h:711
void set_file_appender(std::string filename, bool append, std::string pattern)
inline function, Method to set a file appender to logger
Definition: logger.h:847
#define GR_LOG_ASSERT(logger, cond, msg)
Definition: logger.h:745
#define GR_LOG_ADD_APPENDER(logger, appender)
Definition: logger.h:709
GR_RUNTIME_API void gr_logger_config(const std::string config_filename, unsigned int watch_period=0)
Function to call configuration macro from python. Note: Configuration is only updated if filename or ...
Logger class for referencing loggers in python. Not needed in C++ (use macros) Wraps and manipulates ...
Definition: logger.h:762
void set_console_appender(std::string target, std::string pattern)
inline function, Method to set a console appender to logger
Definition: logger.h:837
void notice(std::string msg)
inline function, wrapper for NOTICE message
Definition: logger.h:793
#define GR_LOG_FATAL(logger, msg)
Definition: logger.h:742
#define GR_LOG_ASSIGN_LOGPTR(logger, name)
Definition: logger.h:700
#define GR_LOG_INFO(logger, msg)
Definition: logger.h:736
#define GR_LOG_ERRORIF(logger, cond, msg)
Definition: logger.h:744