Time based rotating log. More...
#include <Cronolog.h>
Public Member Functions | |
Cronolog (const string &name, const string ¤t_dir, const string &archive_dir) | |
Constructor. More... | |
virtual | ~Cronolog () |
Destructor. More... | |
void | write (time_t now, const string &line) |
Writes a log line. More... | |
void | sync () |
Syncs the log file. More... | |
Private Member Functions | |
void | roll (time_t now) |
Rolls the log. More... | |
Private Attributes | |
mutex | m_mutex |
Mutex for serializing access to member variables More... | |
string | m_name |
Basename of log file. More... | |
string | m_current_dir |
Directory containing current symlink of log file. More... | |
string | m_archive_dir |
Directory containing log archives. More... | |
time_t | m_roll_time |
Time (seconds since Epoch) when next roll is required. More... | |
int | m_fd |
Log file descriptor. More... | |
Time based rotating log.
This class provides a simple time based rotating log file similar to that of cronolog (http://cronolog.org/). It maintains a log file and will roll it over at the end of the day. It writes each daily log into a file relative to the archive directory with the following format:
YYYY-MM/DD/<log-name>
Where YYYY is the year, MM is the two-digit month, and DD is the two-digit day of the month. It also sets up a symlink in the current directory to point to the current log archive file. For example, if the name of the log is SlowQuery.log
and the current directory is /opt/hypertable/0.9.8.0/log
and the log archive directory is /opt/hypertable/0.9.8.0/log/archive
, then a listing of the current directory on 2014-09-11 would show the following symlink:
SlowQuery.log -> /opt/hypertable/0.9.8.0/log/archive/2014-09/11/SlowQuery.log
Definition at line 59 of file Cronolog.h.
Cronolog::Cronolog | ( | const string & | name, |
const string & | current_dir, | ||
const string & | archive_dir | ||
) |
Constructor.
name | Basename of log file |
current_dir | Directory in which the current symlink is created |
archive_dir | Root archive directory |
Definition at line 51 of file Cronolog.cc.
|
virtual |
|
private |
Rolls the log.
This method performs the following actions:
now | Current time (seconds since Epoch) |
Definition at line 92 of file Cronolog.cc.
void Cronolog::sync | ( | ) |
Syncs the log file.
Calls fsync on the log file descriptor.
Definition at line 86 of file Cronolog.cc.
void Cronolog::write | ( | time_t | now, |
const string & | line | ||
) |
Writes a log line.
This method first checks to see if the log needs to be rolled by comparing now
with m_roll_time and calls roll() if a roll is needed. It then writes the log line as follows:
now line
To guarantee that the write makes it into the log, the object can be destroyed or the sync() method can be called.
now | Current time as seconds since Epoch |
line | Log line to write |
Definition at line 67 of file Cronolog.cc.
|
private |
Directory containing log archives.
Definition at line 114 of file Cronolog.h.
|
private |
Directory containing current symlink of log file.
Definition at line 111 of file Cronolog.h.
|
private |
Log file descriptor.
Definition at line 120 of file Cronolog.h.
|
private |
Mutex for serializing access to member variables
Definition at line 105 of file Cronolog.h.
|
private |
Basename of log file.
Definition at line 108 of file Cronolog.h.
|
private |
Time (seconds since Epoch) when next roll is required.
Definition at line 117 of file Cronolog.h.