This commit is contained in:
gabime
2014-12-21 02:47:04 +02:00
parent fa677017a6
commit 40a55b8e34
38 changed files with 2995 additions and 2995 deletions

View File

@@ -46,22 +46,22 @@ template<class Mutex>
class base_sink:public sink
{
public:
base_sink():_mutex() {}
virtual ~base_sink() = default;
base_sink():_mutex() {}
virtual ~base_sink() = default;
base_sink(const base_sink&) = delete;
base_sink& operator=(const base_sink&) = delete;
base_sink(const base_sink&) = delete;
base_sink& operator=(const base_sink&) = delete;
void log(const details::log_msg& msg) override
{
std::lock_guard<Mutex> lock(_mutex);
_sink_it(msg);
}
void log(const details::log_msg& msg) override
{
std::lock_guard<Mutex> lock(_mutex);
_sink_it(msg);
}
protected:
virtual void _sink_it(const details::log_msg& msg) = 0;
Mutex _mutex;
virtual void _sink_it(const details::log_msg& msg) = 0;
Mutex _mutex;
};
}
}

View File

@@ -43,20 +43,20 @@ template<class Mutex>
class simple_file_sink : public base_sink<Mutex>
{
public:
explicit simple_file_sink(const std::string &filename,
bool auto_flush=false):
_file_helper(auto_flush)
{
_file_helper.open(filename);
}
explicit simple_file_sink(const std::string &filename,
bool auto_flush=false):
_file_helper(auto_flush)
{
_file_helper.open(filename);
}
protected:
void _sink_it(const details::log_msg& msg) override
{
_file_helper.write(msg);
}
void _sink_it(const details::log_msg& msg) override
{
_file_helper.write(msg);
}
private:
details::file_helper _file_helper;
details::file_helper _file_helper;
};
typedef simple_file_sink<std::mutex> simple_file_sink_mt;
@@ -69,80 +69,80 @@ template<class Mutex>
class rotating_file_sink : public base_sink<Mutex>
{
public:
rotating_file_sink(const std::string &base_filename, const std::string &extension,
std::size_t max_size, std::size_t max_files,
bool auto_flush=false):
_base_filename(base_filename),
_extension(extension),
_max_size(max_size),
_max_files(max_files),
_current_size(0),
_file_helper(auto_flush)
{
_file_helper.open(calc_filename(_base_filename, 0, _extension));
}
rotating_file_sink(const std::string &base_filename, const std::string &extension,
std::size_t max_size, std::size_t max_files,
bool auto_flush=false):
_base_filename(base_filename),
_extension(extension),
_max_size(max_size),
_max_files(max_files),
_current_size(0),
_file_helper(auto_flush)
{
_file_helper.open(calc_filename(_base_filename, 0, _extension));
}
protected:
void _sink_it(const details::log_msg& msg) override
{
_current_size += msg.formatted.size();
if (_current_size > _max_size)
{
_rotate();
_current_size = msg.formatted.size();
}
_file_helper.write(msg);
}
void _sink_it(const details::log_msg& msg) override
{
_current_size += msg.formatted.size();
if (_current_size > _max_size)
{
_rotate();
_current_size = msg.formatted.size();
}
_file_helper.write(msg);
}
private:
static std::string calc_filename(const std::string& filename, std::size_t index, const std::string& extension)
{
details::fmt::MemoryWriter w;
if (index)
w.write("{}.{}.{}", filename, index, extension);
else
w.write("{}.{}", filename, extension);
return w.str();
}
static std::string calc_filename(const std::string& filename, std::size_t index, const std::string& extension)
{
details::fmt::MemoryWriter w;
if (index)
w.write("{}.{}.{}", filename, index, extension);
else
w.write("{}.{}", filename, extension);
return w.str();
}
// Rotate files:
// log.txt -> log.1.txt
// log.1.txt -> log2.txt
// log.2.txt -> log3.txt
// log.3.txt -> delete
// Rotate files:
// log.txt -> log.1.txt
// log.1.txt -> log2.txt
// log.2.txt -> log3.txt
// log.3.txt -> delete
void _rotate()
{
_file_helper.close();
for (auto i = _max_files; i > 0; --i)
{
std::string src = calc_filename(_base_filename, i - 1, _extension);
std::string target = calc_filename(_base_filename, i, _extension);
void _rotate()
{
_file_helper.close();
for (auto i = _max_files; i > 0; --i)
{
std::string src = calc_filename(_base_filename, i - 1, _extension);
std::string target = calc_filename(_base_filename, i, _extension);
if (details::file_helper::file_exists(target))
{
if (std::remove(target.c_str()) != 0)
{
throw spdlog_ex("rotating_file_sink: failed removing " + target);
}
}
if (details::file_helper::file_exists(src) && std::rename(src.c_str(), target.c_str()))
{
throw spdlog_ex("rotating_file_sink: failed renaming " + src + " to " + target);
}
}
_file_helper.reopen(true);
}
std::string _base_filename;
std::string _extension;
std::size_t _max_size;
std::size_t _max_files;
std::size_t _current_size;
details::file_helper _file_helper;
if (details::file_helper::file_exists(target))
{
if (std::remove(target.c_str()) != 0)
{
throw spdlog_ex("rotating_file_sink: failed removing " + target);
}
}
if (details::file_helper::file_exists(src) && std::rename(src.c_str(), target.c_str()))
{
throw spdlog_ex("rotating_file_sink: failed renaming " + src + " to " + target);
}
}
_file_helper.reopen(true);
}
std::string _base_filename;
std::string _extension;
std::size_t _max_size;
std::size_t _max_files;
std::size_t _current_size;
details::file_helper _file_helper;
};
typedef rotating_file_sink<std::mutex> rotating_file_sink_mt;
@@ -155,56 +155,56 @@ template<class Mutex>
class daily_file_sink:public base_sink<Mutex>
{
public:
explicit daily_file_sink(const std::string& base_filename,
const std::string& extension,
bool auto_flush=false):
_base_filename(base_filename),
_extension(extension),
_midnight_tp (_calc_midnight_tp() ),
_file_helper(auto_flush)
{
_file_helper.open(calc_filename(_base_filename, _extension));
}
explicit daily_file_sink(const std::string& base_filename,
const std::string& extension,
bool auto_flush=false):
_base_filename(base_filename),
_extension(extension),
_midnight_tp (_calc_midnight_tp() ),
_file_helper(auto_flush)
{
_file_helper.open(calc_filename(_base_filename, _extension));
}
protected:
void _sink_it(const details::log_msg& msg) override
{
if (std::chrono::system_clock::now() >= _midnight_tp)
{
_file_helper.close();
_file_helper.open(calc_filename(_base_filename, _extension));
_midnight_tp = _calc_midnight_tp();
}
_file_helper.write(msg);
}
void _sink_it(const details::log_msg& msg) override
{
if (std::chrono::system_clock::now() >= _midnight_tp)
{
_file_helper.close();
_file_helper.open(calc_filename(_base_filename, _extension));
_midnight_tp = _calc_midnight_tp();
}
_file_helper.write(msg);
}
private:
// Return next midnight's time_point
static std::chrono::system_clock::time_point _calc_midnight_tp()
{
using namespace std::chrono;
auto now = system_clock::now();
time_t tnow = std::chrono::system_clock::to_time_t(now);
tm date = spdlog::details::os::localtime(tnow);
date.tm_hour = date.tm_min = date.tm_sec = 0;
auto midnight = std::chrono::system_clock::from_time_t(std::mktime(&date));
return system_clock::time_point(midnight + hours(24));
}
// Return next midnight's time_point
static std::chrono::system_clock::time_point _calc_midnight_tp()
{
using namespace std::chrono;
auto now = system_clock::now();
time_t tnow = std::chrono::system_clock::to_time_t(now);
tm date = spdlog::details::os::localtime(tnow);
date.tm_hour = date.tm_min = date.tm_sec = 0;
auto midnight = std::chrono::system_clock::from_time_t(std::mktime(&date));
return system_clock::time_point(midnight + hours(24));
}
//Create filename for the form basename.YYYY-MM-DD.extension
static std::string calc_filename(const std::string& basename, const std::string& extension)
{
std::tm tm = spdlog::details::os::localtime();
details::fmt::MemoryWriter w;
w.write("{}.{:04d}-{:02d}-{:02d}.{}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, extension);
return w.str();
}
//Create filename for the form basename.YYYY-MM-DD.extension
static std::string calc_filename(const std::string& basename, const std::string& extension)
{
std::tm tm = spdlog::details::os::localtime();
details::fmt::MemoryWriter w;
w.write("{}.{:04d}-{:02d}-{:02d}.{}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, extension);
return w.str();
}
std::string _base_filename;
std::string _extension;
std::chrono::system_clock::time_point _midnight_tp;
details::file_helper _file_helper;
std::string _base_filename;
std::string _extension;
std::chrono::system_clock::time_point _midnight_tp;
details::file_helper _file_helper;
};

View File

@@ -37,8 +37,8 @@ template <class Mutex>
class null_sink : public base_sink < Mutex >
{
protected:
void _sink_it(const details::log_msg&) override
{}
void _sink_it(const details::log_msg&) override
{}
};
typedef null_sink<details::null_mutex> null_sink_st;

View File

@@ -39,17 +39,17 @@ template<class Mutex>
class ostream_sink: public base_sink<Mutex>
{
public:
explicit ostream_sink(std::ostream& os) :_ostream(os) {}
ostream_sink(const ostream_sink&) = delete;
ostream_sink& operator=(const ostream_sink&) = delete;
virtual ~ostream_sink() = default;
explicit ostream_sink(std::ostream& os) :_ostream(os) {}
ostream_sink(const ostream_sink&) = delete;
ostream_sink& operator=(const ostream_sink&) = delete;
virtual ~ostream_sink() = default;
protected:
virtual void _sink_it(const details::log_msg& msg) override
{
_ostream.write(msg.formatted.data(), msg.formatted.size());
}
std::ostream& _ostream;
virtual void _sink_it(const details::log_msg& msg) override
{
_ostream.write(msg.formatted.data(), msg.formatted.size());
}
std::ostream& _ostream;
};
typedef ostream_sink<std::mutex> ostream_sink_mt;

View File

@@ -33,8 +33,8 @@ namespace sinks
class sink
{
public:
virtual ~sink() {}
virtual void log(const details::log_msg& msg) = 0;
virtual ~sink() {}
virtual void log(const details::log_msg& msg) = 0;
};
}
}

