Hikyuu
Log.h
浏览该文件的文档.
1 /*
2  * Log.h
3  *
4  * Created on: 2013-2-1
5  * Author: fasiondog
6  */
7 
8 #ifndef LOG_H_
9 #define LOG_H_
10 
11 #include <string>
12 
13 #define USE_SPDLOG_FOR_LOGGING 1
14 //#define USE_BOOST_LOG_FOR_LOGGING 1
15 //#define USE_STDOUT_FOR_LOGGING 1
16 
17 #ifdef USE_BOOST_LOG_FOR_LOGGING
18 #include <boost/log/core.hpp>
19 #include <boost/log/trivial.hpp>
20 #include <boost/log/expressions.hpp>
21 #endif
22 
23 #ifdef USE_SPDLOG_FOR_LOGGING
24 #include <spdlog/spdlog.h>
25 #endif
26 
27 #ifdef USE_STDOUT_FOR_LOGGING
28 #include "datetime/Datetime.h"
29 #endif
30 
31 #ifndef HKU_API
32 #define HKU_API
33 #endif
34 
35 namespace hku {
36 
37 enum LOG_LEVEL {
38  DEBUG = 0,
39  TRACE = 1,
40  INFO = 2,
41  WARN = 3,
42  ERROR = 4,
43  FATAL = 5,
44  NO_PRINT = 6,
45 };
46 
51 void HKU_API init_logger(const std::string& configue_name);
52 
58 
64 
65 
66 
67 /**********************************************
68  * Use Boost.log for logging
69  *
70  *********************************************/
71 #ifdef USE_BOOST_LOG_FOR_LOGGING
72 #define HKU_DEBUG(msg) if (get_log_level() <= LOG_LEVEL::DEBUG) {\
73  BOOST_LOG_TRIVIAL(debug) << msg; }
74 #define HKU_TRACE(msg) if (get_log_level() <= LOG_LEVEL::TRACE) {\
75  BOOST_LOG_TRIVIAL(trace) << msg; }
76 #define HKU_INFO(msg) if (get_log_level() <= LOG_LEVEL::INFO) {\
77  BOOST_LOG_TRIVIAL(info) << msg; }
78 #define HKU_WARN(msg) if (get_log_level() <= LOG_LEVEL::WARN) {\
79  BOOST_LOG_TRIVIAL(warning) << msg; }
80 #define HKU_ERROR(msg) if (get_log_level() <= LOG_LEVEL::ERROR) {\
81  BOOST_LOG_TRIVIAL(error) << msg; }
82 #define HKU_FATAL(msg) if (get_log_level() <= LOG_LEVEL::FATAL) {\
83  BOOST_LOG_TRIVIAL(fatal) << msg; }
84 #endif /* for USE_BOOST_LOG_FOR_LOGGING */
85 
86 
87 /**********************************************
88  * Use SPDLOG for logging
89  * note: SPDLOG TRACE < DEBUG < INFO < WARN
90  *
91  *********************************************/
92 #ifdef USE_SPDLOG_FOR_LOGGING
93 #define HKU_DEBUG(msg) if (get_log_level() <= LOG_LEVEL::DEBUG) {\
94  std::stringstream buf (std::stringstream::out); \
95  buf << msg;\
96  spdlog::get("hikyuu")->debug(buf.str().c_str());}
97 #define HKU_TRACE(msg) if (get_log_level() <= LOG_LEVEL::TRACE){\
98  std::stringstream buf (std::stringstream::out); \
99  buf << msg;\
100  spdlog::get("hikyuu")->trace(buf.str().c_str());}
101 #define HKU_INFO(msg) if (get_log_level() <= LOG_LEVEL::INFO) {\
102  std::stringstream buf (std::stringstream::out); \
103  buf << msg;\
104  spdlog::get("hikyuu")->info(buf.str().c_str());}
105 #define HKU_WARN(msg) if (get_log_level() <= LOG_LEVEL::WARN) {\
106  std::stringstream buf (std::stringstream::out); \
107  buf << msg;\
108  spdlog::get("hikyuu")->warn(buf.str().c_str());}
109 #define HKU_ERROR(msg) if (get_log_level() <= LOG_LEVEL::ERROR) {\
110  std::stringstream buf (std::stringstream::out); \
111  buf << msg;\
112  spdlog::get("hikyuu")->error(buf.str().c_str());}
113 #define HKU_FATAL(msg) if (get_log_level() <= LOG_LEVEL::FATAL) {\
114  std::stringstream buf (std::stringstream::out); \
115  buf << msg;\
116  spdlog::get("hikyuu")->critical(buf.str().c_str());}
117 #endif
118 
119 
120 /**********************************************
121  * Use STDOUT for logging
122  *
123  *********************************************/
124 #ifdef USE_STDOUT_FOR_LOGGING
125 #define HKU_DEBUG(msg) if (get_log_level() <= LOG_LEVEL::DEBUG) {\
126  std::stringstream buf (std::stringstream::out);\
127  buf << Datetime::now() << " [DEBUG] " << msg;\
128  std::cout << buf.str() << std::endl}
129 #define HKU_TRACE(msg) if (get_log_level() <= LOG_LEVEL::TRACE) {\
130  std::stringstream buf (std::stringstream::out);\
131  buf << Datetime::now() << " [TRACE] " << msg;\
132  std::cout << buf.str() << std::endl;}
133 #define HKU_INFO(msg) if (get_log_level() <= LOG_LEVEL::INFO) {\
134  std::stringstream buf (std::stringstream::out);\
135  buf << Datetime::now() << " [INFO] " << msg;\
136  std::cout << buf.str() << std::endl;}
137 #define HKU_WARN(msg) if (get_log_level() <= LOG_LEVEL::WARN) {\
138  std::stringstream buf (std::stringstream::out);\
139  buf << Datetime::now() << " [WARN] " << msg;\
140  std::cout << buf.str() << std::endl;}
141 #define HKU_ERROR(msg) if (get_log_level() <= LOG_LEVEL::ERROR) {\
142  std::stringstream buf (std::stringstream::out);\
143  buf << Datetime::now() << " [ERROR] " << msg;\
144  std::cout << buf.str() << std::endl;}
145 #define HKU_FATAL(msg) if (get_log_level() <= LOG_LEVEL::FATAL) {\
146  std::stringstream buf (std::stringstream::out);\
147  buf << Datetime::now() << " [FATAL] " << msg;\
148  std::cout << buf.str() << std::endl;}
149 #endif /* for USE_STDOUT_FOR_LOGGING */
150 
151 } /* namespace hku */
152 #endif /* LOG_H_ */
Definition: Log.h:38
std::string string
Definition: DataType.h:55
void set_log_level(LOG_LEVEL level)
设置日志级别
Definition: Log.cpp:29
LOG_LEVEL
Definition: Log.h:37
#define HKU_API
Definition: Log.h:32
LOG_LEVEL get_log_level()
获取当前日志级别
Definition: Log.cpp:25
Definition: Log.h:42
Definition: Log.h:43
Definition: Log.h:44
void init_logger(const std::string &configure_name)
初始化 LOGGER
Definition: Log.cpp:51
Hikyuu核心命名空间,包含股票数据的管理、指标实现、交易系统框架等
Definition: Block.cpp:11
Definition: Log.h:39
Definition: Log.h:41
Definition: Log.h:40