Commit 095cb1f5 authored by gabime's avatar gabime

Added _flush() to base_sink

parent e215758b
...@@ -42,10 +42,7 @@ public: ...@@ -42,10 +42,7 @@ public:
flush(); flush();
} }
void flush() override
{
fflush(target_file_);
}
protected: protected:
virtual void _sink_it(const details::log_msg& msg) override virtual void _sink_it(const details::log_msg& msg) override
...@@ -66,6 +63,11 @@ protected: ...@@ -66,6 +63,11 @@ protected:
} }
flush(); flush();
} }
void _flush() override
{
fflush(target_file_);
}
FILE* target_file_; FILE* target_file_;
bool should_do_colors_; bool should_do_colors_;
std::map<level::level_enum, std::string> colors_; std::map<level::level_enum, std::string> colors_;
......
...@@ -36,9 +36,14 @@ public: ...@@ -36,9 +36,14 @@ public:
std::lock_guard<Mutex> lock(_mutex); std::lock_guard<Mutex> lock(_mutex);
_sink_it(msg); _sink_it(msg);
} }
void flush() SPDLOG_FINAL override
{
_flush();
}
protected: 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; Mutex _mutex;
}; };
} }
......
...@@ -44,13 +44,15 @@ protected: ...@@ -44,13 +44,15 @@ protected:
} }
} }
void _flush() override
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
for (auto &sink : _sinks)
sink->flush();
}
public: public:
void flush() override
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
for (auto &sink : _sinks)
sink->flush();
}
void add_sink(std::shared_ptr<sink> sink) void add_sink(std::shared_ptr<sink> sink)
{ {
......
...@@ -33,10 +33,7 @@ public: ...@@ -33,10 +33,7 @@ public:
{ {
_file_helper.open(filename, truncate); _file_helper.open(filename, truncate);
} }
void flush() override
{
_file_helper.flush();
}
void set_force_flush(bool force_flush) void set_force_flush(bool force_flush)
{ {
_force_flush = force_flush; _force_flush = force_flush;
...@@ -49,6 +46,10 @@ protected: ...@@ -49,6 +46,10 @@ protected:
if(_force_flush) if(_force_flush)
_file_helper.flush(); _file_helper.flush();
} }
void _flush() override
{
_file_helper.flush();
}
private: private:
details::file_helper _file_helper; details::file_helper _file_helper;
bool _force_flush; bool _force_flush;
...@@ -76,11 +77,7 @@ public: ...@@ -76,11 +77,7 @@ public:
_current_size = _file_helper.size(); //expensive. called only once _current_size = _file_helper.size(); //expensive. called only once
} }
void flush() override
{
_file_helper.flush();
}
protected: protected:
void _sink_it(const details::log_msg& msg) override void _sink_it(const details::log_msg& msg) override
{ {
...@@ -93,6 +90,11 @@ protected: ...@@ -93,6 +90,11 @@ protected:
_file_helper.write(msg); _file_helper.write(msg);
} }
void _flush() override
{
_file_helper.flush();
}
private: private:
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)
{ {
...@@ -194,10 +196,6 @@ public: ...@@ -194,10 +196,6 @@ public:
_file_helper.open(FileNameCalc::calc_filename(_base_filename)); _file_helper.open(FileNameCalc::calc_filename(_base_filename));
} }
void flush() override
{
_file_helper.flush();
}
protected: protected:
void _sink_it(const details::log_msg& msg) override void _sink_it(const details::log_msg& msg) override
...@@ -210,6 +208,11 @@ protected: ...@@ -210,6 +208,11 @@ protected:
_file_helper.write(msg); _file_helper.write(msg);
} }
void _flush() override
{
_file_helper.flush();
}
private: private:
std::chrono::system_clock::time_point _next_rotation_tp() std::chrono::system_clock::time_point _next_rotation_tp()
{ {
......
...@@ -30,15 +30,16 @@ public: ...@@ -30,15 +30,16 @@ public:
{ {
} }
void flush() override
{
}
protected: protected:
void _sink_it(const details::log_msg& msg) override void _sink_it(const details::log_msg& msg) override
{ {
OutputDebugStringA(msg.formatted.c_str()); OutputDebugStringA(msg.formatted.c_str());
} }
void _flush() override
{}
}; };
typedef msvc_sink<std::mutex> msvc_sink_mt; typedef msvc_sink<std::mutex> msvc_sink_mt;
......
...@@ -22,7 +22,7 @@ protected: ...@@ -22,7 +22,7 @@ protected:
void _sink_it(const details::log_msg&) override void _sink_it(const details::log_msg&) override
{} {}
void flush() override void _flush() override
{} {}
}; };
......
...@@ -29,14 +29,14 @@ public: ...@@ -29,14 +29,14 @@ public:
static std::shared_ptr<MyType> instance = std::make_shared<MyType>(); static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
return instance; return instance;
} }
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); fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stdout);
flush(); flush();
} }
void flush() override void _flush() override
{ {
fflush(stdout); fflush(stdout);
} }
...@@ -58,14 +58,14 @@ public: ...@@ -58,14 +58,14 @@ public:
static std::shared_ptr<MyType> instance = std::make_shared<MyType>(); static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
return instance; return instance;
} }
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); fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stderr);
flush(); flush();
} }
void flush() override void _flush() override
{ {
fflush(stderr); fflush(stderr);
} }
......
...@@ -50,6 +50,7 @@ public: ...@@ -50,6 +50,7 @@ public:
wincolor_sink(const wincolor_sink& other) = delete; wincolor_sink(const wincolor_sink& other) = delete;
wincolor_sink& operator=(const wincolor_sink& other) = delete; wincolor_sink& operator=(const wincolor_sink& other) = delete;
protected:
virtual void _sink_it(const details::log_msg& msg) override virtual void _sink_it(const details::log_msg& msg) override
{ {
auto color = colors_[msg.level]; auto color = colors_[msg.level];
...@@ -58,7 +59,7 @@ public: ...@@ -58,7 +59,7 @@ public:
SetConsoleTextAttribute(out_handle_, orig_attribs); //reset to orig colors SetConsoleTextAttribute(out_handle_, orig_attribs); //reset to orig colors
} }
virtual void flush() override virtual void _flush() override
{ {
// windows console always flushed? // windows console always flushed?
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment