Add new log level USER

master
Chunting Gu 6 years ago
parent ec753c14c3
commit 3309e7896a

@ -20,7 +20,7 @@ if(WIN32)
endif() endif()
set(WEBCC_ENABLE_LOG 1 CACHE STRING "Enable logging? (1:Yes, 0:No)") set(WEBCC_ENABLE_LOG 1 CACHE STRING "Enable logging? (1:Yes, 0:No)")
set(WEBCC_LOG_LEVEL 2 CACHE STRING "Log level (0:VERB, 1:INFO, 2:WARN or 3:ERRO)") set(WEBCC_LOG_LEVEL 2 CACHE STRING "Log level (0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO)")
set(WEBCC_ENABLE_SSL 0 CACHE STRING "Enable SSL/HTTPS (need OpenSSL)? (1:Yes, 0:No)") set(WEBCC_ENABLE_SSL 0 CACHE STRING "Enable SSL/HTTPS (need OpenSSL)? (1:Yes, 0:No)")
set(WEBCC_ENABLE_GZIP 0 CACHE STRING "Enable gzip compression (need Zlib)? (1:Yes, 0:No)") set(WEBCC_ENABLE_GZIP 0 CACHE STRING "Enable gzip compression (need Zlib)? (1:Yes, 0:No)")

