mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-11-29 18:39:10 +08:00
同步 hku_utils
This commit is contained in:
parent
099e99d0a8
commit
4ed0d93024
12
config.h.in
12
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}
|
||||
|
||||
|
@ -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_ */
|
@ -29,6 +29,7 @@
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "config.h"
|
||||
#include "utilities/Log.h"
|
||||
#include "utilities/osdef.h"
|
||||
#include "utilities/cppdef.h"
|
||||
|
@ -6,7 +6,6 @@
|
||||
*/
|
||||
|
||||
#include <thread>
|
||||
#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<spdlog::logger> 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<spdlog::logger> 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<spdlog::sinks::rotating_file_sink_mt> 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<spdlog::sinks::rotating_file_sink_mt>(log_filename, 1024 * 1024 * 10, 3);
|
||||
rotating_sink->set_level(spdlog::level::warn);
|
||||
}
|
||||
|
||||
std::vector<spdlog::sink_ptr> sinks{stdout_sink};
|
||||
if (rotating_sink) {
|
||||
sinks.emplace_back(rotating_sink);
|
||||
}
|
||||
|
||||
spdlog::init_thread_pool(8192, 1);
|
||||
logger = std::make_shared<spdlog::async_logger>(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<spdlog::logger> logger = spdlog::get(logname);
|
||||
if (logger) {
|
||||
@ -91,18 +103,27 @@ void initLogger(bool inJupyter) {
|
||||
stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||
}
|
||||
stdout_sink->set_level(spdlog::level::trace);
|
||||
std::string log_filename = fmt::format("{}/.hikyuu/hikyuu.log", getUserDir());
|
||||
auto rotating_sink =
|
||||
std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_filename, 1024 * 1024 * 10, 3);
|
||||
rotating_sink->set_level(spdlog::level::warn);
|
||||
std::vector<spdlog::sink_ptr> sinks{stdout_sink, rotating_sink};
|
||||
|
||||
std::shared_ptr<spdlog::sinks::rotating_file_sink_mt> 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<spdlog::sinks::rotating_file_sink_mt>(log_filename, 1024 * 1024 * 10, 3);
|
||||
rotating_sink->set_level(spdlog::level::warn);
|
||||
}
|
||||
|
||||
std::vector<spdlog::sink_ptr> sinks{stdout_sink};
|
||||
if (rotating_sink) {
|
||||
sinks.emplace_back(rotating_sink);
|
||||
}
|
||||
logger = std::make_shared<spdlog::logger>(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<std::chrono::milliseconds>(now.time_since_epoch()).count() -
|
||||
|
@ -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 <spdlog/spdlog.h>
|
||||
#include <spdlog/fmt/ostr.h>
|
||||
#if HKU_USE_SPDLOG_ASYNC_LOGGER
|
||||
@ -30,26 +39,20 @@
|
||||
#include <fmt/format.h>
|
||||
#include <fmt/chrono.h>
|
||||
|
||||
#ifdef HKU_ENABLE_STACK_TRACE
|
||||
#ifndef HKU_ENABLE_STACK_TRACE
|
||||
#define HKU_ENABLE_STACK_TRACE 0
|
||||
#endif
|
||||
|
||||
#if HKU_ENABLE_STACK_TRACE
|
||||
#include <boost/stacktrace.hpp>
|
||||
#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<spdlog::logger> HKU_API getHikyuuLogger();
|
||||
std::shared_ptr<spdlog::logger> 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<spdlog::logger> 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 */
|
||||
|
@ -17,7 +17,8 @@
|
||||
#include <map>
|
||||
#include <boost/any.hpp>
|
||||
|
||||
#include "../config.h"
|
||||
#include "hikyuu/config.h"
|
||||
#include "hikyuu/utilities/config.h"
|
||||
|
||||
#if HKU_SUPPORT_SERIALIZATION
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
@ -15,8 +15,8 @@
|
||||
#include <fmt/ranges.h>
|
||||
#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;
|
||||
|
@ -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 */
|
@ -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<DBConnectBase> {
|
||||
class HKU_UTILS_API DBConnectBase : public std::enable_shared_from_this<DBConnectBase> {
|
||||
PARAMETER_SUPPORT // NOSONAR
|
||||
|
||||
public :
|
||||
|
@ -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<std::string> &upgrade_scripts, int start_version,
|
||||
const char *create_script) {
|
||||
HKU_TRACE("check {} database version ...", module_name);
|
||||
|
@ -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<std::string> &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<std::string> &upgrade_scripts, int start_version = 2,
|
||||
const char *create_script = nullptr);
|
||||
|
||||
} // namespace hku
|
@ -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();
|
||||
|
@ -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 { \
|
||||
|
@ -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) {
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
|
@ -8,7 +8,7 @@
|
||||
*/
|
||||
|
||||
#include <thread>
|
||||
#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<std::string>("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);
|
||||
|
@ -25,7 +25,7 @@ namespace hku {
|
||||
* SQLite连接
|
||||
* @ingroup SQLite
|
||||
*/
|
||||
class HKU_API SQLiteConnect : public DBConnectBase {
|
||||
class HKU_UTILS_API SQLiteConnect : public DBConnectBase {
|
||||
public:
|
||||
/**
|
||||
* 构造函数
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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<SQLiteConnect>& conn,
|
||||
const std::string& dst, int n_page = -1,
|
||||
static BackupResult onlineBackup(const std::shared_ptr<SQLiteConnect> &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
|
@ -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")
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
41
xmake.lua
41
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")
|
||||
|
Loading…
Reference in New Issue
Block a user