diff --git a/tile/base/internal/logging.cc b/tile/base/internal/logging.cc index da17c4c..f14e07e 100644 --- a/tile/base/internal/logging.cc +++ b/tile/base/internal/logging.cc @@ -1,5 +1,6 @@ #include "tile/base/internal/logging.h" #include +#include #include #include #include @@ -9,6 +10,14 @@ namespace tile { namespace internal { 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 { std::vector * GetProviers() @@ -97,7 +106,12 @@ public: { 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'; char original_final_char = '\0'; @@ -108,11 +122,18 @@ public: message_text_[num_chars_to_log_] = '\0'; { - std::lock_guard _{g_log_mutex}; + std::lock_guard _(g_log_mutex); (this->*(send_method_))(); ++g_num_messages[severity_]; 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; } @@ -163,8 +184,10 @@ private: 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_); } - LogToSinks(severity_, fullname(), basename(), line(), time(), message_text_ + num_prefix_chars_, - num_chars_to_log_ - num_prefix_chars_ - 1); + // HACK: temp fix + 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() @@ -392,6 +415,11 @@ LogSink::ToString(LogSeverity severity, static void 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); fflush(output); } @@ -446,7 +474,12 @@ LogToSinks(LogSeverity severity, std::lock_guard _(g_sink_mutex); for (auto &&sink : g_sinks) { if (sink->ShouldLog(severity)) { - sink->Send(severity, full_filename, base_filename, line, time, message, message_len); + try { + 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() { std::lock_guard _(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 * diff --git a/tile/base/internal/logging.h b/tile/base/internal/logging.h index 5bcc717..ef9c6a5 100644 --- a/tile/base/internal/logging.h +++ b/tile/base/internal/logging.h @@ -610,6 +610,7 @@ public: using Ptr = std::shared_ptr; virtual ~LogSink(); + virtual const char *name() const = 0; virtual bool ShouldLog(LogSeverity severity); virtual void Send(LogSeverity severity, const char *full_filename, diff --git a/tile/base/internal/logging_test.cc b/tile/base/internal/logging_test.cc index 6592036..8c05296 100644 --- a/tile/base/internal/logging_test.cc +++ b/tile/base/internal/logging_test.cc @@ -11,13 +11,15 @@ struct AwesomeLogSink : public tile::LogSink { static std::shared_ptr Create() { return std::make_shared(); } - virtual void Send(tile::LogSeverity severity, - const char *full_filename, - const char *base_filename, - int line, - const tile::LogMessageTime &logmsgtime, - const char *message, - size_t message_len) override + const char *name() const override { return "AwesomeLogSink"; } + + void Send(tile::LogSeverity severity, + const char *full_filename, + const char *base_filename, + int line, + const tile::LogMessageTime &logmsgtime, + const char *message, + size_t message_len) override { msgs.emplace_back(std::string(message, message_len)); } diff --git a/tile/base/logging/basic_file_sink.h b/tile/base/logging/basic_file_sink.h index b0d4d18..03cf5f0 100644 --- a/tile/base/logging/basic_file_sink.h +++ b/tile/base/logging/basic_file_sink.h @@ -14,6 +14,9 @@ public: static Ptr Create(const std::string &filepath); ~BasicFileSink() override; + + const char *name() const override { return "BasicFileSink"; } + void Send(LogSeverity severity, const char *full_filename, const char *base_filename, diff --git a/tile/base/logging/console_sink.cc b/tile/base/logging/console_sink.cc index c1f353f..453c62e 100644 --- a/tile/base/logging/console_sink.cc +++ b/tile/base/logging/console_sink.cc @@ -22,12 +22,8 @@ ConsoleSink::Send(LogSeverity severity, size_t message_len) { auto msg = ToString(severity, full_filename, base_filename, line, logmsgtime, message, message_len); - while (!msg.empty() && msg.back() == '\n') { msg.pop_back(); } - if (severity >= TILE_FATAL) { - fprintf(stderr, "%s\n", msg.c_str()); - } else { - fprintf(stdout, "%s\n", msg.c_str()); - } + while (!msg.empty() && msg.back() != '\n') { msg.push_back('\n'); } + ColoredWriteToStdout(severity, msg.c_str(), msg.size()); } void diff --git a/tile/base/logging/console_sink.h b/tile/base/logging/console_sink.h index 73d7c39..10abf1f 100644 --- a/tile/base/logging/console_sink.h +++ b/tile/base/logging/console_sink.h @@ -11,6 +11,8 @@ public: using Ptr = std::shared_ptr; static Ptr Create(); + const char *name() const override { return "ConsoleSink"; } + ~ConsoleSink() override; void Send(LogSeverity severity, const char *full_filename, diff --git a/tile/base/logging/splitter_sink.h b/tile/base/logging/splitter_sink.h index 58b45e8..1cd814c 100644 --- a/tile/base/logging/splitter_sink.h +++ b/tile/base/logging/splitter_sink.h @@ -14,6 +14,8 @@ public: static Ptr Create(); static Ptr Create(std::initializer_list> init_list); + const char *name() const override { return "SplitterSink"; } + ~SplitterSink() override; void Send(LogSeverity severity, diff --git a/tile/base/logging/splitter_sink_test.cc b/tile/base/logging/splitter_sink_test.cc index 1728026..1e7e0af 100644 --- a/tile/base/logging/splitter_sink_test.cc +++ b/tile/base/logging/splitter_sink_test.cc @@ -8,6 +8,8 @@ public: static Ptr Create() { return std::shared_ptr(new SimpleSink()); } + const char *name() const override { return "SimpleSink"; } + void Send(LogSeverity severity, const char *full_filename, const char *base_filename,