add log level check

This commit is contained in:
UnknownObject
2022-11-20 15:45:30 +08:00
parent e0f8a4da37
commit 9df50c1310
320 changed files with 237 additions and 205 deletions
+213 -156
View File
@@ -19,89 +19,104 @@
#include <unistd.h>
#include <sys/syscall.h> // For SYS_xxx definitions
#include <sys/types.h>
#include "logger.h"
#endif // defined(_WIN32) || defined(_WIN64)
namespace webcc {
namespace webcc
{
// -----------------------------------------------------------------------------
static std::string g_main_thread_id;
static std::string g_main_thread_id;
static const char* kLevelNames[] = {
"VERB", "INFO", "USER", "WARN", "ERRO"
};
static const char* kLevelNames[] = {
"VERB", "INFO", "USER", "WARN", "ERRO"
};
// -----------------------------------------------------------------------------
//UnknownObject: Add For Dynamic Log Level
static bool UseDynamicLogLevel = false;
static int DynamicLogLevel = WEBCC_VERB;
static FILE* FOpen(const fs::path& path, bool overwrite) {
// -----------------------------------------------------------------------------
static FILE* FOpen(const fs::path& path, bool overwrite)
{
#if (defined(_WIN32) || defined(_WIN64))
return _wfopen(path.wstring().c_str(), overwrite ? L"w+" : L"a+");
return _wfopen(path.wstring().c_str(), overwrite ? L"w+" : L"a+");
#else
return fopen(path.string().c_str(), overwrite ? "w+" : "a+");
return fopen(path.string().c_str(), overwrite ? "w+" : "a+");
#endif // defined(_WIN32) || defined(_WIN64)
}
}
struct Logger {
Logger() = default;
struct Logger
{
Logger() = default;
~Logger() {
if (file != nullptr) {
fclose(file);
}
}
~Logger()
{
if (file != nullptr)
{
fclose(file);
}
}
void Init(const fs::path& path, int _modes) {
modes = _modes;
void Init(const fs::path& path, int _modes)
{
modes = _modes;
// Create log file only if necessary.
if ((modes & LOG_FILE) != 0 && !path.empty()) {
file = FOpen(path, (modes & LOG_OVERWRITE) != 0);
}
}
// Create log file only if necessary.
if ((modes & LOG_FILE) != 0 && !path.empty())
{
file = FOpen(path, (modes & LOG_OVERWRITE) != 0);
}
}
FILE* file = nullptr;
int modes = 0;
std::mutex mutex;
};
FILE* file = nullptr;
int modes = 0;
std::mutex mutex;
};
// Global logger.
static Logger g_logger;
// Global logger.
static Logger g_logger;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// The colors related code was adapted from Loguru. See:
// https://github.com/emilk/loguru/blob/master/loguru.cpp
// Thanks to Loguru!
// The colors related code was adapted from Loguru. See:
// https://github.com/emilk/loguru/blob/master/loguru.cpp
// Thanks to Loguru!
static const bool g_terminal_has_color = []() {
static const bool g_terminal_has_color = []()
{
#if (defined(_WIN32) || defined(_WIN64))
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
// NOTE: Need Windows 10.
HANDLE h_output = GetStdHandle(STD_OUTPUT_HANDLE);
if (h_output != INVALID_HANDLE_VALUE) {
DWORD mode = 0;
GetConsoleMode(h_output, &mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
return SetConsoleMode(h_output, mode) != 0;
}
return false;
HANDLE h_output = GetStdHandle(STD_OUTPUT_HANDLE);
if (h_output != INVALID_HANDLE_VALUE)
{
DWORD mode = 0;
GetConsoleMode(h_output, &mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
return SetConsoleMode(h_output, mode) != 0;
}
return false;
#else
const char* term = getenv("TERM");
if (term == nullptr) {
return false;
}
const char* term = getenv("TERM");
if (term == nullptr)
{
return false;
}
return strcmp(term, "cygwin") == 0 || strcmp(term, "linux") == 0 ||
strcmp(term, "rxvt-unicode-256color") == 0 ||
strcmp(term, "screen") == 0 || strcmp(term, "screen-256color") == 0 ||
strcmp(term, "screen.xterm-256color") == 0 ||
strcmp(term, "tmux-256color") == 0 || strcmp(term, "xterm") == 0 ||
strcmp(term, "xterm-256color") == 0 ||
strcmp(term, "xterm-termite") == 0 || strcmp(term, "xterm-color") == 0;
return strcmp(term, "cygwin") == 0 || strcmp(term, "linux") == 0 ||
strcmp(term, "rxvt-unicode-256color") == 0 ||
strcmp(term, "screen") == 0 || strcmp(term, "screen-256color") == 0 ||
strcmp(term, "screen.xterm-256color") == 0 ||
strcmp(term, "tmux-256color") == 0 || strcmp(term, "xterm") == 0 ||
strcmp(term, "xterm-256color") == 0 ||
strcmp(term, "xterm-termite") == 0 || strcmp(term, "xterm-color") == 0;
#endif
}();
}();
#if (defined(_WIN32) || defined(_WIN64))
#define VTSEQ(ID) ("\x1b[1;" #ID "m")
@@ -133,140 +148,182 @@ static const bool g_terminal_has_color = []() {
// std::this_thread::get_id() returns a very long ID (same as pthread_self())
// on Linux, e.g., 140219133990656. syscall(SYS_gettid) is much prefered because
// it's shorter and the same as `ps -T -p <pid>` output.
static std::string DoGetThreadID() {
static std::string DoGetThreadID()
{
#if (defined(_WIN32) || defined(_WIN64))
auto thread_id = std::this_thread::get_id();
std::ostringstream ss;
ss << thread_id;
return ss.str();
auto thread_id = std::this_thread::get_id();
std::ostringstream ss;
ss << thread_id;
return ss.str();
#else
return std::to_string(syscall(SYS_gettid));
return std::to_string(syscall(SYS_gettid));
#endif
}
}
static std::string GetThreadID() {
std::string thread_id = DoGetThreadID();
if (thread_id == g_main_thread_id) {
return "main";
}
return thread_id;
}
static std::string GetThreadID()
{
std::string thread_id = DoGetThreadID();
if (thread_id == g_main_thread_id)
{
return "main";
}
return thread_id;
}
static fs::path InitLogPath(const fs::path& dir) {
if (dir.empty()) {
return fs::current_path() / WEBCC_LOG_FILE_NAME;
}
static fs::path InitLogPath(const fs::path& dir)
{
if (dir.empty())
{
return fs::current_path() / WEBCC_LOG_FILE_NAME;
}
fs::error_code ec;
if (!fs::exists(dir, ec) || !fs::is_directory(dir, ec)) {
if (!fs::create_directories(dir, ec) || ec) {
return {};
}
}
fs::error_code ec;
if (!fs::exists(dir, ec) || !fs::is_directory(dir, ec))
{
if (!fs::create_directories(dir, ec) || ec)
{
return {};
}
}
return (dir / WEBCC_LOG_FILE_NAME);
}
return (dir / WEBCC_LOG_FILE_NAME);
}
void LogInit(const fs::path& dir, int modes) {
// Suppose this is called from the main thread.
g_main_thread_id = DoGetThreadID();
void LogInit(const fs::path& dir, int modes)
{
// Suppose this is called from the main thread.
g_main_thread_id = DoGetThreadID();
if ((modes & LOG_FILE) != 0) {
g_logger.Init(InitLogPath(dir), modes);
} else {
g_logger.Init({}, modes);
}
}
if ((modes & LOG_FILE) != 0)
{
g_logger.Init(InitLogPath(dir), modes);
}
else
{
g_logger.Init({}, modes);
}
}
static std::string GetTimestamp() {
using system_clock = std::chrono::system_clock;
using milliseconds = std::chrono::milliseconds;
void LogInit(const fs::path& dir, int modes, int log_level)
{
g_main_thread_id = DoGetThreadID();
if ((modes & LOG_FILE) != 0)
{
g_logger.Init(InitLogPath(dir), modes);
}
else
{
g_logger.Init({}, modes);
}
UseDynamicLogLevel = true;
DynamicLogLevel = log_level;
}
auto now = system_clock::now();
std::time_t t = system_clock::to_time_t(now);
static std::string GetTimestamp()
{
using system_clock = std::chrono::system_clock;
using milliseconds = std::chrono::milliseconds;
std::ostringstream ss;
ss << std::put_time(std::localtime(&t), "%Y-%m-%d %H:%M:%S");
auto now = system_clock::now();
std::time_t t = system_clock::to_time_t(now);
milliseconds milli_seconds =
std::chrono::duration_cast<milliseconds>(now.time_since_epoch());
std::string micro_seconds_str = std::to_string(milli_seconds.count() % 1000);
while (micro_seconds_str.size() < 3) {
micro_seconds_str = "0" + micro_seconds_str;
}
std::ostringstream ss;
ss << std::put_time(std::localtime(&t), "%Y-%m-%d %H:%M:%S");
ss << "." << micro_seconds_str;
milliseconds milli_seconds =
std::chrono::duration_cast<milliseconds>(now.time_since_epoch());
std::string micro_seconds_str = std::to_string(milli_seconds.count() % 1000);
while (micro_seconds_str.size() < 3)
{
micro_seconds_str = "0" + micro_seconds_str;
}
return ss.str();
}
ss << "." << micro_seconds_str;
void Log(int level, const char* file, int line, const char* format, ...) {
assert(format != nullptr);
return ss.str();
}
std::string timestamp = GetTimestamp();
std::string thread_id = GetThreadID();
void Log(int level, const char* file, int line, const char* format, ...)
{
assert(format != nullptr);
if ((g_logger.modes & LOG_FILE) != 0 && g_logger.file != nullptr) {
std::lock_guard<std::mutex> lock(g_logger.mutex);
if (UseDynamicLogLevel && (DynamicLogLevel > level))
return;
va_list args;
va_start(args, format);
std::string timestamp = GetTimestamp();
std::string thread_id = GetThreadID();
fprintf(g_logger.file, "%s, %s, %7s, %25s, %4d, ",
timestamp.c_str(), kLevelNames[level], thread_id.c_str(),
file, line);
if ((g_logger.modes & LOG_FILE) != 0 && g_logger.file != nullptr)
{
std::lock_guard<std::mutex> lock(g_logger.mutex);
vfprintf(g_logger.file, format, args);
va_list args;
va_start(args, format);
fprintf(g_logger.file, "\n");
fprintf(g_logger.file, "%s, %s, %7s, %25s, %4d, ",
timestamp.c_str(), kLevelNames[level], thread_id.c_str(),
file, line);
if ((g_logger.modes & LOG_FLUSH) != 0) {
fflush(g_logger.file);
}
vfprintf(g_logger.file, format, args);
va_end(args);
}
fprintf(g_logger.file, "\n");
if ((g_logger.modes & LOG_CONSOLE) != 0) {
std::lock_guard<std::mutex> lock(g_logger.mutex);
if ((g_logger.modes & LOG_FLUSH) != 0)
{
fflush(g_logger.file);
}
va_list args;
va_start(args, format);
va_end(args);
}
if (g_terminal_has_color) {
if (level < WEBCC_WARN) {
fprintf(stderr, "%s%s, %s, %7s, %25s, %4d, ",
TERM_RESET,
timestamp.c_str(), kLevelNames[level], thread_id.c_str(),
file, line);
} else {
fprintf(stderr, "%s%s%s, %s, %7s, %25s, %4d, ",
TERM_RESET,
level == WEBCC_WARN ? TERM_YELLOW : TERM_RED,
timestamp.c_str(), kLevelNames[level], thread_id.c_str(),
file, line);
}
if ((g_logger.modes & LOG_CONSOLE) != 0)
{
std::lock_guard<std::mutex> lock(g_logger.mutex);
vfprintf(stderr, format, args);
va_list args;
va_start(args, format);
fprintf(stderr, "%s\n", TERM_RESET);
} else {
fprintf(stderr, "%s, %s, %7s, %25s, %4d, ",
timestamp.c_str(), kLevelNames[level], thread_id.c_str(),
file, line);
if (g_terminal_has_color)
{
if (level < WEBCC_WARN)
{
fprintf(stderr, "%s%s, %s, %7s, %25s, %4d, ",
TERM_RESET,
timestamp.c_str(), kLevelNames[level], thread_id.c_str(),
file, line);
}
else
{
fprintf(stderr, "%s%s%s, %s, %7s, %25s, %4d, ",
TERM_RESET,
level == WEBCC_WARN ? TERM_YELLOW : TERM_RED,
timestamp.c_str(), kLevelNames[level], thread_id.c_str(),
file, line);
}
vfprintf(stderr, format, args);
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
}
fprintf(stderr, "%s\n", TERM_RESET);
}
else
{
fprintf(stderr, "%s, %s, %7s, %25s, %4d, ",
timestamp.c_str(), kLevelNames[level], thread_id.c_str(),
file, line);
if ((g_logger.modes & LOG_FLUSH) != 0) {
fflush(stderr);
}
vfprintf(stderr, format, args);
va_end(args);
}
}
fprintf(stderr, "\n");
}
if ((g_logger.modes & LOG_FLUSH) != 0)
{
fflush(stderr);
}
va_end(args);
}
}
} // namespace webcc
+19 -15
View File
@@ -28,25 +28,29 @@
#define WEBCC_LOG_FILE_NAME "webcc.log"
namespace webcc {
namespace webcc
{
enum LogMode {
LOG_FILE = 1, // Log to file.
LOG_CONSOLE = 2, // Log to console.
LOG_FLUSH = 4, // Flush on each log.
LOG_OVERWRITE = 8, // Overwrite any existing log file.
};
enum LogMode
{
LOG_FILE = 1, // Log to file.
LOG_CONSOLE = 2, // Log to console.
LOG_FLUSH = 4, // Flush on each log.
LOG_OVERWRITE = 8, // Overwrite any existing log file.
};
// Commonly used modes.
const int LOG_CONSOLE_FILE_APPEND = LOG_CONSOLE | LOG_FILE;
const int LOG_CONSOLE_FILE_OVERWRITE = LOG_CONSOLE | LOG_FILE | LOG_OVERWRITE;
const int LOG_FILE_OVERWRITE = LOG_FILE | LOG_OVERWRITE;
// Commonly used modes.
const int LOG_CONSOLE_FILE_APPEND = LOG_CONSOLE | LOG_FILE;
const int LOG_CONSOLE_FILE_OVERWRITE = LOG_CONSOLE | LOG_FILE | LOG_OVERWRITE;
const int LOG_FILE_OVERWRITE = LOG_FILE | LOG_OVERWRITE;
// Initialize logger.
// If |dir| is empty, log file will be generated in current directory.
void LogInit(const fs::path& dir, int modes);
// Initialize logger.
// If |dir| is empty, log file will be generated in current directory.
void LogInit(const fs::path& dir, int modes);
void Log(int level, const char* file, int line, const char* format, ...);
void LogInit(const fs::path& dir, int modes, int log_level);
void Log(int level, const char* file, int line, const char* format, ...);
} // namespace webcc