From 3e16e435042959dd72149e385924f3ca887de036 Mon Sep 17 00:00:00 2001 From: joker Date: Mon, 8 Apr 2024 00:21:56 +0800 Subject: [PATCH] logging --- build.sh | 3 - muduo/base/Logging.cc | 156 +++++++++++++----------------------------- muduo/base/Logging.h | 102 ++++++++++++--------------- 3 files changed, 92 insertions(+), 169 deletions(-) diff --git a/build.sh b/build.sh index 76e41ba0e..4d65baaaf 100755 --- a/build.sh +++ b/build.sh @@ -22,6 +22,3 @@ mkdir -p $BUILD_DIR/$BUILD_TYPE-cpp17 \ # Use the following command to run all the unit tests # at the dir $BUILD_DIR/$BUILD_TYPE : # CTEST_OUTPUT_ON_FAILURE=TRUE make test - -# cd $SOURCE_DIR && doxygen - diff --git a/muduo/base/Logging.cc b/muduo/base/Logging.cc index c09a8ce4c..9172a2e74 100644 --- a/muduo/base/Logging.cc +++ b/muduo/base/Logging.cc @@ -5,47 +5,27 @@ #include "muduo/base/Logging.h" -#include "muduo/base/CurrentThread.h" -#include "muduo/base/Timestamp.h" -#include "muduo/base/TimeZone.h" - #include #include #include #include -namespace muduo -{ +#include "muduo/base/CurrentThread.h" +#include "muduo/base/TimeZone.h" +#include "muduo/base/Timestamp.h" -/* -class LoggerImpl -{ - public: - typedef Logger::LogLevel LogLevel; - LoggerImpl(LogLevel level, int old_errno, const char* file, int line); - void finish(); - - Timestamp time_; - LogStream stream_; - LogLevel level_; - int line_; - const char* fullname_; - const char* basename_; -}; -*/ +namespace muduo { __thread char t_errnobuf[512]; __thread char t_time[64]; __thread time_t t_lastSecond; -const char* strerror_tl(int savedErrno) -{ +const char* strerror_tl(int savedErrno) { return strerror_r(savedErrno, t_errnobuf, sizeof t_errnobuf); } -Logger::LogLevel initLogLevel() -{ +Logger::LogLevel initLogLevel() { if (::getenv("MUDUO_LOG_TRACE")) return Logger::TRACE; else if (::getenv("MUDUO_LOG_DEBUG")) @@ -67,13 +47,9 @@ const char* LogLevelName[Logger::NUM_LOG_LEVELS] = }; // helper class for known string length at compile time -class T -{ +class T { public: - T(const char* str, unsigned len) - :str_(str), - len_(len) - { + T(const char* str, unsigned len) : str_(str), len_(len) { assert(strlen(str) == len_); } @@ -81,29 +57,23 @@ class T const unsigned len_; }; -inline LogStream& operator<<(LogStream& s, T v) -{ +inline LogStream& operator<<(LogStream& s, T v) { s.append(v.str_, v.len_); return s; } -inline LogStream& operator<<(LogStream& s, const Logger::SourceFile& v) -{ +inline LogStream& operator<<(LogStream& s, const Logger::SourceFile& v) { s.append(v.data_, v.size_); return s; } -void defaultOutput(const char* msg, int len) -{ +void defaultOutput(const char* msg, int len) { size_t n = fwrite(msg, 1, len, stdout); - //FIXME check n + // FIXME check n (void)n; } -void defaultFlush() -{ - fflush(stdout); -} +void defaultFlush() { fflush(stdout); } Logger::OutputFunc g_output = defaultOutput; Logger::FlushFunc g_flush = defaultFlush; @@ -113,115 +83,87 @@ TimeZone g_logTimeZone; using namespace muduo; -Logger::Impl::Impl(LogLevel level, int savedErrno, const SourceFile& file, int line) - : time_(Timestamp::now()), - stream_(), - level_(level), - line_(line), - basename_(file) -{ +Logger::Impl::Impl(LogLevel level, int savedErrno, const SourceFile& file, + int line) + : time_(Timestamp::now()), + stream_(), + level_(level), + line_(line), + basename_(file) { formatTime(); CurrentThread::tid(); stream_ << T(CurrentThread::tidString(), CurrentThread::tidStringLength()); stream_ << T(LogLevelName[level], 6); - if (savedErrno != 0) - { + if (savedErrno != 0) { stream_ << strerror_tl(savedErrno) << " (errno=" << savedErrno << ") "; } } -void Logger::Impl::formatTime() -{ +void Logger::Impl::formatTime() { int64_t microSecondsSinceEpoch = time_.microSecondsSinceEpoch(); - time_t seconds = static_cast(microSecondsSinceEpoch / Timestamp::kMicroSecondsPerSecond); - int microseconds = static_cast(microSecondsSinceEpoch % Timestamp::kMicroSecondsPerSecond); - if (seconds != t_lastSecond) - { + time_t seconds = static_cast(microSecondsSinceEpoch / + Timestamp::kMicroSecondsPerSecond); + int microseconds = static_cast(microSecondsSinceEpoch % + Timestamp::kMicroSecondsPerSecond); + if (seconds != t_lastSecond) { t_lastSecond = seconds; struct tm tm_time; - if (g_logTimeZone.valid()) - { + if (g_logTimeZone.valid()) { tm_time = g_logTimeZone.toLocalTime(seconds); - } - else - { - ::gmtime_r(&seconds, &tm_time); // FIXME TimeZone::fromUtcTime + } else { + ::gmtime_r(&seconds, &tm_time); // FIXME TimeZone::fromUtcTime } - int len = snprintf(t_time, sizeof(t_time), "%4d%02d%02d %02d:%02d:%02d", - tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday, - tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec); - assert(len == 17); (void)len; + int len = + snprintf(t_time, sizeof(t_time), "%4d%02d%02d %02d:%02d:%02d", + tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday, + tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec); + assert(len == 17); + (void)len; } - if (g_logTimeZone.valid()) - { + if (g_logTimeZone.valid()) { Fmt us(".%06d ", microseconds); assert(us.length() == 8); stream_ << T(t_time, 17) << T(us.data(), 8); - } - else - { + } else { Fmt us(".%06dZ ", microseconds); assert(us.length() == 9); stream_ << T(t_time, 17) << T(us.data(), 9); } } -void Logger::Impl::finish() -{ +void Logger::Impl::finish() { stream_ << " - " << basename_ << ':' << line_ << '\n'; } -Logger::Logger(SourceFile file, int line) - : impl_(INFO, 0, file, line) -{ -} +Logger::Logger(SourceFile file, int line) : impl_(INFO, 0, file, line) {} Logger::Logger(SourceFile file, int line, LogLevel level, const char* func) - : impl_(level, 0, file, line) -{ + : impl_(level, 0, file, line) { impl_.stream_ << func << ' '; } Logger::Logger(SourceFile file, int line, LogLevel level) - : impl_(level, 0, file, line) -{ -} + : impl_(level, 0, file, line) {} Logger::Logger(SourceFile file, int line, bool toAbort) - : impl_(toAbort?FATAL:ERROR, errno, file, line) -{ -} + : impl_(toAbort ? FATAL : ERROR, errno, file, line) {} -Logger::~Logger() -{ +Logger::~Logger() { impl_.finish(); const LogStream::Buffer& buf(stream().buffer()); g_output(buf.data(), buf.length()); - if (impl_.level_ == FATAL) - { + if (impl_.level_ == FATAL) { g_flush(); abort(); } } -void Logger::setLogLevel(Logger::LogLevel level) -{ - g_logLevel = level; -} +void Logger::setLogLevel(Logger::LogLevel level) { g_logLevel = level; } -void Logger::setOutput(OutputFunc out) -{ - g_output = out; -} +void Logger::setOutput(OutputFunc out) { g_output = out; } -void Logger::setFlush(FlushFunc flush) -{ - g_flush = flush; -} +void Logger::setFlush(FlushFunc flush) { g_flush = flush; } -void Logger::setTimeZone(const TimeZone& tz) -{ - g_logTimeZone = tz; -} +void Logger::setTimeZone(const TimeZone& tz) { g_logTimeZone = tz; } diff --git a/muduo/base/Logging.h b/muduo/base/Logging.h index 64a5ae763..e45f7a324 100644 --- a/muduo/base/Logging.h +++ b/muduo/base/Logging.h @@ -3,24 +3,20 @@ // // Author: Shuo Chen (chenshuo at chenshuo dot com) -#ifndef MUDUO_BASE_LOGGING_H -#define MUDUO_BASE_LOGGING_H +#pragma once + +#include #include "muduo/base/LogStream.h" #include "muduo/base/Timestamp.h" -#include - -namespace muduo -{ +namespace muduo { class TimeZone; -class Logger -{ +class Logger { public: - enum LogLevel - { + enum LogLevel { TRACE, DEBUG, INFO, @@ -31,28 +27,20 @@ class Logger }; // compile time calculation of basename of source file - class SourceFile - { + class SourceFile { public: - template - SourceFile(const char (&arr)[N]) - : data_(arr), - size_(N-1) - { - const char* slash = strrchr(data_, '/'); // builtin function - if (slash) - { + template + SourceFile(const char (&arr)[N]) : data_(arr), size_(N - 1) { + const char* slash = strrchr(data_, '/'); // builtin function + if (slash) { data_ = slash + 1; size_ -= static_cast(data_ - arr); } } - explicit SourceFile(const char* filename) - : data_(filename) - { + explicit SourceFile(const char* filename) : data_(filename) { const char* slash = strrchr(filename, '/'); - if (slash) - { + if (slash) { data_ = slash + 1; } size_ = static_cast(strlen(data_)); @@ -80,32 +68,26 @@ class Logger static void setTimeZone(const TimeZone& tz); private: - -class Impl -{ - public: - typedef Logger::LogLevel LogLevel; - Impl(LogLevel level, int old_errno, const SourceFile& file, int line); - void formatTime(); - void finish(); - - Timestamp time_; - LogStream stream_; - LogLevel level_; - int line_; - SourceFile basename_; -}; + class Impl { + public: + typedef Logger::LogLevel LogLevel; + Impl(LogLevel level, int old_errno, const SourceFile& file, int line); + void formatTime(); + void finish(); + + Timestamp time_; + LogStream stream_; + LogLevel level_; + int line_; + SourceFile basename_; + }; Impl impl_; - }; extern Logger::LogLevel g_logLevel; -inline Logger::LogLevel Logger::logLevel() -{ - return g_logLevel; -} +inline Logger::LogLevel Logger::logLevel() { return g_logLevel; } // // CAUTION: do not write: @@ -128,15 +110,20 @@ inline Logger::LogLevel Logger::logLevel() * 2. 调用stream函数返回一个LogStream对象 * 3. 调用operator<<来输出日志 */ -#define LOG_TRACE if (muduo::Logger::logLevel() <= muduo::Logger::TRACE) \ +#define LOG_TRACE \ + if (muduo::Logger::logLevel() <= muduo::Logger::TRACE) \ muduo::Logger(__FILE__, __LINE__, muduo::Logger::TRACE, __func__).stream() -#define LOG_DEBUG if (muduo::Logger::logLevel() <= muduo::Logger::DEBUG) \ +#define LOG_DEBUG \ + if (muduo::Logger::logLevel() <= muduo::Logger::DEBUG) \ muduo::Logger(__FILE__, __LINE__, muduo::Logger::DEBUG, __func__).stream() -#define LOG_INFO if (muduo::Logger::logLevel() <= muduo::Logger::INFO) \ +#define LOG_INFO \ + if (muduo::Logger::logLevel() <= muduo::Logger::INFO) \ muduo::Logger(__FILE__, __LINE__).stream() #define LOG_WARN muduo::Logger(__FILE__, __LINE__, muduo::Logger::WARN).stream() -#define LOG_ERROR muduo::Logger(__FILE__, __LINE__, muduo::Logger::ERROR).stream() -#define LOG_FATAL muduo::Logger(__FILE__, __LINE__, muduo::Logger::FATAL).stream() +#define LOG_ERROR \ + muduo::Logger(__FILE__, __LINE__, muduo::Logger::ERROR).stream() +#define LOG_FATAL \ + muduo::Logger(__FILE__, __LINE__, muduo::Logger::FATAL).stream() #define LOG_SYSERR muduo::Logger(__FILE__, __LINE__, false).stream() #define LOG_SYSFATAL muduo::Logger(__FILE__, __LINE__, true).stream() @@ -147,20 +134,17 @@ const char* strerror_tl(int savedErrno); // Check that the input is non NULL. This very useful in constructor // initializer lists. -#define CHECK_NOTNULL(val) \ - ::muduo::CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val)) +#define CHECK_NOTNULL(val) \ + ::muduo::CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", \ + (val)) // A small helper for CHECK_NOTNULL(). template -T* CheckNotNull(Logger::SourceFile file, int line, const char *names, T* ptr) -{ - if (ptr == NULL) - { - Logger(file, line, Logger::FATAL).stream() << names; +T* CheckNotNull(Logger::SourceFile file, int line, const char* names, T* ptr) { + if (ptr == NULL) { + Logger(file, line, Logger::FATAL).stream() << names; } return ptr; } } // namespace muduo - -#endif // MUDUO_BASE_LOGGING_H