Switch log level control from runtime to compile time
This commit is contained in:
@@ -8,6 +8,7 @@ option(WEBCC_BUILD_EXAMPLE "Build examples?" ON)
|
|||||||
|
|
||||||
if(WEBCC_ENABLE_LOG)
|
if(WEBCC_ENABLE_LOG)
|
||||||
add_definitions(-DWEBCC_ENABLE_LOG)
|
add_definitions(-DWEBCC_ENABLE_LOG)
|
||||||
|
add_definitions(-DWEBCC_LOG_LEVEL=0)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(WEBCC_ENABLE_SOAP)
|
if(WEBCC_ENABLE_SOAP)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ void Test(boost::asio::io_context& ioc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
LOG_INIT(webcc::ERRO, 0);
|
LOG_INIT(0);
|
||||||
|
|
||||||
boost::asio::io_context ioc;
|
boost::asio::io_context ioc;
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ void Test() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
LOG_INIT(webcc::ERRO, 0);
|
LOG_INIT(0);
|
||||||
|
|
||||||
Test();
|
Test();
|
||||||
Test();
|
Test();
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ int main(int argc, char* argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INIT(webcc::ERRO, 0);
|
LOG_INIT(0);
|
||||||
|
|
||||||
std::string host = argv[1];
|
std::string host = argv[1];
|
||||||
std::string port = argv[2];
|
std::string port = argv[2];
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ int main(int argc, char* argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INIT(webcc::VERB, 0);
|
LOG_INIT(0);
|
||||||
|
|
||||||
std::string host = argv[1];
|
std::string host = argv[1];
|
||||||
std::string port = argv[2];
|
std::string port = argv[2];
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ int main(int argc, char* argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INIT(webcc::VERB, 0);
|
LOG_INIT(0);
|
||||||
|
|
||||||
unsigned short port = std::atoi(argv[1]);
|
unsigned short port = std::atoi(argv[1]);
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
#include "calc_client.h"
|
#include "calc_client.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
LOG_INIT(webcc::VERB, 0);
|
LOG_INIT(0);
|
||||||
|
|
||||||
CalcClient calc;
|
CalcClient calc;
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ int main(int argc, char* argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INIT(webcc::INFO, 0);
|
LOG_INIT(0);
|
||||||
|
|
||||||
unsigned short port = std::atoi(argv[1]);
|
unsigned short port = std::atoi(argv[1]);
|
||||||
|
|
||||||
|
|||||||
+2
-8
@@ -14,13 +14,12 @@
|
|||||||
namespace webcc {
|
namespace webcc {
|
||||||
|
|
||||||
struct Logger {
|
struct Logger {
|
||||||
int level;
|
|
||||||
int modes;
|
int modes;
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Global logger.
|
// Global logger.
|
||||||
static Logger g_logger{ VERB };
|
static Logger g_logger{ 0 };
|
||||||
|
|
||||||
static std::thread::id g_main_thread_id;
|
static std::thread::id g_main_thread_id;
|
||||||
|
|
||||||
@@ -28,9 +27,8 @@ static const char* kLevelNames[] = {
|
|||||||
"VERB", "INFO", "WARN", "ERRO", "FATA"
|
"VERB", "INFO", "WARN", "ERRO", "FATA"
|
||||||
};
|
};
|
||||||
|
|
||||||
void LogInit(int level, int modes) {
|
void LogInit(int modes) {
|
||||||
g_logger.modes = modes;
|
g_logger.modes = modes;
|
||||||
g_logger.level = level;
|
|
||||||
|
|
||||||
// Suppose LogInit() is called from the main thread.
|
// Suppose LogInit() is called from the main thread.
|
||||||
g_main_thread_id = std::this_thread::get_id();
|
g_main_thread_id = std::this_thread::get_id();
|
||||||
@@ -76,10 +74,6 @@ static std::string GetThreadID() {
|
|||||||
void LogWrite(int level, const char* file, int line, const char* format, ...) {
|
void LogWrite(int level, const char* file, int line, const char* format, ...) {
|
||||||
assert(format != nullptr);
|
assert(format != nullptr);
|
||||||
|
|
||||||
if (g_logger.level > level) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(g_logger.mutex);
|
std::lock_guard<std::mutex> lock(g_logger.mutex);
|
||||||
|
|
||||||
va_list va_ptr_console;
|
va_list va_ptr_console;
|
||||||
|
|||||||
+65
-22
@@ -7,75 +7,118 @@
|
|||||||
|
|
||||||
#include <cstring> // for strrchr()
|
#include <cstring> // for strrchr()
|
||||||
|
|
||||||
namespace webcc {
|
#define WEBCC_VERB 0
|
||||||
|
#define WEBCC_INFO 1
|
||||||
|
#define WEBCC_WARN 2
|
||||||
|
#define WEBCC_ERRO 3
|
||||||
|
#define WEBCC_FATA 4
|
||||||
|
|
||||||
enum LogLevel {
|
// Default log level.
|
||||||
VERB = 0,
|
#ifndef WEBCC_LOG_LEVEL
|
||||||
INFO,
|
#define WEBCC_LOG_LEVEL WEBCC_WARN
|
||||||
WARN,
|
#endif
|
||||||
ERRO,
|
|
||||||
FATA,
|
namespace webcc {
|
||||||
};
|
|
||||||
|
|
||||||
enum LogMode {
|
enum LogMode {
|
||||||
FLUSH = 1,
|
FLUSH = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
void LogInit(int level, int modes);
|
void LogInit(int modes);
|
||||||
|
|
||||||
void LogWrite(int level, const char* file, int line, const char* format, ...);
|
void LogWrite(int level, const char* file, int line, const char* format, ...);
|
||||||
|
|
||||||
} // namespace webcc
|
} // namespace webcc
|
||||||
|
|
||||||
// Initialize the logger with a level.
|
// Initialize the logger with a level.
|
||||||
// E.g., LOG_INIT(ERRO, FLUSH)
|
// E.g., LOG_INIT(FLUSH)
|
||||||
#define LOG_INIT(level, modes) webcc::LogInit(level, modes);
|
#define LOG_INIT(modes) webcc::LogInit(modes);
|
||||||
|
|
||||||
#if (defined(WIN32) || defined(_WIN64))
|
#if (defined(WIN32) || defined(_WIN64))
|
||||||
|
|
||||||
// See: https://stackoverflow.com/a/8488201
|
// See: https://stackoverflow.com/a/8488201
|
||||||
#define __FILENAME__ std::strrchr("\\" __FILE__, '\\') + 1
|
#define __FILENAME__ std::strrchr("\\" __FILE__, '\\') + 1
|
||||||
|
|
||||||
|
#if WEBCC_LOG_LEVEL <= WEBCC_VERB
|
||||||
#define LOG_VERB(format, ...) \
|
#define LOG_VERB(format, ...) \
|
||||||
webcc::LogWrite(webcc::VERB, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
|
webcc::LogWrite(WEBCC_VERB, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
|
||||||
|
#else
|
||||||
|
#define LOG_VERB(format, ...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if WEBCC_LOG_LEVEL <= WEBCC_INFO
|
||||||
#define LOG_INFO(format, ...) \
|
#define LOG_INFO(format, ...) \
|
||||||
webcc::LogWrite(webcc::INFO, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
|
webcc::LogWrite(WEBCC_INFO, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
|
||||||
|
#else
|
||||||
|
#define LOG_INFO(format, ...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if WEBCC_LOG_LEVEL <= WEBCC_WARN
|
||||||
#define LOG_WARN(format, ...) \
|
#define LOG_WARN(format, ...) \
|
||||||
webcc::LogWrite(webcc::WARN, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
|
webcc::LogWrite(WEBCC_WARN, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
|
||||||
|
#else
|
||||||
|
#define LOG_WARN(format, ...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if WEBCC_LOG_LEVEL <= WEBCC_ERRO
|
||||||
#define LOG_ERRO(format, ...) \
|
#define LOG_ERRO(format, ...) \
|
||||||
webcc::LogWrite(webcc::ERRO, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
|
webcc::LogWrite(WEBCC_ERRO, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
|
||||||
|
#else
|
||||||
|
#define LOG_ERRO(format, ...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if WEBCC_LOG_LEVEL <= WEBCC_FATA
|
||||||
#define LOG_FATA(format, ...) \
|
#define LOG_FATA(format, ...) \
|
||||||
webcc::LogWrite(webcc::FATA, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
|
webcc::LogWrite(WEBCC_FATA, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
|
||||||
|
#else
|
||||||
|
#define LOG_FATA(format, ...)
|
||||||
|
#endif
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
// See: https://stackoverflow.com/a/8488201
|
// See: https://stackoverflow.com/a/8488201
|
||||||
#define __FILENAME__ std::strrchr("/" __FILE__, '/') + 1
|
#define __FILENAME__ std::strrchr("/" __FILE__, '/') + 1
|
||||||
|
|
||||||
|
#if WEBCC_LOG_LEVEL <= WEBCC_VERB
|
||||||
#define LOG_VERB(format, args...) \
|
#define LOG_VERB(format, args...) \
|
||||||
webcc::LogWrite(webcc::VERB, __FILENAME__, __LINE__, format, ##args);
|
webcc::LogWrite(WEBCC_VERB, __FILENAME__, __LINE__, format, ##args);
|
||||||
|
#else
|
||||||
|
#define LOG_VERB(format, args...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if WEBCC_LOG_LEVEL <= WEBCC_INFO
|
||||||
#define LOG_INFO(format, args...) \
|
#define LOG_INFO(format, args...) \
|
||||||
webcc::LogWrite(webcc::INFO, __FILENAME__, __LINE__, format, ##args);
|
webcc::LogWrite(WEBCC_INFO, __FILENAME__, __LINE__, format, ##args);
|
||||||
|
#else
|
||||||
|
#define LOG_INFO(format, args...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if WEBCC_LOG_LEVEL <= WEBCC_WARN
|
||||||
#define LOG_WARN(format, args...) \
|
#define LOG_WARN(format, args...) \
|
||||||
webcc::LogWrite(webcc::WARN, __FILENAME__, __LINE__, format, ##args);
|
webcc::LogWrite(WEBCC_WARN, __FILENAME__, __LINE__, format, ##args);
|
||||||
|
#else
|
||||||
|
#define LOG_WARN(format, args...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if WEBCC_LOG_LEVEL <= WEBCC_ERRO
|
||||||
#define LOG_ERRO(format, args...) \
|
#define LOG_ERRO(format, args...) \
|
||||||
webcc::LogWrite(webcc::ERRO, __FILENAME__, __LINE__, format, ##args);
|
webcc::LogWrite(WEBCC_ERRO, __FILENAME__, __LINE__, format, ##args);
|
||||||
|
#else
|
||||||
|
#define LOG_ERRO(format, args...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if WEBCC_LOG_LEVEL <= WEBCC_FATA
|
||||||
#define LOG_FATA(format, args...) \
|
#define LOG_FATA(format, args...) \
|
||||||
webcc::LogWrite(webcc::FATA, __FILENAME__, __LINE__, format, ##args);
|
webcc::LogWrite(WEBCC_FATA, __FILENAME__, __LINE__, format, ##args);
|
||||||
|
#else
|
||||||
|
#define LOG_FATA(format, args...)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // defined(WIN32) || defined(_WIN64)
|
#endif // defined(WIN32) || defined(_WIN64)
|
||||||
|
|
||||||
#else // WEBCC_ENABLE_LOG == 0
|
#else // WEBCC_ENABLE_LOG == 0
|
||||||
|
|
||||||
#define LOG_INIT(level, modes)
|
#define LOG_INIT(modes)
|
||||||
|
|
||||||
#if (defined(WIN32) || defined(_WIN64))
|
#if (defined(WIN32) || defined(_WIN64))
|
||||||
#define LOG_VERB(format, ...)
|
#define LOG_VERB(format, ...)
|
||||||
|
|||||||
@@ -18,6 +18,14 @@ class SoapResponse : public SoapMessage {
|
|||||||
result_name_ = result_name;
|
result_name_ = result_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void set_result(const std::string& result) {
|
||||||
|
result_ = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_result(std::string&& result) {
|
||||||
|
result_ = std::move(result);
|
||||||
|
}
|
||||||
|
|
||||||
std::string result_moved() {
|
std::string result_moved() {
|
||||||
return std::move(result_);
|
return std::move(result_);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user