hikyuu2/hikyuu_cpp/hikyuu/Log.h

429 lines
15 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
#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 {
2020-04-10 01:54:49 +08:00
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, ///< 关闭日志打印
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
/*#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__)
#define HKU_FATAL(...) SPDLOG_LOGGER_CRITICAL(hku::getHikyuuLogger(), __VA_ARGS__)*/
#define HKU_TRACE(...) \
do { \
if (isLogInMainThread() || getIORedirectToPythonCount() <= 0) { \
SPDLOG_LOGGER_TRACE(hku::getHikyuuLogger(), __VA_ARGS__); \
} \
} while (0)
#define HKU_DEBUG(...) \
do { \
if (isLogInMainThread() || getIORedirectToPythonCount() <= 0) { \
SPDLOG_LOGGER_DEBUG(hku::getHikyuuLogger(), __VA_ARGS__); \
} \
} while (0)
#define HKU_INFO(...) \
do { \
if (isLogInMainThread() || getIORedirectToPythonCount() <= 0) { \
SPDLOG_LOGGER_INFO(hku::getHikyuuLogger(), __VA_ARGS__); \
} \
} while (0)
#define HKU_WARN(...) \
do { \
if (isLogInMainThread() || getIORedirectToPythonCount() <= 0) { \
SPDLOG_LOGGER_WARN(hku::getHikyuuLogger(), __VA_ARGS__); \
} \
} while (0)
#define HKU_ERROR(...) \
do { \
if (isLogInMainThread() || getIORedirectToPythonCount() <= 0) { \
SPDLOG_LOGGER_ERROR(hku::getHikyuuLogger(), __VA_ARGS__); \
} \
} while (0)
#define HKU_FATAL(...) \
do { \
if (isLogInMainThread() || getIORedirectToPythonCount() <= 0) { \
SPDLOG_LOGGER_CRITICAL(hku::getHikyuuLogger(), __VA_ARGS__); \
} \
} while (0)
2015-01-07 01:26:14 +08:00
2020-04-10 01:54:49 +08:00
#if HKU_USE_SPDLOG_ASYNC_LOGGER
void initLogger();
#else
void initLogger();
#endif
2020-04-10 01:54:49 +08:00
#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 initLogger();
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
/**
* false hku::exception ,
* @note
*/
#define HKU_CHECK(expr, ...) \
do { \
if (!(expr)) { \
throw hku::exception(fmt::format("CHECK({}) {} [{}] ({}:{})", #expr, \
fmt::format(__VA_ARGS__), __FUNCTION__, __FILE__, \
__LINE__)); \
} \
} while (0)
/**
* false,
* @note
*/
#define HKU_CHECK_THROW(expr, except, ...) \
do { \
if (!(expr)) { \
throw except(fmt::format("CHECK({}) {} [{}] ({}:{})", #expr, fmt::format(__VA_ARGS__), \
__FUNCTION__, __FILE__, __LINE__)); \
} \
} while (0)
#if HKU_DISABLE_ASSERT
#define HKU_ASSERT(expr)
#define HKU_ASSERT_M(expr, ...)
#else /* #if HKU_DISABLE_ASSERT */
/**
* false hku::exception
* @note HKU_DISABLE_ASSERT
*/
2021-01-13 23:23:59 +08:00
#define HKU_ASSERT(expr) \
do { \
if (!(expr)) { \
std::string err_msg(fmt::format("ASSERT({})", #expr)); \
HKU_ERROR(err_msg); \
throw hku::exception( \
fmt::format("{} [{}] ({}:{})", err_msg, __FUNCTION__, __FILE__, __LINE__)); \
} \
} while (0)
/**
* false hku::exception ,
* @note HKU_DISABLE_ASSERT
*/
2021-01-13 23:23:59 +08:00
#define HKU_ASSERT_M(expr, ...) \
do { \
if (!(expr)) { \
std::string err_msg(fmt::format("ASSERT({}) {}", #expr, fmt::format(__VA_ARGS__))); \
HKU_ERROR(err_msg); \
throw hku::exception( \
fmt::format("{} [{}] ({}:{})", err_msg, __FUNCTION__, __FILE__, __LINE__)); \
} \
} while (0)
#endif /* #if HKU_DISABLE_ASSERT */
/** 抛出 hku::exception 及传入信息 */
#define HKU_THROW(...) \
do { \
throw hku::exception(fmt::format("EXCEPTION: {} [{}] ({}:{})", fmt::format(__VA_ARGS__), \
__FUNCTION__, __FILE__, __LINE__)); \
} while (0)
/** 抛出指定异常及传入信息 */
#define HKU_THROW_EXCEPTION(except, ...) \
do { \
throw except(fmt::format("EXCEPTION: {} [{}] ({}:{})", fmt::format(__VA_ARGS__), \
__FUNCTION__, __FILE__, __LINE__)); \
} while (0)
/**
* 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; \
}
2020-12-25 23:51:36 +08:00
extern std::string g_unknown_error_msg;
#define HKU_ERROR_UNKNOWN HKU_ERROR(g_unknown_error_msg)
#define HKU_FATAL_UNKNOWN HKU_ERROR(g_unknown_error_msg)
/** @} */
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_ */