OnixS C++ SGX Titan ITCH Market Data Handler  1.2.2
API documentation
HandlerLogger.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*
4 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
5 *
6 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
7 * and international copyright treaties.
8 *
9 * Access to and use of the software is governed by the terms of the applicable ONIXS Software
10 * Services Agreement (the Agreement) and Customer end user license agreements granting
11 * a non-assignable, non-transferable and non-exclusive license to use the software
12 * for it's own data processing purposes under the terms defined in the Agreement.
13 *
14 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
15 * of this source code or associated reference material to any other location for further reproduction
16 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
17 *
18 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
19 * the terms of the Agreement is a violation of copyright law.
20 */
21 
24 
25 #include <OnixS/HandlerCore/HandlerLogger.h>
26 
27 #include <logging/FileOutput.h>
28 
29 #include "NamespaceHelper.h"
30 
31 //////////////////////////////////////////////////////////////////////////
32 // Source info defines
33 
34 #ifdef _MSC_VER
35 #define FUNCTION_NAME __FUNCTION__
36 #else
37 #define FUNCTION_NAME __PRETTY_FUNCTION__
38 #endif
39 
40 #define DIRECTEDGE_SOURCE_INFO ::OnixS::Logging::SourceInfo(__FILE__, __LINE__, FUNCTION_NAME)
41 #define DIRECTEDGE_SOURCE_FUNC ::OnixS::Logging::SourceInfo(nullptr, 0, FUNCTION_NAME)
42 #define DIRECTEDGE_SOURCE_LINE ::OnixS::Logging::SourceInfo(__FILE__, __LINE__, nullptr)
43 
44 
45 #ifdef _DEBUG
46 #define EXCEPTION_SOURCE_INFO DIRECTEDGE_SOURCE_INFO
47 #define LOG_SOURCE_INFO DIRECTEDGE_SOURCE_INFO
48 #else
49 #ifdef _TRACE
50 #define EXCEPTION_SOURCE_INFO DIRECTEDGE_SOURCE_FUNC
51 #define LOG_SOURCE_INFO DIRECTEDGE_SOURCE_FUNC
52 #else
53 #define EXCEPTION_SOURCE_INFO DIRECTEDGE_SOURCE_FUNC
54 #define LOG_SOURCE_INFO OnixS::Logging::SourceInfo()
55 #endif
56 #endif
57 
58 //////////////////////////////////////////////////////////////////////////
59 // format macros
60 
61 // declaration of formatting signature for types by const ref
62 #define DECL_FORMAT(ftype) \
63 TextBuilder & operator<<(TextBuilder & o, const ftype & obj)
64 
65 // declaration of formatting signature for types by value
66 #define DECL_FORMAT_BYVAL(ftype) \
67 TextBuilder & operator<<(TextBuilder & o, ftype obj)
68 
69 // definition of formatting for types by const ref
70 #define STRUCT_FORMAT(ftype, fformat)\
71 TextBuilder & operator<<(TextBuilder & o, const ftype & obj) {\
72 return o << Util::indent(4) fformat << Util::indent(-4);\
73 }
74 
75 #define STRUCT_FORMAT_T(ftype, fformat)\
76 TextBuilder & operator<<(TextBuilder & o, const ftype & obj) {\
77 return o << '[' << #ftype << "]\n" << indent(4) fformat << indent(-4);\
78 }
79 
80 // definition of formatting for enums
81 #define ENUM_FORMAT(fenumbox, fformat)\
82 TextBuilder & operator<<(TextBuilder & o, fenumbox::Enum obj) {\
83 typedef fenumbox __FENUM_BOX__;\
84 switch(obj) { fformat default: if(obj ) o << int(obj) << " (?)";\
85  else format_null_value(o); } return o; }
86 // single enum value formatting
87 #define FENUM(fenum) case __FENUM_BOX__::fenum: o << int(__FENUM_BOX__::fenum) << " ("#fenum")"; break;
88 
89 // definition of API formatting for enums
90 #define API_ENUM_FORMAT(fenumbox, fformat)\
91 const char ONIXS_API * enumToString(fenumbox::Enum value) {\
92 typedef fenumbox __FENUM_BOX__;\
93 switch(value) { fformat default: return "#Invalid enumeration value"; } }
94 
95 // single API enum value formatting
96 #define ENUM_STR(fenum) case __FENUM_BOX__::fenum: return #fenum;
97 
98 #define VAR(var) create_var_format(", "#var, var)
99 #define VARLN(var) '\n' << create_var_format(#var, var)
100 #define COMPLEX_FIELD(s) '\n' << indent(4) << create_var_format(#s, obj.s) << indent(-4)
101 #define FIELD(var) '\n' << create_var_format(#var, obj.var)
102 #define FIELD_AS(var, ftype) '\n' << create_var_format(#var, ftype(obj.var))
103 #define DATE_FIELD(var) FIELD_AS(var, SDateWrapper)
104 #define TIME_FIELD(var) FIELD_AS(var, STimeWrapper)
105 #define BOOL_FIELD(var) FIELD_AS(var, OMexBool::Enum)
106 
107 ONIXS_HANDLER_NAMESPACE_BEGIN
108 
109 #define ASSERT_ARG(arg, cond) if(arg cond) {} else \
110 throw ArgumentExceptionImpl("Argument value does not meet condition: "#arg" "#cond, #arg, EXCEPTION_SOURCE_INFO)
111 
112 #ifdef _DEBUG
113 #define DEBUG_ASSERT_ARG(arg, cond) ASSERT_ARG(arg, cond)
114 #else
115 #define DEBUG_ASSERT_ARG(arg, cond) ((void)0)
116 #endif
117 
118 #define ASSERT_COND(cond) if(cond) {} else \
119 throw OperationExceptionImpl("Object state does not meet condition: "#cond, EXCEPTION_SOURCE_INFO)
120 
121 #ifdef _DEBUG
122 #define DEBUG_ASSERT_COND(cond) ASSERT_COND(cond)
123 #else
124 #define DEBUG_ASSERT_COND(cond) ((void)0)
125 #endif
126 
127 #if (defined(_TRACE) || defined(_DEBUG))
128 #ifdef _DEBUG
129 #define DEFAULT_LOG_LEVEL Logging::LOG_LEVEL_DEBUG
130 #else
131 #define DEFAULT_LOG_LEVEL Logging::LOG_LEVEL_TRACE
132 #endif
133 #else
134 #define DEFAULT_LOG_LEVEL Logging::LOG_LEVEL_INFO
135 #endif
136 
137 
138 #ifdef _TRACE
139 /// Use this macro as first line of traced function
140 #define TRACE_STATE(state, logger) if(state.changed()) logger.log(ONIXS_LOG_TRACE << "|===> " __FUNCTION__)
141 #else
142 #define TRACE_STATE(state, logger) do {} while(false)
143 #endif
144 
145 
146 #ifdef _TRACE
147 /// Use this macro as first line of traced function
148 #define TRACE_FUNC_MINI(logger) logger.log(ONIXS_LOG_TRACE << ">>> " __FUNCTION__ " >>>")
149 #else
150 #define TRACE_FUNC_MINI(logger) do {} while(false)
151 #endif
152 
153 typedef boost::scoped_ptr<HandlerCore::Common::HandlerLogger> LoggerPtr;
154 
155 using namespace Util;
156 
157 template<typename T>
159 {
160  const Util::ValuePtr name;
161  const T value;
162 
163  // construct from literal
164  template<size_t NLen>
165  VarFormatHelper(const char( & name_)[NLen], const T & value_)
166  : name(name_, NLen - 1 /* !!! do not include terminator */), value(value_) {}
167 
169  name(other.name.duplicate()),
170  value(other.value)
171  {}
172 
173 private:
174  VarFormatHelper& operator=(const VarFormatHelper&);
175 };
176 
177 // helper function-dispatcher
178 template<size_t NLen, typename T>
179 VarFormatHelper<T> create_var_format(const char( & name)[NLen], const T & value)
180 {
181  return VarFormatHelper<T>(name, value);
182 };
183 
184 // dispatch unsigned char as unsigned int
185 template<size_t NLen>
186 VarFormatHelper<unsigned> create_var_format(const char( & name)[NLen], unsigned char value)
187 {
188  return VarFormatHelper<unsigned>(name, value);
189 };
190 
191 // to do not inference with unsigned char explicit declaration
192 template<size_t NLen>
193 VarFormatHelper<char> create_var_format(const char( & name)[NLen], char value)
194 {
195  return VarFormatHelper<char>(name, value);
196 };
197 
198 // dispatch char[NVal] field value as literal without zero terminator
199 // trims spaces at the end
200 template<size_t NLen, size_t NVal>
201 VarFormatHelper<ValuePtr> create_var_format(const char( & name)[NLen], const char( & value)[NVal])
202 {
203  // do trim
204  int last = -1;
205  const char * src = value;
206  char c;
207 
208  for (int i = 0; (i < NVal) && (c = *src++); i++)
209  if (c != ' ')
210  last = i;
211 
212  return VarFormatHelper<OnixS::Util::ValuePtr>(name, OnixS::Util::ValuePtr(value, last+1));
213 };
214 
215 TextBuilder & format_var_name(TextBuilder & o, ValuePtr name);
216 TextBuilder & format_null_value(TextBuilder & o);
217 
218 template<typename T>
219 inline TextBuilder & operator<<(TextBuilder & o, const VarFormatHelper<T> & var)
220 {
221  return format_var_name(o, var.name) << var.value;
222 }
223 
224 //DECL_FORMAT(SDateWrapper);
225 //DECL_FORMAT(STimeWrapper);
228 
230  HandlerCore::Common::HandlerLogger*, OnixS::Logging::LogFacility *, const char *contextName) ONIXS_NOEXCEPT;
231 
232 
233 #define ONIXS_EXCEPTION_TRACED_CALLBACK_EX(callback, logger, logFacility, contextName) \
234 try { callback;\
235 logger->log( ONIXS_LOG_DEBUG[logFacility] << "Callback " << contextName << " called successfully");\
236 } catch(...) { HANDLER_NAMESPACE::userExceptionHandler(logger, logFacility, contextName); } \
237 
238 #define ONIXS_EXCEPTION_TRACED_CALLBACK(callback, contextName) ONIXS_EXCEPTION_TRACED_CALLBACK_EX(callback, logger_, this, contextName)
239 
240 Logging::FileOutput::PermissionOption getOutputPermission(LogFilePermission::Enum value);
241 
242 ONIXS_HANDLER_NAMESPACE_END
boost::scoped_ptr< HandlerCore::Common::HandlerLogger > LoggerPtr
void userExceptionHandler(HandlerCore::Common::HandlerLogger *, OnixS::Logging::LogFacility *, const char *contextName) ONIXS_NOEXCEPT
TextBuilder & format_var_name(TextBuilder &o, ValuePtr name)
VarFormatHelper(const char(&name_)[NLen], const T &value_)
Logging::FileOutput::PermissionOption getOutputPermission(LogFilePermission::Enum value)
#define DECL_FORMAT(ftype)
Definition: HandlerLogger.h:62
char last(const std::string &str)
TextBuilder & format_null_value(TextBuilder &o)
VarFormatHelper< ValuePtr > create_var_format(const char(&name)[NLen], const char(&value)[NVal])