@ -189,8 +189,8 @@ void PrintBookList(const std::list<Book>& books) {
void Help(const char* argv0) { void Help(const char* argv0) {
std::cout << "Usage: " << argv0 << " <url> [timeout]" << std::endl; std::cout << "Usage: " << argv0 << " <url> [timeout]" << std::endl;
std::cout << " E.g.," << std::endl; std::cout << " E.g.," << std::endl;
std::cout << " " << argv0 << "http://localhost:8080" << std::endl; std::cout << " " << argv0 << " http://localhost:8080" << std::endl;
std::cout << " " << argv0 << "http://localhost:8080 2" << std::endl; std::cout << " " << argv0 << " http://localhost:8080 2" << std::endl;
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {

@ -7,7 +7,7 @@
#define WEBCC_ENABLE_LOG 1 #define WEBCC_ENABLE_LOG 1
#if WEBCC_ENABLE_LOG #if WEBCC_ENABLE_LOG
// 0:VERB, 1:INFO, 2:WARN or 3:ERRO // 0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO
#define WEBCC_LOG_LEVEL 2 #define WEBCC_LOG_LEVEL 2
#endif #endif

@ -9,7 +9,7 @@
#define WEBCC_ENABLE_LOG @WEBCC_ENABLE_LOG@ #define WEBCC_ENABLE_LOG @WEBCC_ENABLE_LOG@
#if WEBCC_ENABLE_LOG #if WEBCC_ENABLE_LOG
// 0:VERB, 1:INFO, 2:WARN or 3:ERRO // 0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO
#define WEBCC_LOG_LEVEL @WEBCC_LOG_LEVEL@ #define WEBCC_LOG_LEVEL @WEBCC_LOG_LEVEL@
#endif #endif

@ -29,7 +29,7 @@ namespace webcc {
static std::string g_main_thread_id; static std::string g_main_thread_id;
static const char* kLevelNames[] = { static const char* kLevelNames[] = {
"VERB", "INFO", "WARN", "ERRO", "FATA" "VERB", "INFO", "USER", "WARN", "ERRO"
}; };
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -72,8 +72,6 @@ static Logger g_logger;
// https://github.com/emilk/loguru/blob/master/loguru.cpp // https://github.com/emilk/loguru/blob/master/loguru.cpp
// Thanks to Loguru! // Thanks to Loguru!
bool g_colorlogtostderr = true;
static const bool g_terminal_has_color = []() { static const bool g_terminal_has_color = []() {
#if (defined(_WIN32) || defined(_WIN64)) #if (defined(_WIN32) || defined(_WIN64))
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
@ -104,60 +102,30 @@ static const bool g_terminal_has_color = []() {
#endif #endif
}(); }();
// Colors
#if (defined(_WIN32) || defined(_WIN64)) #if (defined(_WIN32) || defined(_WIN64))
#define VTSEQ(ID) ("\x1b[1;" #ID "m") #define VTSEQ(ID) ("\x1b[1;" #ID "m")
#else #else
#define VTSEQ(ID) ("\x1b[" #ID "m") #define VTSEQ(ID) ("\x1b[" #ID "m")
#endif #endif
const char* TerminalBlack() { // Colors
return g_terminal_has_color ? VTSEQ(30) : ""; #define TERM_BLACK VTSEQ(30)
} #define TERM_RED VTSEQ(31)
const char* TerminalRed() { #define TERM_GREEN VTSEQ(32)
return g_terminal_has_color ? VTSEQ(31) : ""; #define TERM_YELLOW VTSEQ(33)
} #define TERM_BLUE VTSEQ(34)
const char* TerminalGreen() { #define TERM_PURPLE VTSEQ(35)
return g_terminal_has_color ? VTSEQ(32) : ""; #define TERM_CYAN VTSEQ(36)
} #define TERM_LLIGHT_GRAY VTSEQ(37)
const char* TerminalYellow() { #define TERM_LLIGHT_RED VTSEQ(91)
return g_terminal_has_color ? VTSEQ(33) : ""; #define TERM_DIM VTSEQ(2)
}
const char* TerminalBlue() {
return g_terminal_has_color ? VTSEQ(34) : "";
}
const char* TerminalPurple() {
return g_terminal_has_color ? VTSEQ(35) : "";
}
const char* TerminalCyan() {
return g_terminal_has_color ? VTSEQ(36) : "";
}
const char* TerminalLightGray() {
return g_terminal_has_color ? VTSEQ(37) : "";
}
const char* TerminalWhite() {
return g_terminal_has_color ? VTSEQ(37) : "";
}
const char* TerminalLightRed() {
return g_terminal_has_color ? VTSEQ(91) : "";
}
const char* TerminalDim() {
return g_terminal_has_color ? VTSEQ(2) : "";
}
// Formating // Formating
const char* TerminalBold() { #define TERM_BOLD VTSEQ(1)
return g_terminal_has_color ? VTSEQ(1) : ""; #define TERM_UNDERLINE VTSEQ(4)
}
const char* TerminalUnderline() {
return g_terminal_has_color ? VTSEQ(4) : "";
}
// You should end each line with this! // You should end each line with this!
const char* TerminalReset() { #define TERM_RESET VTSEQ(0)
return g_terminal_has_color ? VTSEQ(0) : "";
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -269,23 +237,23 @@ void Log(int level, const char* file, int line, const char* format, ...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
if (g_colorlogtostderr && g_terminal_has_color) { if (g_terminal_has_color) {
if (level < WEBCC_WARN) { if (level < WEBCC_WARN) {
fprintf(stderr, "%s%s, %s, %7s, %20s, %4d, ", fprintf(stderr, "%s%s, %s, %7s, %20s, %4d, ",
TerminalReset(), TERM_RESET,
timestamp.c_str(), kLevelNames[level], thread_id.c_str(), timestamp.c_str(), kLevelNames[level], thread_id.c_str(),
file, line); file, line);
} else { } else {
fprintf(stderr, "%s%s%s, %s, %7s, %20s, %4d, ", fprintf(stderr, "%s%s%s, %s, %7s, %20s, %4d, ",
TerminalReset(), TERM_RESET,
level == WEBCC_WARN ? TerminalYellow() : TerminalRed(), level == WEBCC_WARN ? TERM_YELLOW : TERM_RED,
timestamp.c_str(), kLevelNames[level], thread_id.c_str(), timestamp.c_str(), kLevelNames[level], thread_id.c_str(),
file, line); file, line);
} }
vfprintf(stderr, format, args); vfprintf(stderr, format, args);
fprintf(stderr, "%s\n", TerminalReset()); fprintf(stderr, "%s\n", TERM_RESET);
} else { } else {
fprintf(stderr, "%s, %s, %7s, %20s, %4d, ", fprintf(stderr, "%s, %s, %7s, %20s, %4d, ",
timestamp.c_str(), kLevelNames[level], thread_id.c_str(), timestamp.c_str(), kLevelNames[level], thread_id.c_str(),

@ -10,14 +10,18 @@
#include <string> #include <string>
// Log levels. // Log levels.
#define WEBCC_VERB 0 // Similar to DEBUG in other projects. // VERB is similar to DEBUG commonly used by other projects.
// USER is for the users who want to log their own logs but don't want any
// VERB or INFO.
#define WEBCC_VERB 0
#define WEBCC_INFO 1 #define WEBCC_INFO 1
#define WEBCC_WARN 2 #define WEBCC_USER 2
#define WEBCC_ERRO 3 #define WEBCC_WARN 3
#define WEBCC_ERRO 4
// Default log level. // Default log level.
#ifndef WEBCC_LOG_LEVEL #ifndef WEBCC_LOG_LEVEL
#define WEBCC_LOG_LEVEL WEBCC_WARN #define WEBCC_LOG_LEVEL WEBCC_USER
#endif #endif
#define WEBCC_LOG_FILE_NAME "webcc.log" #define WEBCC_LOG_FILE_NAME "webcc.log"
@ -71,6 +75,13 @@ void Log(int level, const char* file, int line, const char* format, ...);
#define LOG_INFO(format, ...) #define LOG_INFO(format, ...)
#endif #endif
#if WEBCC_LOG_LEVEL <= WEBCC_USER
#define LOG_USER(format, ...) \
webcc::Log(WEBCC_USER, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
#else
#define LOG_INFO(format, ...)
#endif
#if WEBCC_LOG_LEVEL <= WEBCC_WARN #if WEBCC_LOG_LEVEL <= WEBCC_WARN
#define LOG_WARN(format, ...) \ #define LOG_WARN(format, ...) \
webcc::Log(WEBCC_WARN, __FILENAME__, __LINE__, format, ##__VA_ARGS__); webcc::Log(WEBCC_WARN, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
@ -104,6 +115,13 @@ void Log(int level, const char* file, int line, const char* format, ...);
#define LOG_INFO(format, args...) #define LOG_INFO(format, args...)
#endif #endif
#if WEBCC_LOG_LEVEL <= WEBCC_USER
#define LOG_USER(format, args...) \
webcc::Log(WEBCC_USER, __FILENAME__, __LINE__, format, ##args);
#else
#define LOG_INFO(format, args...)
#endif
#if WEBCC_LOG_LEVEL <= WEBCC_WARN #if WEBCC_LOG_LEVEL <= WEBCC_WARN
#define LOG_WARN(format, args...) \ #define LOG_WARN(format, args...) \
webcc::Log(WEBCC_WARN, __FILENAME__, __LINE__, format, ##args); webcc::Log(WEBCC_WARN, __FILENAME__, __LINE__, format, ##args);

Loading…
Cancel
Save