0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Hypertable::Cronolog Class Reference

Time based rotating log. More...

#include <Cronolog.h>

Collaboration diagram for Hypertable::Cronolog:
Collaboration graph
[legend]

Public Member Functions

 Cronolog (const string &name, const string &current_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...
 

Detailed Description

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.

Constructor & Destructor Documentation

Cronolog::Cronolog ( const string &  name,
const string &  current_dir,
const string &  archive_dir 
)

Constructor.

Parameters
nameBasename of log file
current_dirDirectory in which the current symlink is created
archive_dirRoot archive directory

Definition at line 51 of file Cronolog.cc.

Cronolog::~Cronolog ( )
virtual

Destructor.

Closes the log file.

Definition at line 59 of file Cronolog.cc.

Member Function Documentation

void Cronolog::roll ( time_t  now)
private

Rolls the log.

This method performs the following actions:

  • Removes the current symlink if it exists
  • Formulates the pathname of the new archive file, creating all parent directories if they do not exist
  • Opens the new log archive file
  • Creates the current symlink to point to the new log archive file
  • Sets m_roll_time to the next time (seconds since Epoch) the log needs to be rolled
    Parameters
    nowCurrent 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.

Parameters
nowCurrent time as seconds since Epoch
lineLog line to write

Definition at line 67 of file Cronolog.cc.

Member Data Documentation

string Hypertable::Cronolog::m_archive_dir
private

Directory containing log archives.

Definition at line 114 of file Cronolog.h.

string Hypertable::Cronolog::m_current_dir
private

Directory containing current symlink of log file.

Definition at line 111 of file Cronolog.h.

int Hypertable::Cronolog::m_fd
private

Log file descriptor.

Definition at line 120 of file Cronolog.h.

mutex Hypertable::Cronolog::m_mutex
private

Mutex for serializing access to member variables

Definition at line 105 of file Cronolog.h.

string Hypertable::Cronolog::m_name
private

Basename of log file.

Definition at line 108 of file Cronolog.h.

time_t Hypertable::Cronolog::m_roll_time
private

Time (seconds since Epoch) when next roll is required.

Definition at line 117 of file Cronolog.h.


The documentation for this class was generated from the following files: