| 1 |
#include <pbcopper/logging/Logging.h> |
| 2 |
|
| 3 |
#include <pbcopper/utility/Unused.h> |
| 4 |
|
| 5 |
#include <chrono> |
| 6 |
#include <iostream> |
| 7 |
#include <ostream> |
| 8 |
|
| 9 |
#include <cassert> |
| 10 |
#include <cstddef> |
| 11 |
#include <ctime> |
| 12 |
|
| 13 |
namespace PacBio { |
| 14 |
namespace Logging { |
| 15 |
|
| 16 |
// --------- |
| 17 |
// Logger |
| 18 |
// --------- |
| 19 |
|
| 20 |
Logger::Logger(std::ostream& out, const LogLevel level) : Logger{out, LogConfig{level}} {} |
| 21 |
|
| 22 |
Logger::Logger(const std::filesystem::path& filename, const LogLevel level) |
| 23 |
: Logger{filename, LogConfig{level}} |
| 24 |
{} |
| 25 |
|
| 26 |
Logger::Logger(std::ostream& out, const LogConfig& config) |
| 27 |
: stream_{out}, config_{config}, writer_{&Logger::MessageWriter, this} |
| 28 |
{} |
| 29 |
|
| 30 |
Logger::Logger(const std::filesystem::path& filename, const LogConfig& config) |
| 31 |
: logFile_{filename}, stream_{logFile_}, config_{config}, writer_{&Logger::MessageWriter, this} |
| 32 |
{} |
| 33 |
|
| 34 |
Logger::~Logger() |
| 35 |
{ |
| 36 |
if (!writer_.joinable()) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// place a terminal sentinel for MessageWriter to know it's done |
| 41 |
{ |
| 42 |
std::lock_guard<std::mutex> g(m_); |
| 43 |
queue_.emplace(std::unique_ptr<LogLevelStream>()); |
| 44 |
} |
| 45 |
pushed_.notify_all(); |
| 46 |
|
| 47 |
// wait for everything to be flushed |
| 48 |
{ |
| 49 |
std::unique_lock<std::mutex> lk(m_); |
| 50 |
popped_.wait(lk, [this]() { return queue_.empty(); }); |
| 51 |
// endl implicitly flushes, so no need to call os_.flush() here |
| 52 |
} |
| 53 |
|
| 54 |
// join writer thread |
| 55 |
writer_.join(); |
| 56 |
} |
| 57 |
|
| 58 |
Logger& Logger::operator<<(std::unique_ptr<LogLevelStream>&& ptr) |
| 59 |
{ |
| 60 |
if (!writer_.joinable()) { |
| 61 |
throw std::runtime_error("this logger is dead!"); |
| 62 |
} |
| 63 |
{ |
| 64 |
std::lock_guard<std::mutex> g(m_); |
| 65 |
queue_.emplace(std::forward<std::unique_ptr<LogLevelStream>>(ptr)); |
| 66 |
} |
| 67 |
pushed_.notify_all(); |
| 68 |
return *this; |
| 69 |
} |
| 70 |
|
| 71 |
Logger& Logger::Current(Logger* newLogger) |
| 72 |
{ |
| 73 |
// NOTE: non-owning |
| 74 |
static Logger* currentLogger = nullptr; |
| 75 |
|
| 76 |
// If a new logger is explicitly provided, use it. |
| 77 |
if (newLogger) { |
| 78 |
currentLogger = newLogger; |
| 79 |
} else { |
| 80 |
// If we don't have a current logger set yet, create via Default(). |
| 81 |
// Default() maintains its own logger's lifetime. |
| 82 |
if (!currentLogger) { |
| 83 |
currentLogger = &(Default()); |
| 84 |
} |
| 85 |
} |
| 86 |
return *currentLogger; |
| 87 |
} |
| 88 |
|
| 89 |
LogLevel CurrentLogLevel() { return Logger::Current().Level(); } |
| 90 |
|
| 91 |
Logger& Logger::Default(Logger* logger) |
| 92 |
{ |
| 93 |
static auto logger_ = std::make_unique<Logger>(std::cerr, LogLevel::INFO); |
| 94 |
if (logger) { |
| 95 |
logger_.reset(logger); |
| 96 |
} |
| 97 |
return *logger_; |
| 98 |
} |
| 99 |
|
| 100 |
bool Logger::Handles(const LogLevel level) const { return level >= config_.Level; } |
| 101 |
|
| 102 |
LogLevel Logger::Level() const { return config_.Level; } |
| 103 |
|
| 104 |
void Logger::MessageWriter() |
| 105 |
{ |
| 106 |
while (true) { |
| 107 |
std::unique_ptr<LogLevelStream> ptr; |
| 108 |
|
| 109 |
// wait on messages to arrive in the queue_, and pop them off |
| 110 |
{ |
| 111 |
std::unique_lock<std::mutex> lk(m_); |
| 112 |
pushed_.wait(lk, [&ptr, this]() { |
| 113 |
if (queue_.empty()) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
ptr = std::move(queue_.front()); |
| 117 |
queue_.pop(); |
| 118 |
return true; |
| 119 |
}); |
| 120 |
} |
| 121 |
|
| 122 |
// if we've reached the null terminator, notify flush and stop |
| 123 |
if (!ptr) { |
| 124 |
popped_.notify_all(); |
| 125 |
break; |
| 126 |
} |
| 127 |
|
| 128 |
// otherwise, push the message onto os_ |
| 129 |
const LogLevel level = std::get<0>(*ptr); |
| 130 |
if (Handles(level)) { |
| 131 |
auto& os = stream_.get(); |
| 132 |
os << std::get<1>(*ptr).str() << std::endl; |
| 133 |
} |
| 134 |
|
| 135 |
// and notify flush we delivered a message to os_, |
| 136 |
popped_.notify_all(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
} // namespace Logging |
| 141 |
} // namespace PacBio |