mirror of
https://github.com/gabime/spdlog.git
synced 2025-11-16 09:28:56 +08:00
clang-format
This commit is contained in:
@@ -7,34 +7,35 @@
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
|
||||
#include "sink.h"
|
||||
#include "../details/os.h"
|
||||
#include "sink.h"
|
||||
|
||||
#include <android/log.h>
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <android/log.h>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
#if !defined(SPDLOG_ANDROID_RETRIES)
|
||||
#define SPDLOG_ANDROID_RETRIES 2
|
||||
#endif
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
namespace spdlog { namespace sinks {
|
||||
|
||||
/*
|
||||
* Android sink (logging using __android_log_write)
|
||||
* __android_log_write is thread-safe. No lock is needed.
|
||||
*/
|
||||
* Android sink (logging using __android_log_write)
|
||||
* __android_log_write is thread-safe. No lock is needed.
|
||||
*/
|
||||
class android_sink : public sink
|
||||
{
|
||||
public:
|
||||
explicit android_sink(const std::string& tag = "spdlog", bool use_raw_msg = false): _tag(tag), _use_raw_msg(use_raw_msg) {}
|
||||
explicit android_sink(const std::string &tag = "spdlog", bool use_raw_msg = false)
|
||||
: _tag(tag)
|
||||
, _use_raw_msg(use_raw_msg)
|
||||
{
|
||||
}
|
||||
|
||||
void log(const details::log_msg& msg) override
|
||||
void log(const details::log_msg &msg) override
|
||||
{
|
||||
const android_LogPriority priority = convert_to_android(msg.level);
|
||||
const char *msg_output = (_use_raw_msg ? msg.raw.c_str() : msg.formatted.c_str());
|
||||
@@ -42,7 +43,7 @@ public:
|
||||
// See system/core/liblog/logger_write.c for explanation of return value
|
||||
int ret = __android_log_write(priority, _tag.c_str(), msg_output);
|
||||
int retry_count = 0;
|
||||
while ((ret == -11/*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES))
|
||||
while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES))
|
||||
{
|
||||
details::os::sleep_for_millis(5);
|
||||
ret = __android_log_write(priority, _tag.c_str(), msg_output);
|
||||
@@ -55,14 +56,12 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void flush() override
|
||||
{
|
||||
}
|
||||
void flush() override {}
|
||||
|
||||
private:
|
||||
static android_LogPriority convert_to_android(spdlog::level::level_enum level)
|
||||
{
|
||||
switch(level)
|
||||
switch (level)
|
||||
{
|
||||
case spdlog::level::trace:
|
||||
return ANDROID_LOG_VERBOSE;
|
||||
@@ -85,7 +84,6 @@ private:
|
||||
bool _use_raw_msg;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,28 +5,25 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base_sink.h"
|
||||
#include "../common.h"
|
||||
#include "../details/os.h"
|
||||
#include "base_sink.h"
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
namespace spdlog { namespace sinks {
|
||||
|
||||
/**
|
||||
* This sink prefixes the output with an ANSI escape sequence color code depending on the severity
|
||||
* of the message.
|
||||
* If no color terminal detected, omit the escape codes.
|
||||
*/
|
||||
template <class Mutex>
|
||||
class ansicolor_sink : public base_sink<Mutex>
|
||||
template <class Mutex> class ansicolor_sink : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
explicit ansicolor_sink(FILE* file) : target_file_(file)
|
||||
explicit ansicolor_sink(FILE *file)
|
||||
: target_file_(file)
|
||||
{
|
||||
should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal();
|
||||
colors_[level::trace] = cyan;
|
||||
@@ -43,7 +40,7 @@ public:
|
||||
_flush();
|
||||
}
|
||||
|
||||
void set_color(level::level_enum color_level, const std::string& color)
|
||||
void set_color(level::level_enum color_level, const std::string &color)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
|
||||
colors_[color_level] = color;
|
||||
@@ -80,13 +77,13 @@ public:
|
||||
const std::string on_white = "\033[47m";
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
// Wrap the originally formatted message in color codes.
|
||||
// If color is not supported in the terminal, log as is instead.
|
||||
if (should_do_colors_)
|
||||
{
|
||||
const std::string& prefix = colors_[msg.level];
|
||||
const std::string &prefix = colors_[msg.level];
|
||||
fwrite(prefix.data(), sizeof(char), prefix.size(), target_file_);
|
||||
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), target_file_);
|
||||
fwrite(reset.data(), sizeof(char), reset.size(), target_file_);
|
||||
@@ -104,34 +101,33 @@ protected:
|
||||
fflush(target_file_);
|
||||
}
|
||||
|
||||
FILE* target_file_;
|
||||
FILE *target_file_;
|
||||
bool should_do_colors_;
|
||||
std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
|
||||
};
|
||||
|
||||
|
||||
template<class Mutex>
|
||||
class ansicolor_stdout_sink: public ansicolor_sink<Mutex>
|
||||
template <class Mutex> class ansicolor_stdout_sink : public ansicolor_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
ansicolor_stdout_sink(): ansicolor_sink<Mutex>(stdout)
|
||||
{}
|
||||
ansicolor_stdout_sink()
|
||||
: ansicolor_sink<Mutex>(stdout)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
using ansicolor_stdout_sink_mt = ansicolor_stdout_sink<std::mutex>;
|
||||
using ansicolor_stdout_sink_st = ansicolor_stdout_sink<details::null_mutex>;
|
||||
|
||||
template<class Mutex>
|
||||
class ansicolor_stderr_sink: public ansicolor_sink<Mutex>
|
||||
template <class Mutex> class ansicolor_stderr_sink : public ansicolor_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
ansicolor_stderr_sink(): ansicolor_sink<Mutex>(stderr)
|
||||
{}
|
||||
ansicolor_stderr_sink()
|
||||
: ansicolor_sink<Mutex>(stderr)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
using ansicolor_stderr_sink_mt = ansicolor_stderr_sink<std::mutex>;
|
||||
using ansicolor_stderr_sink_st = ansicolor_stderr_sink<details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
||||
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
@@ -10,27 +10,23 @@
|
||||
// all locking is taken care of here so no locking needed by the implementers..
|
||||
//
|
||||
|
||||
#include "sink.h"
|
||||
#include "../formatter.h"
|
||||
#include "../common.h"
|
||||
#include "../details/log_msg.h"
|
||||
#include "../formatter.h"
|
||||
#include "sink.h"
|
||||
|
||||
#include <mutex>
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
template<class Mutex>
|
||||
class base_sink : public sink
|
||||
namespace spdlog { namespace sinks {
|
||||
template <class Mutex> class base_sink : public sink
|
||||
{
|
||||
public:
|
||||
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) SPDLOG_FINAL override
|
||||
void log(const details::log_msg &msg) SPDLOG_FINAL override
|
||||
{
|
||||
std::lock_guard<Mutex> lock(_mutex);
|
||||
_sink_it(msg);
|
||||
@@ -43,9 +39,8 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void _sink_it(const details::log_msg& msg) = 0;
|
||||
virtual void _sink_it(const details::log_msg &msg) = 0;
|
||||
virtual void _flush() = 0;
|
||||
Mutex _mutex;
|
||||
};
|
||||
}
|
||||
}
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
@@ -11,32 +11,31 @@
|
||||
#include "sink.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
// Distribution sink (mux). Stores a vector of sinks which get called when log is called
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
template<class Mutex>
|
||||
class dist_sink : public base_sink<Mutex>
|
||||
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;
|
||||
explicit dist_sink()
|
||||
: _sinks()
|
||||
{
|
||||
}
|
||||
dist_sink(const dist_sink &) = delete;
|
||||
dist_sink &operator=(const dist_sink &) = delete;
|
||||
|
||||
protected:
|
||||
std::vector<std::shared_ptr<sink>> _sinks;
|
||||
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
for (auto &sink : _sinks)
|
||||
{
|
||||
if( sink->should_log( msg.level))
|
||||
if (sink->should_log(msg.level))
|
||||
{
|
||||
sink->log(msg);
|
||||
}
|
||||
@@ -66,5 +65,4 @@ public:
|
||||
using dist_sink_mt = dist_sink<std::mutex>;
|
||||
using dist_sink_st = dist_sink<details::null_mutex>;
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
@@ -5,31 +5,28 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base_sink.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "../details/file_helper.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "../fmt/fmt.h"
|
||||
#include "base_sink.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <cerrno>
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
namespace spdlog { namespace sinks {
|
||||
/*
|
||||
* Trivial file sink with single file as target
|
||||
*/
|
||||
template <class Mutex>
|
||||
class simple_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
template <class Mutex> class simple_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
explicit simple_file_sink(const filename_t &filename, bool truncate = false):_force_flush(false)
|
||||
explicit simple_file_sink(const filename_t &filename, bool truncate = false)
|
||||
: _force_flush(false)
|
||||
{
|
||||
_file_helper.open(filename, truncate);
|
||||
}
|
||||
@@ -40,10 +37,10 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
_file_helper.write(msg);
|
||||
if(_force_flush)
|
||||
if (_force_flush)
|
||||
_file_helper.flush();
|
||||
}
|
||||
|
||||
@@ -63,23 +60,21 @@ using simple_file_sink_st = simple_file_sink<details::null_mutex>;
|
||||
/*
|
||||
* Rotating file sink based on size
|
||||
*/
|
||||
template <class Mutex>
|
||||
class rotating_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
template <class Mutex> class rotating_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
rotating_file_sink(filename_t base_filename,
|
||||
std::size_t max_size, std::size_t max_files) :
|
||||
_base_filename(std::move(base_filename)),
|
||||
_max_size(max_size),
|
||||
_max_files(max_files)
|
||||
rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files)
|
||||
: _base_filename(std::move(base_filename))
|
||||
, _max_size(max_size)
|
||||
, _max_files(max_files)
|
||||
{
|
||||
_file_helper.open(calc_filename(_base_filename, 0));
|
||||
_current_size = _file_helper.size(); //expensive. called only once
|
||||
_current_size = _file_helper.size(); // expensive. called only once
|
||||
}
|
||||
|
||||
// calc filename according to index and file extension if exists.
|
||||
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
|
||||
static filename_t calc_filename(const filename_t& filename, std::size_t index)
|
||||
static filename_t calc_filename(const filename_t &filename, std::size_t index)
|
||||
{
|
||||
typename std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
|
||||
if (index != 0u)
|
||||
@@ -96,7 +91,7 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
_current_size += msg.formatted.size();
|
||||
if (_current_size > _max_size)
|
||||
@@ -158,13 +153,14 @@ using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
|
||||
struct default_daily_file_name_calculator
|
||||
{
|
||||
// Create filename for the form filename.YYYY-MM-DD_hh-mm.ext
|
||||
static filename_t calc_filename(const filename_t& filename)
|
||||
static filename_t calc_filename(const filename_t &filename)
|
||||
{
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
filename_t basename, ext;
|
||||
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
|
||||
std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
|
||||
w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, ext);
|
||||
w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
||||
tm.tm_hour, tm.tm_min, ext);
|
||||
return w.str();
|
||||
}
|
||||
};
|
||||
@@ -175,7 +171,7 @@ struct default_daily_file_name_calculator
|
||||
struct dateonly_daily_file_name_calculator
|
||||
{
|
||||
// Create filename for the form basename.YYYY-MM-DD
|
||||
static filename_t calc_filename(const filename_t& filename)
|
||||
static filename_t calc_filename(const filename_t &filename)
|
||||
{
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
filename_t basename, ext;
|
||||
@@ -189,18 +185,14 @@ struct dateonly_daily_file_name_calculator
|
||||
/*
|
||||
* Rotating file sink based on date. rotates at midnight
|
||||
*/
|
||||
template<class Mutex, class FileNameCalc = default_daily_file_name_calculator>
|
||||
class daily_file_sink SPDLOG_FINAL :public base_sink < Mutex >
|
||||
template <class Mutex, class FileNameCalc = default_daily_file_name_calculator> class daily_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
//create daily file sink which rotates on given time
|
||||
daily_file_sink(
|
||||
filename_t base_filename,
|
||||
int rotation_hour,
|
||||
int rotation_minute) :
|
||||
_base_filename(std::move(base_filename)),
|
||||
_rotation_h(rotation_hour),
|
||||
_rotation_m(rotation_minute)
|
||||
// create daily file sink which rotates on given time
|
||||
daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute)
|
||||
: _base_filename(std::move(base_filename))
|
||||
, _rotation_h(rotation_hour)
|
||||
, _rotation_m(rotation_minute)
|
||||
{
|
||||
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
|
||||
throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
|
||||
@@ -208,9 +200,8 @@ public:
|
||||
_file_helper.open(FileNameCalc::calc_filename(_base_filename));
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
if (std::chrono::system_clock::now() >= _rotation_tp)
|
||||
{
|
||||
@@ -239,7 +230,7 @@ private:
|
||||
{
|
||||
return rotation_time;
|
||||
}
|
||||
return{ rotation_time + std::chrono::hours(24) };
|
||||
return {rotation_time + std::chrono::hours(24)};
|
||||
}
|
||||
|
||||
filename_t _base_filename;
|
||||
@@ -252,5 +243,4 @@ private:
|
||||
using daily_file_sink_mt = daily_file_sink<std::mutex>;
|
||||
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
@@ -7,43 +7,35 @@
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
#include "base_sink.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "base_sink.h"
|
||||
|
||||
#include <winbase.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
namespace spdlog { namespace sinks {
|
||||
/*
|
||||
* MSVC sink (logging using OutputDebugStringA)
|
||||
*/
|
||||
template<class Mutex>
|
||||
class msvc_sink : public base_sink<Mutex>
|
||||
* MSVC sink (logging using OutputDebugStringA)
|
||||
*/
|
||||
template <class Mutex> class msvc_sink : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
explicit msvc_sink()
|
||||
{
|
||||
}
|
||||
explicit msvc_sink() {}
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
OutputDebugStringA(msg.formatted.c_str());
|
||||
}
|
||||
|
||||
void _flush() override
|
||||
{}
|
||||
void _flush() override {}
|
||||
};
|
||||
|
||||
using msvc_sink_mt = msvc_sink<std::mutex>;
|
||||
using msvc_sink_st = msvc_sink<details::null_mutex>;
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,31 +5,22 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base_sink.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "base_sink.h"
|
||||
|
||||
#include <mutex>
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
namespace spdlog { namespace sinks {
|
||||
|
||||
template <class Mutex>
|
||||
class null_sink : public base_sink<Mutex>
|
||||
template <class Mutex> class null_sink : public base_sink<Mutex>
|
||||
{
|
||||
protected:
|
||||
void _sink_it(const details::log_msg&) override
|
||||
{}
|
||||
|
||||
void _flush() override
|
||||
{}
|
||||
void _sink_it(const details::log_msg &) override {}
|
||||
|
||||
void _flush() override {}
|
||||
};
|
||||
|
||||
using null_sink_mt = null_sink<details::null_mutex>;
|
||||
using null_sink_st = null_sink<details::null_mutex>;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
@@ -8,23 +8,23 @@
|
||||
#include "../details/null_mutex.h"
|
||||
#include "base_sink.h"
|
||||
|
||||
#include <ostream>
|
||||
#include <mutex>
|
||||
#include <ostream>
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
template<class Mutex>
|
||||
class ostream_sink : public base_sink<Mutex>
|
||||
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;
|
||||
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;
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
_ostream.write(msg.formatted.data(), msg.formatted.size());
|
||||
if (_force_flush)
|
||||
@@ -36,12 +36,11 @@ protected:
|
||||
_ostream.flush();
|
||||
}
|
||||
|
||||
std::ostream& _ostream;
|
||||
std::ostream &_ostream;
|
||||
bool _force_flush;
|
||||
};
|
||||
|
||||
using ostream_sink_mt = ostream_sink<std::mutex>;
|
||||
using ostream_sink_st = ostream_sink<details::null_mutex>;
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
@@ -7,16 +7,13 @@
|
||||
|
||||
#include "../details/log_msg.h"
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
namespace spdlog { namespace sinks {
|
||||
class sink
|
||||
{
|
||||
public:
|
||||
virtual ~sink() = default;
|
||||
|
||||
virtual void log(const details::log_msg& msg) = 0;
|
||||
virtual void log(const details::log_msg &msg) = 0;
|
||||
virtual void flush() = 0;
|
||||
|
||||
bool should_log(level::level_enum msg_level) const;
|
||||
@@ -24,7 +21,7 @@ public:
|
||||
level::level_enum level() const;
|
||||
|
||||
private:
|
||||
level_t _level{ level::trace };
|
||||
level_t _level{level::trace};
|
||||
};
|
||||
|
||||
inline bool sink::should_log(level::level_enum msg_level) const
|
||||
@@ -42,5 +39,4 @@ inline level::level_enum sink::level() const
|
||||
return static_cast<spdlog::level::level_enum>(_level.load(std::memory_order_relaxed));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
@@ -12,13 +12,9 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
namespace spdlog { namespace sinks {
|
||||
|
||||
template <class Mutex>
|
||||
class stdout_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
template <class Mutex> class stdout_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
using MyType = stdout_sink<Mutex>;
|
||||
|
||||
@@ -32,7 +28,7 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stdout);
|
||||
_flush();
|
||||
@@ -47,8 +43,7 @@ protected:
|
||||
using stdout_sink_mt = stdout_sink<std::mutex>;
|
||||
using stdout_sink_st = stdout_sink<details::null_mutex>;
|
||||
|
||||
template <class Mutex>
|
||||
class stderr_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
template <class Mutex> class stderr_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
using MyType = stderr_sink<Mutex>;
|
||||
|
||||
@@ -62,7 +57,7 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stderr);
|
||||
_flush();
|
||||
@@ -77,5 +72,4 @@ protected:
|
||||
using stderr_sink_mt = stderr_sink<std::mutex>;
|
||||
using stderr_sink_st = stderr_sink<details::null_mutex>;
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
@@ -9,18 +9,14 @@
|
||||
|
||||
#ifdef SPDLOG_ENABLE_SYSLOG
|
||||
|
||||
#include "sink.h"
|
||||
#include "../details/log_msg.h"
|
||||
#include "sink.h"
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <syslog.h>
|
||||
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
namespace spdlog { namespace sinks {
|
||||
/**
|
||||
* Sink that write to syslog using the `syscall()` library call.
|
||||
*
|
||||
@@ -30,8 +26,8 @@ class syslog_sink : public sink
|
||||
{
|
||||
public:
|
||||
//
|
||||
syslog_sink(const std::string& ident = "", int syslog_option=0, int syslog_facility=LOG_USER):
|
||||
_ident(ident)
|
||||
syslog_sink(const std::string &ident = "", int syslog_option = 0, int syslog_facility = LOG_USER)
|
||||
: _ident(ident)
|
||||
{
|
||||
_priorities[static_cast<size_t>(level::trace)] = LOG_DEBUG;
|
||||
_priorities[static_cast<size_t>(level::debug)] = LOG_DEBUG;
|
||||
@@ -41,8 +37,8 @@ public:
|
||||
_priorities[static_cast<size_t>(level::critical)] = LOG_CRIT;
|
||||
_priorities[static_cast<size_t>(level::off)] = LOG_INFO;
|
||||
|
||||
//set ident to be program name if empty
|
||||
::openlog(_ident.empty()? nullptr:_ident.c_str(), syslog_option, syslog_facility);
|
||||
// set ident to be program name if empty
|
||||
::openlog(_ident.empty() ? nullptr : _ident.c_str(), syslog_option, syslog_facility);
|
||||
}
|
||||
|
||||
~syslog_sink() override
|
||||
@@ -50,22 +46,19 @@ public:
|
||||
::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.raw.str().c_str());
|
||||
}
|
||||
|
||||
void flush() override
|
||||
{
|
||||
}
|
||||
|
||||
void flush() override {}
|
||||
|
||||
private:
|
||||
std::array<int, 7> _priorities;
|
||||
//must store the ident because the man says openlog might use the pointer as is and not a string copy
|
||||
// must store the ident because the man says openlog might use the pointer as is and not a string copy
|
||||
const std::string _ident;
|
||||
|
||||
//
|
||||
@@ -76,7 +69,6 @@ private:
|
||||
return _priorities[static_cast<size_t>(msg.level)];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,24 +5,20 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base_sink.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "../common.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "base_sink.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <wincon.h>
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
namespace spdlog { namespace sinks {
|
||||
/*
|
||||
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
|
||||
*/
|
||||
template <class Mutex>
|
||||
class wincolor_sink : public base_sink<Mutex>
|
||||
template <class Mutex> class wincolor_sink : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
const WORD BOLD = FOREGROUND_INTENSITY;
|
||||
@@ -31,13 +27,14 @@ public:
|
||||
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
||||
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
|
||||
|
||||
wincolor_sink(HANDLE std_handle): out_handle_(std_handle)
|
||||
wincolor_sink(HANDLE std_handle)
|
||||
: out_handle_(std_handle)
|
||||
{
|
||||
colors_[level::trace] = CYAN;
|
||||
colors_[level::debug] = CYAN;
|
||||
colors_[level::info] = WHITE | BOLD;
|
||||
colors_[level::warn] = YELLOW | BOLD;
|
||||
colors_[level::err] = RED | BOLD; // red bold
|
||||
colors_[level::err] = RED | BOLD; // red bold
|
||||
colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background
|
||||
colors_[level::off] = 0;
|
||||
}
|
||||
@@ -47,8 +44,8 @@ public:
|
||||
this->flush();
|
||||
}
|
||||
|
||||
wincolor_sink(const wincolor_sink& other) = delete;
|
||||
wincolor_sink& operator=(const wincolor_sink& other) = delete;
|
||||
wincolor_sink(const wincolor_sink &other) = delete;
|
||||
wincolor_sink &operator=(const wincolor_sink &other) = delete;
|
||||
|
||||
// change the color for the given level
|
||||
void set_color(level::level_enum level, WORD color)
|
||||
@@ -58,12 +55,12 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
auto color = colors_[msg.level];
|
||||
auto orig_attribs = set_console_attribs(color);
|
||||
WriteConsoleA(out_handle_, msg.formatted.data(), static_cast<DWORD>(msg.formatted.size()), nullptr, nullptr);
|
||||
SetConsoleTextAttribute(out_handle_, orig_attribs); //reset to orig colors
|
||||
SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors
|
||||
}
|
||||
|
||||
void _flush() override
|
||||
@@ -85,19 +82,20 @@ private:
|
||||
back_color &= static_cast<WORD>(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY));
|
||||
// keep the background color unchanged
|
||||
SetConsoleTextAttribute(out_handle_, attribs | back_color);
|
||||
return orig_buffer_info.wAttributes; //return orig attribs
|
||||
return orig_buffer_info.wAttributes; // return orig attribs
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// windows color console to stdout
|
||||
//
|
||||
template <class Mutex>
|
||||
class wincolor_stdout_sink : public wincolor_sink<Mutex>
|
||||
template <class Mutex> class wincolor_stdout_sink : public wincolor_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
wincolor_stdout_sink() : wincolor_sink<Mutex>(GetStdHandle(STD_OUTPUT_HANDLE))
|
||||
{}
|
||||
wincolor_stdout_sink()
|
||||
: wincolor_sink<Mutex>(GetStdHandle(STD_OUTPUT_HANDLE))
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
using wincolor_stdout_sink_mt = wincolor_stdout_sink<std::mutex>;
|
||||
@@ -106,16 +104,16 @@ using wincolor_stdout_sink_st = wincolor_stdout_sink<details::null_mutex>;
|
||||
//
|
||||
// windows color console to stderr
|
||||
//
|
||||
template <class Mutex>
|
||||
class wincolor_stderr_sink : public wincolor_sink<Mutex>
|
||||
template <class Mutex> class wincolor_stderr_sink : public wincolor_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
wincolor_stderr_sink() : wincolor_sink<Mutex>(GetStdHandle(STD_ERROR_HANDLE))
|
||||
{}
|
||||
wincolor_stderr_sink()
|
||||
: wincolor_sink<Mutex>(GetStdHandle(STD_ERROR_HANDLE))
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
using wincolor_stderr_sink_mt = wincolor_stderr_sink<std::mutex>;
|
||||
using wincolor_stderr_sink_st = wincolor_stderr_sink<details::null_mutex>;
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
@@ -9,21 +9,16 @@
|
||||
|
||||
#include "msvc_sink.h"
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
namespace spdlog { namespace sinks {
|
||||
|
||||
/*
|
||||
* Windows debug sink (logging using OutputDebugStringA, synonym for msvc_sink)
|
||||
*/
|
||||
template <class Mutex>
|
||||
using windebug_sink = msvc_sink<Mutex>;
|
||||
* Windows debug sink (logging using OutputDebugStringA, synonym for msvc_sink)
|
||||
*/
|
||||
template <class Mutex> using windebug_sink = msvc_sink<Mutex>;
|
||||
|
||||
using windebug_sink_mt = msvc_sink_mt;
|
||||
using windebug_sink_st = msvc_sink_st;
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace spdlog::sinks
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user