mirror of
https://github.com/gabime/spdlog.git
synced 2025-03-31 02:12:40 +08:00
Modified sinks to be templates with Mutex param to support single threaded sinks
This commit is contained in:
parent
38468e64d8
commit
6c7793d47b
@ -17,8 +17,8 @@ namespace c11log
|
|||||||
namespace sinks
|
namespace sinks
|
||||||
{
|
{
|
||||||
|
|
||||||
|
template<class Mutex>
|
||||||
class async_sink : public base_sink
|
class async_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using q_type = details::blocking_queue<details::log_msg>;
|
using q_type = details::blocking_queue<details::log_msg>;
|
||||||
@ -54,28 +54,30 @@ private:
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// async_sink class implementation
|
// async_sink class implementation
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
template<class Mutex>
|
||||||
inline c11log::sinks::async_sink::async_sink(const q_type::size_type max_queue_size)
|
inline c11log::sinks::async_sink<Mutex>::async_sink(const q_type::size_type max_queue_size)
|
||||||
:_sinks(),
|
:_sinks(),
|
||||||
_active(true),
|
_active(true),
|
||||||
_q(max_queue_size),
|
_q(max_queue_size),
|
||||||
_back_thread(&async_sink::_thread_loop, this)
|
_back_thread(&async_sink::_thread_loop, this)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
inline c11log::sinks::async_sink::~async_sink()
|
template<class Mutex>
|
||||||
|
inline c11log::sinks::async_sink<Mutex>::~async_sink()
|
||||||
{
|
{
|
||||||
_shutdown();
|
_shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class Mutex>
|
||||||
inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
|
inline void c11log::sinks::async_sink<Mutex>::_sink_it(const details::log_msg& msg)
|
||||||
{
|
{
|
||||||
if(!_active || msg.formatted.empty())
|
if(!_active || msg.formatted.empty())
|
||||||
return;
|
return;
|
||||||
_q.push(msg);
|
_q.push(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void c11log::sinks::async_sink::_thread_loop()
|
template<class Mutex>
|
||||||
|
inline void c11log::sinks::async_sink<Mutex>::_thread_loop()
|
||||||
{
|
{
|
||||||
static std::chrono::seconds pop_timeout { 1 };
|
static std::chrono::seconds pop_timeout { 1 };
|
||||||
while (_active)
|
while (_active)
|
||||||
@ -93,23 +95,27 @@ inline void c11log::sinks::async_sink::_thread_loop()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void c11log::sinks::async_sink::add_sink(logger::sink_ptr sink)
|
template<class Mutex>
|
||||||
|
inline void c11log::sinks::async_sink<Mutex>::add_sink(logger::sink_ptr sink)
|
||||||
{
|
{
|
||||||
_sinks.push_back(sink);
|
_sinks.push_back(sink);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr sink)
|
template<class Mutex>
|
||||||
|
inline void c11log::sinks::async_sink<Mutex>::remove_sink(logger::sink_ptr sink)
|
||||||
{
|
{
|
||||||
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink), _sinks.end());
|
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink), _sinks.end());
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
inline c11log::sinks::async_sink::q_type& c11log::sinks::async_sink::q()
|
template<class Mutex>
|
||||||
|
inline c11log::sinks::async_sink::q_type& c11log::sinks::async_sink<Mutex>::q()
|
||||||
{
|
{
|
||||||
return _q;
|
return _q;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
|
||||||
inline void c11log::sinks::async_sink::shutdown(const std::chrono::milliseconds& timeout)
|
template<class Mutex>
|
||||||
|
inline void c11log::sinks::async_sink<Mutex>::shutdown(const std::chrono::milliseconds& timeout)
|
||||||
{
|
{
|
||||||
if(timeout > std::chrono::milliseconds::zero())
|
if(timeout > std::chrono::milliseconds::zero())
|
||||||
{
|
{
|
||||||
@ -122,7 +128,9 @@ inline void c11log::sinks::async_sink::shutdown(const std::chrono::milliseconds&
|
|||||||
_shutdown();
|
_shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void c11log::sinks::async_sink::_shutdown()
|
|
||||||
|
template<class Mutex>
|
||||||
|
inline void c11log::sinks::async_sink<Mutex>::_shutdown()
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(_shutdown_mutex);
|
std::lock_guard<std::mutex> guard(_shutdown_mutex);
|
||||||
if(_active)
|
if(_active)
|
||||||
|
@ -1,20 +1,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include<string>
|
#include<string>
|
||||||
|
#include<mutex>
|
||||||
#include<atomic>
|
#include<atomic>
|
||||||
|
#include "isink.h"
|
||||||
#include "../formatter.h"
|
#include "../formatter.h"
|
||||||
#include "../common_types.h"
|
#include "../common_types.h"
|
||||||
#include "../details/log_msg.h"
|
#include "../details/log_msg.h"
|
||||||
|
|
||||||
|
|
||||||
namespace c11log
|
namespace c11log
|
||||||
{
|
{
|
||||||
namespace sinks
|
namespace sinks
|
||||||
{
|
{
|
||||||
class base_sink
|
template<class Mutex>
|
||||||
|
class base_sink:public isink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
base_sink(): _enabled(true) {}
|
base_sink():_mutex(), _enabled(true) {}
|
||||||
virtual ~base_sink() = default;
|
virtual ~base_sink() = default;
|
||||||
|
|
||||||
base_sink(const base_sink&) = delete;
|
base_sink(const base_sink&) = delete;
|
||||||
@ -24,6 +27,7 @@ public:
|
|||||||
{
|
{
|
||||||
if (_enabled)
|
if (_enabled)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<Mutex> lock(_mutex);
|
||||||
_sink_it(msg);
|
_sink_it(msg);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -40,7 +44,9 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void _sink_it(const details::log_msg& msg) = 0;
|
virtual void _sink_it(const details::log_msg& msg) = 0;
|
||||||
|
Mutex _mutex;
|
||||||
std::atomic<bool> _enabled;
|
std::atomic<bool> _enabled;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,48 +3,50 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <mutex>
|
|
||||||
#include "base_sink.h"
|
#include "base_sink.h"
|
||||||
|
#include <mutex>
|
||||||
|
#include "../details/null_mutex.h"
|
||||||
#include "../details/flush_helper.h"
|
#include "../details/flush_helper.h"
|
||||||
#include "../details/blocking_queue.h"
|
#include "../details/blocking_queue.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace c11log
|
namespace c11log
|
||||||
{
|
{
|
||||||
namespace sinks
|
namespace sinks
|
||||||
{
|
{
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Thread safe, trivial file sink with single file as target
|
* Trivial file sink with single file as target
|
||||||
*/
|
*/
|
||||||
class simple_file_sink : public base_sink
|
template<class Mutex>
|
||||||
|
class simple_file_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit simple_file_sink(const std::string &filename,
|
explicit simple_file_sink(const std::string &filename,
|
||||||
const std::string& extension,
|
const std::size_t flush_every=0):
|
||||||
const std::size_t flush_every=0)
|
_ofstream(filename, std::ofstream::binary|std::ofstream::app),
|
||||||
: _mutex(),
|
_flush_helper(flush_every)
|
||||||
_ofstream(filename + "." + extension, std::ofstream::binary|std::ofstream::app),
|
|
||||||
_flush_helper(flush_every)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
void _sink_it(const details::log_msg& msg) override
|
void _sink_it(const details::log_msg& msg) override
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
_flush_helper.write(msg.formatted, _ofstream);
|
_flush_helper.write(msg.formatted, _ofstream);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
std::mutex _mutex;
|
|
||||||
std::ofstream _ofstream;
|
std::ofstream _ofstream;
|
||||||
details::file_flush_helper _flush_helper;
|
details::file_flush_helper _flush_helper;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef simple_file_sink<std::mutex> simple_file_sink_mt;
|
||||||
|
typedef simple_file_sink<details::null_mutex> simple_file_sink_st;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Thread safe, rotating file sink based on size
|
* Rotating file sink based on size
|
||||||
*/
|
*/
|
||||||
class rotating_file_sink : public base_sink
|
template<class Mutex>
|
||||||
|
class rotating_file_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
rotating_file_sink(const std::string &base_filename, const std::string &extension,
|
rotating_file_sink(const std::string &base_filename, const std::string &extension,
|
||||||
@ -55,7 +57,6 @@ public:
|
|||||||
_max_size(max_size),
|
_max_size(max_size),
|
||||||
_max_files(max_files),
|
_max_files(max_files),
|
||||||
_current_size(0),
|
_current_size(0),
|
||||||
_mutex(),
|
|
||||||
_ofstream(_calc_filename(_base_filename, 0, _extension), std::ofstream::binary),
|
_ofstream(_calc_filename(_base_filename, 0, _extension), std::ofstream::binary),
|
||||||
_flush_helper(flush_every)
|
_flush_helper(flush_every)
|
||||||
{
|
{
|
||||||
@ -64,8 +65,6 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
void _sink_it(const details::log_msg& msg) override
|
void _sink_it(const details::log_msg& msg) override
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
|
|
||||||
_current_size += msg.formatted.size();
|
_current_size += msg.formatted.size();
|
||||||
if (_current_size > _max_size)
|
if (_current_size > _max_size)
|
||||||
{
|
{
|
||||||
@ -89,10 +88,13 @@ private:
|
|||||||
|
|
||||||
|
|
||||||
// Rotate old files:
|
// Rotate old files:
|
||||||
// log.n-1.txt -> log.n.txt
|
|
||||||
// log n-2.txt -> log.n-1.txt
|
|
||||||
// ...
|
|
||||||
// log.txt -> log.1.txt
|
// log.txt -> log.1.txt
|
||||||
|
// log.n-1.txt -> log.n.txt
|
||||||
|
// log.n-2.txt -> log.n-1.txt
|
||||||
|
// log.n-3.txt ->..
|
||||||
|
// log.n.txt -> log.txt
|
||||||
|
|
||||||
|
|
||||||
void _rotate()
|
void _rotate()
|
||||||
{
|
{
|
||||||
_ofstream.close();
|
_ofstream.close();
|
||||||
@ -112,15 +114,18 @@ private:
|
|||||||
std::size_t _max_size;
|
std::size_t _max_size;
|
||||||
std::size_t _max_files;
|
std::size_t _max_files;
|
||||||
std::size_t _current_size;
|
std::size_t _current_size;
|
||||||
std::mutex _mutex;
|
|
||||||
std::ofstream _ofstream;
|
std::ofstream _ofstream;
|
||||||
details::file_flush_helper _flush_helper;
|
details::file_flush_helper _flush_helper;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef rotating_file_sink<std::mutex> rotating_file_sink_mt;
|
||||||
|
typedef rotating_file_sink<details::null_mutex>rotating_file_sink_st;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Thread safe, rotating file sink based on date. rotates at midnight
|
* Rotating file sink based on date. rotates at midnight
|
||||||
*/
|
*/
|
||||||
class daily_file_sink:public base_sink
|
template<class Mutex>
|
||||||
|
class daily_file_sink:public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit daily_file_sink(const std::string& base_filename,
|
explicit daily_file_sink(const std::string& base_filename,
|
||||||
@ -129,7 +134,6 @@ public:
|
|||||||
_base_filename(base_filename),
|
_base_filename(base_filename),
|
||||||
_extension(extension),
|
_extension(extension),
|
||||||
_midnight_tp (_calc_midnight_tp() ),
|
_midnight_tp (_calc_midnight_tp() ),
|
||||||
_mutex(),
|
|
||||||
_ofstream(_calc_filename(_base_filename, _extension), std::ofstream::binary|std::ofstream::app),
|
_ofstream(_calc_filename(_base_filename, _extension), std::ofstream::binary|std::ofstream::app),
|
||||||
_flush_helper(flush_every)
|
_flush_helper(flush_every)
|
||||||
{
|
{
|
||||||
@ -138,7 +142,6 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
void _sink_it(const details::log_msg& msg) override
|
void _sink_it(const details::log_msg& msg) override
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
if (std::chrono::system_clock::now() >= _midnight_tp)
|
if (std::chrono::system_clock::now() >= _midnight_tp)
|
||||||
{
|
{
|
||||||
_ofstream.close();
|
_ofstream.close();
|
||||||
@ -175,10 +178,12 @@ private:
|
|||||||
std::string _base_filename;
|
std::string _base_filename;
|
||||||
std::string _extension;
|
std::string _extension;
|
||||||
std::chrono::system_clock::time_point _midnight_tp;
|
std::chrono::system_clock::time_point _midnight_tp;
|
||||||
std::mutex _mutex;
|
|
||||||
std::ofstream _ofstream;
|
std::ofstream _ofstream;
|
||||||
details::file_flush_helper _flush_helper;
|
details::file_flush_helper _flush_helper;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef daily_file_sink<std::mutex> daily_file_sink_mt;
|
||||||
|
typedef daily_file_sink<details::null_mutex> daily_file_sink_st;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <mutex>
|
|
||||||
#include <memory>
|
|
||||||
#include "base_sink.h"
|
#include "base_sink.h"
|
||||||
|
|
||||||
namespace c11log {
|
namespace c11log {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
class null_sink : public base_sink
|
|
||||||
|
template <class Mutex>
|
||||||
|
class null_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
void _sink_it(const details::log_msg&) override
|
void _sink_it(const details::log_msg&) override
|
||||||
|
Loading…
Reference in New Issue
Block a user