00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #pragma once
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdarg.h>
00025 #include "OptionalIncludeForWindows.h"
00026 #include <crtdbg.h>
00027 #include <sstream>
00028 #include <vector>
00029 #include <string>
00030
00031
00032
00033 #pragma warning( push )
00034 #pragma warning( disable : 4996 ) // vsprintf declared deprecated
00035
00036
00037 namespace TestToolBox
00038 {
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #define UTIL_GET_FORMATTED_LINE(in_formatStr) \
00050 char formattedLine [4096]; \
00051 va_list argPtr; \
00052 va_start (argPtr, in_formatStr); \
00053 vsprintf (formattedLine, in_formatStr, argPtr); \
00054 strcat(formattedLine,"\n"); \
00055 va_end (argPtr);
00056
00057 #define UTIL_GET_FORMATTED_LINE_NO_NEWLINE(in_formatStr) \
00058 char formattedLine [4096]; \
00059 va_list argPtr; \
00060 va_start (argPtr, in_formatStr); \
00061 vsprintf (formattedLine, in_formatStr, argPtr); \
00062 va_end (argPtr);
00063
00064
00065
00066
00067
00068
00069 namespace OutputLevel
00070 {
00071
00072 typedef enum
00073 {
00074 eSILENT,
00075 eFATAL,
00076 eERROR,
00077 eREGULAR,
00078 eVERBOUS
00079 } Enum;
00080 }
00081
00082
00083
00084
00085
00086
00087 template<class T>
00088 inline std::string ToString(const T& in_source)
00089 {
00090 std::ostringstream oss;
00091
00092 static_cast<std::ostream&>(oss) << in_source;
00093 return oss.str();
00094 }
00095
00096
00097 inline std::string ToString(const bool& in_source)
00098 {
00099 std::ostringstream oss;
00100
00101 static_cast<std::ostream&>(oss) << std::boolalpha << in_source;
00102 return oss.str();
00103 }
00104
00105
00106 inline void TrimStr(std::string& io_str)
00107 {
00108 std::string::size_type pos = io_str.find_last_not_of(" \r\n");
00109 if(pos != std::string::npos)
00110 {
00111 io_str.erase(pos + 1);
00112 pos = io_str.find_first_not_of(" \r\n");
00113 if(pos != std::string::npos)
00114 {
00115 io_str.erase(0, pos);
00116 }
00117 }
00118 else
00119 {
00120 io_str.erase(io_str.begin(), io_str.end());
00121 }
00122 }
00123
00124
00125 inline std::vector<std::string> SplitString(
00126 std::string const & in_string,
00127 std::string const & in_separators = ",")
00128 {
00129 std::vector<std::string> strList;
00130
00131 std::string s = in_string;
00132 while (s.length() > 0)
00133 {
00134 std::string::size_type pos = s.find_first_of(in_separators);
00135 std::string part = s.substr(0,pos);
00136 if (part.length() > 0)
00137 {
00138 strList.push_back(part);
00139 }
00140 s.erase(0,pos);
00141 if (pos != std::string::npos)
00142 {
00143
00144 s.erase(0,1);
00145 }
00146 }
00147
00148 return strList;
00149
00150 }
00151
00152
00153
00154
00155 inline void AddFileNameAndLineNumber (
00156 std::ostringstream& in_rOutStr,
00157 const char* in_fileName,
00158 const long in_lineNum)
00159 {
00160 if (in_fileName)
00161 {
00162 in_rOutStr << in_fileName << " (" << in_lineNum << "): ";
00163 }
00164
00165 }
00166
00167
00168
00169
00170
00171
00172 template<class T>
00173 int NumElements (T const & in_container)
00174 {
00175 return static_cast<int>(in_container.size());
00176 }
00177
00178 template<class T>
00179 int LastIndex (T const & in_container)
00180 {
00181 return static_cast<int>(in_container.size()) -1;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 extern char const* TRC_theModuleName;
00195
00196
00197
00198
00199
00200 extern int TRC_theTraceLevel;
00201
00202
00203
00204
00205 #define TRC
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 inline int SimpleTraceOutput(
00217 unsigned int in_traceLevel,
00218 const char* in_fileName,
00219 const char* in_functionName,
00220 const char* in_objectName,
00221 const char* in_traceStr, ...)
00222 {
00223 char outLine [4096];
00224 UTIL_GET_FORMATTED_LINE(in_traceStr);
00225
00226
00227 char const * pFileNameWithoutPath = strrchr(in_fileName,'\\');
00228 pFileNameWithoutPath = pFileNameWithoutPath ? pFileNameWithoutPath + 1 : in_fileName;
00229
00230
00231 char const * pFunctionNameWithoutClass = strrchr(in_functionName,':');
00232 pFunctionNameWithoutClass = pFunctionNameWithoutClass ? pFunctionNameWithoutClass + 1 : in_functionName;
00233
00234 if (in_objectName)
00235 {
00236 sprintf(outLine, "%-15s; %d; %x; %x; %-20s; %-20s; %s: %s",
00237 TRC_theModuleName,
00238 in_traceLevel,
00239 GetCurrentThreadId(),
00240 GetCurrentProcessId(),
00241 pFileNameWithoutPath,
00242 pFunctionNameWithoutClass,
00243 in_objectName,
00244 formattedLine);
00245 }
00246 else
00247 {
00248 sprintf(outLine, "%-15s; %d; %x; %x; %-20s; %-20s; %s",
00249 TRC_theModuleName,
00250 in_traceLevel,
00251 GetCurrentThreadId(),
00252 GetCurrentProcessId(),
00253 pFileNameWithoutPath,
00254 pFunctionNameWithoutClass,
00255 formattedLine);
00256 }
00257 OutputDebugStringA(outLine);
00258 return 0;
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 #define TRACE_LEVEL TestToolBox::TRC_theTraceLevel
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 #define TL_VAL_ERROR 2
00280
00281
00282
00283
00284
00285 #define TL_VAL_PROD 4
00286
00287
00288
00289
00290
00291 #define TL_VAL_DEBUG 6
00292
00293
00294 #ifdef TRACE_IS_ON
00295 #define TRCF(x) static const char *trace_fct=x;
00296
00297 #define TRC0(Level, Msg) TRC(Level,Msg);
00298 #define TRC1(Level, Msg,p1) TRC(Level,Msg,p1);
00299 #define TRC2(Level, Msg,p1,p2) TRC(Level,Msg,p1,p2);
00300 #define TRC3(Level, Msg,p1,p2,p3) TRC(Level,Msg,p1,p2,p3);
00301 #define TRC4(Level, Msg,p1,p2,p3,p4) TRC(Level,Msg,p1,p2,p3,p4);
00302 #define TRC5(Level, Msg,p1,p2,p3,p4,p5) TRC(Level,Msg,p1,p2,p3,p4,p5);
00303
00304 #define TL_DEBUG TRACE_LEVEL>=TL_VAL_DEBUG) && TestToolBox::SimpleTraceOutput(TL_VAL_DEBUG, __FILE__, trace_fct, 0
00305 #define TL_ERROR TRACE_LEVEL>=TL_VAL_ERROR) && TestToolBox::SimpleTraceOutput(TL_VAL_ERROR, __FILE__, trace_fct, 0
00306 #define TL_PROD TRACE_LEVEL>=TL_VAL_PROD) && TestToolBox::SimpleTraceOutput(TL_VAL_PROD, __FILE__, trace_fct, 0
00307
00308 #define TL_DEBUGN TRACE_LEVEL>=TL_VAL_DEBUG) && TestToolBox::SimpleTraceOutput(TL_VAL_DEBUG, __FILE__, trace_fct, m_traceName
00309 #define TL_ERRORN TRACE_LEVEL>=TL_VAL_ERROR) && TestToolBox::SimpleTraceOutput(TL_VAL_ERROR, __FILE__, trace_fct, m_traceName
00310 #define TL_PRODN TRACE_LEVEL>=TL_VAL_PROD) && TestToolBox::SimpleTraceOutput(TL_VAL_PROD, __FILE__, trace_fct, m_traceName
00311
00312 #else // trace is switched off via preprocessor
00313
00314 #define TRCF(x)
00315
00316 #define TRC0(Level, Msg)
00317 #define TRC1(Level, Msg,p1)
00318 #define TRC2(Level, Msg,p1,p2)
00319 #define TRC3(Level, Msg,p1,p2,p3)
00320 #define TRC4(Level, Msg,p1,p2,p3,p4)
00321 #define TRC5(Level, Msg,p1,p2,p3,p4,p5)
00322
00323 #define TL_DEBUG 0) && SimpleTraceOutput(0, __FILE__, "", 0
00324 #define TL_ERROR 0) && SimpleTraceOutput(0, __FILE__, "", 0
00325 #define TL_PROD 0) && SimpleTraceOutput(0, __FILE__, "", 0
00326
00327 #define TL_DEBUGN 0) && SimpleTraceOutput(0, __FILE__, "", m_traceName
00328 #define TL_ERRORN 0) && SimpleTraceOutput(0, __FILE__, "", m_traceName
00329 #define TL_PRODN 0) && SimpleTraceOutput(0, __FILE__, "", m_traceName
00330
00331 #endif
00332
00333
00334
00335
00336
00337
00338
00339 #define TN(formatStr) "%s: " formatStr, m_traceName
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 #define TTB_TRC_DEF_GLOBALS(moduleName,traceLevel) \
00351 char const * TestToolBox::TRC_theModuleName = moduleName; \
00352 int TestToolBox::TRC_theTraceLevel = traceLevel;
00353
00354
00355
00356
00357
00358
00359
00360
00361 #define TTB_TRC_SET_TRACE_LEVEL_FROM_COMMANDLINE () \
00362 \
00363 char* pCmdLine = GetCommandLineA(); \
00364 const char* OPT_TRACE = "-trace"; \
00365 size_t lenTraceParam; \
00366 char traceParam [200]; \
00367 \
00368 char* pTraceToken = strstr(pCmdLine, OPT_TRACE); \
00369 while (pTraceToken) \
00370 { \
00371 pCmdLine = pTraceToken + strlen (OPT_TRACE); \
00372 pCmdLine += strspn(pCmdLine, " \t"); \
00373 lenTraceParam = strcspn(pCmdLine," \t"); \
00374 strncpy(traceParam, pCmdLine, lenTraceParam); \
00375 traceParam[lenTraceParam] = '\0'; \
00376 pCmdLine += lenTraceParam; \
00377 \
00378 char moduleName [200]; \
00379 char traceLevelStr [200]; \
00380 char* pLevel = strrchr(traceParam,'.'); \
00381 if (pLevel) \
00382 { \
00383 size_t lenModName = pLevel - traceParam; \
00384 size_t lenTraceLevel = strlen(traceParam) - lenModName - 1; \
00385 strncpy(moduleName, traceParam, lenModName); \
00386 moduleName[lenModName] = '\0'; \
00387 strncpy(traceLevelStr, pLevel+1, lenTraceLevel); \
00388 traceLevelStr[lenTraceLevel] = '\0'; \
00389 if (!strcmp(moduleName, TRC_theModuleName)) \
00390 { \
00391 TRC_theTraceLevel = atoi(traceLevelStr); \
00392 TRC(TL_PROD, \
00393 "TraceLevel set to %d (according commandline)", \
00394 TRC_theTraceLevel); \
00395 } \
00396 } \
00397 \
00398 pTraceToken = strstr(pCmdLine, OPT_TRACE); \
00399 }
00400
00401 inline void SetTraceLevelFromCommandLine(void)
00402 {
00403 TRCF("SetTraceLevelFromCommandLine");
00404
00405 char* pCmdLine = GetCommandLineA();
00406 const char* OPT_TRACE = "-trace";
00407 size_t lenTraceParam;
00408 char traceParam [200];
00409
00410 char* pTraceToken = strstr(pCmdLine, OPT_TRACE);
00411 while (pTraceToken)
00412 {
00413 pCmdLine = pTraceToken + strlen (OPT_TRACE);
00414 pCmdLine += strspn(pCmdLine, " \t");
00415 lenTraceParam = strcspn(pCmdLine," \t");
00416 strncpy(traceParam, pCmdLine, lenTraceParam);
00417 traceParam[lenTraceParam] = '\0';
00418 pCmdLine += lenTraceParam;
00419
00420 char moduleName [200];
00421 char traceLevelStr [200];
00422 char* pLevel = strrchr(traceParam,'.');
00423 if (pLevel)
00424 {
00425 size_t lenModName = pLevel - traceParam;
00426 size_t lenTraceLevel = strlen(traceParam) - lenModName - 1;
00427 strncpy(moduleName, traceParam, lenModName);
00428 moduleName[lenModName] = '\0';
00429 strncpy(traceLevelStr, pLevel+1, lenTraceLevel);
00430 traceLevelStr[lenTraceLevel] = '\0';
00431 if (!strcmp(moduleName, TRC_theModuleName))
00432 {
00433 TRC_theTraceLevel = atoi(traceLevelStr);
00434 TRC(TL_PROD,
00435 "TraceLevel set to %d (according commandline)",
00436 TRC_theTraceLevel);
00437 }
00438 }
00439
00440 pTraceToken = strstr(pCmdLine, OPT_TRACE);
00441 }
00442 }
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478 #define DEF_ERRID ErrorId errId = 0;
00479
00480 #define CALL_FAILED(FunctionCall) \
00481 0 != (errId = ((FunctionCall)))
00482
00483
00484
00485 #define ASSERT_ON_ERROR(FunctionCall) \
00486 errId = ((FunctionCall)); assert(0 == errId)
00487
00488 #define BREAK_ON_ERROR(FunctionCall) \
00489 errId = ((FunctionCall)); if (errId){break;}
00490
00491
00492 #define SEND_ON_ERROR(FunctionCall) \
00493 errId = ((FunctionCall)); if (errId){SendErrorToGui(errId);}
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 #ifdef _DEBUG
00505 #define SET_CRT_DEBUG_FIELD(a) \
00506 _CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
00507 #define CLEAR_CRT_DEBUG_FIELD(a) \
00508 _CrtSetDbgFlag(~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
00509 #else
00510 #define SET_CRT_DEBUG_FIELD(a) ((void) 0)
00511 #define CLEAR_CRT_DEBUG_FIELD(a) ((void) 0)
00512 #endif
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 #define IGNORE_PARAM(p) p = p
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 #ifdef TTB_NOT_INLINE
00538 #define TTB_INLINE
00539 #else
00540 #define TTB_INLINE inline
00541 #endif
00542
00543 }
00544
00545 #pragma warning( pop )
00546
00547