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
00173
00174
00175
00176
00177 extern char const* TRC_theModuleName;
00178
00179
00180
00181
00182
00183 extern int TRC_theTraceLevel;
00184
00185
00186
00187
00188 #define TRC
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 inline int SimpleTraceOutput(
00200 unsigned int in_traceLevel,
00201 const char* in_fileName,
00202 const char* in_functionName,
00203 const char* in_objectName,
00204 const char* in_traceStr, ...)
00205 {
00206 char outLine [4096];
00207 UTIL_GET_FORMATTED_LINE(in_traceStr);
00208
00209
00210 char const * pFileNameWithoutPath = strrchr(in_fileName,'\\');
00211 pFileNameWithoutPath = pFileNameWithoutPath ? pFileNameWithoutPath + 1 : in_fileName;
00212
00213
00214 char const * pFunctionNameWithoutClass = strrchr(in_functionName,':');
00215 pFunctionNameWithoutClass = pFunctionNameWithoutClass ? pFunctionNameWithoutClass + 1 : in_functionName;
00216
00217 if (in_objectName)
00218 {
00219 sprintf(outLine, "%-15s; %d; %x; %x; %-20s; %-20s; %s: %s",
00220 TRC_theModuleName,
00221 in_traceLevel,
00222 GetCurrentThreadId(),
00223 GetCurrentProcessId(),
00224 pFileNameWithoutPath,
00225 pFunctionNameWithoutClass,
00226 in_objectName,
00227 formattedLine);
00228 }
00229 else
00230 {
00231 sprintf(outLine, "%-15s; %d; %x; %x; %-20s; %-20s; %s",
00232 TRC_theModuleName,
00233 in_traceLevel,
00234 GetCurrentThreadId(),
00235 GetCurrentProcessId(),
00236 pFileNameWithoutPath,
00237 pFunctionNameWithoutClass,
00238 formattedLine);
00239 }
00240 OutputDebugStringA(outLine);
00241 return 0;
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 #define TRACE_LEVEL TestToolBox::TRC_theTraceLevel
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 #define TL_VAL_ERROR 2
00263
00264
00265
00266
00267
00268 #define TL_VAL_PROD 4
00269
00270
00271
00272
00273
00274 #define TL_VAL_DEBUG 6
00275
00276
00277 #ifdef TRACE_IS_ON
00278 #define TRCF(x) static const char *trace_fct=x;
00279
00280 #define TRC0(Level, Msg) TRC(Level,Msg);
00281 #define TRC1(Level, Msg,p1) TRC(Level,Msg,p1);
00282 #define TRC2(Level, Msg,p1,p2) TRC(Level,Msg,p1,p2);
00283 #define TRC3(Level, Msg,p1,p2,p3) TRC(Level,Msg,p1,p2,p3);
00284 #define TRC4(Level, Msg,p1,p2,p3,p4) TRC(Level,Msg,p1,p2,p3,p4);
00285 #define TRC5(Level, Msg,p1,p2,p3,p4,p5) TRC(Level,Msg,p1,p2,p3,p4,p5);
00286
00287 #define TL_DEBUG TRACE_LEVEL>=TL_VAL_DEBUG) && TestToolBox::SimpleTraceOutput(TL_VAL_DEBUG, __FILE__, trace_fct, 0
00288 #define TL_ERROR TRACE_LEVEL>=TL_VAL_ERROR) && TestToolBox::SimpleTraceOutput(TL_VAL_ERROR, __FILE__, trace_fct, 0
00289 #define TL_PROD TRACE_LEVEL>=TL_VAL_PROD) && TestToolBox::SimpleTraceOutput(TL_VAL_PROD, __FILE__, trace_fct, 0
00290
00291 #define TL_DEBUGN TRACE_LEVEL>=TL_VAL_DEBUG) && TestToolBox::SimpleTraceOutput(TL_VAL_DEBUG, __FILE__, trace_fct, m_traceName
00292 #define TL_ERRORN TRACE_LEVEL>=TL_VAL_ERROR) && TestToolBox::SimpleTraceOutput(TL_VAL_ERROR, __FILE__, trace_fct, m_traceName
00293 #define TL_PRODN TRACE_LEVEL>=TL_VAL_PROD) && TestToolBox::SimpleTraceOutput(TL_VAL_PROD, __FILE__, trace_fct, m_traceName
00294
00295 #else // trace is switched off via preprocessor
00296
00297 #define TRCF(x)
00298
00299 #define TRC0(Level, Msg)
00300 #define TRC1(Level, Msg,p1)
00301 #define TRC2(Level, Msg,p1,p2)
00302 #define TRC3(Level, Msg,p1,p2,p3)
00303 #define TRC4(Level, Msg,p1,p2,p3,p4)
00304 #define TRC5(Level, Msg,p1,p2,p3,p4,p5)
00305
00306 #define TL_DEBUG 0) && SimpleTraceOutput(0, __FILE__, "", 0
00307 #define TL_ERROR 0) && SimpleTraceOutput(0, __FILE__, "", 0
00308 #define TL_PROD 0) && SimpleTraceOutput(0, __FILE__, "", 0
00309
00310 #define TL_DEBUGN 0) && SimpleTraceOutput(0, __FILE__, "", m_traceName
00311 #define TL_ERRORN 0) && SimpleTraceOutput(0, __FILE__, "", m_traceName
00312 #define TL_PRODN 0) && SimpleTraceOutput(0, __FILE__, "", m_traceName
00313
00314 #endif
00315
00316
00317
00318
00319
00320
00321
00322 #define TN(formatStr) "%s: " formatStr, m_traceName
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 #define TTB_TRC_DEF_GLOBALS(moduleName,traceLevel) \
00334 char const * TestToolBox::TRC_theModuleName = moduleName; \
00335 int TestToolBox::TRC_theTraceLevel = traceLevel;
00336
00337
00338
00339
00340
00341
00342
00343
00344 #define TTB_TRC_SET_TRACE_LEVEL_FROM_COMMANDLINE () \
00345 \
00346 char* pCmdLine = GetCommandLineA(); \
00347 const char* OPT_TRACE = "-trace"; \
00348 size_t lenTraceParam; \
00349 char traceParam [200]; \
00350 \
00351 char* pTraceToken = strstr(pCmdLine, OPT_TRACE); \
00352 while (pTraceToken) \
00353 { \
00354 pCmdLine = pTraceToken + strlen (OPT_TRACE); \
00355 pCmdLine += strspn(pCmdLine, " \t"); \
00356 lenTraceParam = strcspn(pCmdLine," \t"); \
00357 strncpy(traceParam, pCmdLine, lenTraceParam); \
00358 traceParam[lenTraceParam] = '\0'; \
00359 pCmdLine += lenTraceParam; \
00360 \
00361 char moduleName [200]; \
00362 char traceLevelStr [200]; \
00363 char* pLevel = strrchr(traceParam,'.'); \
00364 if (pLevel) \
00365 { \
00366 size_t lenModName = pLevel - traceParam; \
00367 size_t lenTraceLevel = strlen(traceParam) - lenModName - 1; \
00368 strncpy(moduleName, traceParam, lenModName); \
00369 moduleName[lenModName] = '\0'; \
00370 strncpy(traceLevelStr, pLevel+1, lenTraceLevel); \
00371 traceLevelStr[lenTraceLevel] = '\0'; \
00372 if (!strcmp(moduleName, TRC_theModuleName)) \
00373 { \
00374 TRC_theTraceLevel = atoi(traceLevelStr); \
00375 TRC(TL_PROD, \
00376 "TraceLevel set to %d (according commandline)", \
00377 TRC_theTraceLevel); \
00378 } \
00379 } \
00380 \
00381 pTraceToken = strstr(pCmdLine, OPT_TRACE); \
00382 }
00383
00384 inline void SetTraceLevelFromCommandLine(void)
00385 {
00386 TRCF("SetTraceLevelFromCommandLine");
00387
00388 char* pCmdLine = GetCommandLineA();
00389 const char* OPT_TRACE = "-trace";
00390 size_t lenTraceParam;
00391 char traceParam [200];
00392
00393 char* pTraceToken = strstr(pCmdLine, OPT_TRACE);
00394 while (pTraceToken)
00395 {
00396 pCmdLine = pTraceToken + strlen (OPT_TRACE);
00397 pCmdLine += strspn(pCmdLine, " \t");
00398 lenTraceParam = strcspn(pCmdLine," \t");
00399 strncpy(traceParam, pCmdLine, lenTraceParam);
00400 traceParam[lenTraceParam] = '\0';
00401 pCmdLine += lenTraceParam;
00402
00403 char moduleName [200];
00404 char traceLevelStr [200];
00405 char* pLevel = strrchr(traceParam,'.');
00406 if (pLevel)
00407 {
00408 size_t lenModName = pLevel - traceParam;
00409 size_t lenTraceLevel = strlen(traceParam) - lenModName - 1;
00410 strncpy(moduleName, traceParam, lenModName);
00411 moduleName[lenModName] = '\0';
00412 strncpy(traceLevelStr, pLevel+1, lenTraceLevel);
00413 traceLevelStr[lenTraceLevel] = '\0';
00414 if (!strcmp(moduleName, TRC_theModuleName))
00415 {
00416 TRC_theTraceLevel = atoi(traceLevelStr);
00417 TRC(TL_PROD,
00418 "TraceLevel set to %d (according commandline)",
00419 TRC_theTraceLevel);
00420 }
00421 }
00422
00423 pTraceToken = strstr(pCmdLine, OPT_TRACE);
00424 }
00425 }
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461 #define DEF_ERRID ErrorId errId = 0;
00462
00463 #define CALL_FAILED(FunctionCall) \
00464 0 != (errId = ((FunctionCall)))
00465
00466
00467
00468 #define ASSERT_ON_ERROR(FunctionCall) \
00469 errId = ((FunctionCall)); assert(0 == errId)
00470
00471 #define BREAK_ON_ERROR(FunctionCall) \
00472 errId = ((FunctionCall)); if (errId){break;}
00473
00474
00475 #define SEND_ON_ERROR(FunctionCall) \
00476 errId = ((FunctionCall)); if (errId){SendErrorToGui(errId);}
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 #ifdef _DEBUG
00488 #define SET_CRT_DEBUG_FIELD(a) \
00489 _CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
00490 #define CLEAR_CRT_DEBUG_FIELD(a) \
00491 _CrtSetDbgFlag(~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
00492 #else
00493 #define SET_CRT_DEBUG_FIELD(a) ((void) 0)
00494 #define CLEAR_CRT_DEBUG_FIELD(a) ((void) 0)
00495 #endif
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 #define IGNORE_PARAM(p) p = p
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520 #ifdef TTB_NOT_INLINE
00521 #define TTB_INLINE
00522 #else
00523 #define TTB_INLINE inline
00524 #endif
00525
00526 }
00527
00528 #pragma warning( pop )
00529
00530