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