hikyuu2/hikyuu_cpp/hikyuu/Log.cpp

131 lines
3.9 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* Log.cpp
*
* Created on: 2013-2-1
* Author: fasiondog
*/
#include <thread>
2020-04-12 02:02:20 +08:00
#include "config.h"
2020-03-13 02:01:22 +08:00
#include "GlobalInitializer.h"
2015-01-07 01:26:14 +08:00
#include "Log.h"
2020-04-10 01:54:49 +08:00
#if USE_SPDLOG_LOGGER
// 使用 stdout_color 将无法将日志输出重定向至 python
//#include <spdlog/sinks/stdout_color_sinks.h>
2019-11-07 02:53:27 +08:00
#include <iostream>
#include "spdlog/sinks/ostream_sink.h"
//#include "spdlog/sinks/rotating_file_sink.h"
2020-04-10 01:54:49 +08:00
#if HKU_USE_SPDLOG_ASYNC_LOGGER
#include <spdlog/async.h>
2020-04-10 01:54:49 +08:00
#endif /* HKU_USE_SPDLOG_ASYNC_LOGGER */
#endif /* #if USE_SPDLOG_LOGGER */
namespace hku {
static std::thread::id g_main_thread_id = std::this_thread::get_id();
static int g_ioredirect_to_python_count = 0;
bool isLogInMainThread() {
return std::this_thread::get_id() == g_main_thread_id;
}
int getIORedirectToPythonCount() {
return g_ioredirect_to_python_count;
}
void increaseIORedicrectToPythonCount() {
g_ioredirect_to_python_count++;
}
void decreaseIORedicrectToPythonCount() {
g_ioredirect_to_python_count--;
}
2019-11-07 02:53:27 +08:00
static LOG_LEVEL g_log_level = TRACE;
2020-12-25 23:51:36 +08:00
std::string g_unknown_error_msg{"Unknown error!"};
2020-04-10 01:54:49 +08:00
LOG_LEVEL get_log_level() {
return g_log_level;
}
2019-11-07 02:53:27 +08:00
/**********************************************
* Use SPDLOG for logging
*********************************************/
2020-04-10 01:54:49 +08:00
#if USE_SPDLOG_LOGGER
2020-05-01 00:36:20 +08:00
std::shared_ptr<spdlog::logger> getHikyuuLogger() {
return spdlog::get("hikyuu");
}
2020-04-10 01:54:49 +08:00
#if HKU_USE_SPDLOG_ASYNC_LOGGER
void initLogger() {
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>();
2019-11-07 02:53:27 +08:00
stdout_sink->set_level(spdlog::level::trace);
spdlog::init_thread_pool(8192, 1);
std::vector<spdlog::sink_ptr> sinks{stdout_sink};
auto logger = std::make_shared<spdlog::async_logger>("hikyuu", sinks.begin(), sinks.end(),
spdlog::thread_pool(),
2019-11-07 02:53:27 +08:00
spdlog::async_overflow_policy::block);
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);
}
2020-04-10 01:54:49 +08:00
#else /* #if HKU_USE_SPDLOG_ASYNC_LOGGER */
2019-11-07 02:53:27 +08:00
void initLogger() {
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>();
2019-11-07 02:53:27 +08:00
stdout_sink->set_level(spdlog::level::trace);
2020-04-03 02:05:27 +08:00
auto logger = std::make_shared<spdlog::logger>("hikyuu", stdout_sink);
2019-11-07 02:53:27 +08:00
logger->set_level(spdlog::level::trace);
logger->flush_on(spdlog::level::trace);
2020-10-03 23:58:04 +08:00
// 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:%#)");
2019-11-07 02:53:27 +08:00
spdlog::register_logger(logger);
}
2020-04-10 01:54:49 +08:00
#endif /* #if HKU_USE_SPDLOG_ASYNC_LOGGER */
2020-04-03 02:05:27 +08:00
2020-04-10 01:54:49 +08:00
void set_log_level(LOG_LEVEL level) {
g_log_level = level;
getHikyuuLogger()->set_level((spdlog::level::level_enum)level);
2019-02-11 15:20:43 +08:00
}
2020-05-01 23:59:53 +08:00
#else /* #if USE_SPDLOG_LOGGER */
2020-04-10 01:54:49 +08:00
/**********************************************
* Use SPDLOG for logging
*********************************************/
void initLogger() {}
2020-04-10 01:54:49 +08:00
2019-02-11 15:20:43 +08:00
void set_log_level(LOG_LEVEL level) {
2020-04-10 01:54:49 +08:00
g_log_level = level;
}
2020-05-01 23:59:53 +08:00
std::string HKU_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() -
std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count() * 1000;
time_t tt = std::chrono::system_clock::to_time_t(now);
#ifdef _WIN32
struct tm now_time;
localtime_s(&now_time, &tt);
#else
struct tm now_time;
localtime_r(&tt, &now_time);
#endif
return fmt::format("{:%Y-%m-%d %H:%M:%S}.{:<3d}", now_time, dis_millseconds);
}
2020-04-10 01:54:49 +08:00
#endif /* #if USE_SPDLOG_LOGGER */
2020-04-03 02:05:27 +08:00
} // namespace hku