dodo
0.0.1
A C++ library to create containerized Linux services
|
Go to the documentation of this file.
31 #include <yaml-cpp/yaml.h>
33 #include <sys/types.h>
38 #include <sys/syscall.h>
50 gethostname( hostname, 256 );
90 std::string entry =
"";
92 if ( level <=
levels_[Console] ) {
94 std::cout << entry << std::endl;
99 if ( entry.length() == 0 ) entry =
formatMessage( level, message );
105 std::stringstream ss;
156 std::stringstream ss;
158 gettimeofday( &tv,
nullptr );
164 std::set<std::string> trail;
167 if ( std::regex_match( entry.path().string(), log_regex ) ) {
168 trail.insert( entry.path().string() );
173 std::string path = *trail.begin();
174 unlink( path.c_str() );
175 trail.erase( trail.begin() );
206 std::stringstream ss;
208 gettimeofday( &tv,
nullptr );
size_t max_file_trail
The maximum number of files to keep besides the active log file.
static std::string LogLevelAsString(LogLevel level, bool acronym)
Return the LogLevel as a string (as used in console and file).
@ Error
The program signaled an error.
bool exists(const KeyPath &keypath) const
Return true if the KeyPath exists.
static Config * getConfig()
return the singleton.
Waits for and locks the Mutex on construction, unlocks the Mutex when this Mutexer is destructed.
FileParams file_params_
Parameters for the file Destination.
void error(const std::string &message)
Log a Error log entry.
std::string getAppName() const
Return the application name.
static threads::Mutex mutex_
threads::Mutex to serialize log writing.
int facility
The syslog facility to use.
void log(LogLevel level, const std::string &message)
Log a log entry.
std::string active_log
the filename of the active log
bool directoryExists(const std::string &path)
Return true when the directory exists.
void getFileParams(const Config &config)
Get FileParams from the Config into file_params_.
Logger(const Config &config)
Construct against a config.
void statistics(const std::string &message)
Log a Statistics log entry.
static const Config::KeyPath config_dodo_common_logger_syslog_level
The dodo.common.logger.syslog.level node.
@ Statistics
The program signaled runtime statistics.
size_t filesize
current file size
virtual ~Logger()
Destructor.
static Logger * logger_
The singleton.
@ Fatal
The program could not continue.
void warning(const std::string &message)
Log a Warning log entry.
void debug(const std::string &message)
Log a Debug log entry.
static const Config::KeyPath config_dodo_common_logger_file_directory
The dodo.common.logger.file.directory node.
std::ofstream file
the ofstream of the active log.
@ Warning
The program signaled a warning.
std::map< uint8_t, LogLevel > levels_
Map Destination to LogLevel.
std::string hostname_
The hostname cached in the constructor.
static const Config::KeyPath config_dodo_common_logger_syslog
The dodo.common.logger.syslog node.
std::string formatMessage(LogLevel level, const std::string &message)
Format a LogLine.
static const Config::KeyPath config_dodo_common_logger_console_level
The dodo.common.logger.console.level node.
@ Trace
The program produced trace info.
Singleton interface to a (read-only) deployment configuration, combining data from the deployment con...
static const Config::KeyPath config_dodo_common_logger_file_max_file_trail
The dodo.common.logger.file.max-file-trail node.
#define throw_Exception(what)
Throws an Exception, passes FILE and LINE to constructor.
void trace(const std::string &message)
Log a Trace log entry.
@ Info
The program signaled an informational message.
size_t max_size_mib
The maximum size of a logfile.
SyslogParams syslog_params_
Parameters for the syslog Destination.
size_t getFileSize(const std::string &path)
Return the size of the file.
static Logger * getLogger()
Get the Logger singleton - initialize() must have been called first.
Common and utility interfaces.
static const Config::KeyPath config_dodo_common_logger_file_level
The dodo.common.logger.file.level node.
static const Config::KeyPath config_dodo_common_logger_syslog_facility
The dodo.common.logger.syslog.facility node.
static Logger * initialize(const Config &config)
Initialize the Logger singleton.
LogLevel
The level of a log entry.
T getValue(const KeyPath &keypath) const
Get the value at keypath.
uint8_t destinations_
Destinations as bit flags.
int mapLeveltoSyslog(LogLevel level)
Map a LogLevel to a syslog level.
void fatal(const std::string &message)
Log a Fatal log entry.
void info(const std::string &message)
Log a Info log entry.
size_t rotate_throttle_counter_
Track when rotation was last checked.
static LogLevel StringAsLogLevel(const std::string slevel)
Return the string as a LogLevel.
void checkRotate()
Check and rotate the log file if it exceeds the size limit, delete older logs if needed.
std::string formatDateTimeUTC(const struct timeval &tv)
Return a datetime string in UTC (2020-07-01T20:14:36.442929Z)
static const Config::KeyPath config_dodo_common_logger_file
The dodo.common.logger.file node.
@ Debug
The program produced debug info.
bool directoryWritable(const std::string &path)
Return true when the directory exists and is writable to the caller.
static const Config::KeyPath config_dodo_common_logger_file_max_size_mib
The dodo.common.logger.file.max-size-mib node.
std::string directory
The directory to write the log files in.