View File

@@ -38,7 +38,7 @@ template <class Mutex>
class stdout_sink : public ostream_sink<Mutex>
{
public:
stdout_sink() : ostream_sink<Mutex>(std::cout) {}
stdout_sink() : ostream_sink<Mutex>(std::cout) {}
};
@@ -50,7 +50,7 @@ template <class Mutex>
class stderr_sink : public ostream_sink<Mutex>
{
public:
stderr_sink() : ostream_sink<Mutex>(std::cerr) {}
stderr_sink() : ostream_sink<Mutex>(std::cerr) {}
};
typedef stderr_sink<std::mutex> stderr_sink_mt;

View File

@@ -46,51 +46,51 @@ namespace sinks
class syslog_sink : public sink
{
public:
//
syslog_sink(const std::string& ident = "", int syslog_option=0, int syslog_facility=LOG_USER):
_ident(ident)
{
_priorities[static_cast<int>(level::trace)] = LOG_DEBUG;
_priorities[static_cast<int>(level::debug)] = LOG_DEBUG;
_priorities[static_cast<int>(level::info)] = LOG_INFO;
_priorities[static_cast<int>(level::notice)] = LOG_NOTICE;
_priorities[static_cast<int>(level::warn)] = LOG_WARNING;
_priorities[static_cast<int>(level::err)] = LOG_ERR;
_priorities[static_cast<int>(level::critical)] = LOG_CRIT;
_priorities[static_cast<int>(level::alert)] = LOG_ALERT;
_priorities[static_cast<int>(level::emerg)] = LOG_EMERG;
_priorities[static_cast<int>(level::off)] = LOG_INFO;
//
syslog_sink(const std::string& ident = "", int syslog_option=0, int syslog_facility=LOG_USER):
_ident(ident)
{
_priorities[static_cast<int>(level::trace)] = LOG_DEBUG;
_priorities[static_cast<int>(level::debug)] = LOG_DEBUG;
_priorities[static_cast<int>(level::info)] = LOG_INFO;
_priorities[static_cast<int>(level::notice)] = LOG_NOTICE;
_priorities[static_cast<int>(level::warn)] = LOG_WARNING;
_priorities[static_cast<int>(level::err)] = LOG_ERR;
_priorities[static_cast<int>(level::critical)] = LOG_CRIT;
_priorities[static_cast<int>(level::alert)] = LOG_ALERT;
_priorities[static_cast<int>(level::emerg)] = LOG_EMERG;
_priorities[static_cast<int>(level::off)] = LOG_INFO;
//set ident to be program name if empty
::openlog(_ident.empty()? nullptr:_ident.c_str(), syslog_option, syslog_facility);
}
~syslog_sink()
{
::closelog();
}
//set ident to be program name if empty
::openlog(_ident.empty()? nullptr:_ident.c_str(), syslog_option, syslog_facility);
}
~syslog_sink()
{
::closelog();
}
syslog_sink(const syslog_sink&) = delete;
syslog_sink& operator=(const syslog_sink&) = delete;
syslog_sink(const syslog_sink&) = delete;
syslog_sink& operator=(const syslog_sink&) = delete;
void log(const details::log_msg &msg) override
{
::syslog(syslog_prio_from_level(msg), "%s", msg.formatted.str().c_str());
}
void log(const details::log_msg &msg) override
{
::syslog(syslog_prio_from_level(msg), "%s", msg.formatted.str().c_str());
}
private:
std::array<int, 10> _priorities;
//must store the ident because the man says openlog might use the pointer as is and not a string copy
const std::string _ident;
std::array<int, 10> _priorities;
//must store the ident because the man says openlog might use the pointer as is and not a string copy
const std::string _ident;
//
// Simply maps spdlog's log level to syslog priority level.
//
int syslog_prio_from_level(const details::log_msg &msg) const
{
return _priorities[static_cast<int>(msg.level)];
}
//
// Simply maps spdlog's log level to syslog priority level.
//
int syslog_prio_from_level(const details::log_msg &msg) const
{
return _priorities[static_cast<int>(msg.level)];
}
};
}
}