From 4ed0d93024ea5d01d20a457ccf6ccd35375c5e47 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Wed, 17 Jul 2024 14:19:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=20hku=5Futils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.h.in | 12 -- config_utils.h.in | 21 ++- hikyuu_cpp/hikyuu/DataType.h | 1 + hikyuu_cpp/hikyuu/utilities/Log.cpp | 69 ++++++---- hikyuu_cpp/hikyuu/utilities/Log.h | 123 +++++++++++------- hikyuu_cpp/hikyuu/utilities/Parameter.h | 3 +- .../hikyuu/utilities/db_connect/DBCondition.h | 6 +- .../hikyuu/utilities/db_connect/DBConnect.h | 10 ++ .../utilities/db_connect/DBConnectBase.h | 3 +- .../hikyuu/utilities/db_connect/DBUpgrade.cpp | 2 +- .../hikyuu/utilities/db_connect/DBUpgrade.h | 6 +- .../utilities/db_connect/SQLStatementBase.h | 4 +- .../hikyuu/utilities/db_connect/TableMacro.h | 3 +- .../db_connect/mysql/MySQLConnect.cpp | 3 +- .../utilities/db_connect/mysql/MySQLConnect.h | 2 +- .../db_connect/mysql/MySQLStatement.h | 6 +- .../db_connect/sqlite/SQLiteConnect.cpp | 6 +- .../db_connect/sqlite/SQLiteConnect.h | 2 +- .../db_connect/sqlite/SQLiteStatement.h | 2 +- .../utilities/db_connect/sqlite/SQLiteUtil.h | 12 +- hikyuu_cpp/hikyuu/xmake.lua | 2 - hikyuu_cpp/unit_test/xmake.lua | 6 - hikyuu_pywrap/xmake.lua | 2 - xmake.lua | 41 ++++-- 24 files changed, 211 insertions(+), 136 deletions(-) diff --git a/config.h.in b/config.h.in index 798234db..0344c552 100644 --- a/config.h.in +++ b/config.h.in @@ -19,18 +19,6 @@ // 检查下标越界 #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_SPDLOG_ASYNC_LOGGER ${USE_SPDLOG_ASYNC_LOGGER} - -// spdlog默认日志级别 -#define SPDLOG_ACTIVE_LEVEL ${LOG_ACTIVE_LEVEL} - // 启用MSVC内存泄漏检查 #define ENABLE_MSVC_LEAK_DETECT ${ENABLE_MSVC_LEAK_DETECT} diff --git a/config_utils.h.in b/config_utils.h.in index 2ed22e83..f7437193 100644 --- a/config_utils.h.in +++ b/config_utils.h.in @@ -2,16 +2,33 @@ #ifndef HKU_UTILS_CONFIG_H_ #define HKU_UTILS_CONFIG_H_ +#include "osdef.h" + // clang-format off -#define HKU_SUPPORT_DATETIME 1 +${define HKU_ENABLE_MYSQL} +#if HKU_ENABLE_MYSQL && HKU_OS_WINDOWS +#ifndef NOMINMAX +#define NOMINMAX +#endif +#endif -#define HKU_ENABLE_INI_PARSER 1 +${define HKU_ENABLE_SQLITE} +${define HKU_ENABLE_SQLCIPHER} +${define HKU_SQL_TRACE} + +${define HKU_SUPPORT_DATETIME} + +${define HKU_ENABLE_INI_PARSER} ${define HKU_ENABLE_STACK_TRACE} ${define HKU_CLOSE_SPEND_TIME} +${define HKU_DEFAULT_LOG_NAME} +${define HKU_USE_SPDLOG_ASYNC_LOGGER} +${define HKU_LOG_ACTIVE_LEVEL} + // clang-format on #endif /* HKU_UTILS_CONFIG_H_ */ \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu/DataType.h b/hikyuu_cpp/hikyuu/DataType.h index 99f5f1ad..42011faf 100644 --- a/hikyuu_cpp/hikyuu/DataType.h +++ b/hikyuu_cpp/hikyuu/DataType.h @@ -29,6 +29,7 @@ #include #include +#include "config.h" #include "utilities/Log.h" #include "utilities/osdef.h" #include "utilities/cppdef.h" diff --git a/hikyuu_cpp/hikyuu/utilities/Log.cpp b/hikyuu_cpp/hikyuu/utilities/Log.cpp index 9b211a6b..06f388da 100644 --- a/hikyuu_cpp/hikyuu/utilities/Log.cpp +++ b/hikyuu_cpp/hikyuu/utilities/Log.cpp @@ -6,7 +6,6 @@ */ #include -#include "hikyuu/config.h" #include "hikyuu/GlobalInitializer.h" #include "os.h" #include "Log.h" @@ -23,14 +22,12 @@ #endif /* HKU_USE_SPDLOG_ASYNC_LOGGER */ #endif /* #if USE_SPDLOG_LOGGER */ +#ifndef HKU_DEFAULT_LOG_NAME +#define HKU_DEFAULT_LOG_NAME "hikyuu" +#endif + namespace hku { -static std::thread::id g_main_thread_id = std::this_thread::get_id(); - -bool isLogInMainThread() { - return std::this_thread::get_id() == g_main_thread_id; -} - static LOG_LEVEL g_log_level = LOG_LEVEL::LOG_TRACE; std::string g_unknown_error_msg{"Unknown error!"}; @@ -44,12 +41,12 @@ LOG_LEVEL get_log_level() { *********************************************/ #if USE_SPDLOG_LOGGER std::shared_ptr getHikyuuLogger() { - return spdlog::get("hikyuu"); + return spdlog::get(HKU_DEFAULT_LOG_NAME); } #if HKU_USE_SPDLOG_ASYNC_LOGGER -void initLogger(bool inJupyter) { - std::string logname = "hikyuu"; +void HKU_UTILS_API initLogger(bool inJupyter) { + std::string logname(HKU_DEFAULT_LOG_NAME); spdlog::drop(logname); std::shared_ptr logger = spdlog::get(logname); if (logger) { @@ -63,8 +60,21 @@ void initLogger(bool inJupyter) { } stdout_sink->set_level(spdlog::level::trace); - spdlog::init_thread_pool(8192, 1); + std::shared_ptr rotating_sink; + if (createDir(fmt::format("{}/.hikyuu", getUserDir()))) { + std::string log_filename = + fmt::format("{}/.hikyuu/{}.log", getUserDir(), HKU_DEFAULT_LOG_NAME); + rotating_sink = + std::make_shared(log_filename, 1024 * 1024 * 10, 3); + rotating_sink->set_level(spdlog::level::warn); + } + std::vector sinks{stdout_sink}; + if (rotating_sink) { + sinks.emplace_back(rotating_sink); + } + + spdlog::init_thread_pool(8192, 1); logger = std::make_shared(logname, sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); @@ -72,13 +82,15 @@ void initLogger(bool inJupyter) { logger->set_level(spdlog::level::trace); logger->flush_on(spdlog::level::trace); logger->set_pattern("%Y-%m-%d %H:%M:%S.%e [%^HKU-%L%$] - %v [%!]"); - spdlog::register_logger(logger); + // logger->set_pattern("%^%Y-%m-%d %H:%M:%S.%e [HKU-%L] - %v (%s:%#)%$"); + // spdlog::register_logger(logger); + spdlog::set_default_logger(logger); } #else /* #if HKU_USE_SPDLOG_ASYNC_LOGGER */ -void initLogger(bool inJupyter) { - std::string logname = "hikyuu"; +void HKU_UTILS_API initLogger(bool inJupyter) { + std::string logname(HKU_DEFAULT_LOG_NAME); spdlog::drop(logname); std::shared_ptr logger = spdlog::get(logname); if (logger) { @@ -91,18 +103,27 @@ void initLogger(bool inJupyter) { stdout_sink = std::make_shared(); } stdout_sink->set_level(spdlog::level::trace); - std::string log_filename = fmt::format("{}/.hikyuu/hikyuu.log", getUserDir()); - auto rotating_sink = - std::make_shared(log_filename, 1024 * 1024 * 10, 3); - rotating_sink->set_level(spdlog::level::warn); - std::vector sinks{stdout_sink, rotating_sink}; + + std::shared_ptr rotating_sink; + if (createDir(fmt::format("{}/.hikyuu", getUserDir()))) { + std::string log_filename = + fmt::format("{}/.hikyuu/{}.log", getUserDir(), HKU_DEFAULT_LOG_NAME); + rotating_sink = + std::make_shared(log_filename, 1024 * 1024 * 10, 3); + rotating_sink->set_level(spdlog::level::warn); + } + + std::vector sinks{stdout_sink}; + if (rotating_sink) { + sinks.emplace_back(rotating_sink); + } logger = std::make_shared(logname, sinks.begin(), sinks.end()); logger->set_level(spdlog::level::trace); logger->flush_on(spdlog::level::trace); - // logger->set_pattern("%Y-%m-%d %H:%M:%S.%e [%^HKU-%L%$] - %v [%!] (%@)"); - // logger->set_pattern("%Y-%m-%d %H:%M:%S.%e [%^HKU-%L%$] - %v (%@)"); logger->set_pattern("%Y-%m-%d %H:%M:%S.%e [%^HKU-%L%$] - %v (%s:%#)"); - spdlog::register_logger(logger); + // logger->set_pattern("%^%Y-%m-%d %H:%M:%S.%e [HKU-%L] - %v (%s:%#)%$"); + // spdlog::register_logger(logger); + spdlog::set_default_logger(logger); } #endif /* #if HKU_USE_SPDLOG_ASYNC_LOGGER */ @@ -116,13 +137,13 @@ void set_log_level(LOG_LEVEL level) { /********************************************** * Use SPDLOG for logging *********************************************/ -void initLogger(bool inJupyter) {} +void HKU_UTILS_API initLogger(bool inJupyter) {} void set_log_level(LOG_LEVEL level) { g_log_level = level; } -std::string HKU_API getLocalTime() { +std::string HKU_UTILS_API getLocalTime() { auto now = std::chrono::system_clock::now(); uint64_t dis_millseconds = std::chrono::duration_cast(now.time_since_epoch()).count() - diff --git a/hikyuu_cpp/hikyuu/utilities/Log.h b/hikyuu_cpp/hikyuu/utilities/Log.h index 7e819521..0097e35a 100644 --- a/hikyuu_cpp/hikyuu/utilities/Log.h +++ b/hikyuu_cpp/hikyuu/utilities/Log.h @@ -13,11 +13,20 @@ #define NOMINMAX #endif -#include "../config.h" +#include "config.h" #include "exception.h" +#ifndef HKU_LOG_ACTIVE_LEVEL +#define HKU_LOG_ACTIVE_LEVEL 0 +#endif + +#if !defined(USE_SPDLOG_LOGGER) +#define USE_SPDLOG_LOGGER 1 +#endif + // clang-format off #if USE_SPDLOG_LOGGER + #define SPDLOG_ACTIVE_LEVEL HKU_LOG_ACTIVE_LEVEL #include #include #if HKU_USE_SPDLOG_ASYNC_LOGGER @@ -30,26 +39,20 @@ #include #include -#ifdef HKU_ENABLE_STACK_TRACE +#ifndef HKU_ENABLE_STACK_TRACE +#define HKU_ENABLE_STACK_TRACE 0 +#endif + +#if HKU_ENABLE_STACK_TRACE #include #endif -#ifndef HKU_API -#define HKU_API +#ifndef HKU_UTILS_API +#define HKU_UTILS_API #endif namespace hku { -/** - * @ingroup Utilities - * @addtogroup logging Logging tools 日志工具 - * @details 打印等级: - * TRACE < DEBUG < INFO < WARN < ERROR < FATAL - * @{ - */ - -bool HKU_API isLogInMainThread(); - /********************************************** * Use SPDLOG for logging *********************************************/ @@ -69,15 +72,15 @@ enum LOG_LEVEL { * 获取当前日志级别 * @return */ -LOG_LEVEL HKU_API get_log_level(); +LOG_LEVEL HKU_UTILS_API get_log_level(); /** * 设置日志级别 * @param level 指定的日志级别 */ -void HKU_API set_log_level(LOG_LEVEL level); +void HKU_UTILS_API set_log_level(LOG_LEVEL level); -std::shared_ptr HKU_API getHikyuuLogger(); +std::shared_ptr HKU_UTILS_API getHikyuuLogger(); #define HKU_TRACE(...) SPDLOG_LOGGER_TRACE(hku::getHikyuuLogger(), __VA_ARGS__) #define HKU_DEBUG(...) SPDLOG_LOGGER_DEBUG(hku::getHikyuuLogger(), __VA_ARGS__) @@ -86,7 +89,7 @@ std::shared_ptr HKU_API getHikyuuLogger(); #define HKU_ERROR(...) SPDLOG_LOGGER_ERROR(hku::getHikyuuLogger(), __VA_ARGS__) #define HKU_FATAL(...) SPDLOG_LOGGER_CRITICAL(hku::getHikyuuLogger(), __VA_ARGS__) -void initLogger(bool inJupyter = false); +void HKU_UTILS_API initLogger(bool inJupyter = false); #else enum LOG_LEVEL { @@ -99,14 +102,14 @@ enum LOG_LEVEL { LOG_OFF = 6, }; -LOG_LEVEL HKU_API get_log_level(); -void HKU_API set_log_level(LOG_LEVEL level); -void initLogger(bool inJupyter = false); +LOG_LEVEL HKU_UTILS_API get_log_level(); +void HKU_UTILS_API set_log_level(LOG_LEVEL level); +void HKU_UTILS_API initLogger(bool inJupyter = false); /** 获取系统当前时间,精确到毫秒,如:2001-01-02 13:01:02.001 */ -std::string HKU_API getLocalTime(); +std::string HKU_UTILS_API getLocalTime(); -#if LOG_ACTIVE_LEVEL <= 0 +#if HKU_LOG_ACTIVE_LEVEL <= 0 #define HKU_TRACE(...) \ fmt::print("[{}] [HKU-T] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \ __LINE__); @@ -114,7 +117,7 @@ std::string HKU_API getLocalTime(); #define HKU_TRACE(...) #endif -#if LOG_ACTIVE_LEVEL <= 1 +#if HKU_LOG_ACTIVE_LEVEL <= 1 #define HKU_DEBUG(...) \ fmt::print("[{}] [HKU-D] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \ __LINE__); @@ -122,7 +125,7 @@ std::string HKU_API getLocalTime(); #define HKU_DEBUG(...) #endif -#if LOG_ACTIVE_LEVEL <= 2 +#if HKU_LOG_ACTIVE_LEVEL <= 2 #define HKU_INFO(...) \ fmt::print("[{}] [HKU-I] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \ __LINE__); @@ -130,7 +133,7 @@ std::string HKU_API getLocalTime(); #define HKU_INFO(...) #endif -#if LOG_ACTIVE_LEVEL <= 3 +#if HKU_LOG_ACTIVE_LEVEL <= 3 #define HKU_WARN(...) \ fmt::print("[{}] [HKU-W] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \ __LINE__); @@ -138,7 +141,7 @@ std::string HKU_API getLocalTime(); #define HKU_WARN(...) #endif -#if LOG_ACTIVE_LEVEL <= 4 +#if HKU_LOG_ACTIVE_LEVEL <= 4 #define HKU_ERROR(...) \ fmt::print("[{}] [HKU-E] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \ __LINE__); @@ -146,7 +149,7 @@ std::string HKU_API getLocalTime(); #define HKU_ERROR(...) #endif -#if LOG_ACTIVE_LEVEL <= 5 +#if HKU_LOG_ACTIVE_LEVEL <= 5 #define HKU_FATAL(...) \ fmt::print("[{}] [HKU-F] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \ __LINE__); @@ -166,7 +169,7 @@ std::string HKU_API getLocalTime(); #define HKU_FUNCTION __FUNCTION__ #endif -#ifndef HKU_ENABLE_STACK_TRACE +#if !HKU_ENABLE_STACK_TRACE /** * 若表达式为 false,将抛出 hku::exception 异常, 并附带传入信息 * @note 用于外部入参及结果检查 @@ -212,23 +215,9 @@ std::string HKU_API getLocalTime(); __FILE__, __LINE__)); \ } \ } while (0) -#endif // #ifndef HKU_ENABLE_STACK_TRACE +#endif // #if !HKU_ENABLE_STACK_TRACE -#if HKU_DEBUG_MODE -#define HKU_ASSERT_DEBUG(expr) -#else -/** 仅在 debug 模式下生效 */ -#define HKU_ASSERT_DEBUG(expr) \ - do { \ - if (!(expr)) { \ - std::string err_msg(fmt::format("HKU_ASSERT({})", #expr)); \ - throw hku::exception( \ - fmt::format("{} [{}] ({}:{})", err_msg, HKU_FUNCTION, __FILE__, __LINE__)); \ - } \ - } while (0) - -#endif /* #if HKU_DEBUG_MODE */ -#ifdef HKU_ENABLE_STACK_TRACE +#if HKU_ENABLE_STACK_TRACE /** * 若表达式为 false,将抛出 hku::exception 异常 * @note 仅用于内部入参检查,编译时可通过 HKU_DISABLE_ASSERT 宏关闭 @@ -253,9 +242,9 @@ std::string HKU_API getLocalTime(); } \ } while (0) -#endif // #ifndef HKU_ENABLE_STACK_TRACE +#endif // #if HKU_ENABLE_STACK_TRACE -#ifndef HKU_ENABLE_STACK_TRACE +#if !HKU_ENABLE_STACK_TRACE /** 抛出 hku::exception 及传入信息 */ #define HKU_THROW(...) \ do { \ @@ -286,7 +275,7 @@ std::string HKU_API getLocalTime(); throw except( \ fmt::format("EXCEPTION: {} [{}] ({}:{})", errmsg, HKU_FUNCTION, __FILE__, __LINE__)); \ } while (0) -#endif // #ifndef HKU_ENABLE_STACK_TRACE +#endif // #if !HKU_ENABLE_STACK_TRACE /** * 满足指定条件时,打印 TRACE 信息 @@ -426,6 +415,44 @@ extern std::string g_unknown_error_msg; #define HKU_ERROR_UNKNOWN HKU_ERROR(g_unknown_error_msg) #define HKU_FATAL_UNKNOWN HKU_FATAL(g_unknown_error_msg) +#define CLASS_LOGGER_IMP(cls) \ +public: \ + inline static const char* ms_logger = #cls; + +#define CLS_TRACE(...) HKU_TRACE(fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_DEBUG(...) HKU_DEBUG(fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_INFO(...) HKU_INFO(fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_WARN(...) HKU_WARN(fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_ERROR(...) HKU_ERROR(fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_FATAL(...) HKU_FATAL(fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) + +#define CLS_TRACE_IF(expr, ...) \ + HKU_TRACE_IF(expr, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_DEBUG_IF(expr, ...) \ + HKU_DEBUG_IF(expr, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_INFO_IF(expr, ...) \ + HKU_INFO_IF(expr, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_WARN_IF(expr, ...) \ + HKU_WARN_IF(expr, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_ERROR_IF(expr, ...) \ + HKU_ERROR_IF(expr, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_FATAL_IF(expr, ...) \ + HKU_FATAL_IF(expr, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) + +#define CLS_IF_RETURN(expr, ret) HKU_IF_RETURN(expr, ret) +#define CLS_TRACE_IF_RETURN(expr, ret, ...) \ + HKU_TRACE_IF_RETURN(expr, ret, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_DEBUG_IF_RETURN(expr, ret, ...) \ + HKU_DEBUG_IF_RETURN(expr, ret, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_INFO_IF_RETURN(expr, ret, ...) \ + HKU_INFO_IF_RETURN(expr, ret, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_WARN_IF_RETURN(expr, ret, ...) \ + HKU_WARN_IF_RETURN(expr, ret, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_ERROR_IF_RETURN(expr, ret, ...) \ + HKU_ERROR_IF_RETURN(expr, ret, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) +#define CLS_FATAL_IF_RETURN(expr, ret, ...) \ + HKU_FATAL_IF_RETURN(expr, ret, fmt::format("[{}] {}", ms_logger, fmt::format(__VA_ARGS__))) + /** @} */ } /* namespace hku */ diff --git a/hikyuu_cpp/hikyuu/utilities/Parameter.h b/hikyuu_cpp/hikyuu/utilities/Parameter.h index 8b8fb569..6b9042e2 100644 --- a/hikyuu_cpp/hikyuu/utilities/Parameter.h +++ b/hikyuu_cpp/hikyuu/utilities/Parameter.h @@ -17,7 +17,8 @@ #include #include -#include "../config.h" +#include "hikyuu/config.h" +#include "hikyuu/utilities/config.h" #if HKU_SUPPORT_SERIALIZATION #include diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/DBCondition.h b/hikyuu_cpp/hikyuu/utilities/db_connect/DBCondition.h index 376df11c..59898048 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/DBCondition.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/DBCondition.h @@ -15,8 +15,8 @@ #include #include "hikyuu/utilities/Log.h" -#ifndef HKU_API -#define HKU_API +#ifndef HKU_UTILS_API +#define HKU_UTILS_API #endif namespace hku { @@ -38,7 +38,7 @@ struct LIMIT { int limit = 1; }; -class HKU_API DBCondition { +class HKU_UTILS_API DBCondition { public: DBCondition() = default; DBCondition(const DBCondition &) = default; diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/DBConnect.h b/hikyuu_cpp/hikyuu/utilities/db_connect/DBConnect.h index 30d8a9ee..e0a33ae7 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/DBConnect.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/DBConnect.h @@ -17,4 +17,14 @@ #include "TableMacro.h" #include "DBUpgrade.h" +#include "hikyuu/utilities/config.h" +#if HKU_ENABLE_MYSQL +#include "mysql/MySQLConnect.h" +#endif + +#if HKU_ENABLE_SQLITE +#include "sqlite/SQLiteConnect.h" +#include "sqlite/SQLiteUtil.h" +#endif + #endif /* HIKYUU_DB_CONNECT_H */ \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/DBConnectBase.h b/hikyuu_cpp/hikyuu/utilities/db_connect/DBConnectBase.h index b91ec716..fb6ea81b 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/DBConnectBase.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/DBConnectBase.h @@ -10,7 +10,6 @@ #ifndef HIKYUU_DB_CONNECT_DBCONNECTBASE_H #define HIKYUU_DB_CONNECT_DBCONNECTBASE_H -#include "../../DataType.h" #include "../../utilities/Parameter.h" #include "../Null.h" #include "DBCondition.h" @@ -26,7 +25,7 @@ class SQLResultSet; * 数据库连接基类 * @ingroup DBConnect */ -class HKU_API DBConnectBase : public std::enable_shared_from_this { +class HKU_UTILS_API DBConnectBase : public std::enable_shared_from_this { PARAMETER_SUPPORT // NOSONAR public : diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.cpp b/hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.cpp index f523e057..ac3c2797 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.cpp +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.cpp @@ -12,7 +12,7 @@ namespace hku { /* * 升级和创建数据库 */ -void HKU_API DBUpgrade(const DBConnectPtr &driver, const char *module_name, +void HKU_UTILS_API DBUpgrade(const DBConnectPtr &driver, const char *module_name, const std::vector &upgrade_scripts, int start_version, const char *create_script) { HKU_TRACE("check {} database version ...", module_name); diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.h b/hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.h index 883fa1d7..dd70ea46 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.h @@ -21,8 +21,8 @@ namespace hku { * @param create_script 数据库创建脚本,若对应的数据库不存在则使用该脚本创建数据库 * @ingroup DataDriver */ -void HKU_API DBUpgrade(const DBConnectPtr &driver, const char *module_name, - const std::vector &upgrade_scripts, int start_version = 2, - const char *create_script = nullptr); +void HKU_UTILS_API DBUpgrade(const DBConnectPtr &driver, const char *module_name, + const std::vector &upgrade_scripts, int start_version = 2, + const char *create_script = nullptr); } // namespace hku \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/SQLStatementBase.h b/hikyuu_cpp/hikyuu/utilities/db_connect/SQLStatementBase.h index 1fc9d057..a76580c1 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/SQLStatementBase.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/SQLStatementBase.h @@ -34,7 +34,7 @@ public: * SQL Statement 基类 * @ingroup DBConnect */ -class HKU_API SQLStatementBase { +class HKU_UTILS_API SQLStatementBase { public: /** * 构造函数 @@ -183,7 +183,7 @@ inline void SQLStatementBase::bind(int idx, float item) { } inline void SQLStatementBase::exec() { -#ifdef HKU_SQL_TRACE +#if HKU_SQL_TRACE HKU_DEBUG(m_sql_string); #endif sub_exec(); diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/TableMacro.h b/hikyuu_cpp/hikyuu/utilities/db_connect/TableMacro.h index a6d9b956..b8a4e3eb 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/TableMacro.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/TableMacro.h @@ -2168,8 +2168,7 @@ public: } #define TABLE_NO_AUTOID_BIND12(ROWID, table, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12) \ - pprivate: \ - uint64_t m_rowid = 0; \ + pprivate : uint64_t m_rowid = 0; \ \ public: \ bool valid() const { \ diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLConnect.cpp b/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLConnect.cpp index 068c260d..fbf6a2e9 100755 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLConnect.cpp +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLConnect.cpp @@ -7,6 +7,7 @@ * Author: fasiondog */ +#include "hikyuu/utilities/config.h" #include "MySQLConnect.h" #ifdef __GNUC__ @@ -110,7 +111,7 @@ bool MySQLConnect::ping() { } int64_t MySQLConnect::exec(const std::string& sql_string) { -#ifdef HKU_SQL_TRACE +#if HKU_SQL_TRACE HKU_DEBUG(sql_string); #endif if (!m_mysql) { diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLConnect.h b/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLConnect.h index 948ebd10..bf66202e 100755 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLConnect.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLConnect.h @@ -22,7 +22,7 @@ namespace hku { -class HKU_API MySQLConnect : public DBConnectBase { +class HKU_UTILS_API MySQLConnect : public DBConnectBase { public: explicit MySQLConnect(const Parameter ¶m); virtual ~MySQLConnect(); diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLStatement.h b/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLStatement.h index e49bb10f..3df3a012 100755 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLStatement.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/mysql/MySQLStatement.h @@ -26,13 +26,13 @@ typedef bool my_bool; #endif -#ifndef HKU_API -#define HKU_API +#ifndef HKU_UTILS_API +#define HKU_UTILS_API #endif namespace hku { -class HKU_API MySQLStatement : public SQLStatementBase { +class HKU_UTILS_API MySQLStatement : public SQLStatementBase { public: MySQLStatement() = delete; MySQLStatement(DBConnectBase *driver, const std::string &sql_statement); diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.cpp b/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.cpp index 2728c7bb..b8ae369b 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.cpp +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.cpp @@ -8,7 +8,7 @@ */ #include -#include "../../../config.h" +#include "hikyuu/utilities/config.h" #include "hikyuu/utilities/Log.h" #include "SQLiteConnect.h" #include "SQLiteStatement.h" @@ -35,7 +35,7 @@ SQLiteConnect::SQLiteConnect(const Parameter ¶m) : DBConnectBase(param), m_d int rc = sqlite3_open_v2(m_dbname.c_str(), &m_db, flags, NULL); SQL_CHECK(rc == SQLITE_OK, rc, sqlite3_errmsg(m_db)); -#ifdef HKU_ENABLE_SQLCIPHER +#if HKU_ENABLE_SQLCIPHER if (haveParam("key")) { std::string key = getParam("key"); if (!key.empty()) { @@ -91,7 +91,7 @@ void SQLiteConnect::close() { } int64_t SQLiteConnect::exec(const std::string &sql_string) { -#ifdef HKU_SQL_TRACE +#if HKU_SQL_TRACE HKU_DEBUG(sql_string); #endif int rc = sqlite3_exec(m_db, sql_string.c_str(), NULL, NULL, NULL); diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.h b/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.h index 34effb7b..a1cd42c3 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteConnect.h @@ -25,7 +25,7 @@ namespace hku { * SQLite连接 * @ingroup SQLite */ -class HKU_API SQLiteConnect : public DBConnectBase { +class HKU_UTILS_API SQLiteConnect : public DBConnectBase { public: /** * 构造函数 diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteStatement.h b/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteStatement.h index 593e5cbc..516c4c48 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteStatement.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteStatement.h @@ -19,7 +19,7 @@ namespace hku { * SQLite Statemen * @ingroup DBConnect */ -class HKU_API SQLiteStatement : public SQLStatementBase { +class HKU_UTILS_API SQLiteStatement : public SQLStatementBase { public: SQLiteStatement() = delete; diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteUtil.h b/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteUtil.h index 53c46b39..f5ac2d96 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteUtil.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/sqlite/SQLiteUtil.h @@ -15,7 +15,7 @@ namespace hku { * @brief SQLite 其他相关操作方法集合 * @ingroup DBConnect */ -class HKU_API SQLiteUtil { +class HKU_UTILS_API SQLiteUtil { public: SQLiteUtil() = default; ~SQLiteUtil() = default; @@ -43,8 +43,8 @@ public: * @param n_page 分批备份时每次循环备份的 page 数,小于等于0时,一次性备份,不进行分批备份 * @param step_sleep 分批备份时每次循环后,休眠间隔时长(毫秒),以便让出cpu */ - static BackupResult onlineBackup(const std::shared_ptr& conn, - const std::string& dst, int n_page = -1, + static BackupResult onlineBackup(const std::shared_ptr &conn, + const std::string &dst, int n_page = -1, int step_sleep = 250) noexcept; /** * @brief 在线备份数据库,不影响其他数据库连接进行操作 @@ -53,7 +53,7 @@ public: * @param n_page 分批备份时每次循环备份的 page 数,小于等于0时,一次性备份,不进行分批备份 * @param step_sleep 分批备份时每次循环后,休眠间隔时长(毫秒),以便让出cpu */ - static BackupResult onlineBackup(const std::string& src, const std::string& dst, + static BackupResult onlineBackup(const std::string &src, const std::string &dst, int n_page = -1, int step_sleep = 250) noexcept; /** @@ -65,7 +65,7 @@ public: * @param save_bad 是否保存损坏的数据,将 dst 及 dst-journal 加上后缀 .bad 另存 * @return RecoverResult */ - static RecoverResult recoverFromBackup(const std::string& backup, const std::string& dst, + static RecoverResult recoverFromBackup(const std::string &backup, const std::string &dst, bool save_bad = false) noexcept; /** @@ -74,7 +74,7 @@ public: * @return true 指定的数据库文件及其日志文件都被成功删除或都不存在时,返回成功 * @return false 指定的数据文件及其日志文件,其中一个删除失败都会返回删除失败 */ - static bool removeDBFile(const std::string& dbfilename); + static bool removeDBFile(const std::string &dbfilename); }; } // namespace hku \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu/xmake.lua b/hikyuu_cpp/hikyuu/xmake.lua index 3cbd9318..90fab2ef 100644 --- a/hikyuu_cpp/hikyuu/xmake.lua +++ b/hikyuu_cpp/hikyuu/xmake.lua @@ -7,8 +7,6 @@ target("hikyuu") -- set_kind("shared") -- end - add_options("hdf5", "mysql", "sqlite", "tdx", "feedback", "stacktrace", "log_level") - add_packages("boost", "fmt", "spdlog", "flatbuffers", "nng", "nlohmann_json", "cpp-httplib") if is_plat("windows", "linux", "cross") then add_packages("sqlite3") diff --git a/hikyuu_cpp/unit_test/xmake.lua b/hikyuu_cpp/unit_test/xmake.lua index ffd55333..40108e33 100644 --- a/hikyuu_cpp/unit_test/xmake.lua +++ b/hikyuu_cpp/unit_test/xmake.lua @@ -59,8 +59,6 @@ target("unit-test") set_kind("binary") set_default(false) - add_options("hdf5", "mysql", "sqlite", "tdx", "feedback", "stacktrace", "log_level") - add_packages("boost", "fmt", "spdlog", "doctest", "sqlite3") if get_config("mysql") then if is_plat("macosx") then @@ -107,8 +105,6 @@ target("small-test") set_kind("binary") set_default(false) - add_options("hdf5", "mysql", "sqlite", "tdx", "feedback", "stacktrace", "log_level") - add_packages("boost", "fmt", "spdlog", "doctest", "sqlite3") if get_config("mysql") then if is_plat("macosx") then @@ -154,8 +150,6 @@ target("real-test") set_kind("binary") set_default(false) - add_options("hdf5", "mysql", "sqlite", "tdx", "feedback", "stacktrace", "log_level") - add_packages("boost", "fmt", "spdlog", "doctest", "sqlite3") if get_config("mysql") then if is_plat("macosx") then diff --git a/hikyuu_pywrap/xmake.lua b/hikyuu_pywrap/xmake.lua index 1a67540f..7647f5cc 100644 --- a/hikyuu_pywrap/xmake.lua +++ b/hikyuu_pywrap/xmake.lua @@ -9,8 +9,6 @@ target("core") -- --set_enable(false) --set_enable(false)会彻底禁用这个target,连target的meta也不会被加载,vcproj不会保留它 -- end - add_options("hdf5", "mysql", "sqlite", "tdx", "feedback", "stacktrace", "log_level") - add_deps("hikyuu") add_packages("boost", "fmt", "spdlog", "flatbuffers", "pybind11", "cpp-httplib") if is_plat("windows") then diff --git a/xmake.lua b/xmake.lua index 62e5725a..04e1b6f0 100644 --- a/xmake.lua +++ b/xmake.lua @@ -50,13 +50,19 @@ option("tdx") set_description("Enable tdx kdata engine.") option_end() +option("sql_trace") + set_default(false) + set_showmenu(true) + set_category("hikyuu") + set_description("打印执行的 SQL 语句") +option_end() + -- 注意:stacktrace 在 windows 下会严重影响性能 option("stacktrace") set_default(false) set_showmenu(true) set_category("hikyuu") set_description("Enable check/assert with stack trace info.") - add_defines("HKU_ENABLE_STACK_TRACE") option_end() option("spend_time") @@ -77,7 +83,7 @@ option("low_precision") set_default(false) set_showmenu(true) set_category("hikyuu") - set_description("Enable send feedback.") + set_description("Enable low precision.") option_end() option("log_level") @@ -88,6 +94,13 @@ option("log_level") set_description("set log level") option_end() +option("async_log") + set_default(false) + set_showmenu(true) + set_category("hikyuu") + set_description("Use async log.") +option_end() + option("leak_check") set_default(false) set_showmenu(true) @@ -116,19 +129,19 @@ if is_mode("debug") then level = "trace" end if level == "trace" then - set_configvar("LOG_ACTIVE_LEVEL", 0) + set_configvar("HKU_LOG_ACTIVE_LEVEL", 0) elseif level == "debug" then - set_configvar("LOG_ACTIVE_LEVEL", 1) + set_configvar("HKU_LOG_ACTIVE_LEVEL", 1) elseif level == "info" then - set_configvar("LOG_ACTIVE_LEVEL", 2) + set_configvar("HKU_LOG_ACTIVE_LEVEL", 2) elseif level == "warn" then - set_configvar("LOG_ACTIVE_LEVEL", 3) + set_configvar("HKU_LOG_ACTIVE_LEVEL", 3) elseif level == "error" then - set_configvar("LOG_ACTIVE_LEVEL", 4) + set_configvar("HKU_LOG_ACTIVE_LEVEL", 4) elseif level == "fatal" then - set_configvar("LOG_ACTIVE_LEVEL", 5) + set_configvar("HKU_LOG_ACTIVE_LEVEL", 5) else - set_configvar("LOG_ACTIVE_LEVEL", 6) + set_configvar("HKU_LOG_ACTIVE_LEVEL", 6) end if is_mode("debug") then @@ -136,7 +149,6 @@ if is_mode("debug") then else set_configvar("HKU_DEBUG_MODE", 0) end -set_configvar("USE_SPDLOG_LOGGER", 1) -- 是否使用spdlog作为日志输出 set_configvar("USE_SPDLOG_ASYNC_LOGGER", 0) -- 使用异步的spdlog set_configvar("CHECK_ACCESS_BOUND", 1) if is_plat("macosx") or get_config("leak_check") then @@ -152,12 +164,21 @@ set_configvar("HKU_ENABLE_LEAK_DETECT", get_config("leak_check") and 1 or 0) set_configvar("HKU_ENABLE_SEND_FEEDBACK", get_config("feedback") and 1 or 0) set_configvar("HKU_ENABLE_HDF5_KDATA", get_config("hdf5") and 1 or 0) +set_configvar("HKU_ENABLE_MYSQL", get_config("mysql") and 1 or 0) set_configvar("HKU_ENABLE_MYSQL_KDATA", get_config("mysql") and 1 or 0) +set_configvar("HKU_ENABLE_SQLITE", (get_config("sqlite") or get_config("hdf5")) and 1 or 0) set_configvar("HKU_ENABLE_SQLITE_KDATA", get_config("sqlite") and 1 or 0) set_configvar("HKU_ENABLE_TDX_KDATA", get_config("tdx") and 1 or 0) set_configvar("HKU_USE_LOW_PRECISION", get_config("low_precision") and 1 or 0) +set_configvar("HKU_DEFAULT_LOG_NAME", "hikyuu") +set_configvar("HKU_SUPPORT_DATETIME", 1) +set_configvar("HKU_ENABLE_SQLCIPHER", 0) +set_configvar("HKU_SQL_TRACE", get_config("sql_trace")) +set_configvar("HKU_ENABLE_INI_PARSER", 1) +set_configvar("HKU_USE_SPDLOG_ASYNC_LOGGER", get_config("async_log") and 1 or 0) +set_configvar("HKU_ENABLE_STACK_TRACE", get_config("stacktrace") and 1 or 0) set_configvar("HKU_CLOSE_SPEND_TIME", get_config("spend_time") and 0 or 1) set_warnings("all")