diff --git a/config.h.in b/config.h.in index e3b9db1e4..c9ddcc3fe 100644 --- a/config.h.in +++ b/config.h.in @@ -2,6 +2,8 @@ #ifndef HIKYUU_CONFIG_H_ #define HIKYUU_CONFIG_H_ +// clang-format off + #define HKU_VERSION "${VERSION}" #define HKU_VERSION_MAJOR ${VERSION_MAJOR} #define HKU_VERSION_MINOR ${VERSION_MINOR} @@ -19,11 +21,21 @@ // 检查下标越界 #define CHECK_ACCESS_BOUND ${CHECK_ACCESS_BOUND} +// 默认激活的日志级别 +#define LOG_ACTIVE_LEVEL ${LOG_ACTIVE_LEVEL} + +// 是否使用 spdlog +#define USE_SPDLOG_LOGGER ${USE_SPDLOG_LOGGER} + // 使用异步 logger -#define HKU_USE_ASYNC_LOGGER ${USE_ASYNC_LOGGER} -#define SPDLOG_ACTIVE_LEVEL ${SPDLOG_ACTIVE_LEVEL} +#define HKU_USE_SPDLOG_ASYNC_LOGGER ${USE_SPDLOG_ASYNC_LOGGER} + +// spdlog默认日志级别 +#define SPDLOG_ACTIVE_LEVEL ${LOG_ACTIVE_LEVEL} // 关闭 HKU_ASSERT #define HKU_DISABLE_ASSERT ${HKU_DISABLE_ASSERT} +// clang-format on + #endif /* HIKYUU_CONFIG_H_ */ \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu/GlobalInitializer.cpp b/hikyuu_cpp/hikyuu/GlobalInitializer.cpp index 09ce60be8..f9a661496 100644 --- a/hikyuu_cpp/hikyuu/GlobalInitializer.cpp +++ b/hikyuu_cpp/hikyuu/GlobalInitializer.cpp @@ -33,7 +33,7 @@ void GlobalInitializer::init() { _CrtSetBreakAlloc(-1); #endif - inner::init_logger(); + init_logger(); DataDriverFactory::init(); auto& sm = StockManager::instance(); } @@ -45,7 +45,9 @@ void GlobalInitializer::clean() { H5close(); - spdlog::drop_all(); +#if USE_SPDLOG_LOGGER + spdlog::shutdown(); +#endif #ifdef MSVC_LEAKER_DETECT // MSVC 内存泄露检测,输出至 VS 的输出窗口 diff --git a/hikyuu_cpp/hikyuu/Log.cpp b/hikyuu_cpp/hikyuu/Log.cpp index 6e633ce7a..be6a05a34 100644 --- a/hikyuu_cpp/hikyuu/Log.cpp +++ b/hikyuu_cpp/hikyuu/Log.cpp @@ -7,25 +7,31 @@ #include "GlobalInitializer.h" #include "Log.h" + +#if USE_SPDLOG_LOGGER #include #include //#include "spdlog/sinks/ostream_sink.h" //#include "spdlog/sinks/rotating_file_sink.h" -#if HKU_USE_ASYNC_LOGGER +#if HKU_USE_SPDLOG_ASYNC_LOGGER #include -#endif /* HKU_USE_ASYNC_LOGGER */ +#endif /* HKU_USE_SPDLOG_ASYNC_LOGGER */ +#endif /* #if USE_SPDLOG_LOGGER */ namespace hku { -namespace inner { - static LOG_LEVEL g_log_level = TRACE; +LOG_LEVEL get_log_level() { + return g_log_level; +} + /********************************************** * Use SPDLOG for logging *********************************************/ -#if HKU_USE_ASYNC_LOGGER +#if USE_SPDLOG_LOGGER +#if HKU_USE_SPDLOG_ASYNC_LOGGER void init_logger() { // auto stdout_sink = std::make_shared(std::cout, true); auto stdout_sink = std::make_shared(); @@ -44,13 +50,11 @@ void init_logger() { spdlog::register_logger(logger); } -} /*namespace inner */ - std::shared_ptr getHikyuuLogger() { return spdlog::get("hikyuu"); } -#else /* #if HKU_USE_ASYNC_LOGGER */ +#else /* #if HKU_USE_SPDLOG_ASYNC_LOGGER */ void init_logger() { auto stdout_sink = std::make_shared(); @@ -62,21 +66,26 @@ void init_logger() { spdlog::register_logger(logger); } -} /*namespace inner */ - std::shared_ptr HKU_API getHikyuuLogger() { return spdlog::get("hikyuu"); // inner::g_hikyuu_logger; } -#endif /* #if HKU_USE_ASYNC_LOGGER */ +#endif /* #if HKU_USE_SPDLOG_ASYNC_LOGGER */ -LOG_LEVEL get_log_level() { - return inner::g_log_level; +void set_log_level(LOG_LEVEL level) { + g_log_level = level; + getHikyuuLogger()->set_level((spdlog::level::level_enum)level); } +#else /* #if USE_SPDLOG_LOGGER */ +/********************************************** + * Use SPDLOG for logging + *********************************************/ +void init_logger() {} + void set_log_level(LOG_LEVEL level) { - inner::g_log_level = level; - getHikyuuLogger()->set_level((spdlog::level::level_enum)level); + g_log_level = level; } +#endif /* #if USE_SPDLOG_LOGGER */ } // namespace hku diff --git a/hikyuu_cpp/hikyuu/Log.h b/hikyuu_cpp/hikyuu/Log.h index f71eb0014..17dc8a7ef 100644 --- a/hikyuu_cpp/hikyuu/Log.h +++ b/hikyuu_cpp/hikyuu/Log.h @@ -10,9 +10,13 @@ #define HIKUU_LOG_H_ #include "config.h" -#include + +#if USE_SPDLOG_LOGGER #include #include +#endif + +#include #include #ifndef HKU_API @@ -29,14 +33,19 @@ namespace hku { * @{ */ +/********************************************** + * Use SPDLOG for logging + *********************************************/ +#if USE_SPDLOG_LOGGER +/** 日志级别 */ enum LOG_LEVEL { - TRACE = SPDLOG_LEVEL_TRACE, - DEBUG = SPDLOG_LEVEL_DEBUG, - INFO = SPDLOG_LEVEL_INFO, - WARN = SPDLOG_LEVEL_WARN, - ERROR = SPDLOG_LEVEL_ERROR, - FATAL = SPDLOG_LEVEL_CRITICAL, - OFF = SPDLOG_LEVEL_OFF, + TRACE = SPDLOG_LEVEL_TRACE, ///< 跟踪 + DEBUG = SPDLOG_LEVEL_DEBUG, ///< 调试 + INFO = SPDLOG_LEVEL_INFO, ///< 一般信息 + WARN = SPDLOG_LEVEL_WARN, ///< 告警 + ERROR = SPDLOG_LEVEL_ERROR, ///< 错误 + FATAL = SPDLOG_LEVEL_CRITICAL, ///< 致命 + OFF = SPDLOG_LEVEL_OFF, ///< 关闭日志打印 }; /** @@ -51,15 +60,12 @@ LOG_LEVEL HKU_API get_log_level(); */ void HKU_API set_log_level(LOG_LEVEL level); -#if HKU_USE_ASYNC_LOGGER +#if HKU_USE_SPDLOG_ASYNC_LOGGER std::shared_ptr HKU_API getHikyuuLogger(); #else std::shared_ptr HKU_API getHikyuuLogger(); -#endif /* #if HKU_USE_ASYNC_LOGGER */ +#endif /* #if HKU_USE_SPDLOG_ASYNC_LOGGER */ -/********************************************** - * Use SPDLOG for logging - *********************************************/ #define HKU_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__) #define HKU_TRACE(...) SPDLOG_LOGGER_TRACE(hku::getHikyuuLogger(), __VA_ARGS__) @@ -75,17 +81,77 @@ std::shared_ptr HKU_API getHikyuuLogger(); #define HKU_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__) #define HKU_ERROR(...) SPDLOG_LOGGER_ERROR(hku::getHikyuuLogger(), __VA_ARGS__) -#define HKU_LOGGER_FATAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__) +#define HKU_LOGGER_FATAL(logger, ...) \ + SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__) #define HKU_FATAL(...) SPDLOG_LOGGER_CRITICAL(hku::getHikyuuLogger(), __VA_ARGS__) -namespace inner { -#if HKU_USE_ASYNC_LOGGER +#if HKU_USE_SPDLOG_ASYNC_LOGGER void init_logger(); #else void init_logger(); #endif -} /* namespace inner */ + +#else +enum LOG_LEVEL { + TRACE = 0, + DEBUG = 1, + INFO = 2, + WARN = 3, + ERROR = 4, + FATAL = 5, + OFF = 6, +}; + +LOG_LEVEL HKU_API get_log_level(); +void HKU_API set_log_level(LOG_LEVEL level); +void init_logger(); + +#if LOG_ACTIVE_LEVEL <= 0 +#define HKU_TRACE(...) \ + fmt::print("[HKU-T] - {} ({}:{})\n", fmt::format(__VA_ARGS__), __FILE__, __LINE__); +#else +#define HKU_TRACE(...) +#endif + +#if LOG_ACTIVE_LEVEL <= 1 +#define HKU_DEBUG(...) \ + fmt::print("[HKU-D] - {} ({}:{})\n", fmt::format(__VA_ARGS__), __FILE__, __LINE__); +#else +#define HKU_DEBUG(...) +#endif + +#if LOG_ACTIVE_LEVEL <= 2 +#define HKU_INFO(...) \ + fmt::print("[HKU-I] - {} ({}:{})\n", fmt::format(__VA_ARGS__), __FILE__, __LINE__); +#else +#define HKU_INFO(...) +#endif + +#if LOG_ACTIVE_LEVEL <= 3 +#define HKU_WARN(...) \ + fmt::print("[HKU-W] - {} ({}:{})\n", fmt::format(__VA_ARGS__), __FILE__, __LINE__); +#else +#define HKU_WARN(...) +#endif + +#if LOG_ACTIVE_LEVEL <= 4 +#define HKU_ERROR(...) \ + fmt::print("[HKU-E] - {} ({}:{})\n", fmt::format(__VA_ARGS__), __FILE__, __LINE__); +#else +#define HKU_ERROR(...) +#endif + +#if LOG_ACTIVE_LEVEL <= 5 +#define HKU_FATAL(...) \ + fmt::print("[HKU-F] - {} ({}:{})\n", fmt::format(__VA_ARGS__), __FILE__, __LINE__); +#else +#define HKU_FATAL(...) +#endif + +#endif /* USE_SPDLOG_LOGGER */ /** @} */ + } /* namespace hku */ + #endif /* HIKUU_LOG_H_ */ diff --git a/hikyuu_cpp/hikyuu/hikyuu.cpp b/hikyuu_cpp/hikyuu/hikyuu.cpp index 9c7e030fa..9d93d592f 100644 --- a/hikyuu_cpp/hikyuu/hikyuu.cpp +++ b/hikyuu_cpp/hikyuu/hikyuu.cpp @@ -13,8 +13,6 @@ namespace hku { static Parameter g_hikyuu_context; void hikyuu_init(const string& config_file_name) { - // inner::init_logger(); - IniParser config; try { config.read(config_file_name); diff --git a/hikyuu_cpp/hikyuu/utilities/ConnectPool.h b/hikyuu_cpp/hikyuu/utilities/ConnectPool.h index ea1466caa..4859c8c17 100644 --- a/hikyuu_cpp/hikyuu/utilities/ConnectPool.h +++ b/hikyuu_cpp/hikyuu/utilities/ConnectPool.h @@ -11,6 +11,7 @@ #define HIKYUU_UTILITIES_CONNECTPOOL_H #include +#include #include #include "Parameter.h" @@ -85,6 +86,7 @@ class ConnectPool { m_connectList.push(p); } else { delete p; + p = nullptr; m_count--; } } else { diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.cpp b/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.cpp index 6d2e999da..31b537479 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.cpp +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.cpp @@ -7,6 +7,7 @@ * Author: fasiondog */ +#include #include "../../../config.h" #include "../../../Log.h" #include "SQLiteConnect.h" diff --git a/hikyuu_cpp/unit_test/hikyuu/datetime/test_TimeDelta.cpp b/hikyuu_cpp/unit_test/hikyuu/datetime/test_TimeDelta.cpp index b188ebb6e..f9848f1cf 100644 --- a/hikyuu_cpp/unit_test/hikyuu/datetime/test_TimeDelta.cpp +++ b/hikyuu_cpp/unit_test/hikyuu/datetime/test_TimeDelta.cpp @@ -400,6 +400,8 @@ TEST_CASE("test_TimeDelta_Datetime_operator") { CHECK(d == Datetime(2019, 12, 20)); } +#if HKU_SUPPORT_SERIALIZATION + /** @par 检测点 */ TEST_CASE("test_TimeDelat_serialize") { string filename(StockManager::instance().tmpdir()); @@ -422,4 +424,6 @@ TEST_CASE("test_TimeDelat_serialize") { CHECK(td1 == td2); } +#endif /* #if HKU_SUPPORT_SERIALIZATION */ + /** @} */ \ No newline at end of file diff --git a/hikyuu_cpp/unit_test/hikyuu/test_main.cpp b/hikyuu_cpp/unit_test/hikyuu/test_main.cpp index 2649537dc..352c6b798 100644 --- a/hikyuu_cpp/unit_test/hikyuu/test_main.cpp +++ b/hikyuu_cpp/unit_test/hikyuu/test_main.cpp @@ -50,7 +50,8 @@ int main(int argc, char** argv) { // !!! THIS IS JUST AN EXAMPLE SHOWING HOW DEFAULTS/OVERRIDES ARE SET !!! // defaults - // context.addFilter("test-case-exclude", "*math*"); // exclude test cases with "math" in their name + // context.addFilter("test-case-exclude", "*math*"); // exclude test cases with "math" in their + // name context.setOption("abort-after", 5); // stop test execution after 5 failed assertions context.setOption("order-by", "name"); // sort the test cases by their name diff --git a/hikyuu_cpp/unit_test/xmake.lua b/hikyuu_cpp/unit_test/xmake.lua index b835dde2c..a7b1e783d 100644 --- a/hikyuu_cpp/unit_test/xmake.lua +++ b/hikyuu_cpp/unit_test/xmake.lua @@ -86,7 +86,7 @@ target("small-test") -- add files add_files("./hikyuu/hikyuu/**.cpp"); - -- add_files("./hikyuu/hikyuu/test_StockManager.cpp"); + --add_files("./hikyuu/hikyuu/test_StockManager.cpp"); add_files("./hikyuu/test_main.cpp") target_end() \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 3ec0d5f46..cfd0db35c 100644 --- a/xmake.lua +++ b/xmake.lua @@ -5,10 +5,15 @@ set_project("hikyuu") -- version set_version("1.1.5") -set_configvar("USE_ASYNC_LOGGER", 0) -set_configvar("SPDLOG_ACTIVE_LEVEL", 2) +if is_mode("debug") then + set_configvar("LOG_ACTIVE_LEVEL", 0) -- 激活的日志级别 +else + set_configvar("LOG_ACTIVE_LEVEL", 2) -- 激活的日志级别 +end +set_configvar("USE_SPDLOG_LOGGER", 0) -- 是否使用spdlog作为日志输出 +set_configvar("USE_SPDLOG_ASYNC_LOGGER", 0) -- 使用异步的spdlog set_configvar("CHECK_ACCESS_BOUND", 1) -set_configvar("SUPPORT_SERIALIZATION", ifelse(is_mode("debug"), 0, 1)) +set_configvar("SUPPORT_SERIALIZATION", is_mode("release") and 1 or 0) set_configvar("SUPPORT_TEXT_ARCHIVE", 0) set_configvar("SUPPORT_XML_ARCHIVE", 1) set_configvar("SUPPORT_BINARY_ARCHIVE", 1)