mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-02-20 15:14:26 +01:00
Moved trantor's logger to core. It's a bit messy right now, but It will get cleaned up later.
This commit is contained in:
parent
94fd22eee9
commit
aeaecd4b76
@ -20,98 +20,98 @@
|
||||
#endif
|
||||
|
||||
#define RLOG_TRACE(str) \
|
||||
Logger::log_trace(str);
|
||||
RLogger::log_trace(str);
|
||||
|
||||
#define RLOG_MSG(str) \
|
||||
Logger::log_message(str);
|
||||
RLogger::log_message(str);
|
||||
|
||||
#define RLOG_WARN(str) \
|
||||
Logger::log_warning(str);
|
||||
RLogger::log_warning(str);
|
||||
|
||||
#define RLOG_ERR(str) \
|
||||
Logger::log_error(str);
|
||||
RLogger::log_error(str);
|
||||
|
||||
#define ERR_FAIL_MSG(msg) \
|
||||
Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
RLogger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
return;
|
||||
|
||||
#define ERR_FAIL_V_MSG(val, msg) \
|
||||
Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
RLogger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
return val;
|
||||
|
||||
#define ERR_FAIL_INDEX(index, size) \
|
||||
if ((index < 0) || (index >= size)) {\
|
||||
Logger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, ""); \
|
||||
RLogger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, ""); \
|
||||
return;\
|
||||
} else\
|
||||
((void)0)\
|
||||
|
||||
#define ERR_FAIL_INDEX_MSG(index, size, msg) \
|
||||
if ((index < 0) || (index >= size)) {\
|
||||
Logger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, msg); \
|
||||
RLogger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, msg); \
|
||||
return;\
|
||||
} else\
|
||||
((void)0)\
|
||||
|
||||
#define ERR_FAIL_INDEX_V(index, size, val) \
|
||||
if ((index < 0) || (index >= size)) {\
|
||||
Logger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, ""); \
|
||||
RLogger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, ""); \
|
||||
return val;\
|
||||
} else\
|
||||
((void)0)\
|
||||
|
||||
#define ERR_FAIL_INDEX_V_MSG(index, size, val, msg) \
|
||||
if ((index < 0) || (index >= size)) {\
|
||||
Logger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, msg); \
|
||||
RLogger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, msg); \
|
||||
return val;\
|
||||
} else\
|
||||
((void)0)\
|
||||
|
||||
#define ERR_FAIL_COND(cond) \
|
||||
if (cond) {\
|
||||
Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, "ERR_FAIL_COND: \"" #cond "\" is true!"); \
|
||||
RLogger::_log_error(__FUNCTION__, __FILE__, __LINE__, "ERR_FAIL_COND: \"" #cond "\" is true!"); \
|
||||
return;\
|
||||
} else\
|
||||
((void)0)\
|
||||
|
||||
#define ERR_FAIL_COND_MSG(cond, msg) \
|
||||
if (cond) {\
|
||||
Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
RLogger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
return;\
|
||||
} else\
|
||||
((void)0)\
|
||||
|
||||
#define ERR_FAIL_COND_V(cond, val) \
|
||||
if (cond) {\
|
||||
Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, "ERR_FAIL_COND: \"" #cond "\" is true!"); \
|
||||
RLogger::_log_error(__FUNCTION__, __FILE__, __LINE__, "ERR_FAIL_COND: \"" #cond "\" is true!"); \
|
||||
return val;\
|
||||
} else\
|
||||
((void)0)\
|
||||
|
||||
#define ERR_FAIL_COND_V_MSG(cond, val, msg) \
|
||||
if (cond) {\
|
||||
Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
RLogger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
return val;\
|
||||
} else\
|
||||
((void)0)\
|
||||
|
||||
#define ERR_CONTINUE(cond) \
|
||||
if (cond) {\
|
||||
Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, "ERR_CONTINUE: \"" #cond "\" is true!"); \
|
||||
RLogger::_log_error(__FUNCTION__, __FILE__, __LINE__, "ERR_CONTINUE: \"" #cond "\" is true!"); \
|
||||
continue;\
|
||||
} else\
|
||||
((void)0)\
|
||||
|
||||
#define ERR_CONTINUE_MSG(cond, msg) \
|
||||
if (cond) {\
|
||||
Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
RLogger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
continue;\
|
||||
} else\
|
||||
((void)0)\
|
||||
|
||||
#define ERR_CONTINUE_ACTION(cond, action) \
|
||||
if (cond) {\
|
||||
Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, "ERR_CONTINUE: \"" #cond "\" is true!"); \
|
||||
RLogger::_log_error(__FUNCTION__, __FILE__, __LINE__, "ERR_CONTINUE: \"" #cond "\" is true!"); \
|
||||
action;\
|
||||
continue;\
|
||||
} else\
|
||||
@ -119,7 +119,7 @@
|
||||
|
||||
#define ERR_CONTINUE_ACTION_MSG(cond, action, msg) \
|
||||
if (cond) {\
|
||||
Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
RLogger::_log_error(__FUNCTION__, __FILE__, __LINE__, msg); \
|
||||
action;\
|
||||
continue;\
|
||||
} else\
|
||||
@ -127,14 +127,14 @@
|
||||
|
||||
#define CRASH_INDEX(index, size) \
|
||||
if ((index < 0) || (index >= size)) {\
|
||||
Logger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, "CRASH!"); \
|
||||
RLogger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, "CRASH!"); \
|
||||
GENERATE_TRAP \
|
||||
} else\
|
||||
((void)0)\
|
||||
|
||||
#define CRASH_COND(cond) \
|
||||
if (cond) {\
|
||||
Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, "CRASH_COND: \"" #cond "\" is true!"); \
|
||||
RLogger::_log_error(__FUNCTION__, __FILE__, __LINE__, "CRASH_COND: \"" #cond "\" is true!"); \
|
||||
GENERATE_TRAP \
|
||||
} else\
|
||||
((void)0)\
|
||||
|
@ -12,7 +12,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <trantor/utils/AsyncFileLogger.h>
|
||||
#include "async_file_logger.h"
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
#ifdef __linux__
|
147
core/log/async_file_logger.h
Normal file
147
core/log/async_file_logger.h
Normal file
@ -0,0 +1,147 @@
|
||||
/**
|
||||
*
|
||||
* @file AsyncFileLogger.h
|
||||
* @author An Tao
|
||||
*
|
||||
* Public header file in trantor lib.
|
||||
*
|
||||
* Copyright 2018, An Tao. All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the License file.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/math/date.h"
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
namespace trantor {
|
||||
using StringPtr = std::shared_ptr<std::string>;
|
||||
using StringPtrQueue = std::queue<StringPtr>;
|
||||
|
||||
/**
|
||||
* @brief This class implements utility functions for writing logs to files
|
||||
* asynchronously.
|
||||
*
|
||||
*/
|
||||
class AsyncFileLogger {
|
||||
protected:
|
||||
AsyncFileLogger(const AsyncFileLogger &) = delete;
|
||||
AsyncFileLogger &operator=(const AsyncFileLogger &) = delete;
|
||||
// some uncopyable classes maybe support move constructor....
|
||||
AsyncFileLogger(AsyncFileLogger &&) noexcept(true) = default;
|
||||
AsyncFileLogger &operator=(AsyncFileLogger &&) noexcept(true) = default;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Write the message to the log file.
|
||||
*
|
||||
* @param msg
|
||||
* @param len
|
||||
*/
|
||||
void output(const char *msg, const uint64_t len);
|
||||
|
||||
/**
|
||||
* @brief Flush data from memory buffer to the log file.
|
||||
*
|
||||
*/
|
||||
void flush();
|
||||
|
||||
/**
|
||||
* @brief Start writing log files.
|
||||
*
|
||||
*/
|
||||
void startLogging();
|
||||
|
||||
/**
|
||||
* @brief Set the size limit of log files. When the log file size reaches
|
||||
* the limit, the log file is switched.
|
||||
*
|
||||
* @param limit
|
||||
*/
|
||||
void setFileSizeLimit(uint64_t limit) {
|
||||
sizeLimit_ = limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the log file name.
|
||||
*
|
||||
* @param baseName The base name of the log file.
|
||||
* @param extName The extended name of the log file.
|
||||
* @param path The location where the log file is stored.
|
||||
*/
|
||||
void setFileName(const std::string &baseName,
|
||||
const std::string &extName = ".log",
|
||||
const std::string &path = "./") {
|
||||
fileBaseName_ = baseName;
|
||||
extName[0] == '.' ? fileExtName_ = extName : fileExtName_ = std::string(".") + extName;
|
||||
filePath_ = path;
|
||||
if (filePath_.length() == 0)
|
||||
filePath_ = "./";
|
||||
if (filePath_[filePath_.length() - 1] != '/')
|
||||
filePath_ = filePath_ + "/";
|
||||
}
|
||||
~AsyncFileLogger();
|
||||
AsyncFileLogger();
|
||||
|
||||
protected:
|
||||
std::mutex mutex_;
|
||||
std::condition_variable cond_;
|
||||
StringPtr logBufferPtr_;
|
||||
StringPtr nextBufferPtr_;
|
||||
StringPtrQueue writeBuffers_;
|
||||
StringPtrQueue tmpBuffers_;
|
||||
void writeLogToFile(const StringPtr buf);
|
||||
std::unique_ptr<std::thread> threadPtr_;
|
||||
bool stopFlag_{ false };
|
||||
void logThreadFunc();
|
||||
std::string filePath_{ "./" };
|
||||
std::string fileBaseName_{ "trantor" };
|
||||
std::string fileExtName_{ ".log" };
|
||||
uint64_t sizeLimit_{ 20 * 1024 * 1024 };
|
||||
|
||||
class LoggerFile {
|
||||
protected:
|
||||
LoggerFile(const LoggerFile &) = delete;
|
||||
LoggerFile &operator=(const LoggerFile &) = delete;
|
||||
// some uncopyable classes maybe support move constructor....
|
||||
LoggerFile(LoggerFile &&) noexcept(true) = default;
|
||||
LoggerFile &operator=(LoggerFile &&) noexcept(true) = default;
|
||||
|
||||
public:
|
||||
LoggerFile(const std::string &filePath,
|
||||
const std::string &fileBaseName,
|
||||
const std::string &fileExtName);
|
||||
~LoggerFile();
|
||||
void writeLog(const StringPtr buf);
|
||||
uint64_t getLength();
|
||||
explicit operator bool() const {
|
||||
return fp_ != nullptr;
|
||||
}
|
||||
void flush();
|
||||
|
||||
protected:
|
||||
FILE *fp_{ nullptr };
|
||||
Date creationDate_;
|
||||
std::string fileFullName_;
|
||||
std::string filePath_;
|
||||
std::string fileBaseName_;
|
||||
std::string fileExtName_;
|
||||
static uint64_t fileSeq_;
|
||||
};
|
||||
|
||||
std::unique_ptr<LoggerFile> loggerFilePtr_;
|
||||
|
||||
uint64_t lostCounter_{ 0 };
|
||||
void swapBuffer();
|
||||
};
|
||||
|
||||
} // namespace trantor
|
@ -18,7 +18,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <trantor/utils/LogStream.h>
|
||||
#include "log_stream.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
@ -219,16 +219,16 @@ Fmt::Fmt(const char *fmt, T val) {
|
||||
|
||||
// Explicit instantiations
|
||||
|
||||
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, char);
|
||||
template Fmt::Fmt(const char *fmt, char);
|
||||
|
||||
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, short);
|
||||
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, unsigned short);
|
||||
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, int);
|
||||
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, unsigned int);
|
||||
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, long);
|
||||
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, unsigned long);
|
||||
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, long long);
|
||||
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, unsigned long long);
|
||||
template Fmt::Fmt(const char *fmt, short);
|
||||
template Fmt::Fmt(const char *fmt, unsigned short);
|
||||
template Fmt::Fmt(const char *fmt, int);
|
||||
template Fmt::Fmt(const char *fmt, unsigned int);
|
||||
template Fmt::Fmt(const char *fmt, long);
|
||||
template Fmt::Fmt(const char *fmt, unsigned long);
|
||||
template Fmt::Fmt(const char *fmt, long long);
|
||||
template Fmt::Fmt(const char *fmt, unsigned long long);
|
||||
|
||||
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, float);
|
||||
template TRANTOR_EXPORT Fmt::Fmt(const char *fmt, double);
|
||||
template Fmt::Fmt(const char *fmt, float);
|
||||
template Fmt::Fmt(const char *fmt, double);
|
246
core/log/log_stream.h
Normal file
246
core/log/log_stream.h
Normal file
@ -0,0 +1,246 @@
|
||||
/**
|
||||
*
|
||||
* @file LogStream.h
|
||||
* @author An Tao
|
||||
*
|
||||
* Public header file in trantor lib.
|
||||
*
|
||||
* Copyright 2018, An Tao. All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the License file.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
// Taken from muduo lib and modified. Classes in this file are used internally.
|
||||
#include <assert.h>
|
||||
#include <string.h> // memcpy
|
||||
#include <string>
|
||||
|
||||
namespace trantor {
|
||||
namespace detail {
|
||||
static constexpr size_t kSmallBuffer{ 4000 };
|
||||
static constexpr size_t kLargeBuffer{ 4000 * 1000 };
|
||||
|
||||
template <int SIZE>
|
||||
class FixedBuffer {
|
||||
protected:
|
||||
FixedBuffer(const FixedBuffer &) = delete;
|
||||
FixedBuffer &operator=(const FixedBuffer &) = delete;
|
||||
// some uncopyable classes maybe support move constructor....
|
||||
FixedBuffer(FixedBuffer &&) noexcept(true) = default;
|
||||
FixedBuffer &operator=(FixedBuffer &&) noexcept(true) = default;
|
||||
|
||||
public:
|
||||
FixedBuffer() :
|
||||
cur_(data_) {
|
||||
setCookie(cookieStart);
|
||||
}
|
||||
|
||||
~FixedBuffer() {
|
||||
setCookie(cookieEnd);
|
||||
}
|
||||
|
||||
bool append(const char * /*restrict*/ buf, size_t len) {
|
||||
if ((size_t)(avail()) > len) {
|
||||
memcpy(cur_, buf, len);
|
||||
cur_ += len;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *data() const {
|
||||
return data_;
|
||||
}
|
||||
int length() const {
|
||||
return static_cast<int>(cur_ - data_);
|
||||
}
|
||||
|
||||
// write to data_ directly
|
||||
char *current() {
|
||||
return cur_;
|
||||
}
|
||||
int avail() const {
|
||||
return static_cast<int>(end() - cur_);
|
||||
}
|
||||
void add(size_t len) {
|
||||
cur_ += len;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
cur_ = data_;
|
||||
}
|
||||
void zeroBuffer() {
|
||||
memset(data_, 0, sizeof(data_));
|
||||
}
|
||||
|
||||
// for used by GDB
|
||||
const char *debugString();
|
||||
void setCookie(void (*cookie)()) {
|
||||
cookie_ = cookie;
|
||||
}
|
||||
// for used by unit test
|
||||
std::string toString() const {
|
||||
return std::string(data_, length());
|
||||
}
|
||||
// StringPiece toStringPiece() const { return StringPiece(data_, length());
|
||||
// }
|
||||
|
||||
private:
|
||||
const char *end() const {
|
||||
return data_ + sizeof data_;
|
||||
}
|
||||
// Must be outline function for cookies.
|
||||
static void cookieStart();
|
||||
static void cookieEnd();
|
||||
|
||||
void (*cookie_)();
|
||||
char data_[SIZE];
|
||||
char *cur_;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
class LogStream {
|
||||
using self = LogStream;
|
||||
|
||||
protected:
|
||||
LogStream(const LogStream &) = delete;
|
||||
LogStream &operator=(const LogStream &) = delete;
|
||||
// some uncopyable classes maybe support move constructor....
|
||||
LogStream(LogStream &&) noexcept(true) = default;
|
||||
LogStream &operator=(LogStream &&) noexcept(true) = default;
|
||||
|
||||
public:
|
||||
using Buffer = detail::FixedBuffer<detail::kSmallBuffer>;
|
||||
|
||||
self &operator<<(bool v) {
|
||||
append(v ? "1" : "0", 1);
|
||||
return *this;
|
||||
}
|
||||
|
||||
self &operator<<(short);
|
||||
self &operator<<(unsigned short);
|
||||
self &operator<<(int);
|
||||
self &operator<<(unsigned int);
|
||||
self &operator<<(long);
|
||||
self &operator<<(unsigned long);
|
||||
self &operator<<(const long long &);
|
||||
self &operator<<(const unsigned long long &);
|
||||
|
||||
self &operator<<(const void *);
|
||||
|
||||
self &operator<<(float &v) {
|
||||
*this << static_cast<double>(v);
|
||||
return *this;
|
||||
}
|
||||
self &operator<<(const double &);
|
||||
self &operator<<(const long double &v);
|
||||
|
||||
self &operator<<(char v) {
|
||||
append(&v, 1);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// self& operator<<(signed char);
|
||||
// self& operator<<(unsigned char);
|
||||
template <int N>
|
||||
self &operator<<(const char (&buf)[N]) {
|
||||
assert(strnlen(buf, N) == N - 1);
|
||||
append(buf, N - 1);
|
||||
return *this;
|
||||
}
|
||||
|
||||
self &operator<<(char *str) {
|
||||
if (str) {
|
||||
append(str, strlen(str));
|
||||
} else {
|
||||
append("(null)", 6);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
self &operator<<(const char *str) {
|
||||
if (str) {
|
||||
append(str, strlen(str));
|
||||
} else {
|
||||
append("(null)", 6);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
self &operator<<(const unsigned char *str) {
|
||||
return operator<<(reinterpret_cast<const char *>(str));
|
||||
}
|
||||
|
||||
self &operator<<(const std::string &v) {
|
||||
append(v.c_str(), v.size());
|
||||
return *this;
|
||||
}
|
||||
|
||||
void append(const char *data, size_t len) {
|
||||
if (exBuffer_.empty()) {
|
||||
if (!buffer_.append(data, len)) {
|
||||
exBuffer_.append(buffer_.data(), buffer_.length());
|
||||
exBuffer_.append(data, len);
|
||||
}
|
||||
} else {
|
||||
exBuffer_.append(data, len);
|
||||
}
|
||||
}
|
||||
// const Buffer& buffer() const { return buffer_; }
|
||||
const char *bufferData() const {
|
||||
if (!exBuffer_.empty()) {
|
||||
return exBuffer_.data();
|
||||
}
|
||||
return buffer_.data();
|
||||
}
|
||||
|
||||
size_t bufferLength() const {
|
||||
if (!exBuffer_.empty()) {
|
||||
return exBuffer_.length();
|
||||
}
|
||||
return buffer_.length();
|
||||
}
|
||||
void resetBuffer() {
|
||||
buffer_.reset();
|
||||
exBuffer_.clear();
|
||||
}
|
||||
|
||||
LogStream() {}
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
void formatInteger(T);
|
||||
|
||||
Buffer buffer_;
|
||||
std::string exBuffer_;
|
||||
};
|
||||
|
||||
class Fmt // : boost::noncopyable
|
||||
{
|
||||
public:
|
||||
template <typename T>
|
||||
Fmt(const char *fmt, T val);
|
||||
|
||||
const char *data() const {
|
||||
return buf_;
|
||||
}
|
||||
int length() const {
|
||||
return length_;
|
||||
}
|
||||
|
||||
private:
|
||||
char buf_[48];
|
||||
int length_;
|
||||
};
|
||||
|
||||
inline LogStream &operator<<(LogStream &s, const Fmt &fmt) {
|
||||
s.append(fmt.data(), fmt.length());
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace trantor
|
@ -1,58 +1,219 @@
|
||||
#include "logger.h"
|
||||
|
||||
#include "core/string.h"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
void Logger::log_trace(const String &str)
|
||||
{
|
||||
log_trace(str.data());
|
||||
#include <stdio.h>
|
||||
#include "logger.h"
|
||||
#include <thread>
|
||||
#ifndef _WIN32
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#include <sstream>
|
||||
#endif
|
||||
#ifdef __FreeBSD__
|
||||
#include <pthread_np.h>
|
||||
#endif
|
||||
|
||||
void RLogger::log_trace(const String &str) {
|
||||
log_trace(str.data());
|
||||
}
|
||||
void Logger::log_trace(const char *str)
|
||||
{
|
||||
printf("T %s\n", str);
|
||||
void RLogger::log_trace(const char *str) {
|
||||
printf("T %s\n", str);
|
||||
}
|
||||
|
||||
void Logger::log_message(const String &str)
|
||||
{
|
||||
log_message(str.data());
|
||||
void RLogger::log_message(const String &str) {
|
||||
log_message(str.data());
|
||||
}
|
||||
void Logger::log_message(const char *str)
|
||||
{
|
||||
printf("M %s\n", str);
|
||||
void RLogger::log_message(const char *str) {
|
||||
printf("M %s\n", str);
|
||||
}
|
||||
|
||||
void Logger::log_warning(const String &str)
|
||||
{
|
||||
log_warning(str.data());
|
||||
void RLogger::log_warning(const String &str) {
|
||||
log_warning(str.data());
|
||||
}
|
||||
void Logger::log_warning(const char *str)
|
||||
{
|
||||
printf("W %s\n", str);
|
||||
void RLogger::log_warning(const char *str) {
|
||||
printf("W %s\n", str);
|
||||
}
|
||||
|
||||
void Logger::log_error(const String &str)
|
||||
{
|
||||
log_error(str.data());
|
||||
void RLogger::log_error(const String &str) {
|
||||
log_error(str.data());
|
||||
}
|
||||
void Logger::log_error(const char *str)
|
||||
{
|
||||
printf("E %s\n", str);
|
||||
void RLogger::log_error(const char *str) {
|
||||
printf("E %s\n", str);
|
||||
}
|
||||
|
||||
void Logger::_log_error(const char *p_function, const char *p_file, int p_line, const char *str)
|
||||
{
|
||||
printf("!ERROR: (%s) %s:%d. %s\n", p_file, p_function, p_line, str);
|
||||
void RLogger::_log_error(const char *p_function, const char *p_file, int p_line, const char *str) {
|
||||
printf("!ERROR: (%s) %s:%d. %s\n", p_file, p_function, p_line, str);
|
||||
}
|
||||
void Logger::_log_error(const char *p_function, const char *p_file, int p_line, const String &str)
|
||||
{
|
||||
printf("!ERROR: (%s) %s:%d. %s\n", p_file, p_function, p_line, str.c_str());
|
||||
void RLogger::_log_error(const char *p_function, const char *p_file, int p_line, const String &str) {
|
||||
printf("!ERROR: (%s) %s:%d. %s\n", p_file, p_function, p_line, str.c_str());
|
||||
}
|
||||
void Logger::_log_msg_error(const char *p_function, const char *p_file, int p_line, const char *p_msg, const char *str)
|
||||
{
|
||||
printf("!ERROR: (%s) %s:%d :: %s. %s\n", p_file, p_function, p_line, str, p_msg);
|
||||
void RLogger::_log_msg_error(const char *p_function, const char *p_file, int p_line, const char *p_msg, const char *str) {
|
||||
printf("!ERROR: (%s) %s:%d :: %s. %s\n", p_file, p_function, p_line, str, p_msg);
|
||||
}
|
||||
void Logger::_log_index_error(const char *p_function, const char *p_file, int p_line, const int index, const int size, const char *str)
|
||||
{
|
||||
printf("!INDEX ERROR: (%s) %s:%d :: index: %d/%d. %s\n", p_file, p_function, p_line, index, size, str);
|
||||
void RLogger::_log_index_error(const char *p_function, const char *p_file, int p_line, const int index, const int size, const char *str) {
|
||||
printf("!INDEX ERROR: (%s) %s:%d :: index: %d/%d. %s\n", p_file, p_function, p_line, index, size, str);
|
||||
}
|
||||
|
||||
namespace trantor {
|
||||
// helper class for known string length at compile time
|
||||
class T {
|
||||
public:
|
||||
T(const char *str, unsigned len) :
|
||||
str_(str), len_(len) {
|
||||
assert(strlen(str) == len_);
|
||||
}
|
||||
|
||||
const char *str_;
|
||||
const unsigned len_;
|
||||
};
|
||||
|
||||
const char *strerror_tl(int savedErrno) {
|
||||
#ifndef _MSC_VER
|
||||
return strerror(savedErrno);
|
||||
#else
|
||||
static thread_local char errMsg[64];
|
||||
(void)strerror_s<sizeof errMsg>(errMsg, savedErrno);
|
||||
return errMsg;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline LogStream &operator<<(LogStream &s, T v) {
|
||||
s.append(v.str_, v.len_);
|
||||
return s;
|
||||
}
|
||||
|
||||
inline LogStream &operator<<(LogStream &s, const Logger::SourceFile &v) {
|
||||
s.append(v.data_, v.size_);
|
||||
return s;
|
||||
}
|
||||
} // namespace trantor
|
||||
using namespace trantor;
|
||||
|
||||
static thread_local uint64_t lastSecond_{ 0 };
|
||||
static thread_local char lastTimeString_[32] = { 0 };
|
||||
#ifdef __linux__
|
||||
static thread_local pid_t threadId_{ 0 };
|
||||
#else
|
||||
static thread_local uint64_t threadId_{ 0 };
|
||||
#endif
|
||||
// static thread_local LogStream logStream_;
|
||||
|
||||
void Logger::formatTime() {
|
||||
uint64_t now = static_cast<uint64_t>(date_.secondsSinceEpoch());
|
||||
uint64_t microSec =
|
||||
static_cast<uint64_t>(date_.microSecondsSinceEpoch() -
|
||||
date_.roundSecond().microSecondsSinceEpoch());
|
||||
if (now != lastSecond_) {
|
||||
lastSecond_ = now;
|
||||
#ifndef _MSC_VER
|
||||
strncpy(lastTimeString_,
|
||||
date_.toFormattedString(false).c_str(),
|
||||
sizeof(lastTimeString_) - 1);
|
||||
#else
|
||||
strncpy_s<sizeof lastTimeString_>(
|
||||
lastTimeString_,
|
||||
date_.toFormattedString(false).c_str(),
|
||||
sizeof(lastTimeString_) - 1);
|
||||
#endif
|
||||
}
|
||||
logStream_ << T(lastTimeString_, 17);
|
||||
char tmp[32];
|
||||
snprintf(tmp,
|
||||
sizeof(tmp),
|
||||
".%06llu UTC ",
|
||||
static_cast<long long unsigned int>(microSec));
|
||||
logStream_ << T(tmp, 12);
|
||||
#ifdef __linux__
|
||||
if (threadId_ == 0)
|
||||
threadId_ = static_cast<pid_t>(::syscall(SYS_gettid));
|
||||
#elif defined __FreeBSD__
|
||||
if (threadId_ == 0) {
|
||||
threadId_ = pthread_getthreadid_np();
|
||||
}
|
||||
#elif defined __OpenBSD__
|
||||
if (threadId_ == 0) {
|
||||
threadId_ = getthrid();
|
||||
}
|
||||
#elif defined _WIN32
|
||||
if (threadId_ == 0) {
|
||||
std::stringstream ss;
|
||||
ss << std::this_thread::get_id();
|
||||
threadId_ = std::stoull(ss.str());
|
||||
}
|
||||
#else
|
||||
if (threadId_ == 0) {
|
||||
pthread_threadid_np(NULL, &threadId_);
|
||||
}
|
||||
#endif
|
||||
logStream_ << threadId_;
|
||||
}
|
||||
static const char *logLevelStr[Logger::LogLevel::kNumberOfLogLevels] = {
|
||||
" TRACE ",
|
||||
" DEBUG ",
|
||||
" INFO ",
|
||||
" WARN ",
|
||||
" ERROR ",
|
||||
" FATAL ",
|
||||
};
|
||||
Logger::Logger(SourceFile file, int line) :
|
||||
sourceFile_(file), fileLine_(line), level_(kInfo) {
|
||||
formatTime();
|
||||
logStream_ << T(logLevelStr[level_], 7);
|
||||
}
|
||||
Logger::Logger(SourceFile file, int line, LogLevel level) :
|
||||
sourceFile_(file), fileLine_(line), level_(level) {
|
||||
formatTime();
|
||||
logStream_ << T(logLevelStr[level_], 7);
|
||||
}
|
||||
Logger::Logger(SourceFile file, int line, LogLevel level, const char *func) :
|
||||
sourceFile_(file), fileLine_(line), level_(level) {
|
||||
formatTime();
|
||||
logStream_ << T(logLevelStr[level_], 7) << "[" << func << "] ";
|
||||
}
|
||||
Logger::Logger(SourceFile file, int line, bool) :
|
||||
sourceFile_(file), fileLine_(line), level_(kFatal) {
|
||||
formatTime();
|
||||
logStream_ << T(logLevelStr[level_], 7);
|
||||
if (errno != 0) {
|
||||
logStream_ << strerror_tl(errno) << " (errno=" << errno << ") ";
|
||||
}
|
||||
}
|
||||
RawLogger::~RawLogger() {
|
||||
if (index_ < 0) {
|
||||
auto &oFunc = Logger::outputFunc_();
|
||||
if (!oFunc)
|
||||
return;
|
||||
oFunc(logStream_.bufferData(), logStream_.bufferLength());
|
||||
} else {
|
||||
auto &oFunc = Logger::outputFunc_(index_);
|
||||
if (!oFunc)
|
||||
return;
|
||||
oFunc(logStream_.bufferData(), logStream_.bufferLength());
|
||||
}
|
||||
}
|
||||
Logger::~Logger() {
|
||||
logStream_ << T(" - ", 3) << sourceFile_ << ':' << fileLine_ << '\n';
|
||||
if (index_ < 0) {
|
||||
auto &oFunc = Logger::outputFunc_();
|
||||
if (!oFunc)
|
||||
return;
|
||||
oFunc(logStream_.bufferData(), logStream_.bufferLength());
|
||||
if (level_ >= kError)
|
||||
Logger::flushFunc_()();
|
||||
} else {
|
||||
auto &oFunc = Logger::outputFunc_(index_);
|
||||
if (!oFunc)
|
||||
return;
|
||||
oFunc(logStream_.bufferData(), logStream_.bufferLength());
|
||||
if (level_ >= kError)
|
||||
Logger::flushFunc_(index_)();
|
||||
}
|
||||
|
||||
// logStream_.resetBuffer();
|
||||
}
|
||||
LogStream &Logger::stream() {
|
||||
return logStream_;
|
||||
}
|
||||
|
@ -1,27 +1,397 @@
|
||||
#ifndef LOGGER_H
|
||||
#define LOGGER_H
|
||||
|
||||
#include "core/math/date.h"
|
||||
#include "log_stream.h"
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
class String;
|
||||
|
||||
class Logger {
|
||||
class RLogger {
|
||||
public:
|
||||
static void log_trace(const String &str);
|
||||
static void log_trace(const char *str);
|
||||
static void log_trace(const String &str);
|
||||
static void log_trace(const char *str);
|
||||
|
||||
static void log_message(const String &str);
|
||||
static void log_message(const char *str);
|
||||
static void log_message(const String &str);
|
||||
static void log_message(const char *str);
|
||||
|
||||
static void log_warning(const String &str);
|
||||
static void log_warning(const char *str);
|
||||
static void log_warning(const String &str);
|
||||
static void log_warning(const char *str);
|
||||
|
||||
static void log_error(const String &str);
|
||||
static void log_error(const char *str);
|
||||
static void log_error(const String &str);
|
||||
static void log_error(const char *str);
|
||||
|
||||
static void _log_error(const char *p_function, const char *p_file, int p_line, const char *str);
|
||||
static void _log_error(const char *p_function, const char *p_file, int p_line, const String &str);
|
||||
static void _log_msg_error(const char *p_function, const char *p_file, int p_line, const char *p_msg, const char *str);
|
||||
static void _log_index_error(const char *p_function, const char *p_file, int p_line, const int index, const int size, const char *str);
|
||||
static void _log_error(const char *p_function, const char *p_file, int p_line, const char *str);
|
||||
static void _log_error(const char *p_function, const char *p_file, int p_line, const String &str);
|
||||
static void _log_msg_error(const char *p_function, const char *p_file, int p_line, const char *p_msg, const char *str);
|
||||
static void _log_index_error(const char *p_function, const char *p_file, int p_line, const int index, const int size, const char *str);
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @file Logger.h
|
||||
* @author An Tao
|
||||
*
|
||||
* Public header file in trantor lib.
|
||||
*
|
||||
* Copyright 2018, An Tao. All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the License file.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace trantor {
|
||||
/**
|
||||
* @brief This class implements log functions.
|
||||
*
|
||||
*/
|
||||
class Logger {
|
||||
protected:
|
||||
Logger(const Logger &) = delete;
|
||||
Logger &operator=(const Logger &) = delete;
|
||||
// some uncopyable classes maybe support move constructor....
|
||||
Logger(Logger &&) noexcept(true) = default;
|
||||
Logger &operator=(Logger &&) noexcept(true) = default;
|
||||
|
||||
public:
|
||||
enum LogLevel {
|
||||
kTrace = 0,
|
||||
kDebug,
|
||||
kInfo,
|
||||
kWarn,
|
||||
kError,
|
||||
kFatal,
|
||||
kNumberOfLogLevels
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Calculate of basename of source files in compile time.
|
||||
*
|
||||
*/
|
||||
class SourceFile {
|
||||
public:
|
||||
template <int N>
|
||||
inline SourceFile(const char (&arr)[N]) :
|
||||
data_(arr), size_(N - 1) {
|
||||
// std::cout<<data_<<std::endl;
|
||||
const char *slash = strrchr(data_, '/'); // builtin function
|
||||
if (slash) {
|
||||
data_ = slash + 1;
|
||||
size_ -= static_cast<int>(data_ - arr);
|
||||
}
|
||||
}
|
||||
|
||||
explicit SourceFile(const char *filename) :
|
||||
data_(filename) {
|
||||
const char *slash = strrchr(filename, '/');
|
||||
if (slash) {
|
||||
data_ = slash + 1;
|
||||
}
|
||||
size_ = static_cast<int>(strlen(data_));
|
||||
}
|
||||
|
||||
const char *data_;
|
||||
int size_;
|
||||
};
|
||||
Logger(SourceFile file, int line);
|
||||
Logger(SourceFile file, int line, LogLevel level);
|
||||
Logger(SourceFile file, int line, bool isSysErr);
|
||||
Logger(SourceFile file, int line, LogLevel level, const char *func);
|
||||
~Logger();
|
||||
Logger &setIndex(int index) {
|
||||
index_ = index;
|
||||
return *this;
|
||||
}
|
||||
LogStream &stream();
|
||||
|
||||
/**
|
||||
* @brief Set the output function.
|
||||
*
|
||||
* @param outputFunc The function to output a log message.
|
||||
* @param flushFunc The function to flush.
|
||||
* @note Logs are output to the standard output by default.
|
||||
*/
|
||||
static void setOutputFunction(
|
||||
std::function<void(const char *msg, const uint64_t len)> outputFunc,
|
||||
std::function<void()> flushFunc,
|
||||
int index = -1) {
|
||||
if (index < 0) {
|
||||
outputFunc_() = outputFunc;
|
||||
flushFunc_() = flushFunc;
|
||||
} else {
|
||||
outputFunc_(index) = outputFunc;
|
||||
flushFunc_(index) = flushFunc;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the log level. Logs below the level are not printed.
|
||||
*
|
||||
* @param level
|
||||
*/
|
||||
static void setLogLevel(LogLevel level) {
|
||||
logLevel_() = level;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the current log level.
|
||||
*
|
||||
* @return LogLevel
|
||||
*/
|
||||
static LogLevel logLevel() {
|
||||
return logLevel_();
|
||||
}
|
||||
|
||||
protected:
|
||||
static void defaultOutputFunction(const char *msg, const uint64_t len) {
|
||||
fwrite(msg, 1, len, stdout);
|
||||
}
|
||||
static void defaultFlushFunction() {
|
||||
fflush(stdout);
|
||||
}
|
||||
void formatTime();
|
||||
static LogLevel &logLevel_() {
|
||||
#ifdef RELEASE
|
||||
static LogLevel logLevel = LogLevel::kInfo;
|
||||
#else
|
||||
static LogLevel logLevel = LogLevel::kDebug;
|
||||
#endif
|
||||
return logLevel;
|
||||
}
|
||||
static std::function<void(const char *msg, const uint64_t len)>
|
||||
&outputFunc_() {
|
||||
static std::function<void(const char *msg, const uint64_t len)>
|
||||
outputFunc = Logger::defaultOutputFunction;
|
||||
return outputFunc;
|
||||
}
|
||||
static std::function<void()> &flushFunc_() {
|
||||
static std::function<void()> flushFunc = Logger::defaultFlushFunction;
|
||||
return flushFunc;
|
||||
}
|
||||
static std::function<void(const char *msg, const uint64_t len)>
|
||||
&outputFunc_(size_t index) {
|
||||
static std::vector<
|
||||
std::function<void(const char *msg, const uint64_t len)> >
|
||||
outputFuncs;
|
||||
if (index < outputFuncs.size()) {
|
||||
return outputFuncs[index];
|
||||
}
|
||||
while (index >= outputFuncs.size()) {
|
||||
outputFuncs.emplace_back(outputFunc_());
|
||||
}
|
||||
return outputFuncs[index];
|
||||
}
|
||||
static std::function<void()> &flushFunc_(size_t index) {
|
||||
static std::vector<std::function<void()> > flushFuncs;
|
||||
if (index < flushFuncs.size()) {
|
||||
return flushFuncs[index];
|
||||
}
|
||||
while (index >= flushFuncs.size()) {
|
||||
flushFuncs.emplace_back(flushFunc_());
|
||||
}
|
||||
return flushFuncs[index];
|
||||
}
|
||||
friend class RawLogger;
|
||||
LogStream logStream_;
|
||||
Date date_{ Date::now() };
|
||||
SourceFile sourceFile_;
|
||||
int fileLine_;
|
||||
LogLevel level_;
|
||||
int index_{ -1 };
|
||||
};
|
||||
|
||||
class RawLogger {
|
||||
protected:
|
||||
RawLogger(const RawLogger &) = delete;
|
||||
RawLogger &operator=(const RawLogger &) = delete;
|
||||
// some uncopyable classes maybe support move constructor....
|
||||
RawLogger(RawLogger &&) noexcept(true) = default;
|
||||
RawLogger &operator=(RawLogger &&) noexcept(true) = default;
|
||||
|
||||
public:
|
||||
RawLogger() {}
|
||||
~RawLogger();
|
||||
RawLogger &setIndex(int index) {
|
||||
index_ = index;
|
||||
return *this;
|
||||
}
|
||||
LogStream &stream() {
|
||||
return logStream_;
|
||||
}
|
||||
|
||||
private:
|
||||
LogStream logStream_;
|
||||
int index_{ -1 };
|
||||
};
|
||||
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define LOG_TRACE \
|
||||
if (0) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#else
|
||||
#define LOG_TRACE \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define LOG_TRACE_TO(index) \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.setIndex(index) \
|
||||
.stream()
|
||||
|
||||
#endif
|
||||
|
||||
#define LOG_DEBUG \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kDebug) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define LOG_DEBUG_TO(index) \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kDebug) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
|
||||
.setIndex(index) \
|
||||
.stream()
|
||||
#define LOG_INFO \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kInfo) \
|
||||
trantor::Logger(__FILE__, __LINE__).stream()
|
||||
#define LOG_INFO_TO(index) \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kInfo) \
|
||||
trantor::Logger(__FILE__, __LINE__).setIndex(index).stream()
|
||||
#define LOG_WARN \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kWarn).stream()
|
||||
#define LOG_WARN_TO(index) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kWarn) \
|
||||
.setIndex(index) \
|
||||
.stream()
|
||||
#define LOG_ERROR \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kError).stream()
|
||||
#define LOG_ERROR_TO(index) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kError) \
|
||||
.setIndex(index) \
|
||||
.stream()
|
||||
#define LOG_FATAL \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kFatal).stream()
|
||||
#define LOG_FATAL_TO(index) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kFatal) \
|
||||
.setIndex(index) \
|
||||
.stream()
|
||||
#define LOG_SYSERR trantor::Logger(__FILE__, __LINE__, true).stream()
|
||||
#define LOG_SYSERR_TO(index) \
|
||||
trantor::Logger(__FILE__, __LINE__, true).setIndex(index).stream()
|
||||
|
||||
#define LOG_RAW trantor::RawLogger().stream()
|
||||
#define LOG_RAW_TO(index) trantor::RawLogger().setIndex(index).stream()
|
||||
|
||||
#define LOG_TRACE_IF(cond) \
|
||||
if ((trantor::Logger::logLevel() <= trantor::Logger::kTrace) && (cond)) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define LOG_DEBUG_IF(cond) \
|
||||
if ((Tensor::Logger::logLevel() <= Tensor::Logger::kDebug) && (cond)) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define LOG_INFO_IF(cond) \
|
||||
if ((Tensor::Logger::logLevel() <= Tensor::Logger::kInfo) && (cond)) \
|
||||
Tensor::Logger(__FILE__, __LINE__).stream()
|
||||
#define LOG_WARN_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kWarn).stream()
|
||||
#define LOG_ERROR_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kError).stream()
|
||||
#define LOG_FATAL_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kFatal).stream()
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define DLOG_TRACE \
|
||||
if (0) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define DLOG_DEBUG \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define DLOG_INFO \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__).stream()
|
||||
#define DLOG_WARN \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kWarn).stream()
|
||||
#define DLOG_ERROR \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kError).stream()
|
||||
#define DLOG_FATAL \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kFatal).stream()
|
||||
|
||||
#define DLOG_TRACE_IF(cond) \
|
||||
if (0) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define DLOG_DEBUG_IF(cond) \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define DLOG_INFO_IF(cond) \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__).stream()
|
||||
#define DLOG_WARN_IF(cond) \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kWarn).stream()
|
||||
#define DLOG_ERROR_IF(cond) \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kError).stream()
|
||||
#define DLOG_FATAL_IF(cond) \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kFatal).stream()
|
||||
#else
|
||||
#define DLOG_TRACE \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define DLOG_DEBUG \
|
||||
if (Tensor::Logger::logLevel() <= Tensor::Logger::kDebug) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define DLOG_INFO \
|
||||
if (Tensor::Logger::logLevel() <= Tensor::Logger::kInfo) \
|
||||
Tensor::Logger(__FILE__, __LINE__).stream()
|
||||
#define DLOG_WARN \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kWarn).stream()
|
||||
#define DLOG_ERROR \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kError).stream()
|
||||
#define DLOG_FATAL \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kFatal).stream()
|
||||
|
||||
#define DLOG_TRACE_IF(cond) \
|
||||
if ((trantor::Logger::logLevel() <= trantor::Logger::kTrace) && (cond)) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define DLOG_DEBUG_IF(cond) \
|
||||
if ((Tensor::Logger::logLevel() <= Tensor::Logger::kDebug) && (cond)) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define DLOG_INFO_IF(cond) \
|
||||
if ((Tensor::Logger::logLevel() <= Tensor::Logger::kInfo) && (cond)) \
|
||||
Tensor::Logger(__FILE__, __LINE__).stream()
|
||||
#define DLOG_WARN_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kWarn).stream()
|
||||
#define DLOG_ERROR_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kError).stream()
|
||||
#define DLOG_FATAL_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kFatal).stream()
|
||||
#endif
|
||||
|
||||
const char *strerror_tl(int savedErrno);
|
||||
} // namespace trantor
|
||||
|
||||
#endif
|
||||
|
@ -14,7 +14,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <trantor/exports.h>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
@ -26,7 +25,7 @@ namespace trantor
|
||||
* @brief This class represents a time point.
|
||||
*
|
||||
*/
|
||||
class TRANTOR_EXPORT Date
|
||||
class Date
|
||||
{
|
||||
public:
|
||||
Date() : microSecondsSinceEpoch_(0){};
|
||||
|
@ -17,7 +17,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/net/inet_address.h"
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <string>
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
|
@ -19,7 +19,7 @@
|
||||
#include <drogon/utils/string_view.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include "core/containers/msg_buffer.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
@ -17,7 +17,7 @@
|
||||
#include <trantor/net/EventLoop.h>
|
||||
#include "core/net/inet_address.h"
|
||||
#include "core/math/date.h"
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
|
||||
#include <http/CacheMap.h>
|
||||
#include <http/Cookie.h>
|
||||
|
@ -9,7 +9,7 @@
|
||||
#include <http/HttpRequest.h>
|
||||
#include <http/HttpResponse.h>
|
||||
#include <drogon/plugins/Plugin.h>
|
||||
#include <trantor/utils/AsyncFileLogger.h>
|
||||
#include "core/log/async_file_logger.h"
|
||||
#include <vector>
|
||||
|
||||
namespace drogon {
|
||||
|
@ -14,7 +14,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <json/json.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/utils/NonCopyable.h>
|
||||
#include <memory>
|
||||
|
||||
|
@ -20,7 +20,7 @@
|
||||
#include <boost/utility/string_view.hpp>
|
||||
#endif
|
||||
|
||||
#include <trantor/utils/LogStream.h>
|
||||
#include "core/log/log_stream.h"
|
||||
|
||||
namespace drogon {
|
||||
#if __cplusplus >= 201703L || (defined _MSC_VER && _MSC_VER > 1900)
|
||||
|
@ -15,7 +15,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <drogon/utils/any.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
|
@ -13,7 +13,7 @@
|
||||
*/
|
||||
|
||||
#include "CacheFile.h"
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#ifdef _WIN32
|
||||
#include <mman.h>
|
||||
#else
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <trantor/net/EventLoop.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <atomic>
|
||||
#include <deque>
|
||||
#include <future>
|
||||
@ -134,7 +134,7 @@ public:
|
||||
for (auto iter = wheels_.rbegin(); iter != wheels_.rend(); ++iter) {
|
||||
iter->clear();
|
||||
}
|
||||
LOG_TRACE << "CacheMap destruct!";
|
||||
//LOG_TRACE << "CacheMap destruct!";
|
||||
}
|
||||
struct MapValue {
|
||||
MapValue(const T2 &value,
|
||||
|
@ -22,7 +22,7 @@
|
||||
#include <stdio.h>
|
||||
#include <trantor/net/EventLoop.h>
|
||||
#include "core/net/inet_address.h"
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include "core/containers/msg_buffer.h"
|
||||
#include <trantor/utils/NonCopyable.h>
|
||||
#include <algorithm>
|
||||
|
@ -18,7 +18,7 @@
|
||||
#include "HttpResponseImpl.h"
|
||||
#include "HttpUtils.h"
|
||||
#include <http/HttpTypes.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include "core/containers/msg_buffer.h"
|
||||
#include <iostream>
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
||||
#include <drogon/HttpViewData.h>
|
||||
#include <drogon/IOThreadStorage.h>
|
||||
#include <sys/stat.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
#include "HttpResponseParser.h"
|
||||
#include "HttpResponseImpl.h"
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include "core/containers/msg_buffer.h"
|
||||
#include <iostream>
|
||||
using namespace trantor;
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
#include "HttpUtils.h"
|
||||
#include <drogon/utils/Utilities.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <unordered_map>
|
||||
|
||||
namespace drogon {
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
#include <drogon/utils/any.h>
|
||||
#include <drogon/utils/optional.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
@ -33,7 +33,7 @@
|
||||
|
||||
#include <drogon/utils/Utilities.h>
|
||||
#include <json/json.h>
|
||||
#include <trantor/utils/AsyncFileLogger.h>
|
||||
#include "core/log/async_file_logger.h"
|
||||
#include <algorithm>
|
||||
|
||||
#include <fstream>
|
||||
|
@ -21,7 +21,7 @@
|
||||
#include <http/HttpRequest.h>
|
||||
#include <http/HttpResponse.h>
|
||||
#include <drogon/utils/Utilities.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <functional>
|
||||
|
||||
#if COZ_PROFILING
|
||||
|
@ -19,7 +19,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#ifndef _WIN32
|
||||
#include <sys/file.h>
|
||||
#include <sys/wait.h>
|
||||
|
@ -13,7 +13,7 @@
|
||||
*/
|
||||
|
||||
#include "PluginsManager.h"
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
|
||||
using namespace drogon;
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <unistd.h>
|
||||
#include <fstream>
|
||||
static void forEachFileIn(
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
|
||||
#include <drogon/utils/Utilities.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#ifdef OPENSSL_FOUND
|
||||
#include <openssl/md5.h>
|
||||
#include <openssl/rand.h>
|
||||
|
@ -13,7 +13,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <trantor/net/TcpConnection.h>
|
||||
#include <trantor/utils/AsyncFileLogger.h>
|
||||
#include "core/log/async_file_logger.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
#include "core/net/inet_address.h"
|
||||
#include <trantor/net/Resolver.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
|
||||
#include "http/HttpRequestImpl.h"
|
||||
#include "http/HttpResponse.h"
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/utils/NonCopyable.h>
|
||||
#include <trantor/exports.h>
|
||||
#include <functional>
|
||||
|
@ -16,7 +16,7 @@
|
||||
// Author: Tao An
|
||||
|
||||
#include <trantor/net/EventLoop.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
|
||||
#include "Channel.h"
|
||||
#include "Poller.h"
|
||||
|
@ -13,7 +13,7 @@
|
||||
*/
|
||||
|
||||
#include <trantor/net/EventLoopThread.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#ifdef __linux__
|
||||
#include <sys/prctl.h>
|
||||
#endif
|
||||
|
@ -14,7 +14,7 @@
|
||||
#include "Connector.h"
|
||||
#include "inner/TcpConnectionImpl.h"
|
||||
#include <trantor/net/EventLoop.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
|
@ -15,7 +15,7 @@
|
||||
#include "Acceptor.h"
|
||||
#include "inner/TcpConnectionImpl.h"
|
||||
#include <trantor/net/TcpServer.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
using namespace trantor;
|
||||
|
@ -15,7 +15,7 @@
|
||||
#pragma once
|
||||
#include <trantor/net/callbacks.h>
|
||||
#include <trantor/utils/NonCopyable.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/net/EventLoopThreadPool.h>
|
||||
#include "core/net/inet_address.h"
|
||||
#include <trantor/net/TcpConnection.h>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "NormalResolver.h"
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#ifdef _WIN32
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
#include "Timer.h"
|
||||
#include <trantor/net/EventLoop.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
|
||||
namespace trantor {
|
||||
std::atomic<TimerId> Timer::timersCreated_ = ATOMIC_VAR_INIT(InvalidTimerId);
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
#include "EpollPoller.h"
|
||||
#include "Channel.h"
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#ifdef __linux__
|
||||
#include <assert.h>
|
||||
#include <poll.h>
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <sys/event.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
namespace trantor {
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include <trantor/utils/AsyncFileLogger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include "core/log/async_file_logger.h"
|
||||
#include <stdlib.h>
|
||||
#include <thread>
|
||||
int main()
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include <trantor/utils/AsyncFileLogger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include "core/log/async_file_logger.h"
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <stdlib.h>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/utils/ConcurrentTaskQueue.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <iostream>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/net/TcpClient.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/net/EventLoopThread.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/net/TcpServer.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/net/EventLoopThread.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/net/TcpServer.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/net/EventLoopThread.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <stdlib.h>
|
||||
#include <thread>
|
||||
int main()
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/net/TcpClient.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/net/EventLoopThread.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/net/TcpServer.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/net/EventLoopThread.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/net/TcpServer.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/net/EventLoopThread.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/utils/SerialTaskQueue.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <stdio.h>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/net/TcpClient.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/net/EventLoopThread.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/net/TcpServer.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/net/EventLoopThread.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/net/EventLoop.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/net/EventLoop.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <iostream>
|
||||
int main()
|
||||
{
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <trantor/utils/TimingWheel.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <memory>
|
||||
class MyClass
|
||||
{
|
||||
|
@ -1,140 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* @file AsyncFileLogger.h
|
||||
* @author An Tao
|
||||
*
|
||||
* Public header file in trantor lib.
|
||||
*
|
||||
* Copyright 2018, An Tao. All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the License file.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <trantor/utils/NonCopyable.h>
|
||||
#include "core/math/date.h"
|
||||
#include <trantor/exports.h>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <condition_variable>
|
||||
#include <sstream>
|
||||
#include <memory>
|
||||
#include <queue>
|
||||
|
||||
namespace trantor
|
||||
{
|
||||
using StringPtr = std::shared_ptr<std::string>;
|
||||
using StringPtrQueue = std::queue<StringPtr>;
|
||||
|
||||
/**
|
||||
* @brief This class implements utility functions for writing logs to files
|
||||
* asynchronously.
|
||||
*
|
||||
*/
|
||||
class TRANTOR_EXPORT AsyncFileLogger : NonCopyable
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Write the message to the log file.
|
||||
*
|
||||
* @param msg
|
||||
* @param len
|
||||
*/
|
||||
void output(const char *msg, const uint64_t len);
|
||||
|
||||
/**
|
||||
* @brief Flush data from memory buffer to the log file.
|
||||
*
|
||||
*/
|
||||
void flush();
|
||||
|
||||
/**
|
||||
* @brief Start writing log files.
|
||||
*
|
||||
*/
|
||||
void startLogging();
|
||||
|
||||
/**
|
||||
* @brief Set the size limit of log files. When the log file size reaches
|
||||
* the limit, the log file is switched.
|
||||
*
|
||||
* @param limit
|
||||
*/
|
||||
void setFileSizeLimit(uint64_t limit)
|
||||
{
|
||||
sizeLimit_ = limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the log file name.
|
||||
*
|
||||
* @param baseName The base name of the log file.
|
||||
* @param extName The extended name of the log file.
|
||||
* @param path The location where the log file is stored.
|
||||
*/
|
||||
void setFileName(const std::string &baseName,
|
||||
const std::string &extName = ".log",
|
||||
const std::string &path = "./")
|
||||
{
|
||||
fileBaseName_ = baseName;
|
||||
extName[0] == '.' ? fileExtName_ = extName
|
||||
: fileExtName_ = std::string(".") + extName;
|
||||
filePath_ = path;
|
||||
if (filePath_.length() == 0)
|
||||
filePath_ = "./";
|
||||
if (filePath_[filePath_.length() - 1] != '/')
|
||||
filePath_ = filePath_ + "/";
|
||||
}
|
||||
~AsyncFileLogger();
|
||||
AsyncFileLogger();
|
||||
|
||||
protected:
|
||||
std::mutex mutex_;
|
||||
std::condition_variable cond_;
|
||||
StringPtr logBufferPtr_;
|
||||
StringPtr nextBufferPtr_;
|
||||
StringPtrQueue writeBuffers_;
|
||||
StringPtrQueue tmpBuffers_;
|
||||
void writeLogToFile(const StringPtr buf);
|
||||
std::unique_ptr<std::thread> threadPtr_;
|
||||
bool stopFlag_{false};
|
||||
void logThreadFunc();
|
||||
std::string filePath_{"./"};
|
||||
std::string fileBaseName_{"trantor"};
|
||||
std::string fileExtName_{".log"};
|
||||
uint64_t sizeLimit_{20 * 1024 * 1024};
|
||||
class LoggerFile : NonCopyable
|
||||
{
|
||||
public:
|
||||
LoggerFile(const std::string &filePath,
|
||||
const std::string &fileBaseName,
|
||||
const std::string &fileExtName);
|
||||
~LoggerFile();
|
||||
void writeLog(const StringPtr buf);
|
||||
uint64_t getLength();
|
||||
explicit operator bool() const
|
||||
{
|
||||
return fp_ != nullptr;
|
||||
}
|
||||
void flush();
|
||||
|
||||
protected:
|
||||
FILE *fp_{nullptr};
|
||||
Date creationDate_;
|
||||
std::string fileFullName_;
|
||||
std::string filePath_;
|
||||
std::string fileBaseName_;
|
||||
std::string fileExtName_;
|
||||
static uint64_t fileSeq_;
|
||||
};
|
||||
std::unique_ptr<LoggerFile> loggerFilePtr_;
|
||||
|
||||
uint64_t lostCounter_{0};
|
||||
void swapBuffer();
|
||||
};
|
||||
|
||||
} // namespace trantor
|
@ -14,7 +14,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <trantor/utils/ConcurrentTaskQueue.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#ifdef __linux__
|
||||
#include <sys/prctl.h>
|
||||
#endif
|
||||
|
@ -1,277 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* @file LogStream.h
|
||||
* @author An Tao
|
||||
*
|
||||
* Public header file in trantor lib.
|
||||
*
|
||||
* Copyright 2018, An Tao. All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the License file.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
// Taken from muduo lib and modified. Classes in this file are used internally.
|
||||
#include <trantor/utils/NonCopyable.h>
|
||||
#include <trantor/exports.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h> // memcpy
|
||||
#include <string>
|
||||
|
||||
namespace trantor
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
static constexpr size_t kSmallBuffer{4000};
|
||||
static constexpr size_t kLargeBuffer{4000 * 1000};
|
||||
|
||||
template <int SIZE>
|
||||
class TRANTOR_EXPORT FixedBuffer : NonCopyable
|
||||
{
|
||||
public:
|
||||
FixedBuffer() : cur_(data_)
|
||||
{
|
||||
setCookie(cookieStart);
|
||||
}
|
||||
|
||||
~FixedBuffer()
|
||||
{
|
||||
setCookie(cookieEnd);
|
||||
}
|
||||
|
||||
bool append(const char * /*restrict*/ buf, size_t len)
|
||||
{
|
||||
if ((size_t)(avail()) > len)
|
||||
{
|
||||
memcpy(cur_, buf, len);
|
||||
cur_ += len;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *data() const
|
||||
{
|
||||
return data_;
|
||||
}
|
||||
int length() const
|
||||
{
|
||||
return static_cast<int>(cur_ - data_);
|
||||
}
|
||||
|
||||
// write to data_ directly
|
||||
char *current()
|
||||
{
|
||||
return cur_;
|
||||
}
|
||||
int avail() const
|
||||
{
|
||||
return static_cast<int>(end() - cur_);
|
||||
}
|
||||
void add(size_t len)
|
||||
{
|
||||
cur_ += len;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
cur_ = data_;
|
||||
}
|
||||
void zeroBuffer()
|
||||
{
|
||||
memset(data_, 0, sizeof(data_));
|
||||
}
|
||||
|
||||
// for used by GDB
|
||||
const char *debugString();
|
||||
void setCookie(void (*cookie)())
|
||||
{
|
||||
cookie_ = cookie;
|
||||
}
|
||||
// for used by unit test
|
||||
std::string toString() const
|
||||
{
|
||||
return std::string(data_, length());
|
||||
}
|
||||
// StringPiece toStringPiece() const { return StringPiece(data_, length());
|
||||
// }
|
||||
|
||||
private:
|
||||
const char *end() const
|
||||
{
|
||||
return data_ + sizeof data_;
|
||||
}
|
||||
// Must be outline function for cookies.
|
||||
static void cookieStart();
|
||||
static void cookieEnd();
|
||||
|
||||
void (*cookie_)();
|
||||
char data_[SIZE];
|
||||
char *cur_;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
class TRANTOR_EXPORT LogStream : NonCopyable
|
||||
{
|
||||
using self = LogStream;
|
||||
|
||||
public:
|
||||
using Buffer = detail::FixedBuffer<detail::kSmallBuffer>;
|
||||
|
||||
self &operator<<(bool v)
|
||||
{
|
||||
append(v ? "1" : "0", 1);
|
||||
return *this;
|
||||
}
|
||||
|
||||
self &operator<<(short);
|
||||
self &operator<<(unsigned short);
|
||||
self &operator<<(int);
|
||||
self &operator<<(unsigned int);
|
||||
self &operator<<(long);
|
||||
self &operator<<(unsigned long);
|
||||
self &operator<<(const long long &);
|
||||
self &operator<<(const unsigned long long &);
|
||||
|
||||
self &operator<<(const void *);
|
||||
|
||||
self &operator<<(float &v)
|
||||
{
|
||||
*this << static_cast<double>(v);
|
||||
return *this;
|
||||
}
|
||||
self &operator<<(const double &);
|
||||
self &operator<<(const long double &v);
|
||||
|
||||
self &operator<<(char v)
|
||||
{
|
||||
append(&v, 1);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// self& operator<<(signed char);
|
||||
// self& operator<<(unsigned char);
|
||||
template <int N>
|
||||
self &operator<<(const char (&buf)[N])
|
||||
{
|
||||
assert(strnlen(buf, N) == N - 1);
|
||||
append(buf, N - 1);
|
||||
return *this;
|
||||
}
|
||||
|
||||
self &operator<<(char *str)
|
||||
{
|
||||
if (str)
|
||||
{
|
||||
append(str, strlen(str));
|
||||
}
|
||||
else
|
||||
{
|
||||
append("(null)", 6);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
self &operator<<(const char *str)
|
||||
{
|
||||
if (str)
|
||||
{
|
||||
append(str, strlen(str));
|
||||
}
|
||||
else
|
||||
{
|
||||
append("(null)", 6);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
self &operator<<(const unsigned char *str)
|
||||
{
|
||||
return operator<<(reinterpret_cast<const char *>(str));
|
||||
}
|
||||
|
||||
self &operator<<(const std::string &v)
|
||||
{
|
||||
append(v.c_str(), v.size());
|
||||
return *this;
|
||||
}
|
||||
|
||||
void append(const char *data, size_t len)
|
||||
{
|
||||
if (exBuffer_.empty())
|
||||
{
|
||||
if (!buffer_.append(data, len))
|
||||
{
|
||||
exBuffer_.append(buffer_.data(), buffer_.length());
|
||||
exBuffer_.append(data, len);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
exBuffer_.append(data, len);
|
||||
}
|
||||
}
|
||||
// const Buffer& buffer() const { return buffer_; }
|
||||
const char *bufferData() const
|
||||
{
|
||||
if (!exBuffer_.empty())
|
||||
{
|
||||
return exBuffer_.data();
|
||||
}
|
||||
return buffer_.data();
|
||||
}
|
||||
|
||||
size_t bufferLength() const
|
||||
{
|
||||
if (!exBuffer_.empty())
|
||||
{
|
||||
return exBuffer_.length();
|
||||
}
|
||||
return buffer_.length();
|
||||
}
|
||||
void resetBuffer()
|
||||
{
|
||||
buffer_.reset();
|
||||
exBuffer_.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
void formatInteger(T);
|
||||
|
||||
Buffer buffer_;
|
||||
std::string exBuffer_;
|
||||
};
|
||||
|
||||
class TRANTOR_EXPORT Fmt // : boost::noncopyable
|
||||
{
|
||||
public:
|
||||
template <typename T>
|
||||
Fmt(const char *fmt, T val);
|
||||
|
||||
const char *data() const
|
||||
{
|
||||
return buf_;
|
||||
}
|
||||
int length() const
|
||||
{
|
||||
return length_;
|
||||
}
|
||||
|
||||
private:
|
||||
char buf_[48];
|
||||
int length_;
|
||||
};
|
||||
|
||||
inline LogStream &operator<<(LogStream &s, const Fmt &fmt)
|
||||
{
|
||||
s.append(fmt.data(), fmt.length());
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace trantor
|
@ -1,187 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Logger.cc
|
||||
* An Tao
|
||||
*
|
||||
* Public header file in trantor lib.
|
||||
*
|
||||
* Copyright 2018, An Tao. All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the License file.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include <thread>
|
||||
#ifndef _WIN32
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#include <sstream>
|
||||
#endif
|
||||
#ifdef __FreeBSD__
|
||||
#include <pthread_np.h>
|
||||
#endif
|
||||
|
||||
namespace trantor {
|
||||
// helper class for known string length at compile time
|
||||
class T {
|
||||
public:
|
||||
T(const char *str, unsigned len) :
|
||||
str_(str), len_(len) {
|
||||
assert(strlen(str) == len_);
|
||||
}
|
||||
|
||||
const char *str_;
|
||||
const unsigned len_;
|
||||
};
|
||||
|
||||
const char *strerror_tl(int savedErrno) {
|
||||
#ifndef _MSC_VER
|
||||
return strerror(savedErrno);
|
||||
#else
|
||||
static thread_local char errMsg[64];
|
||||
(void)strerror_s<sizeof errMsg>(errMsg, savedErrno);
|
||||
return errMsg;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline LogStream &operator<<(LogStream &s, T v) {
|
||||
s.append(v.str_, v.len_);
|
||||
return s;
|
||||
}
|
||||
|
||||
inline LogStream &operator<<(LogStream &s, const Logger::SourceFile &v) {
|
||||
s.append(v.data_, v.size_);
|
||||
return s;
|
||||
}
|
||||
} // namespace trantor
|
||||
using namespace trantor;
|
||||
|
||||
static thread_local uint64_t lastSecond_{ 0 };
|
||||
static thread_local char lastTimeString_[32] = { 0 };
|
||||
#ifdef __linux__
|
||||
static thread_local pid_t threadId_{ 0 };
|
||||
#else
|
||||
static thread_local uint64_t threadId_{ 0 };
|
||||
#endif
|
||||
// static thread_local LogStream logStream_;
|
||||
|
||||
void Logger::formatTime() {
|
||||
uint64_t now = static_cast<uint64_t>(date_.secondsSinceEpoch());
|
||||
uint64_t microSec =
|
||||
static_cast<uint64_t>(date_.microSecondsSinceEpoch() -
|
||||
date_.roundSecond().microSecondsSinceEpoch());
|
||||
if (now != lastSecond_) {
|
||||
lastSecond_ = now;
|
||||
#ifndef _MSC_VER
|
||||
strncpy(lastTimeString_,
|
||||
date_.toFormattedString(false).c_str(),
|
||||
sizeof(lastTimeString_) - 1);
|
||||
#else
|
||||
strncpy_s<sizeof lastTimeString_>(
|
||||
lastTimeString_,
|
||||
date_.toFormattedString(false).c_str(),
|
||||
sizeof(lastTimeString_) - 1);
|
||||
#endif
|
||||
}
|
||||
logStream_ << T(lastTimeString_, 17);
|
||||
char tmp[32];
|
||||
snprintf(tmp,
|
||||
sizeof(tmp),
|
||||
".%06llu UTC ",
|
||||
static_cast<long long unsigned int>(microSec));
|
||||
logStream_ << T(tmp, 12);
|
||||
#ifdef __linux__
|
||||
if (threadId_ == 0)
|
||||
threadId_ = static_cast<pid_t>(::syscall(SYS_gettid));
|
||||
#elif defined __FreeBSD__
|
||||
if (threadId_ == 0) {
|
||||
threadId_ = pthread_getthreadid_np();
|
||||
}
|
||||
#elif defined __OpenBSD__
|
||||
if (threadId_ == 0) {
|
||||
threadId_ = getthrid();
|
||||
}
|
||||
#elif defined _WIN32
|
||||
if (threadId_ == 0) {
|
||||
std::stringstream ss;
|
||||
ss << std::this_thread::get_id();
|
||||
threadId_ = std::stoull(ss.str());
|
||||
}
|
||||
#else
|
||||
if (threadId_ == 0) {
|
||||
pthread_threadid_np(NULL, &threadId_);
|
||||
}
|
||||
#endif
|
||||
logStream_ << threadId_;
|
||||
}
|
||||
static const char *logLevelStr[Logger::LogLevel::kNumberOfLogLevels] = {
|
||||
" TRACE ",
|
||||
" DEBUG ",
|
||||
" INFO ",
|
||||
" WARN ",
|
||||
" ERROR ",
|
||||
" FATAL ",
|
||||
};
|
||||
Logger::Logger(SourceFile file, int line) :
|
||||
sourceFile_(file), fileLine_(line), level_(kInfo) {
|
||||
formatTime();
|
||||
logStream_ << T(logLevelStr[level_], 7);
|
||||
}
|
||||
Logger::Logger(SourceFile file, int line, LogLevel level) :
|
||||
sourceFile_(file), fileLine_(line), level_(level) {
|
||||
formatTime();
|
||||
logStream_ << T(logLevelStr[level_], 7);
|
||||
}
|
||||
Logger::Logger(SourceFile file, int line, LogLevel level, const char *func) :
|
||||
sourceFile_(file), fileLine_(line), level_(level) {
|
||||
formatTime();
|
||||
logStream_ << T(logLevelStr[level_], 7) << "[" << func << "] ";
|
||||
}
|
||||
Logger::Logger(SourceFile file, int line, bool) :
|
||||
sourceFile_(file), fileLine_(line), level_(kFatal) {
|
||||
formatTime();
|
||||
logStream_ << T(logLevelStr[level_], 7);
|
||||
if (errno != 0) {
|
||||
logStream_ << strerror_tl(errno) << " (errno=" << errno << ") ";
|
||||
}
|
||||
}
|
||||
RawLogger::~RawLogger() {
|
||||
if (index_ < 0) {
|
||||
auto &oFunc = Logger::outputFunc_();
|
||||
if (!oFunc)
|
||||
return;
|
||||
oFunc(logStream_.bufferData(), logStream_.bufferLength());
|
||||
} else {
|
||||
auto &oFunc = Logger::outputFunc_(index_);
|
||||
if (!oFunc)
|
||||
return;
|
||||
oFunc(logStream_.bufferData(), logStream_.bufferLength());
|
||||
}
|
||||
}
|
||||
Logger::~Logger() {
|
||||
logStream_ << T(" - ", 3) << sourceFile_ << ':' << fileLine_ << '\n';
|
||||
if (index_ < 0) {
|
||||
auto &oFunc = Logger::outputFunc_();
|
||||
if (!oFunc)
|
||||
return;
|
||||
oFunc(logStream_.bufferData(), logStream_.bufferLength());
|
||||
if (level_ >= kError)
|
||||
Logger::flushFunc_()();
|
||||
} else {
|
||||
auto &oFunc = Logger::outputFunc_(index_);
|
||||
if (!oFunc)
|
||||
return;
|
||||
oFunc(logStream_.bufferData(), logStream_.bufferLength());
|
||||
if (level_ >= kError)
|
||||
Logger::flushFunc_(index_)();
|
||||
}
|
||||
|
||||
// logStream_.resetBuffer();
|
||||
}
|
||||
LogStream &Logger::stream() {
|
||||
return logStream_;
|
||||
}
|
@ -1,383 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* @file Logger.h
|
||||
* @author An Tao
|
||||
*
|
||||
* Public header file in trantor lib.
|
||||
*
|
||||
* Copyright 2018, An Tao. All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the License file.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <trantor/utils/NonCopyable.h>
|
||||
#include "core/math/date.h"
|
||||
#include <trantor/utils/LogStream.h>
|
||||
#include <trantor/exports.h>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
namespace trantor
|
||||
{
|
||||
/**
|
||||
* @brief This class implements log functions.
|
||||
*
|
||||
*/
|
||||
class TRANTOR_EXPORT Logger : public NonCopyable
|
||||
{
|
||||
public:
|
||||
enum LogLevel
|
||||
{
|
||||
kTrace = 0,
|
||||
kDebug,
|
||||
kInfo,
|
||||
kWarn,
|
||||
kError,
|
||||
kFatal,
|
||||
kNumberOfLogLevels
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Calculate of basename of source files in compile time.
|
||||
*
|
||||
*/
|
||||
class SourceFile
|
||||
{
|
||||
public:
|
||||
template <int N>
|
||||
inline SourceFile(const char (&arr)[N]) : data_(arr), size_(N - 1)
|
||||
{
|
||||
// std::cout<<data_<<std::endl;
|
||||
const char *slash = strrchr(data_, '/'); // builtin function
|
||||
if (slash)
|
||||
{
|
||||
data_ = slash + 1;
|
||||
size_ -= static_cast<int>(data_ - arr);
|
||||
}
|
||||
}
|
||||
|
||||
explicit SourceFile(const char *filename) : data_(filename)
|
||||
{
|
||||
const char *slash = strrchr(filename, '/');
|
||||
if (slash)
|
||||
{
|
||||
data_ = slash + 1;
|
||||
}
|
||||
size_ = static_cast<int>(strlen(data_));
|
||||
}
|
||||
|
||||
const char *data_;
|
||||
int size_;
|
||||
};
|
||||
Logger(SourceFile file, int line);
|
||||
Logger(SourceFile file, int line, LogLevel level);
|
||||
Logger(SourceFile file, int line, bool isSysErr);
|
||||
Logger(SourceFile file, int line, LogLevel level, const char *func);
|
||||
~Logger();
|
||||
Logger &setIndex(int index)
|
||||
{
|
||||
index_ = index;
|
||||
return *this;
|
||||
}
|
||||
LogStream &stream();
|
||||
|
||||
/**
|
||||
* @brief Set the output function.
|
||||
*
|
||||
* @param outputFunc The function to output a log message.
|
||||
* @param flushFunc The function to flush.
|
||||
* @note Logs are output to the standard output by default.
|
||||
*/
|
||||
static void setOutputFunction(
|
||||
std::function<void(const char *msg, const uint64_t len)> outputFunc,
|
||||
std::function<void()> flushFunc,
|
||||
int index = -1)
|
||||
{
|
||||
if (index < 0)
|
||||
{
|
||||
outputFunc_() = outputFunc;
|
||||
flushFunc_() = flushFunc;
|
||||
}
|
||||
else
|
||||
{
|
||||
outputFunc_(index) = outputFunc;
|
||||
flushFunc_(index) = flushFunc;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the log level. Logs below the level are not printed.
|
||||
*
|
||||
* @param level
|
||||
*/
|
||||
static void setLogLevel(LogLevel level)
|
||||
{
|
||||
logLevel_() = level;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the current log level.
|
||||
*
|
||||
* @return LogLevel
|
||||
*/
|
||||
static LogLevel logLevel()
|
||||
{
|
||||
return logLevel_();
|
||||
}
|
||||
|
||||
protected:
|
||||
static void defaultOutputFunction(const char *msg, const uint64_t len)
|
||||
{
|
||||
fwrite(msg, 1, len, stdout);
|
||||
}
|
||||
static void defaultFlushFunction()
|
||||
{
|
||||
fflush(stdout);
|
||||
}
|
||||
void formatTime();
|
||||
static LogLevel &logLevel_()
|
||||
{
|
||||
#ifdef RELEASE
|
||||
static LogLevel logLevel = LogLevel::kInfo;
|
||||
#else
|
||||
static LogLevel logLevel = LogLevel::kDebug;
|
||||
#endif
|
||||
return logLevel;
|
||||
}
|
||||
static std::function<void(const char *msg, const uint64_t len)>
|
||||
&outputFunc_()
|
||||
{
|
||||
static std::function<void(const char *msg, const uint64_t len)>
|
||||
outputFunc = Logger::defaultOutputFunction;
|
||||
return outputFunc;
|
||||
}
|
||||
static std::function<void()> &flushFunc_()
|
||||
{
|
||||
static std::function<void()> flushFunc = Logger::defaultFlushFunction;
|
||||
return flushFunc;
|
||||
}
|
||||
static std::function<void(const char *msg, const uint64_t len)>
|
||||
&outputFunc_(size_t index)
|
||||
{
|
||||
static std::vector<
|
||||
std::function<void(const char *msg, const uint64_t len)>>
|
||||
outputFuncs;
|
||||
if (index < outputFuncs.size())
|
||||
{
|
||||
return outputFuncs[index];
|
||||
}
|
||||
while (index >= outputFuncs.size())
|
||||
{
|
||||
outputFuncs.emplace_back(outputFunc_());
|
||||
}
|
||||
return outputFuncs[index];
|
||||
}
|
||||
static std::function<void()> &flushFunc_(size_t index)
|
||||
{
|
||||
static std::vector<std::function<void()>> flushFuncs;
|
||||
if (index < flushFuncs.size())
|
||||
{
|
||||
return flushFuncs[index];
|
||||
}
|
||||
while (index >= flushFuncs.size())
|
||||
{
|
||||
flushFuncs.emplace_back(flushFunc_());
|
||||
}
|
||||
return flushFuncs[index];
|
||||
}
|
||||
friend class RawLogger;
|
||||
LogStream logStream_;
|
||||
Date date_{Date::now()};
|
||||
SourceFile sourceFile_;
|
||||
int fileLine_;
|
||||
LogLevel level_;
|
||||
int index_{-1};
|
||||
};
|
||||
class TRANTOR_EXPORT RawLogger : public NonCopyable
|
||||
{
|
||||
public:
|
||||
~RawLogger();
|
||||
RawLogger &setIndex(int index)
|
||||
{
|
||||
index_ = index;
|
||||
return *this;
|
||||
}
|
||||
LogStream &stream()
|
||||
{
|
||||
return logStream_;
|
||||
}
|
||||
|
||||
private:
|
||||
LogStream logStream_;
|
||||
int index_{-1};
|
||||
};
|
||||
#ifdef NDEBUG
|
||||
#define LOG_TRACE \
|
||||
if (0) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#else
|
||||
#define LOG_TRACE \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define LOG_TRACE_TO(index) \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.setIndex(index) \
|
||||
.stream()
|
||||
|
||||
#endif
|
||||
|
||||
#define LOG_DEBUG \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kDebug) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define LOG_DEBUG_TO(index) \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kDebug) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
|
||||
.setIndex(index) \
|
||||
.stream()
|
||||
#define LOG_INFO \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kInfo) \
|
||||
trantor::Logger(__FILE__, __LINE__).stream()
|
||||
#define LOG_INFO_TO(index) \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kInfo) \
|
||||
trantor::Logger(__FILE__, __LINE__).setIndex(index).stream()
|
||||
#define LOG_WARN \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kWarn).stream()
|
||||
#define LOG_WARN_TO(index) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kWarn) \
|
||||
.setIndex(index) \
|
||||
.stream()
|
||||
#define LOG_ERROR \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kError).stream()
|
||||
#define LOG_ERROR_TO(index) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kError) \
|
||||
.setIndex(index) \
|
||||
.stream()
|
||||
#define LOG_FATAL \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kFatal).stream()
|
||||
#define LOG_FATAL_TO(index) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kFatal) \
|
||||
.setIndex(index) \
|
||||
.stream()
|
||||
#define LOG_SYSERR trantor::Logger(__FILE__, __LINE__, true).stream()
|
||||
#define LOG_SYSERR_TO(index) \
|
||||
trantor::Logger(__FILE__, __LINE__, true).setIndex(index).stream()
|
||||
|
||||
#define LOG_RAW trantor::RawLogger().stream()
|
||||
#define LOG_RAW_TO(index) trantor::RawLogger().setIndex(index).stream()
|
||||
|
||||
#define LOG_TRACE_IF(cond) \
|
||||
if ((trantor::Logger::logLevel() <= trantor::Logger::kTrace) && (cond)) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define LOG_DEBUG_IF(cond) \
|
||||
if ((Tensor::Logger::logLevel() <= Tensor::Logger::kDebug) && (cond)) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define LOG_INFO_IF(cond) \
|
||||
if ((Tensor::Logger::logLevel() <= Tensor::Logger::kInfo) && (cond)) \
|
||||
Tensor::Logger(__FILE__, __LINE__).stream()
|
||||
#define LOG_WARN_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kWarn).stream()
|
||||
#define LOG_ERROR_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kError).stream()
|
||||
#define LOG_FATAL_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kFatal).stream()
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define DLOG_TRACE \
|
||||
if (0) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define DLOG_DEBUG \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define DLOG_INFO \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__).stream()
|
||||
#define DLOG_WARN \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kWarn).stream()
|
||||
#define DLOG_ERROR \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kError).stream()
|
||||
#define DLOG_FATAL \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kFatal).stream()
|
||||
|
||||
#define DLOG_TRACE_IF(cond) \
|
||||
if (0) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define DLOG_DEBUG_IF(cond) \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define DLOG_INFO_IF(cond) \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__).stream()
|
||||
#define DLOG_WARN_IF(cond) \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kWarn).stream()
|
||||
#define DLOG_ERROR_IF(cond) \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kError).stream()
|
||||
#define DLOG_FATAL_IF(cond) \
|
||||
if (0) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kFatal).stream()
|
||||
#else
|
||||
#define DLOG_TRACE \
|
||||
if (trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define DLOG_DEBUG \
|
||||
if (Tensor::Logger::logLevel() <= Tensor::Logger::kDebug) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define DLOG_INFO \
|
||||
if (Tensor::Logger::logLevel() <= Tensor::Logger::kInfo) \
|
||||
Tensor::Logger(__FILE__, __LINE__).stream()
|
||||
#define DLOG_WARN \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kWarn).stream()
|
||||
#define DLOG_ERROR \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kError).stream()
|
||||
#define DLOG_FATAL \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kFatal).stream()
|
||||
|
||||
#define DLOG_TRACE_IF(cond) \
|
||||
if ((trantor::Logger::logLevel() <= trantor::Logger::kTrace) && (cond)) \
|
||||
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
|
||||
.stream()
|
||||
#define DLOG_DEBUG_IF(cond) \
|
||||
if ((Tensor::Logger::logLevel() <= Tensor::Logger::kDebug) && (cond)) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kDebug, __func__) \
|
||||
.stream()
|
||||
#define DLOG_INFO_IF(cond) \
|
||||
if ((Tensor::Logger::logLevel() <= Tensor::Logger::kInfo) && (cond)) \
|
||||
Tensor::Logger(__FILE__, __LINE__).stream()
|
||||
#define DLOG_WARN_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kWarn).stream()
|
||||
#define DLOG_ERROR_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kError).stream()
|
||||
#define DLOG_FATAL_IF(cond) \
|
||||
if (cond) \
|
||||
Tensor::Logger(__FILE__, __LINE__, Tensor::Logger::kFatal).stream()
|
||||
#endif
|
||||
|
||||
const char *strerror_tl(int savedErrno);
|
||||
} // namespace trantor
|
@ -12,7 +12,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/utils/SerialTaskQueue.h>
|
||||
#ifdef __linux__
|
||||
#include <sys/prctl.h>
|
||||
|
@ -15,7 +15,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <trantor/net/EventLoop.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "core/log/logger.h"
|
||||
#include <trantor/exports.h>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
|
Loading…
Reference in New Issue
Block a user