27#ifndef NIDAS_UTIL_LOGGER_H_
28#define NIDAS_UTIL_LOGGER_H_
44#ifndef NIDAS_LOGGER_THREADLOCAL
45#define NIDAS_LOGGER_THREADLOCAL thread_local
53#ifndef NIDAS_LOGGER_GUARD_ACTIVE
54#define NIDAS_LOGGER_GUARD_ACTIVE 0
57namespace nidas {
namespace util {
200#define LOG_CONTEXT(LEVEL) \
201 nidas::util::LEVEL, __FILE__, __PRETTY_FUNCTION__, __LINE__
209#define LOG_STATIC_CONTEXT(LEVEL) \
210 nidas::util::LEVEL, __FILE__, "file_static_scope", __LINE__
213#define LOG_EMERG LOG_CONTEXT(LOGGER_EMERG)
214#define LOG_ALERT LOG_CONTEXT(LOGGER_ALERT)
215#define LOG_CRIT LOG_CONTEXT(LOGGER_CRIT)
216#define LOG_ERR LOG_CONTEXT(LOGGER_ERR)
217#define LOG_WARNING LOG_CONTEXT(LOGGER_WARNING)
218#define LOG_NOTICE LOG_CONTEXT(LOGGER_NOTICE)
219#define LOG_INFO LOG_CONTEXT(LOGGER_INFO)
220#define LOG_DEBUG LOG_CONTEXT(LOGGER_DEBUG)
221#define LOG_VERBOSE LOG_CONTEXT(LOGGER_VERBOSE)
257#define LOGGER_LOGPOINT(LEVEL,TAGS,MSG) \
259 nidas::util::Logger::init(); \
260 static NIDAS_LOGGER_THREADLOCAL nidas::util::LogContext logctxt \
261 (nidas::util::LEVEL, __FILE__,__PRETTY_FUNCTION__, \
263 if (logctxt.active()) \
264 logctxt.log(nidas::util::LogMessage().format MSG); } \
309#define ELOG(MSG) LOGGER_LOGPOINT(LOGGER_EMERG,"",MSG)
310#define ALOG(MSG) LOGGER_LOGPOINT(LOGGER_ALERT,"",MSG)
311#define CLOG(MSG) LOGGER_LOGPOINT(LOGGER_CRITICAL,"",MSG)
312#define PLOG(MSG) LOGGER_LOGPOINT(LOGGER_ERR,"",MSG)
313#define WLOG(MSG) LOGGER_LOGPOINT(LOGGER_WARNING,"",MSG)
314#define NLOG(MSG) LOGGER_LOGPOINT(LOGGER_NOTICE,"",MSG)
315#define ILOG(MSG) LOGGER_LOGPOINT(LOGGER_INFO,"",MSG)
316#define DLOG(MSG) LOGGER_LOGPOINT(LOGGER_DEBUG,"",MSG)
317#define VLOG(MSG) LOGGER_LOGPOINT(LOGGER_VERBOSE,"",MSG)
319#define ELOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_EMERG,TAGS,MSG)
320#define ALOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_ALERT,TAGS,MSG)
321#define CLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_CRITICAL,TAGS,MSG)
322#define PLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_ERR,TAGS,MSG)
323#define WLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_WARNING,TAGS,MSG)
324#define NLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_NOTICE,TAGS,MSG)
325#define ILOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_INFO,TAGS,MSG)
326#define DLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_DEBUG,TAGS,MSG)
327#define VLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_VERBOSE,TAGS,MSG)
446 log(
const std::string& msg)
const;
540 const char*
tags = 0);
542#if NIDAS_LOGGER_GUARD_ACTIVE
664 parse(
const std::string& text);
735 if (name.length() > 0)
859 setParameter(
const std::string& name,
const std::string& value);
868 getParameter(
const std::string& name,
const std::string& dvalue=
"");
898 template <
typename T>
900 getParameterT(
const std::string& name,
const T& dvalue = T());
908 getEnvParameter(
const std::string& name,
const std::string& dvalue=
"");
1007 msg << right.msg.str();
1019 format(
const char *fmt, ...);
1027 operator std::string ()
const
1032 template <
typename T>
1059 inline std::streampos
1091 template <
typename T>
1120 return (*op)(logmsg);
1135 Logger(
const std::string& ident,
int logopt,
int facility,
1136 const char *TZ = 0);
1151 createInstance(
const std::string& ident,
int logopt,
int facility,
1152 const char *TZ = 0);
1220 log(
int severity,
const char* file,
const char* fn,
1221 int line,
const char *fmt, ...);
1241 msg (lc, m.getMessage());
1335 void setTZ(
const char* val);
1372 log(
const std::string& msg)
const
1384 template <
typename T>
A configuration to enable or disable a matching set of log points.
Definition Logger.h:587
LogConfig & parse(const std::string &text)
Parse LogConfig settings from a string specifier, using comma-separated fields to assign to each of t...
Definition Logger.cc:632
int line
Apply this config to log points with the given line number.
Definition Logger.h:613
LogConfig(const std::string &text="")
Construct a default LogConfig which matches and enables every log point with level DEBUG or higher.
Definition Logger.cc:576
int level
For all matching log points, enable the log point if the log level is more severe (lower value) than ...
Definition Logger.h:620
std::string tag_match
Apply this config to log points whose tags include this tag.
Definition Logger.h:606
bool activate
If true, then all matching log points will be enabled.
Definition Logger.h:626
bool matches(const LogContext &lc) const
Return true if this config matches the given LogContext lc.
Definition Logger.cc:590
std::string function_match
Apply this config to log points in functions which contain the given string.
Definition Logger.h:600
std::string filename_match
Apply this config to log points in filenames which contain the given string.
Definition Logger.h:594
LogContextState holds the status members of a LogContext and related behavior.
Definition Logger.h:355
int _level
Definition Logger.h:469
int line() const
Definition Logger.h:398
LogContextState(int level, const char *file, const char *function, int line, const char *tags=0)
The LogContextState constructor initializes all the static context information and also records the I...
Definition Logger.cc:1035
std::string levelName() const
Definition Logger.h:425
const char * filename() const
Definition Logger.h:386
std::string threadName() const
Return the name of the thread which created this context.
Definition Logger.cc:1083
int _line
Definition Logger.h:472
void setActive(bool active)
Enable this log point according to active.
Definition Logger.h:380
int level() const
Definition Logger.h:404
pthread_t _threadId
Definition Logger.h:477
const char * _file
Definition Logger.h:470
const char * tags() const
Definition Logger.h:410
const char * _tags
Definition Logger.h:473
const char * function() const
Definition Logger.h:392
const char * _function
Definition Logger.h:471
bool _active
Definition Logger.h:475
bool active() const
Return true if log messages from this context have been enabled.
Definition Logger.h:371
The LogContext is created at a point in the application code and filled in with details about that lo...
Definition Logger.h:531
~LogContext()
Since the constructor registers this context with the global log points, the destructor unregisters i...
Definition Logger.cc:1091
LogContext(int level, const char *file, const char *function, int line, const char *tags=0)
Initialize LogContextState with the static context information, then add this context to the global r...
Definition Logger.cc:1044
LogContext & operator=(const LogContext &)=delete
LogContext(const LogContext &)=delete
LogContext allocates a resource: it's registration with the global log points.
A class for formatting and streaming a log message.
Definition Logger.h:952
LogMessage & operator=(const LogMessage &right)
Definition Logger.h:1011
std::streampos length()
Return the length of the current message buffer.
Definition Logger.h:1060
~LogMessage()
If this message was associated with a LogContext, then send the completed message to it when this ins...
Definition Logger.h:1041
LogMessage(const std::string &s="")
Create a LogMessage, optionally set to an initial string.
Definition Logger.h:957
const LogContextState * _log_context
Definition Logger.h:1083
void log()
If this LogMessage is associated with a LogContext and if the current message is not empty,...
Definition Logger.h:1071
LogMessage(const LogContextState *lp, const std::string &s="")
Associate this LogMessage with a LogContext.
Definition Logger.h:996
std::string getMessage() const
Definition Logger.h:1022
LogMessage & format(const char *fmt,...)
Definition Logger.cc:556
std::ostringstream msg
Definition Logger.h:1082
LogMessage(const LogMessage &right)
Definition Logger.h:1003
A LogScheme is a vector of LogConfig's and the vector of fields to show in log messages.
Definition Logger.h:680
bool _fallbacks
Definition Logger.h:937
std::string _name
Definition Logger.h:932
static std::string fieldToString(LogScheme::LogField lf)
Convert a LogField to its lower-case string name.
Definition Logger.cc:899
log_configs_v getConfigs()
Return a copy of the LogConfig instances added to this LogScheme.
Definition Logger.cc:859
int logLevel()
Return the highest level of log messages enabled in all the LogConfig instances added to this LogSche...
Definition Logger.cc:867
log_configs_v log_configs
Definition Logger.h:933
bool getShowLogPoints()
Return true if showLogPoints() is enabled.
Definition Logger.h:922
void showLogPoints(bool show)
If show is true, then all the known log points will be listed in log messages, and all future log poi...
Definition Logger.cc:1002
LogScheme(const std::string &name="")
Construct a LogScheme with no LogConfig entries and show fields set to "time,level,...
Definition Logger.cc:745
bool _showlogpoints
Definition Logger.h:936
LogScheme & setName(const std::string &name)
LogScheme names must not be empty, so this method has no effect unless name.length() > 0.
Definition Logger.h:733
const std::string & getName() const
Definition Logger.h:741
LogScheme & addConfig(const LogConfig &lc)
Push a new LogConfig onto the list of existing configs.
Definition Logger.cc:816
void setParameter(const std::string &name, const std::string &value)
Set a parameter for this scheme.
Definition Logger.cc:934
LogScheme & clearConfigs()
Clear the set of LogConfig's in this scheme.
Definition Logger.cc:807
std::vector< LogField > log_fields
Definition Logger.h:934
static LogField stringToField(const std::string &s)
Convert the name of a log information field to its enum, such as Logger::ThreadField.
Definition Logger.cc:885
std::vector< LogConfig > log_configs_v
The type for the vector of LogConfigs returned by getConfigs().
Definition Logger.h:698
std::string getShowFieldsString() const
Return a comma-separated string describing the list of fields to be shown by this scheme.
Definition Logger.cc:793
LogScheme & setShowFields(const std::vector< LogField > &fields)
Set the fields to show in log messages and their order.
Definition Logger.cc:758
std::string getParameter(const std::string &name, const std::string &dvalue="")
Return the string value of the parameter with name name.
Definition Logger.cc:942
std::map< std::string, std::string > _parameters
Definition Logger.h:935
LogField
Definition Logger.h:684
@ AllFields
Definition Logger.h:692
@ NoneField
Definition Logger.h:685
@ LevelField
Definition Logger.h:689
@ TimeField
Definition Logger.h:691
@ FunctionField
Definition Logger.h:687
@ FileField
Definition Logger.h:688
@ MessageField
Definition Logger.h:690
@ ThreadField
Definition Logger.h:686
LogScheme & addFallback(const LogConfig &lc)
Add a fallback LogConfig.
Definition Logger.cc:838
std::string getEnvParameter(const std::string &name, const std::string &dvalue="")
Return the value of the parameter.
Definition Logger.cc:956
void show_log_point(const LogContextState &lp) const
Log a message about the existence and state of the given LogContext @lp.
Definition Logger.cc:982
bool parseParameter(const std::string &text)
Parse a parameter setting using syntax <name>=.
Definition Logger.cc:913
Simple logging class, based on UNIX syslog interface.
Definition Logger.h:1133
char * _saveTZ
Definition Logger.h:1343
static Logger * get_instance_locked(std::ostream *out=0)
Return the current instance or create a default, without locking.
Definition Logger.cc:297
static Logger * _instance
Definition Logger.h:1337
char * _ident
Definition Logger.h:1344
char * _loggerTZ
Definition Logger.h:1342
Logger(const Logger &)
No copying.
void setTZ(const char *val)
Set the timezone to be used in the log messages, whether passed to syslog or formatted to include the...
Definition Logger.cc:325
static void destroyInstance()
Destroy any existing Logger instance.
Definition Logger.cc:260
static Logger * createInstance(const std::string &ident, int logopt, int facility, const char *TZ=0)
Create a syslog-type Logger.
Definition Logger.cc:285
void msg(const LogContextState &lc, const LogMessage &m)
Definition Logger.h:1239
void msg(const LogContextState &lc, const std::string &msg)
Send a log message for the given LogContext lc.
Definition Logger.cc:355
void log(int severity, const char *file, const char *fn, int line, const char *fmt,...)
Build a message from a printf-format string and variable args, and log the message.
Definition Logger.cc:455
Logger()
Definition Logger.cc:239
~Logger()
Definition Logger.cc:248
bool _syslogit
Definition Logger.h:1340
static void init()
Initializes the logging implementation.
Definition Logger.cc:270
std::ostream * _output
Definition Logger.h:1339
Logger & operator=(const Logger &)
No assignment.
static Logger * getInstance()
Retrieve the current Logger singleton instance.
Definition Logger.cc:318
const int LOGGER_EMERGENCY
Definition Logger.h:176
const int LOGGER_DEBUG
Definition Logger.h:186
const int LOGGER_VERBOSE
Definition Logger.h:187
const int LOGGER_PROBLEM
Definition Logger.h:182
const int LOGGER_ERROR
Definition Logger.h:180
const int LOGGER_INFO
Definition Logger.h:185
const int LOGGER_EMERG
Definition Logger.h:175
const int LOGGER_WARNING
Definition Logger.h:183
const int LOGGER_CRIT
Definition Logger.h:178
const int LOGGER_CRITICAL
Definition Logger.h:179
const int LOGGER_NOTICE
Definition Logger.h:184
const int LOGGER_ERR
Definition Logger.h:181
const int LOGGER_ALERT
Definition Logger.h:177
const int LOGGER_NONE
Definition Logger.h:188
static LogScheme getScheme()
Return the current LogScheme, creating the default LogScheme if it has not been set yet.
Definition Logger.cc:508
static bool knownScheme(const std::string &name)
Return true if the scheme with the given name is known, false otherwise.
Definition Logger.cc:517
static void clearSchemes()
Erase all known schemes and reset the active scheme to the name initialized at application start.
Definition Logger.cc:540
static void updateScheme(const LogScheme &scheme)
Update or insert this scheme in the collection of log schemes.
Definition Logger.cc:526
static void setScheme(const std::string &name)
Set the current scheme.
Definition Logger.cc:478
LogMessage & operator<<(const T &t)
Everything streamed to a LogMessage is passed on to the underlying ostringstream, including ostream m...
Definition Logger.h:1095
#define LOG_ALERT
Definition Logger.h:214
LogMessage & operator<<(LogMessage &logmsg, LogMessage &(*op)(LogMessage &))
Template to call LogMessage manipulators like endlog when streamed to a LogMessage.
Definition Logger.h:1118
LogMessage & endlog(LogMessage &logmsg)
LogMessage manipulator which logs the current message buffer, if any, and then clears the message.
Definition Logger.h:1107
#define LOG_EMERG
Definition Logger.h:213
string logLevelToString(int level)
Convert an integral log level to a string name.
Definition Logger.cc:1144
#define LOG_DEBUG
Definition Logger.h:220
T getParameterT(const std::string &name, const T &dvalue=T())
Lookup a parameter with name name in a LogScheme and convert the value to the type of the dvalue para...
Definition Logger.h:1387
int stringToLogLevel(const std::string &slevel)
Convert the name of a log level to its integer value.
Definition Logger.cc:1129
#define LOG_ERR
Definition Logger.h:216
#define LOG_CRIT
Definition Logger.h:215
#define LOG_NOTICE
Definition Logger.h:218
#define LOG_WARNING
Definition Logger.h:217
#define LOG_INFO
Definition Logger.h:219
LogMessage log() const
Return a LogMessage associated with this LogContextState, so the message will be logged through this ...
Definition Logger.h:1379
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31