mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 01:51:38 +08:00
support for color formatting
This commit is contained in:
parent
93d41b2c0e
commit
c8610d9a86
@ -82,6 +82,5 @@ int main(int argc, char *argv[])
|
||||
std::cout << "Delta = " << deltaf << " seconds" << std::endl;
|
||||
std::cout << "Rate = " << rate << "/sec" << std::endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -11,8 +11,8 @@
|
||||
|
||||
#define ELPP_THREAD_SAFE
|
||||
#define ELPP_EXPERIMENTAL_ASYNC
|
||||
#include "easylogging++.h"
|
||||
#include "easylogging++.cc"
|
||||
#include "easylogging++.h"
|
||||
INITIALIZE_EASYLOGGINGPP
|
||||
|
||||
using namespace std;
|
||||
|
@ -10,8 +10,8 @@
|
||||
#include <vector>
|
||||
|
||||
#define ELPP_THREAD_SAFE
|
||||
#include "easylogging++.h"
|
||||
#include "easylogging++.cc"
|
||||
#include "easylogging++.h"
|
||||
INITIALIZE_EASYLOGGINGPP
|
||||
|
||||
using namespace std;
|
||||
|
@ -6,8 +6,8 @@
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
#include "easylogging++.h"
|
||||
#include "easylogging++.cc"
|
||||
#include "easylogging++.h"
|
||||
INITIALIZE_EASYLOGGINGPP
|
||||
|
||||
int main(int, char *[])
|
||||
|
@ -13,7 +13,8 @@
|
||||
#include "g3log/logworker.hpp"
|
||||
|
||||
using namespace std;
|
||||
template <typename T> std::string format(const T &value);
|
||||
template<typename T>
|
||||
std::string format(const T &value);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -8,7 +8,6 @@
|
||||
|
||||
#include "glog/logging.h"
|
||||
|
||||
|
||||
int main(int, char *argv[])
|
||||
{
|
||||
using namespace std::chrono;
|
||||
|
@ -10,13 +10,13 @@
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "log4cplus/logger.h"
|
||||
#include "log4cplus/fileappender.h"
|
||||
#include "log4cplus/layout.h"
|
||||
#include "log4cplus/ndc.h"
|
||||
#include "log4cplus/helpers/loglog.h"
|
||||
#include "log4cplus/helpers/property.h"
|
||||
#include "log4cplus/layout.h"
|
||||
#include "log4cplus/logger.h"
|
||||
#include "log4cplus/loggingmacros.h"
|
||||
#include "log4cplus/ndc.h"
|
||||
|
||||
using namespace log4cplus;
|
||||
|
||||
@ -32,9 +32,7 @@ int main(int argc, char * argv[])
|
||||
int howmany = 1000000;
|
||||
|
||||
log4cplus::initialize();
|
||||
SharedFileAppenderPtr append(
|
||||
new FileAppender(LOG4CPLUS_TEXT("logs/log4cplus-bench-mt.log"), std::ios_base::trunc,
|
||||
true, true));
|
||||
SharedFileAppenderPtr append(new FileAppender(LOG4CPLUS_TEXT("logs/log4cplus-bench-mt.log"), std::ios_base::trunc, true, true));
|
||||
append->setName(LOG4CPLUS_TEXT("File"));
|
||||
|
||||
log4cplus::tstring pattern = LOG4CPLUS_TEXT("%d{%Y-%m-%d %H:%M:%S.%Q}: %p - %m %n");
|
||||
|
@ -7,13 +7,13 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "log4cplus/logger.h"
|
||||
#include "log4cplus/fileappender.h"
|
||||
#include "log4cplus/layout.h"
|
||||
#include "log4cplus/ndc.h"
|
||||
#include "log4cplus/helpers/loglog.h"
|
||||
#include "log4cplus/helpers/property.h"
|
||||
#include "log4cplus/layout.h"
|
||||
#include "log4cplus/logger.h"
|
||||
#include "log4cplus/loggingmacros.h"
|
||||
#include "log4cplus/ndc.h"
|
||||
|
||||
using namespace log4cplus;
|
||||
|
||||
@ -25,9 +25,7 @@ int main(int, char *[])
|
||||
int howmany = 1000000;
|
||||
|
||||
log4cplus::initialize();
|
||||
SharedFileAppenderPtr append(
|
||||
new FileAppender(LOG4CPLUS_TEXT("logs/log4cplus-bench.log"), std::ios_base::trunc,
|
||||
true, true));
|
||||
SharedFileAppenderPtr append(new FileAppender(LOG4CPLUS_TEXT("logs/log4cplus-bench.log"), std::ios_base::trunc, true, true));
|
||||
append->setName(LOG4CPLUS_TEXT("File"));
|
||||
|
||||
log4cplus::tstring pattern = LOG4CPLUS_TEXT("%d{%Y-%m-%d %H:%M:%S.%Q}: %p - %m %n");
|
||||
|
@ -10,13 +10,13 @@
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "log4cpp/Category.hh"
|
||||
#include "log4cpp/Appender.hh"
|
||||
#include "log4cpp/BasicLayout.hh"
|
||||
#include "log4cpp/Category.hh"
|
||||
#include "log4cpp/FileAppender.hh"
|
||||
#include "log4cpp/Layout.hh"
|
||||
#include "log4cpp/BasicLayout.hh"
|
||||
#include "log4cpp/Priority.hh"
|
||||
#include "log4cpp/PatternLayout.hh"
|
||||
#include "log4cpp/Priority.hh"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -7,13 +7,13 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "log4cpp/Category.hh"
|
||||
#include "log4cpp/Appender.hh"
|
||||
#include "log4cpp/BasicLayout.hh"
|
||||
#include "log4cpp/Category.hh"
|
||||
#include "log4cpp/FileAppender.hh"
|
||||
#include "log4cpp/Layout.hh"
|
||||
#include "log4cpp/BasicLayout.hh"
|
||||
#include "log4cpp/Priority.hh"
|
||||
#include "log4cpp/PatternLayout.hh"
|
||||
#include "log4cpp/Priority.hh"
|
||||
|
||||
int main(int, char *[])
|
||||
{
|
||||
|
@ -5,15 +5,14 @@
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
#include "P7_Trace.h"
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
using namespace std::chrono;
|
||||
@ -29,8 +28,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
// create P7 client object
|
||||
std::unique_ptr<IP7_Client, std::function<void(IP7_Client *)>> client(
|
||||
P7_Create_Client(TM("/P7.Pool=1024 /P7.Sink=FileTxt /P7.Dir=logs/p7-bench-mt")),
|
||||
[&](IP7_Client *ptr){
|
||||
P7_Create_Client(TM("/P7.Pool=1024 /P7.Sink=FileTxt /P7.Dir=logs/p7-bench-mt")), [&](IP7_Client *ptr) {
|
||||
if (ptr)
|
||||
ptr->Release();
|
||||
});
|
||||
@ -43,8 +41,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
// create P7 trace object 1
|
||||
std::unique_ptr<IP7_Trace, std::function<void(IP7_Trace *)>> trace(
|
||||
P7_Create_Trace(client.get(), TM("Trace channel 1")),
|
||||
[&](IP7_Trace *ptr){
|
||||
P7_Create_Trace(client.get(), TM("Trace channel 1")), [&](IP7_Trace *ptr) {
|
||||
if (ptr)
|
||||
ptr->Release();
|
||||
});
|
||||
|
@ -4,13 +4,12 @@
|
||||
//
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
#include "P7_Trace.h"
|
||||
|
||||
|
||||
int main(int, char *[])
|
||||
{
|
||||
using namespace std::chrono;
|
||||
@ -22,8 +21,7 @@ int main(int, char *[])
|
||||
|
||||
// create P7 client object
|
||||
std::unique_ptr<IP7_Client, std::function<void(IP7_Client *)>> client(
|
||||
P7_Create_Client(TM("/P7.Pool=1024 /P7.Sink=FileTxt /P7.Dir=logs/p7-bench")),
|
||||
[&](IP7_Client *ptr){
|
||||
P7_Create_Client(TM("/P7.Pool=1024 /P7.Sink=FileTxt /P7.Dir=logs/p7-bench")), [&](IP7_Client *ptr) {
|
||||
if (ptr)
|
||||
ptr->Release();
|
||||
});
|
||||
@ -36,8 +34,7 @@ int main(int, char *[])
|
||||
|
||||
// create P7 trace object 1
|
||||
std::unique_ptr<IP7_Trace, std::function<void(IP7_Trace *)>> trace(
|
||||
P7_Create_Trace(client.get(), TM("Trace channel 1")),
|
||||
[&](IP7_Trace *ptr){
|
||||
P7_Create_Trace(client.get(), TM("Trace channel 1")), [&](IP7_Trace *ptr) {
|
||||
if (ptr)
|
||||
ptr->Release();
|
||||
});
|
||||
@ -55,7 +52,6 @@ int main(int, char *[])
|
||||
for (int i = 0; i < howmany; ++i)
|
||||
trace->P7_INFO(module, TM("p7 message #%d: This is some text for your pleasure"), i);
|
||||
|
||||
|
||||
duration<float> delta = clock::now() - start;
|
||||
float deltaf = delta.count();
|
||||
auto rate = howmany / deltaf;
|
||||
|
@ -5,8 +5,8 @@
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
|
@ -5,8 +5,8 @@
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
|
@ -40,6 +40,9 @@ struct log_msg
|
||||
fmt::MemoryWriter raw;
|
||||
fmt::MemoryWriter formatted;
|
||||
size_t msg_id{0};
|
||||
// wrap this range with color codes
|
||||
size_t color_range_start{0};
|
||||
size_t color_range_end{0};
|
||||
};
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
||||
|
@ -421,7 +421,24 @@ private:
|
||||
std::string _str;
|
||||
};
|
||||
|
||||
// set the color range. expect it to be in the form of "%^colored text%$"
|
||||
class start_color_formatter SPDLOG_FINAL : public flag_formatter
|
||||
{
|
||||
void format(details::log_msg &msg, const std::tm &) override
|
||||
{
|
||||
msg.color_range_start = msg.formatted.size();
|
||||
}
|
||||
};
|
||||
class stop_color_formatter SPDLOG_FINAL : public flag_formatter
|
||||
{
|
||||
void format(details::log_msg &msg, const std::tm &) override
|
||||
{
|
||||
msg.color_range_end = msg.formatted.size();
|
||||
}
|
||||
};
|
||||
|
||||
// Full info formatter
|
||||
|
||||
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
|
||||
class full_formatter SPDLOG_FINAL : public flag_formatter
|
||||
{
|
||||
@ -462,8 +479,13 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
|
||||
msg.formatted << '[' << *msg.logger_name << "] ";
|
||||
#endif
|
||||
|
||||
msg.formatted << '[' << level::to_str(msg.level) << "] ";
|
||||
const char *level_name = level::to_str(msg.level);
|
||||
size_t level_name_size = strlen(level_name);
|
||||
msg.formatted << '[' << fmt::StringRef(level_name, level_name_size) << "] ";
|
||||
msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
|
||||
// wrap the level with color
|
||||
msg.color_range_start = 37;
|
||||
msg.color_range_end = 37 + level_name_size;
|
||||
}
|
||||
};
|
||||
|
||||
@ -491,6 +513,7 @@ inline void spdlog::pattern_formatter::compile_pattern(const std::string &patter
|
||||
{
|
||||
_formatters.push_back(std::move(user_chars));
|
||||
}
|
||||
// if(
|
||||
if (++it != end)
|
||||
{
|
||||
handle_flag(*it);
|
||||
@ -644,6 +667,12 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
|
||||
case ('i'):
|
||||
_formatters.emplace_back(new details::i_formatter());
|
||||
break;
|
||||
case ('^'):
|
||||
_formatters.emplace_back(new details::start_color_formatter());
|
||||
break;
|
||||
case ('$'):
|
||||
_formatters.emplace_back(new details::stop_color_formatter());
|
||||
break;
|
||||
|
||||
default: // Unknown flag appears as is
|
||||
_formatters.emplace_back(new details::ch_formatter('%'));
|
||||
|
@ -28,9 +28,9 @@ public:
|
||||
: target_file_(file)
|
||||
{
|
||||
should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal();
|
||||
colors_[level::trace] = cyan;
|
||||
colors_[level::trace] = magenta;
|
||||
colors_[level::debug] = cyan;
|
||||
colors_[level::info] = reset;
|
||||
colors_[level::info] = green;
|
||||
colors_[level::warn] = yellow + bold;
|
||||
colors_[level::err] = red + bold;
|
||||
colors_[level::critical] = bold + on_red;
|
||||
@ -83,17 +83,20 @@ protected:
|
||||
{
|
||||
// Wrap the originally formatted message in color codes.
|
||||
// If color is not supported in the terminal, log as is instead.
|
||||
if (should_do_colors_)
|
||||
if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
|
||||
{
|
||||
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_);
|
||||
fwrite(clear_line.data(), sizeof(char), clear_line.size(), target_file_);
|
||||
// before color range
|
||||
_print_range(msg, 0, msg.color_range_start);
|
||||
// in color range
|
||||
_print_ccode(colors_[msg.level]);
|
||||
_print_range(msg, msg.color_range_start, msg.color_range_end);
|
||||
_print_ccode(reset);
|
||||
// after color range
|
||||
_print_range(msg, msg.color_range_end, msg.formatted.size());
|
||||
}
|
||||
else
|
||||
{
|
||||
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), target_file_);
|
||||
_print_range(msg, 0, msg.formatted.size());
|
||||
}
|
||||
_flush();
|
||||
}
|
||||
@ -103,6 +106,15 @@ protected:
|
||||
fflush(target_file_);
|
||||
}
|
||||
|
||||
private:
|
||||
void _print_ccode(const std::string &color_code)
|
||||
{
|
||||
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
|
||||
}
|
||||
void _print_range(const details::log_msg &msg, size_t start, size_t end)
|
||||
{
|
||||
fwrite(msg.formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||
}
|
||||
FILE *target_file_;
|
||||
bool should_do_colors_;
|
||||
std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
|
||||
|
Loading…
Reference in New Issue
Block a user