feat/update_config #10

Merged
tqcq merged 26 commits from feat/update_config into master 2024-10-14 10:15:01 +08:00
8 changed files with 68 additions and 20 deletions
Showing only changes of commit 7c4395e897 - Show all commits

View File

@ -1,5 +1,6 @@
#include "tile/base/internal/logging.h" #include "tile/base/internal/logging.h"
#include <cassert> #include <cassert>
#include <cstdio>
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <mutex> #include <mutex>
@ -9,6 +10,14 @@ namespace tile {
namespace internal { namespace internal {
namespace logging { namespace logging {
static const char *LOG_CONST_TABLE[][3] = {
{"\033[46;37m", "\033[36m", "I"},
{"\033[43;37m", "\033[33m", "W"},
{"\033[41;37m", "\033[31m", "E"},
{"\033[44;37m", "\033[34m", "T"},
{"\033[42;37m", "\033[32m", "D"},
};
namespace { namespace {
std::vector<PrefixAppender *> * std::vector<PrefixAppender *> *
GetProviers() GetProviers()
@ -98,6 +107,11 @@ public:
if (has_been_flushed_) { return; } if (has_been_flushed_) { return; }
num_chars_to_log_ = stream_.pcount(); num_chars_to_log_ = stream_.pcount();
if (num_chars_to_log_ == 0) {
message_text_[0] = '\n';
++num_chars_to_log_;
}
const bool append_newline = message_text_[num_chars_to_log_ - 1] != '\n'; const bool append_newline = message_text_[num_chars_to_log_ - 1] != '\n';
char original_final_char = '\0'; char original_final_char = '\0';
@ -108,11 +122,18 @@ public:
message_text_[num_chars_to_log_] = '\0'; message_text_[num_chars_to_log_] = '\0';
{ {
std::lock_guard<std::mutex> _{g_log_mutex}; std::lock_guard<std::mutex> _(g_log_mutex);
(this->*(send_method_))(); (this->*(send_method_))();
++g_num_messages[severity_]; ++g_num_messages[severity_];
WaitForSinks(); WaitForSinks();
if (sink_) { sink_->Flush(); } if (sink_) {
try {
sink_->Flush();
} catch (const std::exception &e) {
fprintf(stderr, "\nLogSink[name=%s], reason=%s\n", sink_->name(), e.what());
assert(false);
}
}
} }
if (append_newline) { message_text_[num_chars_to_log_ - 1] = original_final_char; } if (append_newline) { message_text_[num_chars_to_log_ - 1] = original_final_char; }
@ -163,8 +184,10 @@ private:
if (g_sink_count.load(std::memory_order_relaxed) == 0) { if (g_sink_count.load(std::memory_order_relaxed) == 0) {
ColoredWriteToStdout(severity_, message_text_ + num_prefix_chars_, num_chars_to_log_ - num_chars_to_log_); ColoredWriteToStdout(severity_, message_text_ + num_prefix_chars_, num_chars_to_log_ - num_chars_to_log_);
} }
LogToSinks(severity_, fullname(), basename(), line(), time(), message_text_ + num_prefix_chars_, // HACK: temp fix
num_chars_to_log_ - num_prefix_chars_ - 1); auto msg_len = num_chars_to_log_ - num_prefix_chars_ - 1;
msg_len = msg_len > (LogMessage::kMaxLogMessageLen + 1) ? 0 : msg_len;
LogToSinks(severity_, fullname(), basename(), line(), time(), message_text_ + num_prefix_chars_, msg_len);
} }
void SendToSinkAndLog() void SendToSinkAndLog()
@ -392,6 +415,11 @@ LogSink::ToString(LogSeverity severity,
static void static void
ColoredWriteToStderrOrStdout(FILE *output, LogSeverity severity, const char *message, size_t len) ColoredWriteToStderrOrStdout(FILE *output, LogSeverity severity, const char *message, size_t len)
{ {
char buf[30];
{
int n = snprintf(buf, 30, "%s", internal::logging::LOG_CONST_TABLE[severity][1]);
fwrite(buf, n, 1, output);
}
fwrite(message, len, 1, output); fwrite(message, len, 1, output);
fflush(output); fflush(output);
} }
@ -446,7 +474,12 @@ LogToSinks(LogSeverity severity,
std::lock_guard<std::mutex> _(g_sink_mutex); std::lock_guard<std::mutex> _(g_sink_mutex);
for (auto &&sink : g_sinks) { for (auto &&sink : g_sinks) {
if (sink->ShouldLog(severity)) { if (sink->ShouldLog(severity)) {
try {
sink->Send(severity, full_filename, base_filename, line, time, message, message_len); sink->Send(severity, full_filename, base_filename, line, time, message, message_len);
} catch (const std::exception &e) {
fprintf(stderr, "\nLogSink[name=%s], reason=%s\n", sink->name(), e.what());
assert(false);
}
} }
} }
} }
@ -455,7 +488,14 @@ void
WaitForSinks() WaitForSinks()
{ {
std::lock_guard<std::mutex> _(g_sink_mutex); std::lock_guard<std::mutex> _(g_sink_mutex);
for (auto &&sink : g_sinks) { sink->Flush(); } for (auto &&sink : g_sinks) {
try {
sink->Flush();
} catch (const std::exception &e) {
fprintf(stderr, "\nLogSink[name=%s], reason=%s\n", sink->name(), e.what());
assert(false);
}
}
} }
const char * const char *

View File

@ -610,6 +610,7 @@ public:
using Ptr = std::shared_ptr<LogSink>; using Ptr = std::shared_ptr<LogSink>;
virtual ~LogSink(); virtual ~LogSink();
virtual const char *name() const = 0;
virtual bool ShouldLog(LogSeverity severity); virtual bool ShouldLog(LogSeverity severity);
virtual void Send(LogSeverity severity, virtual void Send(LogSeverity severity,
const char *full_filename, const char *full_filename,

View File

@ -11,7 +11,9 @@
struct AwesomeLogSink : public tile::LogSink { struct AwesomeLogSink : public tile::LogSink {
static std::shared_ptr<AwesomeLogSink> Create() { return std::make_shared<AwesomeLogSink>(); } static std::shared_ptr<AwesomeLogSink> Create() { return std::make_shared<AwesomeLogSink>(); }
virtual void Send(tile::LogSeverity severity, const char *name() const override { return "AwesomeLogSink"; }
void Send(tile::LogSeverity severity,
const char *full_filename, const char *full_filename,
const char *base_filename, const char *base_filename,
int line, int line,

View File

@ -14,6 +14,9 @@ public:
static Ptr Create(const std::string &filepath); static Ptr Create(const std::string &filepath);
~BasicFileSink() override; ~BasicFileSink() override;
const char *name() const override { return "BasicFileSink"; }
void Send(LogSeverity severity, void Send(LogSeverity severity,
const char *full_filename, const char *full_filename,
const char *base_filename, const char *base_filename,

View File

@ -22,12 +22,8 @@ ConsoleSink::Send(LogSeverity severity,
size_t message_len) size_t message_len)
{ {
auto msg = ToString(severity, full_filename, base_filename, line, logmsgtime, message, message_len); auto msg = ToString(severity, full_filename, base_filename, line, logmsgtime, message, message_len);
while (!msg.empty() && msg.back() == '\n') { msg.pop_back(); } while (!msg.empty() && msg.back() != '\n') { msg.push_back('\n'); }
if (severity >= TILE_FATAL) { ColoredWriteToStdout(severity, msg.c_str(), msg.size());
fprintf(stderr, "%s\n", msg.c_str());
} else {
fprintf(stdout, "%s\n", msg.c_str());
}
} }
void void

View File

@ -11,6 +11,8 @@ public:
using Ptr = std::shared_ptr<ConsoleSink>; using Ptr = std::shared_ptr<ConsoleSink>;
static Ptr Create(); static Ptr Create();
const char *name() const override { return "ConsoleSink"; }
~ConsoleSink() override; ~ConsoleSink() override;
void Send(LogSeverity severity, void Send(LogSeverity severity,
const char *full_filename, const char *full_filename,

View File

@ -14,6 +14,8 @@ public:
static Ptr Create(); static Ptr Create();
static Ptr Create(std::initializer_list<std::shared_ptr<LogSink>> init_list); static Ptr Create(std::initializer_list<std::shared_ptr<LogSink>> init_list);
const char *name() const override { return "SplitterSink"; }
~SplitterSink() override; ~SplitterSink() override;
void Send(LogSeverity severity, void Send(LogSeverity severity,

View File

@ -8,6 +8,8 @@ public:
static Ptr Create() { return std::shared_ptr<SimpleSink>(new SimpleSink()); } static Ptr Create() { return std::shared_ptr<SimpleSink>(new SimpleSink()); }
const char *name() const override { return "SimpleSink"; }
void Send(LogSeverity severity, void Send(LogSeverity severity,
const char *full_filename, const char *full_filename,
const char *base_filename, const char *base_filename,