Skip to content

Commit

Permalink
内存泄漏检测优化
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Apr 9, 2020
1 parent 3cee4c6 commit 19a8eef
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 43 deletions.
16 changes: 14 additions & 2 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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_ */
6 changes: 4 additions & 2 deletions hikyuu_cpp/hikyuu/GlobalInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void GlobalInitializer::init() {
_CrtSetBreakAlloc(-1);
#endif

inner::init_logger();
init_logger();
DataDriverFactory::init();
auto& sm = StockManager::instance();
}
Expand All @@ -45,7 +45,9 @@ void GlobalInitializer::clean() {

H5close();

spdlog::drop_all();
#if USE_SPDLOG_LOGGER
spdlog::shutdown();
#endif

#ifdef MSVC_LEAKER_DETECT
// MSVC 内存泄露检测,输出至 VS 的输出窗口
Expand Down
39 changes: 24 additions & 15 deletions hikyuu_cpp/hikyuu/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@

#include "GlobalInitializer.h"
#include "Log.h"

#if USE_SPDLOG_LOGGER
#include <spdlog/sinks/stdout_color_sinks.h>
#include <iostream>
//#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 <spdlog/async.h>
#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<spdlog::sinks::ostream_sink_mt>(std::cout, true);
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
Expand All @@ -44,13 +50,11 @@ void init_logger() {
spdlog::register_logger(logger);
}

} /*namespace inner */

std::shared_ptr<spdlog::async_logger> 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<spdlog::sinks::stdout_color_sink_mt>();
Expand All @@ -62,21 +66,26 @@ void init_logger() {
spdlog::register_logger(logger);
}

} /*namespace inner */

std::shared_ptr<spdlog::logger> 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
100 changes: 83 additions & 17 deletions hikyuu_cpp/hikyuu/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
#define HIKUU_LOG_H_

#include "config.h"
#include <string>

#if USE_SPDLOG_LOGGER
#include <spdlog/spdlog.h>
#include <spdlog/fmt/ostr.h>
#endif

#include <fmt/ostream.h>
#include <fmt/format.h>

#ifndef HKU_API
Expand All @@ -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, ///< 关闭日志打印
};

/**
Expand All @@ -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<spdlog::async_logger> HKU_API getHikyuuLogger();
#else
std::shared_ptr<spdlog::logger> 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__)

Expand All @@ -75,17 +81,77 @@ std::shared_ptr<spdlog::logger> 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_ */
2 changes: 0 additions & 2 deletions hikyuu_cpp/hikyuu/hikyuu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions hikyuu_cpp/hikyuu/utilities/ConnectPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define HIKYUU_UTILITIES_CONNECTPOOL_H

#include <thread>
#include <mutex>
#include <queue>
#include "Parameter.h"

Expand Down Expand Up @@ -85,6 +86,7 @@ class ConnectPool {
m_connectList.push(p);
} else {
delete p;
p = nullptr;
m_count--;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Author: fasiondog
*/

#include <thread>
#include "../../../config.h"
#include "../../../Log.h"
#include "SQLiteConnect.h"
Expand Down
4 changes: 4 additions & 0 deletions hikyuu_cpp/unit_test/hikyuu/datetime/test_TimeDelta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -422,4 +424,6 @@ TEST_CASE("test_TimeDelat_serialize") {
CHECK(td1 == td2);
}

#endif /* #if HKU_SUPPORT_SERIALIZATION */

/** @} */
3 changes: 2 additions & 1 deletion hikyuu_cpp/unit_test/hikyuu/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion hikyuu_cpp/unit_test/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
11 changes: 8 additions & 3 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 19a8eef

Please sign in to comment.