27 #ifndef NIDAS_UTIL_LOGGER_H_
28 #define NIDAS_UTIL_LOGGER_H_
43 #if !defined(SVR4) && ( defined(__GNUC__) && __GNUC__ < 2)
47 namespace nidas {
namespace util {
191 #define LOG_CONTEXT(LEVEL) \
192 nidas::util::LEVEL, __FILE__, __PRETTY_FUNCTION__, __LINE__
200 #define LOG_STATIC_CONTEXT(LEVEL) \
201 nidas::util::LEVEL, __FILE__, "file_static_scope", __LINE__
204 #define LOG_EMERG LOG_CONTEXT(LOGGER_EMERG)
205 #define LOG_ALERT LOG_CONTEXT(LOGGER_ALERT)
206 #define LOG_CRIT LOG_CONTEXT(LOGGER_CRIT)
207 #define LOG_ERR LOG_CONTEXT(LOGGER_ERR)
208 #define LOG_WARNING LOG_CONTEXT(LOGGER_WARNING)
209 #define LOG_NOTICE LOG_CONTEXT(LOGGER_NOTICE)
210 #define LOG_INFO LOG_CONTEXT(LOGGER_INFO)
211 #define LOG_DEBUG LOG_CONTEXT(LOGGER_DEBUG)
212 #define LOG_VERBOSE LOG_CONTEXT(LOGGER_VERBOSE)
248 #define LOGGER_LOGPOINT(LEVEL,TAGS,MSG) \
250 static nidas::util::LogContext logctxt \
251 (nidas::util::LEVEL, __FILE__,__PRETTY_FUNCTION__, \
253 if (logctxt.active()) \
254 logctxt.log(nidas::util::LogMessage().format MSG); } \
299 #define ELOG(MSG) LOGGER_LOGPOINT(LOGGER_EMERG,"",MSG)
300 #define ALOG(MSG) LOGGER_LOGPOINT(LOGGER_ALERT,"",MSG)
301 #define CLOG(MSG) LOGGER_LOGPOINT(LOGGER_CRITICAL,"",MSG)
302 #define PLOG(MSG) LOGGER_LOGPOINT(LOGGER_ERR,"",MSG) // For Problem, as in Error
303 #define WLOG(MSG) LOGGER_LOGPOINT(LOGGER_WARNING,"",MSG)
304 #define NLOG(MSG) LOGGER_LOGPOINT(LOGGER_NOTICE,"",MSG)
305 #define ILOG(MSG) LOGGER_LOGPOINT(LOGGER_INFO,"",MSG)
306 #define DLOG(MSG) LOGGER_LOGPOINT(LOGGER_DEBUG,"",MSG)
307 #define VLOG(MSG) LOGGER_LOGPOINT(LOGGER_VERBOSE,"",MSG)
309 #define ELOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_EMERG,TAGS,MSG)
310 #define ALOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_ALERT,TAGS,MSG)
311 #define CLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_CRITICAL,TAGS,MSG)
312 #define PLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_ERR,TAGS,MSG) // For Problem, as in Error
313 #define WLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_WARNING,TAGS,MSG)
314 #define NLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_NOTICE,TAGS,MSG)
315 #define ILOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_INFO,TAGS,MSG)
316 #define DLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_DEBUG,TAGS,MSG)
317 #define VLOGT(TAGS,MSG) LOGGER_LOGPOINT(LOGGER_VERBOSE,TAGS,MSG)
396 const char*
tags = 0);
471 log(
const std::string& msg)
const;
608 parse(
const std::string& text);
679 if (name.length() > 0)
803 setParameter(
const std::string& name,
const std::string& value);
812 getParameter(
const std::string& name,
const std::string& dvalue=
"");
842 template <
typename T>
844 getParameterT(
const std::string& name,
const T& dvalue = T());
852 getEnvParameter(
const std::string& name,
const std::string& dvalue=
"");
963 format(
const char *fmt, ...);
971 operator std::string ()
const
976 template <
typename T>
1003 inline std::streampos
1035 template <
typename T>
1064 return (*op)(logmsg);
1079 Logger(
const std::string& ident,
int logopt,
int facility,
1080 const char *TZ = 0);
1095 createInstance(
const std::string& ident,
int logopt,
int facility,
1096 const char *TZ = 0);
1157 #if defined(SVR4) || ( defined(__GNUC__) && __GNUC__ > 1 )
1159 log(
int severity,
const char* file,
const char* fn,
1160 int line,
const char *fmt, ...);
1161 #elif defined(__GNUC__)
1281 void setTZ(
const char* val);
1326 log(
const std::string& msg)
const
1338 template <
typename T>
void show_log_point(LogContext &lp)
Definition: Logger.cc:983
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:637
const int LOGGER_NOTICE
Definition: Logger.h:175
std::string filename_match
Apply this config to log points in filenames which contain the given string.
Definition: Logger.h:538
void msg_locked(const nidas::util::LogContext &lc, const std::string &msg)
Other Log classes call this method to log a message when the log mutex is already locked...
Definition: Logger.cc:339
const char * _tags
Definition: Logger.h:498
static void destroyInstance()
Destroy any existing Logger instance.
Definition: Logger.cc:246
char * saveTZ
Definition: Logger.h:1289
const int LOGGER_ALERT
Definition: Logger.h:168
static Logger * createInstance(const std::string &ident, int logopt, int facility, const char *TZ=0)
Create a syslog-type Logger.
Definition: Logger.cc:260
static Logger * get_instance_locked(std::ostream *out=0)
Return the current instance or create a default, without locking.
Definition: Logger.cc:272
const int LOGGER_ERR
Definition: Logger.h:172
const LogContext * _log_context
Definition: Logger.h:1027
std::vector< LogConfig > log_configs_v
The type for the vector of LogConfigs returned by getConfigs().
Definition: Logger.h:642
Logger & operator=(const Logger &)
No assignment.
#define LOG_EMERG
Definition: Logger.h:204
const int LOGGER_CRIT
Definition: Logger.h:169
#define LOG_CRIT
Definition: Logger.h:206
std::ostream * output
Definition: Logger.h:1285
The LogContext is created at a point in the application code and filled in with details about that lo...
Definition: Logger.h:382
LogMessage log() const
Return a LogMessage associated with this LogContext, so the message will be logged through this conte...
Definition: Logger.h:1333
#define LOG_INFO
Definition: Logger.h:210
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:677
pthread_t _threadId
Definition: Logger.h:502
std::string function_match
Apply this config to log points in functions which contain the given string.
Definition: Logger.h:544
int _line
Definition: Logger.h:497
Logger()
Definition: Logger.cc:225
A class for formatting and streaming a log message.
Definition: Logger.h:895
const int LOGGER_CRITICAL
Definition: Logger.h:170
static void updateScheme(const LogScheme &scheme)
Update or insert this scheme in the collection of log schemes.
Definition: Logger.cc:533
void setParameter(const std::string &name, const std::string &value)
Set a parameter for this scheme.
Definition: Logger.cc:939
LogConfig(const std::string &text="")
Construct a default LogConfig which matches and enables every log point with level DEBUG or higher...
Definition: Logger.cc:581
bool matches(const LogContext &lc) const
Return true if this config matches the given LogContext lc.
Definition: Logger.cc:595
int logLevel()
Return the highest level of log messages enabled in all the LogConfig instances added to this LogSche...
Definition: Logger.cc:872
Definition: Logger.cc:136
bool active() const
Return true if log messages from this context would be accepted.
Definition: Logger.h:406
const int LOGGER_NONE
Definition: Logger.h:179
const int LOGGER_DEBUG
Definition: Logger.h:177
LogMessage & operator<<(const T &t)
Everything streamed to a LogMessage is passed on to the underlying ostringstream, including ostream m...
Definition: Logger.h:1039
LogContext & operator=(const LogContext &)
No assignment.
A LogScheme is a vector of LogConfig's and the vector of fields to show in log messages.
Definition: Logger.h:623
LogMessage & endlog(LogMessage &logmsg)
LogMessage manipulator which logs the current message buffer, if any, and then clears the message...
Definition: Logger.h:1051
LogMessage & operator<<(LogMessage &logmsg, LogMessage &(*op)(LogMessage &))
Template to call LogMessage manipulators like endlog when streamed to a LogMessage.
Definition: Logger.h:1062
const int LOGGER_INFO
Definition: Logger.h:176
static nidas::util::Mutex mutex
Definition: Logger.h:1313
void log(va_alist)
Build a message from a printf-format string and variable args, and log the message.
Definition: Logger.cc:444
static void setScheme(const std::string &name)
Set the current scheme.
Definition: Logger.cc:485
#define LOG_ALERT
Definition: Logger.h:205
A configuration to enable or disable a matching set of log points.
Definition: Logger.h:530
static std::string fieldToString(LogScheme::LogField lf)
Convert a LogField to its lower-case string name.
Definition: Logger.cc:904
#define LOG_DEBUG
Definition: Logger.h:211
const int LOGGER_ERROR
Definition: Logger.h:171
bool _fallbacks
Definition: Logger.h:881
int _level
Definition: Logger.h:494
string logLevelToString(int level)
Convert an integral log level to a string name.
Definition: Logger.cc:1118
~Logger()
Definition: Logger.cc:234
std::string getShowFieldsString() const
Return a comma-separated string describing the list of fields to be shown by this scheme...
Definition: Logger.cc:798
std::string tag_match
Apply this config to log points whose tags include this tag.
Definition: Logger.h:550
#define LOG_ERR
Definition: Logger.h:207
bool _showlogpoints
Definition: Logger.h:880
bool syslogit
Definition: Logger.h:1286
~LogMessage()
If this message was associated with a LogContext, then send the completed message to it when this ins...
Definition: Logger.h:985
const char * _function
Definition: Logger.h:496
static Logger * getInstance()
Retrieve the current Logger singleton instance.
Definition: Logger.cc:293
const int LOGGER_VERBOSE
Definition: Logger.h:178
static void clearSchemes()
Erase all known schemes and reset the active scheme to the name initialized at application start...
Definition: Logger.cc:547
std::string threadName() const
Return the name of the thread which created this context.
Definition: Logger.cc:1054
std::string getEnvParameter(const std::string &name, const std::string &dvalue="")
Return the value of the parameter.
Definition: Logger.cc:961
int level() const
Definition: Logger.h:430
const char * _file
Definition: Logger.h:495
log_configs_v getConfigs()
Return a copy of the LogConfig instances added to this LogScheme.
Definition: Logger.cc:864
int line
Apply this config to log points with the given line number.
Definition: Logger.h:557
bool parseParameter(const std::string &text)
Parse a parameter setting using syntax <name>=.
Definition: Logger.cc:918
static LogScheme getScheme()
Return the current LogScheme, creating the default LogScheme if it has not been set yet...
Definition: Logger.cc:515
bool getShowLogPoints()
Return true if showLogPoints() is enabled.
Definition: Logger.h:866
LogScheme & addFallback(const LogConfig &lc)
Add a fallback LogConfig.
Definition: Logger.cc:843
bool _active
Definition: Logger.h:500
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:1341
const int LOGGER_EMERGENCY
Definition: Logger.h:167
~LogContext()
Definition: Logger.cc:1066
void log()
If this LogMessage is associated with a LogContext and if the current message is not empty...
Definition: Logger.h:1015
char * loggerTZ
Definition: Logger.h:1288
#define LOG_WARNING
Definition: Logger.h:208
bool activate
If true, then all matching log points will be enabled.
Definition: Logger.h:570
Simple logging class, based on UNIX syslog interface.
Definition: Logger.h:1077
std::streampos length()
Return the length of the current message buffer.
Definition: Logger.h:1004
LogScheme & setShowFields(const std::vector< LogField > &fields)
Set the fields to show in log messages and their order.
Definition: Logger.cc:763
int line() const
Definition: Logger.h:424
LogScheme(const std::string &name="")
Construct a LogScheme with no LogConfig entries and show fields set to "time,level,message".
Definition: Logger.cc:750
static bool knownScheme(const std::string &name)
Return true if the scheme with the given name is known, false otherwise.
Definition: Logger.cc:524
const int LOGGER_EMERG
Definition: Logger.h:166
void log(const std::string &msg) const
Convenience method which writes the given message to the current Logger instance, passing this object...
Definition: Logger.h:1326
LogMessage(const std::string &s="")
Create a LogMessage, optionally set to an initial string.
Definition: Logger.h:901
#define LOG_NOTICE
Definition: Logger.h:209
LogMessage(const LogContext *lp, const std::string &s="")
Associate this LogMessage with a LogContext.
Definition: Logger.h:940
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:1003
int stringToLogLevel(const std::string &slevel)
Convert the name of a log level to its integer value.
Definition: Logger.cc:1103
const std::string & getName() const
Definition: Logger.h:685
const char * filename() const
Definition: Logger.h:412
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:890
std::string getParameter(const std::string &name, const std::string &dvalue="")
Return the string value of the parameter with name name.
Definition: Logger.cc:947
LogMessage & operator=(const LogMessage &right)
Definition: Logger.h:955
static Logger * _instance
Definition: Logger.h:1283
std::string getMessage() const
Definition: Logger.h:966
const char * tags() const
Definition: Logger.h:436
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:300
char * _ident
Definition: Logger.h:1290
void msg(const LogContext &lc, const LogMessage &m)
Definition: Logger.h:1185
std::string _name
Definition: Logger.h:876
LogScheme & addConfig(const LogConfig &lc)
Push a new LogConfig onto the list of existing configs.
Definition: Logger.cc:821
std::ostringstream msg
Definition: Logger.h:1026
LogContext(int level, const char *file, const char *function, int line, const char *tags=0)
The LogContext constructor initializes all the static context information, adds this context to the g...
Definition: Logger.cc:1025
std::map< std::string, std::string > _parameters
Definition: Logger.h:879
const int LOGGER_PROBLEM
Definition: Logger.h:173
std::vector< LogField > log_fields
Definition: Logger.h:878
void msg(const LogContext &lc, const std::string &msg)
Send a log message for the given LogContext lc.
Definition: Logger.cc:330
int level
For all matching log points, enable the log point if the log level is more severe (lower value) than ...
Definition: Logger.h:564
A C++ wrapper for a POSIX mutex.
Definition: ThreadSupport.h:154
LogScheme & clearConfigs()
Clear the set of LogConfig's in this scheme.
Definition: Logger.cc:812
LogMessage(const LogMessage &right)
Definition: Logger.h:947
LogMessage & format(const char *fmt,...)
Definition: Logger.cc:561
LogField
Definition: Logger.h:627
const int LOGGER_WARNING
Definition: Logger.h:174
std::string levelName() const
Definition: Logger.h:451
log_configs_v log_configs
Definition: Logger.h:877