35 #include <boost/algorithm/string.hpp>
44 #include <sys/types.h>
52 const string &archive_dir)
53 : m_name(name), m_current_dir(current_dir), m_archive_dir(archive_dir) {
60 lock_guard<mutex> lock(
m_mutex);
68 lock_guard<mutex> lock(
m_mutex);
77 sprintf(buf,
"%d ", (
int)now);
81 HT_FATALF(
"write to log file '%s' failed - %s",
82 m_name.c_str(), strerror(errno));
87 lock_guard<mutex> lock(
m_mutex);
101 HT_FATALF(
"Problem removing link '%s'", linkname.c_str());
106 localtime_r(&now, &tmval);
109 1900 + tmval.tm_year, tmval.tm_mon + 1);
113 HT_FATALF(
"Problem creating archive directory '%s'", subdir.c_str());
116 subdir +=
format(
"/%02d", tmval.tm_mday);
120 HT_FATALF(
"Problem creating archive directory '%s'", subdir.c_str());
123 string archive =
format(
"%s/%s", subdir.c_str(), m_name.c_str());
125 m_fd = ::open(archive.c_str(), O_CREAT|O_APPEND|O_WRONLY, 0644);
127 HT_FATALF(
"open(%s, O_CREAT|O_APPEND|O_WRONLY, 0644) failed - %s",
128 archive.c_str(), strerror(errno));
133 if (getcwd(cwd, 1024) ==
nullptr)
134 HT_FATALF(
"getcwd failure - %s", strerror(errno));
139 ::unlink(m_name.c_str());
141 if (symlink(archive.c_str(), m_name.c_str()) < 0)
142 HT_FATALF(
"symlink(%s, %s) failure - %s", archive.c_str(), m_name.c_str(),
146 HT_FATALF(
"chdir(%s) failure - %s", cwd, strerror(errno));
string m_name
Basename of log file.
static bool unlink(const String &fname)
Unlinks (deletes) a file or directory.
virtual ~Cronolog()
Destructor.
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
string m_current_dir
Directory containing current symlink of log file.
static ssize_t write(const String &fname, const std::string &contents)
Writes a String buffer to a file; the file is overwritten if it already exists.
static bool exists(const String &fname)
Checks if a file or directory exists.
void roll(time_t now)
Rolls the log.
void sync()
Syncs the log file.
static bool mkdirs(const String &dirname)
Creates a directory (with all parent directories, if required)
mutex m_mutex
Mutex for serializing access to member variables
Declarations for Cronolog.
File system utility functions.
int m_fd
Log file descriptor.
Logging routines and macros.
Compatibility Macros for C/C++.
#define HT_FATALF(msg,...)
void write(time_t now, const string &line)
Writes a log line.
string m_archive_dir
Directory containing log archives.
A String class based on std::string.
time_t m_roll_time
Time (seconds since Epoch) when next roll is required.
Cronolog(const string &name, const string ¤t_dir, const string &archive_dir)
Constructor.