Initial commit
This commit is contained in:
committed by
Brendan LE GLAUNEC
parent
2af5f4475e
commit
201d7e31c6
@@ -0,0 +1,67 @@
|
||||
//
|
||||
// Copyright(c) 2015 Gabi Melman.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
|
||||
#include <mutex>
|
||||
#include "base_sink.h"
|
||||
#include "../details/null_mutex.h"
|
||||
|
||||
#include <android/log.h>
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
/*
|
||||
* Android sink (logging using __android_log_write)
|
||||
*/
|
||||
template <class Mutex>
|
||||
class base_android_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
explicit base_android_sink(std::string tag = "spdlog") : _tag(tag) {}
|
||||
|
||||
void
|
||||
flush() override {}
|
||||
|
||||
protected:
|
||||
void
|
||||
_sink_it(const details::log_msg& msg) override {
|
||||
const android_LogPriority priority = convert_to_android(msg.level);
|
||||
const int expected_size = msg.formatted.size();
|
||||
const int size = __android_log_write(priority, _tag.c_str(), msg.formatted.c_str());
|
||||
if (size > expected_size) {
|
||||
// Will write a little bit more than original message
|
||||
} else {
|
||||
throw spdlog_ex("Send to Android logcat failed");
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static android_LogPriority
|
||||
convert_to_android(spdlog::level::level_enum level) {
|
||||
switch (level) {
|
||||
case spdlog::level::trace: return ANDROID_LOG_VERBOSE;
|
||||
case spdlog::level::debug: return ANDROID_LOG_DEBUG;
|
||||
case spdlog::level::info: return ANDROID_LOG_INFO;
|
||||
case spdlog::level::notice: return ANDROID_LOG_INFO;
|
||||
case spdlog::level::warn: return ANDROID_LOG_WARN;
|
||||
case spdlog::level::err: return ANDROID_LOG_ERROR;
|
||||
case spdlog::level::critical: return ANDROID_LOG_FATAL;
|
||||
case spdlog::level::alert: return ANDROID_LOG_FATAL;
|
||||
case spdlog::level::emerg: return ANDROID_LOG_FATAL;
|
||||
default: throw spdlog_ex("Incorrect level value");
|
||||
}
|
||||
}
|
||||
|
||||
std::string _tag;
|
||||
};
|
||||
|
||||
typedef base_android_sink<std::mutex> android_sink_mt;
|
||||
typedef base_android_sink<details::null_mutex> android_sink_st;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// Copyright(c) 2015 Gabi Melman.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
#pragma once
|
||||
//
|
||||
// base sink templated over a mutex (either dummy or realy)
|
||||
// concrete implementation should only overrid the _sink_it method.
|
||||
// all locking is taken care of here so no locking needed by the implementors..
|
||||
//
|
||||
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include "./sink.h"
|
||||
#include "../formatter.h"
|
||||
#include "../common.h"
|
||||
#include "../details/log_msg.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
template <class Mutex>
|
||||
class base_sink : public sink {
|
||||
public:
|
||||
base_sink() : _mutex() {}
|
||||
virtual ~base_sink() = default;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void _sink_it(const details::log_msg& msg) = 0;
|
||||
Mutex _mutex;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// Copyright (c) 2015 David Schury, Gabi Melman
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <list>
|
||||
|
||||
#include "../details/log_msg.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "./base_sink.h"
|
||||
#include "./sink.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
template <class Mutex>
|
||||
class dist_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
explicit dist_sink() : _sinks() {}
|
||||
dist_sink(const dist_sink&) = delete;
|
||||
dist_sink& operator=(const dist_sink&) = delete;
|
||||
virtual ~dist_sink() = default;
|
||||
|
||||
protected:
|
||||
void
|
||||
_sink_it(const details::log_msg& msg) override {
|
||||
for (auto iter = _sinks.begin(); iter != _sinks.end(); iter++) (*iter)->log(msg);
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<sink>> _sinks;
|
||||
|
||||
public:
|
||||
void
|
||||
flush() override {
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
|
||||
for (auto iter = _sinks.begin(); iter != _sinks.end(); iter++) (*iter)->flush();
|
||||
}
|
||||
|
||||
void
|
||||
add_sink(std::shared_ptr<sink> sink) {
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
|
||||
if (sink && _sinks.end() == std::find(_sinks.begin(), _sinks.end(), sink)) {
|
||||
_sinks.push_back(sink);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
remove_sink(std::shared_ptr<sink> sink) {
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
|
||||
auto pos = std::find(_sinks.begin(), _sinks.end(), sink);
|
||||
if (pos != _sinks.end()) { _sinks.erase(pos); }
|
||||
}
|
||||
};
|
||||
|
||||
typedef dist_sink<std::mutex> dist_sink_mt;
|
||||
typedef dist_sink<details::null_mutex> dist_sink_st;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
//
|
||||
// Copyright(c) 2015 Gabi Melman.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include "base_sink.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "../details/file_helper.h"
|
||||
#include "../details/format.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
/*
|
||||
* Trivial file sink with single file as target
|
||||
*/
|
||||
template <class Mutex>
|
||||
class simple_file_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
explicit simple_file_sink(const std::string& filename, bool force_flush = false)
|
||||
: _file_helper(force_flush) {
|
||||
_file_helper.open(filename);
|
||||
}
|
||||
void
|
||||
flush() override {
|
||||
_file_helper.flush();
|
||||
}
|
||||
|
||||
protected:
|
||||
void
|
||||
_sink_it(const details::log_msg& msg) override {
|
||||
_file_helper.write(msg);
|
||||
}
|
||||
|
||||
private:
|
||||
details::file_helper _file_helper;
|
||||
};
|
||||
|
||||
typedef simple_file_sink<std::mutex> simple_file_sink_mt;
|
||||
typedef simple_file_sink<details::null_mutex> simple_file_sink_st;
|
||||
|
||||
/*
|
||||
* Rotating file sink based on size
|
||||
*/
|
||||
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 force_flush = false)
|
||||
: _base_filename(base_filename)
|
||||
, _extension(extension)
|
||||
, _max_size(max_size)
|
||||
, _max_files(max_files)
|
||||
, _current_size(0)
|
||||
, _file_helper(force_flush) {
|
||||
_file_helper.open(calc_filename(_base_filename, 0, _extension));
|
||||
_current_size = _file_helper.size(); // expensive. called only once
|
||||
}
|
||||
|
||||
void
|
||||
flush() override {
|
||||
_file_helper.flush();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private:
|
||||
static std::string
|
||||
calc_filename(const std::string& filename, std::size_t index, const std::string& extension) {
|
||||
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
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
typedef rotating_file_sink<std::mutex> rotating_file_sink_mt;
|
||||
typedef rotating_file_sink<details::null_mutex> rotating_file_sink_st;
|
||||
|
||||
/*
|
||||
* Rotating file sink based on date. rotates at midnight
|
||||
*/
|
||||
template <class Mutex>
|
||||
class daily_file_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
// create daily file sink which rotates on given time
|
||||
daily_file_sink(const std::string& base_filename,
|
||||
const std::string& extension,
|
||||
int rotation_hour,
|
||||
int rotation_minute,
|
||||
bool force_flush = false)
|
||||
: _base_filename(base_filename)
|
||||
, _extension(extension)
|
||||
, _rotation_h(rotation_hour)
|
||||
, _rotation_m(rotation_minute)
|
||||
, _file_helper(force_flush) {
|
||||
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
|
||||
throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
|
||||
_rotation_tp = _next_rotation_tp();
|
||||
_file_helper.open(calc_filename(_base_filename, _extension));
|
||||
}
|
||||
|
||||
void
|
||||
flush() override {
|
||||
_file_helper.flush();
|
||||
}
|
||||
|
||||
protected:
|
||||
void
|
||||
_sink_it(const details::log_msg& msg) override {
|
||||
if (std::chrono::system_clock::now() >= _rotation_tp) {
|
||||
_file_helper.open(calc_filename(_base_filename, _extension));
|
||||
_rotation_tp = _next_rotation_tp();
|
||||
}
|
||||
_file_helper.write(msg);
|
||||
}
|
||||
|
||||
private:
|
||||
std::chrono::system_clock::time_point
|
||||
_next_rotation_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 = _rotation_h;
|
||||
date.tm_min = _rotation_m;
|
||||
date.tm_sec = 0;
|
||||
auto rotation_time = std::chrono::system_clock::from_time_t(std::mktime(&date));
|
||||
if (rotation_time > now)
|
||||
return rotation_time;
|
||||
else
|
||||
return system_clock::time_point(rotation_time + 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();
|
||||
fmt::MemoryWriter w;
|
||||
w.write("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.{}",
|
||||
basename,
|
||||
tm.tm_year + 1900,
|
||||
tm.tm_mon + 1,
|
||||
tm.tm_mday,
|
||||
tm.tm_hour,
|
||||
tm.tm_min,
|
||||
extension);
|
||||
return w.str();
|
||||
}
|
||||
|
||||
std::string _base_filename;
|
||||
std::string _extension;
|
||||
int _rotation_h;
|
||||
int _rotation_m;
|
||||
std::chrono::system_clock::time_point _rotation_tp;
|
||||
details::file_helper _file_helper;
|
||||
};
|
||||
|
||||
typedef daily_file_sink<std::mutex> daily_file_sink_mt;
|
||||
typedef daily_file_sink<details::null_mutex> daily_file_sink_st;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Copyright(c) 2015 Gabi Melman.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <mutex>
|
||||
#include "./base_sink.h"
|
||||
#include "../details/null_mutex.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template <class Mutex>
|
||||
class null_sink : public base_sink<Mutex> {
|
||||
protected:
|
||||
void
|
||||
_sink_it(const details::log_msg&) override {}
|
||||
|
||||
void
|
||||
flush() override {}
|
||||
};
|
||||
typedef null_sink<details::null_mutex> null_sink_st;
|
||||
typedef null_sink<std::mutex> null_sink_mt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// Copyright(c) 2015 Gabi Melman.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
|
||||
#include "../details/null_mutex.h"
|
||||
#include "./base_sink.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
template <class Mutex>
|
||||
class ostream_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
explicit ostream_sink(std::ostream& os, bool force_flush = false)
|
||||
: _ostream(os), _force_flush(force_flush) {}
|
||||
ostream_sink(const ostream_sink&) = delete;
|
||||
ostream_sink& operator=(const ostream_sink&) = delete;
|
||||
virtual ~ostream_sink() = default;
|
||||
|
||||
protected:
|
||||
void
|
||||
_sink_it(const details::log_msg& msg) override {
|
||||
_ostream.write(msg.formatted.data(), msg.formatted.size());
|
||||
if (_force_flush) _ostream.flush();
|
||||
}
|
||||
|
||||
void
|
||||
flush() override {
|
||||
_ostream.flush();
|
||||
}
|
||||
|
||||
std::ostream& _ostream;
|
||||
bool _force_flush;
|
||||
};
|
||||
|
||||
typedef ostream_sink<std::mutex> ostream_sink_mt;
|
||||
typedef ostream_sink<details::null_mutex> ostream_sink_st;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Copyright(c) 2015 Gabi Melman.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../details/log_msg.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
class sink {
|
||||
public:
|
||||
virtual ~sink() {}
|
||||
virtual void log(const details::log_msg& msg) = 0;
|
||||
virtual void flush() = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// Copyright(c) 2015 Gabi Melman.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include "./ostream_sink.h"
|
||||
#include "../details/null_mutex.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template <class Mutex>
|
||||
class stdout_sink : public ostream_sink<Mutex> {
|
||||
using MyType = stdout_sink<Mutex>;
|
||||
|
||||
public:
|
||||
stdout_sink() : ostream_sink<Mutex>(std::cout, true) {}
|
||||
static std::shared_ptr<MyType>
|
||||
instance() {
|
||||
static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
|
||||
return instance;
|
||||
}
|
||||
};
|
||||
|
||||
typedef stdout_sink<details::null_mutex> stdout_sink_st;
|
||||
typedef stdout_sink<std::mutex> stdout_sink_mt;
|
||||
|
||||
template <class Mutex>
|
||||
class stderr_sink : public ostream_sink<Mutex> {
|
||||
using MyType = stderr_sink<Mutex>;
|
||||
|
||||
public:
|
||||
stderr_sink() : ostream_sink<Mutex>(std::cerr, true) {}
|
||||
static std::shared_ptr<MyType>
|
||||
instance() {
|
||||
static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
|
||||
return instance;
|
||||
}
|
||||
};
|
||||
|
||||
typedef stderr_sink<std::mutex> stderr_sink_mt;
|
||||
typedef stderr_sink<details::null_mutex> stderr_sink_st;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
//
|
||||
// Copyright(c) 2015 Gabi Melman.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <syslog.h>
|
||||
|
||||
#include "./sink.h"
|
||||
#include "../common.h"
|
||||
#include "../details/log_msg.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
/**
|
||||
* Sink that write to syslog using the `syscall()` library call.
|
||||
*
|
||||
* Locking is not needed, as `syslog()` itself is thread-safe.
|
||||
*/
|
||||
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;
|
||||
|
||||
// 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;
|
||||
|
||||
void
|
||||
log(const details::log_msg& msg) override {
|
||||
::syslog(syslog_prio_from_level(msg), "%s", msg.raw.str().c_str());
|
||||
}
|
||||
|
||||
void
|
||||
flush() override {}
|
||||
|
||||
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;
|
||||
|
||||
//
|
||||
// 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)];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user