Add new log level USER

master
Chunting Gu 6 years ago
parent ec753c14c3
commit 3309e7896a

@ -20,7 +20,7 @@ if(WIN32)
endif()
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_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) {
std::cout << "Usage: " << argv0 << " <url> [timeout]" << std::endl;
std::cout << " E.g.," << 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" << std::endl;
std::cout << " " << argv0 << " http://localhost:8080 2" << std::endl;
}
int main(int argc, char* argv[]) {

@ -7,7 +7,7 @@
#define WEBCC_ENABLE_LOG 1
#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
#endif

@ -9,7 +9,7 @@
#define WEBCC_ENABLE_LOG @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@
#endif

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

@ -10,14 +10,18 @@
#include <string>
// 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_WARN 2
#define WEBCC_ERRO 3
#define WEBCC_USER 2
#define WEBCC_WARN 3
#define WEBCC_ERRO 4
// Default log level.
#ifndef WEBCC_LOG_LEVEL
#define WEBCC_LOG_LEVEL WEBCC_WARN
#define WEBCC_LOG_LEVEL WEBCC_USER
#endif
#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, ...)
#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
#define LOG_WARN(format, ...) \
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...)
#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
#define LOG_WARN(format, args...) \
webcc::Log(WEBCC_WARN, __FILENAME__, __LINE__, format, ##args);

Loading…
Cancel
Save