feat add console sink
Some checks failed
android / build (push) Failing after 22s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Debug) (push) Failing after 6m40s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Release) (push) Failing after 7m41s
linux-arm-gcc / linux-gcc-arm (Debug) (push) Failing after 7m11s
linux-arm-gcc / linux-gcc-arm (Release) (push) Failing after 7m27s
linux-arm-gcc / linux-gcc-armhf (Debug) (push) Failing after 7m20s
linux-arm-gcc / linux-gcc-armhf (Release) (push) Failing after 7m42s
linux-mips-gcc / linux-gcc-mipsel (Debug) (push) Failing after 7m42s
linux-mips-gcc / linux-gcc-mipsel (Release) (push) Failing after 7m54s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Failing after 7m50s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Failing after 8m2s
linux-riscv64-gcc / linux-gcc-riscv64 (Debug) (push) Failing after 8m38s
linux-riscv64-gcc / linux-gcc-riscv64 (Release) (push) Failing after 7m56s
android / build (pull_request) Failing after 41s
linux-x64-clang / linux-clang (Debug) (push) Failing after 33s
linux-x64-clang / linux-clang (Release) (push) Failing after 33s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Debug) (pull_request) Failing after 6m52s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Release) (pull_request) Failing after 8m22s
linux-x64-gcc / linux-gcc (Debug) (push) Failing after 9m52s
linux-x64-gcc / linux-gcc (Release) (push) Failing after 3h6m9s
linux-arm-gcc / linux-gcc-arm (Debug) (pull_request) Failing after 6m57s
linux-arm-gcc / linux-gcc-arm (Release) (pull_request) Failing after 7m25s
linux-arm-gcc / linux-gcc-armhf (Debug) (pull_request) Failing after 7m0s
linux-arm-gcc / linux-gcc-armhf (Release) (pull_request) Failing after 7m29s
linux-x86-gcc / linux-gcc (Debug) (push) Failing after 10m0s
linux-mips-gcc / linux-gcc-mipsel (Debug) (pull_request) Failing after 7m52s
linux-x86-gcc / linux-gcc (Release) (push) Failing after 3h12m8s
linux-mips-gcc / linux-gcc-mipsel (Release) (pull_request) Failing after 8m12s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (pull_request) Failing after 8m13s
linux-mips64-gcc / linux-gcc-mips64el (Release) (pull_request) Failing after 8m20s
linux-riscv64-gcc / linux-gcc-riscv64 (Debug) (pull_request) Failing after 9m15s
linux-riscv64-gcc / linux-gcc-riscv64 (Release) (pull_request) Failing after 7m37s
linux-x64-clang / linux-clang (Debug) (pull_request) Failing after 32s
linux-x64-clang / linux-clang (Release) (pull_request) Failing after 32s
linux-x64-gcc / linux-gcc (Debug) (pull_request) Failing after 10m13s
linux-x86-gcc / linux-gcc (Debug) (pull_request) Failing after 10m8s
linux-x64-gcc / linux-gcc (Release) (pull_request) Failing after 3h11m8s
linux-x86-gcc / linux-gcc (Release) (pull_request) Failing after 3h10m59s

This commit is contained in:
tqcq 2024-10-06 15:25:22 +08:00
parent 6f68f497f4
commit 7c4395e897
8 changed files with 68 additions and 20 deletions

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,