hikyuu2/hikyuu_cpp/hikyuu/Log.h

438 lines
16 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* Log.h
*
* Created on: 2013-2-1
* Author: fasiondog
*/
#pragma once
#ifndef HIKUU_LOG_H_
#define HIKUU_LOG_H_
2015-01-07 01:26:14 +08:00
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include "config.h"
#include "exception.h"
2020-04-10 01:54:49 +08:00
2020-04-12 02:02:20 +08:00
// clang-format off
2020-04-10 01:54:49 +08:00
#if USE_SPDLOG_LOGGER
2020-04-12 02:02:20 +08:00
#include <spdlog/spdlog.h>
#include <spdlog/fmt/ostr.h>
#if HKU_USE_SPDLOG_ASYNC_LOGGER
#include "spdlog/async.h"
#endif
2020-04-10 01:54:49 +08:00
#endif
2020-04-12 02:02:20 +08:00
// clang-format on
2020-04-10 01:54:49 +08:00
#include <fmt/ostream.h>
#include <fmt/format.h>
2020-05-01 23:59:53 +08:00
#include <fmt/chrono.h>
2015-01-07 01:26:14 +08:00
2023-10-15 00:30:45 +08:00
#ifdef HKU_ENABLE_STACK_TRACE
#include <boost/stacktrace.hpp>
#endif
2015-01-07 01:26:14 +08:00
#ifndef HKU_API
#define HKU_API
#endif
namespace hku {
/**
* @ingroup Utilities
* @addtogroup logging Logging tools
* @details
* TRACE < DEBUG < INFO < WARN < ERROR < FATAL
* @{
*/
bool HKU_API isLogInMainThread();
void HKU_API increaseIORedicrectToPythonCount();
void HKU_API decreaseIORedicrectToPythonCount();
int HKU_API getIORedirectToPythonCount();
2020-04-10 01:54:49 +08:00
/**********************************************
* Use SPDLOG for logging
*********************************************/
#if USE_SPDLOG_LOGGER
/** 日志级别 */
2019-02-11 15:20:43 +08:00
enum LOG_LEVEL {
LOG_TRACE = SPDLOG_LEVEL_TRACE, ///< 跟踪
LOG_DEBUG = SPDLOG_LEVEL_DEBUG, ///< 调试
LOG_INFO = SPDLOG_LEVEL_INFO, ///< 一般信息
LOG_WARN = SPDLOG_LEVEL_WARN, ///< 告警
LOG_ERROR = SPDLOG_LEVEL_ERROR, ///< 错误
LOG_FATAL = SPDLOG_LEVEL_CRITICAL, ///< 致命
LOG_OFF = SPDLOG_LEVEL_OFF, ///< 关闭日志打印
2019-02-11 15:20:43 +08:00
};
/**
*
* @return
*/
LOG_LEVEL HKU_API get_log_level();
/**
*
* @param level
2019-02-11 15:20:43 +08:00
*/
void HKU_API set_log_level(LOG_LEVEL level);
2019-02-11 15:20:43 +08:00
std::shared_ptr<spdlog::logger> HKU_API getHikyuuLogger();
2019-02-11 15:20:43 +08:00
2024-03-11 19:40:40 +08:00
#define HKU_TRACE(...) SPDLOG_LOGGER_TRACE(hku::getHikyuuLogger(), __VA_ARGS__)
2020-09-18 00:33:39 +08:00
#define HKU_DEBUG(...) SPDLOG_LOGGER_DEBUG(hku::getHikyuuLogger(), __VA_ARGS__)
2019-12-15 01:25:00 +08:00
#define HKU_INFO(...) SPDLOG_LOGGER_INFO(hku::getHikyuuLogger(), __VA_ARGS__)
#define HKU_WARN(...) SPDLOG_LOGGER_WARN(hku::getHikyuuLogger(), __VA_ARGS__)
#define HKU_ERROR(...) SPDLOG_LOGGER_ERROR(hku::getHikyuuLogger(), __VA_ARGS__)
2024-03-11 19:40:40 +08:00
#define HKU_FATAL(...) SPDLOG_LOGGER_CRITICAL(hku::getHikyuuLogger(), __VA_ARGS__)
2015-01-07 01:26:14 +08:00
2024-03-11 23:05:45 +08:00
void initLogger(bool inJupyter = false);
2020-04-10 01:54:49 +08:00
#else
enum LOG_LEVEL {
LOG_TRACE = 0,
LOG_DEBUG = 1,
LOG_INFO = 2,
LOG_WARN = 3,
LOG_ERROR = 4,
LOG_FATAL = 5,
LOG_OFF = 6,
2020-04-10 01:54:49 +08:00
};
LOG_LEVEL HKU_API get_log_level();
void HKU_API set_log_level(LOG_LEVEL level);
2024-03-11 23:05:45 +08:00
void initLogger(bool inJupyter = false);
2020-04-10 01:54:49 +08:00
2020-05-01 23:59:53 +08:00
/** 获取系统当前时间精确到毫秒2001-01-02 13:01:02.001 */
std::string HKU_API getLocalTime();
2020-04-10 01:54:49 +08:00
#if LOG_ACTIVE_LEVEL <= 0
2020-05-01 23:59:53 +08:00
#define HKU_TRACE(...) \
fmt::print("[{}] [HKU-T] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \
__LINE__);
2020-04-10 01:54:49 +08:00
#else
#define HKU_TRACE(...)
#endif
#if LOG_ACTIVE_LEVEL <= 1
2020-05-01 23:59:53 +08:00
#define HKU_DEBUG(...) \
fmt::print("[{}] [HKU-D] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \
__LINE__);
2020-04-10 01:54:49 +08:00
#else
#define HKU_DEBUG(...)
#endif
#if LOG_ACTIVE_LEVEL <= 2
2020-05-01 23:59:53 +08:00
#define HKU_INFO(...) \
fmt::print("[{}] [HKU-I] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \
__LINE__);
2020-04-10 01:54:49 +08:00
#else
#define HKU_INFO(...)
#endif
#if LOG_ACTIVE_LEVEL <= 3
2020-05-01 23:59:53 +08:00
#define HKU_WARN(...) \
fmt::print("[{}] [HKU-W] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \
__LINE__);
2020-04-10 01:54:49 +08:00
#else
#define HKU_WARN(...)
#endif
#if LOG_ACTIVE_LEVEL <= 4
2020-05-01 23:59:53 +08:00
#define HKU_ERROR(...) \
fmt::print("[{}] [HKU-E] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \
__LINE__);
2020-04-10 01:54:49 +08:00
#else
#define HKU_ERROR(...)
#endif
#if LOG_ACTIVE_LEVEL <= 5
2020-05-01 23:59:53 +08:00
#define HKU_FATAL(...) \
fmt::print("[{}] [HKU-F] - {} ({}:{})\n", getLocalTime(), fmt::format(__VA_ARGS__), __FILE__, \
__LINE__);
2020-04-10 01:54:49 +08:00
#else
#define HKU_FATAL(...)
#endif
#endif /* USE_SPDLOG_LOGGER */
///////////////////////////////////////////////////////////////////////////////
//
// clang/gcc 下使用 __PRETTY_FUNCTION__ 会包含函数参数,可以在编译时指定
// #define HKU_FUNCTION __PRETTY_FUNCTION__
//
///////////////////////////////////////////////////////////////////////////////
#ifndef HKU_FUNCTION
#define HKU_FUNCTION __FUNCTION__
#endif
2023-10-15 00:30:45 +08:00
#ifndef HKU_ENABLE_STACK_TRACE
/**
* false hku::exception ,
* @note
*/
#define HKU_CHECK(expr, ...) \
do { \
if (!(expr)) { \
2024-03-21 18:01:45 +08:00
throw hku::exception(fmt::format("HKU_CHECK({}) {} [{}] ({}:{})", #expr, \
2023-10-15 00:30:45 +08:00
fmt::format(__VA_ARGS__), HKU_FUNCTION, __FILE__, \
__LINE__)); \
} \
} while (0)
/**
* false,
* @note
*/
#define HKU_CHECK_THROW(expr, except, ...) \
do { \
if (!(expr)) { \
2024-03-21 18:01:45 +08:00
throw except(fmt::format("HKU_CHECK({}) {} [{}] ({}:{})", #expr, \
fmt::format(__VA_ARGS__), HKU_FUNCTION, __FILE__, __LINE__)); \
} \
} while (0)
2023-10-15 00:30:45 +08:00
#else
#define HKU_CHECK(expr, ...) \
do { \
if (!(expr)) { \
std::string errmsg = fmt::format(__VA_ARGS__); \
errmsg = fmt::format("{}\n {}", errmsg, to_string(boost::stacktrace::stacktrace())); \
2024-03-21 18:01:45 +08:00
throw hku::exception(fmt::format("HKU_CHECK({}) {} [{}] ({}:{})", #expr, errmsg, \
2023-10-15 00:30:45 +08:00
HKU_FUNCTION, __FILE__, __LINE__)); \
} \
} while (0)
2024-03-21 18:01:45 +08:00
#define HKU_CHECK_THROW(expr, except, ...) \
do { \
if (!(expr)) { \
std::string errmsg = fmt::format(__VA_ARGS__); \
errmsg = fmt::format("{}\n {}", errmsg, to_string(boost::stacktrace::stacktrace())); \
throw except(fmt::format("HKU_CHECK({}) {} [{}] ({}:{})", #expr, errmsg, HKU_FUNCTION, \
__FILE__, __LINE__)); \
} \
2023-10-15 00:30:45 +08:00
} while (0)
#endif // #ifndef HKU_ENABLE_STACK_TRACE
2024-03-24 14:50:14 +08:00
#if HKU_DEBUG_MODE
#define HKU_ASSERT_DEBUG(expr)
#else
/** 仅在 debug 模式下生效 */
#define HKU_ASSERT_DEBUG(expr) \
2024-03-21 18:01:45 +08:00
do { \
if (!(expr)) { \
2024-03-24 14:50:14 +08:00
std::string err_msg(fmt::format("HKU_ASSERT({})", #expr)); \
2024-03-21 18:01:45 +08:00
throw hku::exception( \
fmt::format("{} [{}] ({}:{})", err_msg, HKU_FUNCTION, __FILE__, __LINE__)); \
} \
2023-10-15 00:30:45 +08:00
} while (0)
2024-03-24 14:50:14 +08:00
#endif /* #if HKU_DEBUG_MODE */
#ifdef HKU_ENABLE_STACK_TRACE
2023-10-15 00:30:45 +08:00
/**
2024-03-24 14:50:14 +08:00
* false hku::exception
2023-10-15 00:30:45 +08:00
* @note HKU_DISABLE_ASSERT
*/
2024-03-24 14:50:14 +08:00
#define HKU_ASSERT(expr) \
2024-03-21 18:01:45 +08:00
do { \
if (!(expr)) { \
2024-03-24 14:50:14 +08:00
std::string err_msg(fmt::format("HKU_ASSERT({})\n{}", #expr, \
2024-03-21 18:01:45 +08:00
to_string(boost::stacktrace::stacktrace()))); \
throw hku::exception( \
fmt::format("{} [{}] ({}:{})", err_msg, HKU_FUNCTION, __FILE__, __LINE__)); \
} \
2023-10-15 00:30:45 +08:00
} while (0)
#else
2021-01-13 23:23:59 +08:00
#define HKU_ASSERT(expr) \
do { \
if (!(expr)) { \
2024-03-21 18:01:45 +08:00
std::string err_msg(fmt::format("HKU_ASSERT({})", #expr)); \
2021-01-13 23:23:59 +08:00
throw hku::exception( \
2023-10-15 00:30:45 +08:00
fmt::format("{} [{}] ({}:{})", err_msg, HKU_FUNCTION, __FILE__, __LINE__)); \
2021-01-13 23:23:59 +08:00
} \
} while (0)
2023-10-15 00:30:45 +08:00
#endif // #ifndef HKU_ENABLE_STACK_TRACE
2023-10-15 00:30:45 +08:00
#ifndef HKU_ENABLE_STACK_TRACE
/** 抛出 hku::exception 及传入信息 */
#define HKU_THROW(...) \
do { \
throw hku::exception(fmt::format("EXCEPTION: {} [{}] ({}:{})", fmt::format(__VA_ARGS__), \
2023-10-15 00:30:45 +08:00
HKU_FUNCTION, __FILE__, __LINE__)); \
} while (0)
/** 抛出指定异常及传入信息 */
#define HKU_THROW_EXCEPTION(except, ...) \
do { \
throw except(fmt::format("EXCEPTION: {} [{}] ({}:{})", fmt::format(__VA_ARGS__), \
2023-10-15 00:30:45 +08:00
HKU_FUNCTION, __FILE__, __LINE__)); \
} while (0)
#else
#define HKU_THROW(...) \
do { \
std::string errmsg(fmt::format("{}\n {}", fmt::format(__VA_ARGS__), \
to_string(boost::stacktrace::stacktrace()))); \
throw hku::exception( \
fmt::format("EXCEPTION: {} [{}] ({}:{})", errmsg, HKU_FUNCTION, __FILE__, __LINE__)); \
} while (0)
#define HKU_THROW_EXCEPTION(except, ...) \
do { \
std::string errmsg(fmt::format("{}\n {}", fmt::format(__VA_ARGS__), \
to_string(boost::stacktrace::stacktrace()))); \
throw except( \
fmt::format("EXCEPTION: {} [{}] ({}:{})", errmsg, HKU_FUNCTION, __FILE__, __LINE__)); \
} while (0)
2023-10-15 00:30:45 +08:00
#endif // #ifndef HKU_ENABLE_STACK_TRACE
/**
* TRACE
* @param expr
*/
#define HKU_TRACE_IF(expr, ...) \
if (expr) { \
HKU_TRACE(__VA_ARGS__); \
}
/**
* DEBUG ,
* @param expr
*/
#define HKU_DEBUG_IF(expr, ...) \
if (expr) { \
HKU_DEBUG(__VA_ARGS__); \
}
/**
* INFO ,
* @param expr
*/
#define HKU_INFO_IF(expr, ...) \
if (expr) { \
HKU_INFO(__VA_ARGS__); \
}
/**
* WARN ,
* @param expr
*/
#define HKU_WARN_IF(expr, ...) \
if (expr) { \
HKU_WARN(__VA_ARGS__); \
}
/**
* ERROR ,
* @param expr
*/
#define HKU_ERROR_IF(expr, ...) \
if (expr) { \
HKU_ERROR(__VA_ARGS__); \
}
/**
* FATAL ,
* @param expr
*/
#define HKU_FATAL_IF(expr, ...) \
if (expr) { \
HKU_FATAL(__VA_ARGS__); \
}
/**
*
* @param expr
* @param ret
*/
2020-12-27 19:11:18 +08:00
#define HKU_IF_RETURN(expr, ret) \
if (expr) { \
return ret; \
}
/**
* TRACE ,
* @param expr
* @param ret
*/
#define HKU_TRACE_IF_RETURN(expr, ret, ...) \
if (expr) { \
HKU_TRACE(__VA_ARGS__); \
return ret; \
}
/**
* DEBUG ,
* @param expr
* @param ret
*/
#define HKU_DEBUG_IF_RETURN(expr, ret, ...) \
if (expr) { \
HKU_DEBUG(__VA_ARGS__); \
return ret; \
}
/**
* INFO ,
* @param expr
* @param ret
*/
#define HKU_INFO_IF_RETURN(expr, ret, ...) \
if (expr) { \
HKU_INFO(__VA_ARGS__); \
return ret; \
}
/**
* WARN ,
* @param expr
* @param ret
*/
#define HKU_WARN_IF_RETURN(expr, ret, ...) \
if (expr) { \
HKU_WARN(__VA_ARGS__); \
return ret; \
}
/**
* ERROR ,
* @param expr
* @param ret
*/
#define HKU_ERROR_IF_RETURN(expr, ret, ...) \
if (expr) { \
HKU_ERROR(__VA_ARGS__); \
return ret; \
}
/**
* FATAL ,
* @param expr
* @param ret
*/
#define HKU_FATAL_IF_RETURN(expr, ret, ...) \
if (expr) { \
HKU_FATAL(__VA_ARGS__); \
return ret; \
}
2024-03-11 19:40:40 +08:00
/** 用于 catch (...) 中打印,减少编译后代码大小 */
2020-12-25 23:51:36 +08:00
extern std::string g_unknown_error_msg;
2024-03-11 19:40:40 +08:00
#define HKU_TRACE_UNKNOWN HKU_TRACE(g_unknown_error_msg)
#define HKU_DEBUG_UNKNOWN HKU_DEBUG(g_unknown_error_msg)
#define HKU_INFO_UNKNOWN HKU_INFO(g_unknown_error_msg)
2020-12-25 23:51:36 +08:00
#define HKU_ERROR_UNKNOWN HKU_ERROR(g_unknown_error_msg)
2024-03-11 19:40:40 +08:00
#define HKU_FATAL_UNKNOWN HKU_FATAL(g_unknown_error_msg)
2020-12-25 23:51:36 +08:00
/** @} */
2020-04-10 01:54:49 +08:00
2015-01-07 01:26:14 +08:00
} /* namespace hku */
2020-04-10 01:54:49 +08:00
#endif /* HIKUU_LOG_H_ */