feat add basic_file_sink
This commit is contained in:
parent
454b07debb
commit
cbb103fd02
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@ out/
|
|||||||
build/
|
build/
|
||||||
.cache/
|
.cache/
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
|
.gdb_history
|
||||||
|
@ -167,7 +167,7 @@ set(TILE_SRCS
|
|||||||
"tile/base/internal/thread_pool.cc"
|
"tile/base/internal/thread_pool.cc"
|
||||||
"tile/base/internal/time_keeper.cc"
|
"tile/base/internal/time_keeper.cc"
|
||||||
"tile/base/internal/time_keeper.h"
|
"tile/base/internal/time_keeper.h"
|
||||||
# logging
|
"tile/base/logging/basic_file_sink.cc"
|
||||||
"tile/base/logging/splitter_sink.cc"
|
"tile/base/logging/splitter_sink.cc"
|
||||||
"tile/base/logging/console_sink.cc"
|
"tile/base/logging/console_sink.cc"
|
||||||
"tile/base/net/endpoint.cc"
|
"tile/base/net/endpoint.cc"
|
||||||
|
@ -99,7 +99,7 @@ public:
|
|||||||
++g_num_messages[severity_];
|
++g_num_messages[severity_];
|
||||||
WaitForSinks();
|
WaitForSinks();
|
||||||
if (sink_) {
|
if (sink_) {
|
||||||
sink_->WaitTillSent();
|
sink_->Flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,7 +141,7 @@ private:
|
|||||||
|
|
||||||
void SendToSink() {
|
void SendToSink() {
|
||||||
if (sink_) {
|
if (sink_) {
|
||||||
sink_->send(severity_, fullname(), basename(), line(), time(),
|
sink_->Send(severity_, fullname(), basename(), line(), time(),
|
||||||
message_text_ + num_prefix_chars_,
|
message_text_ + num_prefix_chars_,
|
||||||
num_chars_to_log_ - num_prefix_chars_ - 1);
|
num_chars_to_log_ - num_prefix_chars_ - 1);
|
||||||
}
|
}
|
||||||
@ -278,14 +278,15 @@ void LogMessageTime::CalcGmtOffset() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
LogSink::~LogSink() {}
|
LogSink::~LogSink() {}
|
||||||
void LogSink::send(LogSeverity severity, const char *full_filename,
|
bool LogSink::ShouldLog(LogSeverity severity) { return true; }
|
||||||
|
void LogSink::Send(LogSeverity severity, const char *full_filename,
|
||||||
const char *base_filename, int line,
|
const char *base_filename, int line,
|
||||||
const LogMessageTime &logmsgtime, const char *message,
|
const LogMessageTime &logmsgtime, const char *message,
|
||||||
size_t message_len) {
|
size_t message_len) {
|
||||||
|
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
void LogSink::WaitTillSent() {}
|
void LogSink::Flush() {}
|
||||||
|
|
||||||
std::string LogSink::ToString(LogSeverity severity, const char *file,
|
std::string LogSink::ToString(LogSeverity severity, const char *file,
|
||||||
const char *base_filename, int line,
|
const char *base_filename, int line,
|
||||||
@ -356,14 +357,16 @@ void LogToSinks(LogSeverity severity, const char *full_filename,
|
|||||||
const char *message, size_t message_len) {
|
const char *message, size_t message_len) {
|
||||||
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) {
|
||||||
sink->send(severity, full_filename, base_filename, line, time, message,
|
if (sink->ShouldLog(severity)) {
|
||||||
|
sink->Send(severity, full_filename, base_filename, line, time, message,
|
||||||
message_len);
|
message_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
void WaitForSinks() {
|
void WaitForSinks() {
|
||||||
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) {
|
||||||
sink->WaitTillSent();
|
sink->Flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -700,16 +700,15 @@ public:
|
|||||||
using Ptr = std::shared_ptr<LogSink>;
|
using Ptr = std::shared_ptr<LogSink>;
|
||||||
|
|
||||||
virtual ~LogSink();
|
virtual ~LogSink();
|
||||||
virtual void send(LogSeverity severity, const char *full_filename,
|
virtual bool ShouldLog(LogSeverity severity);
|
||||||
|
virtual void Send(LogSeverity severity, const char *full_filename,
|
||||||
const char *base_filename, int line,
|
const char *base_filename, int line,
|
||||||
const LogMessageTime &logmsgtime, const char *message,
|
const LogMessageTime &logmsgtime, const char *message,
|
||||||
size_t message_len);
|
size_t message_len);
|
||||||
virtual void WaitTillSent();
|
virtual void Flush();
|
||||||
|
std::string ToString(LogSeverity severity, const char *file, int line,
|
||||||
virtual std::string ToString(LogSeverity severity, const char *file,
|
const LogMessageTime &logmsgtime, const char *message,
|
||||||
const char *base_filename, int line,
|
size_t message_len);
|
||||||
const LogMessageTime &logmsgtime,
|
|
||||||
const char *message, size_t message_len);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void ColoredWriteToStdout(LogSeverity severity, const char *message,
|
void ColoredWriteToStdout(LogSeverity severity, const char *message,
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
struct AwesomeLogSink : public tile::LogSink {
|
struct AwesomeLogSink : public tile::LogSink {
|
||||||
virtual void send(tile::LogSeverity severity, const char *full_filename,
|
virtual void Send(tile::LogSeverity severity, const char *full_filename,
|
||||||
const char *base_filename, int line,
|
const char *base_filename, int line,
|
||||||
const tile::LogMessageTime &logmsgtime, const char *message,
|
const tile::LogMessageTime &logmsgtime, const char *message,
|
||||||
size_t message_len) override {
|
size_t message_len) override {
|
||||||
|
19
tile/base/logging/basic_file_sink.cc
Normal file
19
tile/base/logging/basic_file_sink.cc
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include "tile/base/logging/basic_file_sink.h"
|
||||||
|
|
||||||
|
namespace tile {
|
||||||
|
BasicFileSink::BasicFileSink() {}
|
||||||
|
BasicFileSink::~BasicFileSink() {}
|
||||||
|
void BasicFileSink::Send(LogSeverity severity, const char *full_filename,
|
||||||
|
const char *base_filename, int line,
|
||||||
|
const LogMessageTime &logmsgtime, const char *message,
|
||||||
|
size_t message_len) {
|
||||||
|
TILE_CHECK(!filepath_.empty(), "filepath is empty");
|
||||||
|
ofs_ << message << std::endl;
|
||||||
|
}
|
||||||
|
std::string BasicFileSink::filepath() const { return filepath_; }
|
||||||
|
void BasicFileSink::set_filepath(const std::string &filepath) {
|
||||||
|
filepath_ = filepath;
|
||||||
|
ofs_.open(filepath, std::ios::out | std::ios::app);
|
||||||
|
}
|
||||||
|
void BasicFileSink::Flush() { ofs_.flush(); }
|
||||||
|
} // namespace tile
|
31
tile/base/logging/basic_file_sink.h
Normal file
31
tile/base/logging/basic_file_sink.h
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#ifndef TILE_BASE_LOGGING_BASIC_FILE_SINK_H
|
||||||
|
#define TILE_BASE_LOGGING_BASIC_FILE_SINK_H
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "tile/base/internal/logging.h"
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
namespace tile {
|
||||||
|
class BasicFileSink : public LogSink {
|
||||||
|
public:
|
||||||
|
BasicFileSink();
|
||||||
|
~BasicFileSink() override;
|
||||||
|
void Send(LogSeverity severity, const char *full_filename,
|
||||||
|
const char *base_filename, int line,
|
||||||
|
const LogMessageTime &logmsgtime, const char *message,
|
||||||
|
size_t message_len) override;
|
||||||
|
void Flush() override;
|
||||||
|
|
||||||
|
std::string filepath() const;
|
||||||
|
void set_filepath(const std::string &filepath);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string filepath_;
|
||||||
|
std::ofstream ofs_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace tile
|
||||||
|
|
||||||
|
#endif // TILE_BASE_LOGGING_BASIC_FILE_SINK_H
|
@ -17,7 +17,7 @@ std::shared_ptr<SplitterSink> SplitterSink::Create(
|
|||||||
|
|
||||||
SplitterSink::SplitterSink() {}
|
SplitterSink::SplitterSink() {}
|
||||||
SplitterSink::~SplitterSink() {}
|
SplitterSink::~SplitterSink() {}
|
||||||
void SplitterSink::send(LogSeverity severity, const char *full_filename,
|
void SplitterSink::Send(LogSeverity severity, const char *full_filename,
|
||||||
const char *base_filename, int line,
|
const char *base_filename, int line,
|
||||||
const LogMessageTime &logmsgtime, const char *message,
|
const LogMessageTime &logmsgtime, const char *message,
|
||||||
size_t message_len) {
|
size_t message_len) {
|
||||||
@ -25,18 +25,18 @@ void SplitterSink::send(LogSeverity severity, const char *full_filename,
|
|||||||
std::lock_guard<std::mutex> _(sinks_mutex_);
|
std::lock_guard<std::mutex> _(sinks_mutex_);
|
||||||
doing_ = true;
|
doing_ = true;
|
||||||
for (auto &sink : sinks_) {
|
for (auto &sink : sinks_) {
|
||||||
sink->send(severity, full_filename, base_filename, line, logmsgtime,
|
sink->Send(severity, full_filename, base_filename, line, logmsgtime,
|
||||||
message, message_len);
|
message, message_len);
|
||||||
}
|
}
|
||||||
doing_ = false;
|
doing_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SplitterSink::WaitTillSent() {
|
void SplitterSink::Flush() {
|
||||||
assert(!doing_ && "SplitterSink::send() should not be called recursively");
|
assert(!doing_ && "SplitterSink::send() should not be called recursively");
|
||||||
std::lock_guard<std::mutex> _(sinks_mutex_);
|
std::lock_guard<std::mutex> _(sinks_mutex_);
|
||||||
doing_ = true;
|
doing_ = true;
|
||||||
for (auto &sink : sinks_) {
|
for (auto &sink : sinks_) {
|
||||||
sink->WaitTillSent();
|
sink->Flush();
|
||||||
}
|
}
|
||||||
doing_ = false;
|
doing_ = false;
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
#ifndef TILE_BASE_LOGGING_SPLITTER_SINK_H
|
||||||
|
#define TILE_BASE_LOGGING_SPLITTER_SINK_H
|
||||||
|
|
||||||
|
#pragma once
|
||||||
#include "tile/base/internal/logging.h"
|
#include "tile/base/internal/logging.h"
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@ -12,11 +16,11 @@ public:
|
|||||||
|
|
||||||
~SplitterSink() override;
|
~SplitterSink() override;
|
||||||
|
|
||||||
void send(LogSeverity severity, const char *full_filename,
|
void Send(LogSeverity severity, const char *full_filename,
|
||||||
const char *base_filename, int line,
|
const char *base_filename, int line,
|
||||||
const LogMessageTime &logmsgtime, const char *message,
|
const LogMessageTime &logmsgtime, const char *message,
|
||||||
size_t message_len) override;
|
size_t message_len) override;
|
||||||
void WaitTillSent() override;
|
void Flush() override;
|
||||||
|
|
||||||
void AddSink(std::shared_ptr<LogSink> sink);
|
void AddSink(std::shared_ptr<LogSink> sink);
|
||||||
void RemoveSink(std::shared_ptr<LogSink> sink);
|
void RemoveSink(std::shared_ptr<LogSink> sink);
|
||||||
@ -32,3 +36,5 @@ private:
|
|||||||
bool doing_ = false;
|
bool doing_ = false;
|
||||||
};
|
};
|
||||||
} // namespace tile
|
} // namespace tile
|
||||||
|
|
||||||
|
#endif // TILE_BASE_LOGGING_SPLITTER_SINK_H
|
||||||
|
Loading…
Reference in New Issue
Block a user