Skip to content

Commit

Permalink
Make loglines not overlapping
Browse files Browse the repository at this point in the history
  • Loading branch information
badaix committed Apr 21, 2020
1 parent 58d5242 commit 7830809
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions include/aixlog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/ _\ ( )( \/ )( ) / \ / __)
/ \ )( ) ( / (_/\( O )( (_ \
\_/\_/(__)(_/\_)\____/ \__/ \___/
version 1.2.5
version 1.3.0
https://github.com/badaix/aixlog
This file is part of aixlog
Expand Down Expand Up @@ -40,6 +40,8 @@
#include <iostream>
#include <memory>
#include <mutex>
#include <map>
#include <thread>
#include <sstream>
#include <vector>

Expand Down Expand Up @@ -526,7 +528,7 @@ class Log : public std::basic_streambuf<char, std::char_traits<char>>
}

protected:
Log() noexcept
Log() noexcept : last_id_(-1), last_buffer_(nullptr)
{
std::clog.rdbuf(this);
std::clog << Severity() << Type::normal << Tag() << Function() << Conditional() << AixLog::Color::NONE << std::flush;
Expand All @@ -540,19 +542,19 @@ class Log : public std::basic_streambuf<char, std::char_traits<char>>
int sync() override
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (!buffer_.str().empty())
if (!get_stream().str().empty())
{
if (conditional_.is_true())
{
for (const auto& sink : log_sinks_)
{
if ((metadata_.type == Type::all) || (sink->get_type() == Type::all) || (metadata_.type == sink->get_type()))
if (metadata_.severity >= sink->severity)
sink->log(metadata_, buffer_.str());
sink->log(metadata_, get_stream().str());
}
}
buffer_.str("");
buffer_.clear();
get_stream().str("");
get_stream().clear();
}

return 0;
Expand All @@ -566,7 +568,7 @@ class Log : public std::basic_streambuf<char, std::char_traits<char>>
if (c == '\n')
sync();
else
buffer_ << static_cast<char>(c);
get_stream() << static_cast<char>(c);
}
else
{
Expand All @@ -583,7 +585,20 @@ class Log : public std::basic_streambuf<char, std::char_traits<char>>
friend std::ostream& operator<<(std::ostream& os, const Function& function);
friend std::ostream& operator<<(std::ostream& os, const Conditional& conditional);

std::stringstream buffer_;
std::stringstream& get_stream()
{
auto id = std::this_thread::get_id();
if ((last_buffer_ == nullptr) || (last_id_ != id))
{
last_id_ = id;
last_buffer_ = &(buffer_[id]);
}
return *last_buffer_;
}

std::map<std::thread::id, std::stringstream> buffer_;
std::thread::id last_id_;
std::stringstream* last_buffer_ = nullptr;
Metadata metadata_;
Conditional conditional_;
std::vector<log_sink_ptr> log_sinks_;
Expand Down Expand Up @@ -628,6 +643,14 @@ struct SinkFormat : public Sink
if (pos != std::string::npos)
result.replace(pos, 9, Log::to_string(metadata.severity));

pos = result.find("#color_severity");
if (pos != std::string::npos)
{
std::stringstream ss;
ss << TextColor(Color::RED) << Log::to_string(metadata.severity) << TextColor(Color::NONE);
result.replace(pos, 15, ss.str());
}

pos = result.find("#tag_func");
if (pos != std::string::npos)
result.replace(pos, 9, metadata.tag ? metadata.tag.text : (metadata.function ? metadata.function.name : "log"));
Expand Down

0 comments on commit 7830809

Please sign in to comment.