aboutsummaryrefslogtreecommitdiff
path: root/test/TLogHelper.cpp
blob: d96108f54973482f915a3fb40ca2f3e57760ed83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*****************************************************************************/
/* TLogHelper.cpp                         Copyright (c) Ladislav Zezula 2013 */
/*---------------------------------------------------------------------------*/
/* Helper class for reporting StormLib tests                                 */
/* This file should be included directly from StormTest.cpp using #include   */
/*---------------------------------------------------------------------------*/
/*   Date    Ver   Who  Comment                                              */
/* --------  ----  ---  -------                                              */
/* 26.11.13  1.00  Lad  Created                                              */
/*****************************************************************************/

//-----------------------------------------------------------------------------
// String replacements for format strings

#if defined(STORMLIB_WINDOWS) || defined(CASCLIB_PLATFORM_WINDOWS)
#define TEST_PLATFORM_WINDOWS
#endif

#ifdef _MSC_VER
#define fmt_I64u_t _T("%I64u")
#define fmt_I64u_a "%I64u"
#define fmt_I64X_t _T("%I64X")
#define fmt_I64X_a "%I64X"
#else
#define fmt_I64u_t _T("%llu")
#define fmt_I64u_a "%llu"
#define fmt_I64X_t _T("%llX")
#define fmt_I64X_a "%llX"
#endif

#define fmt_X_of_Y_a  "(" fmt_I64u_a " of " fmt_I64u_a ")"

#ifdef __CASCLIB_SELF__
#define TEST_MIN CASCLIB_MIN
#define TEST_PRINT_PREFIX   false
#else
#define TEST_MIN STORMLIB_MIN
#define TEST_PRINT_PREFIX   true
#endif

//-----------------------------------------------------------------------------
// Local functions

inline DWORD TestInterlockedIncrement(DWORD * PtrValue)
{
#ifdef TEST_PLATFORM_WINDOWS
    return (DWORD)InterlockedIncrement((LONG *)(PtrValue));
#elif defined(__GNUC__)
    return __sync_add_and_fetch(PtrValue, 1);
#else
    return ++(*PtrValue);
#endif
}

inline DWORD Test_GetLastError()
{
#if defined(CASCLIB_PLATFORM_WINDOWS)
    return GetCascError();
#else
    return GetLastError();
#endif
}

void TestStrCopy(char * szTarget, size_t cchTarget, const char * szSource, size_t cchSource = -1)
{
    size_t cchToCopy;

    if(cchTarget > 0)
    {
        // Make sure we know the length
        if(cchSource == -1)
            cchSource = strlen(szSource);
        cchToCopy = TEST_MIN((cchTarget - 1), cchSource);

        // Copy the string
        memcpy(szTarget, szSource, cchToCopy);
        szTarget[cchToCopy] = 0;
    }
}

void TestStrCopy(char * szTarget, size_t cchTarget, const wchar_t * szSource, size_t cchSource = -1)
{
    size_t cchToCopy;

    if(cchTarget > 0)
    {
        // Make sure we know the length
        if(cchSource == -1)
            cchSource = wcslen(szSource);
        cchToCopy = TEST_MIN((cchTarget - 1), cchSource);

        wcstombs(szTarget, szSource, cchToCopy);
        szTarget[cchToCopy] = 0;
    }
}

void TestStrCopy(wchar_t * szTarget, size_t cchTarget, const char * szSource, size_t cchSource = -1)
{
    size_t cchToCopy;

    if(cchTarget > 0)
    {
        // Make sure we know the length
        if(cchSource == -1)
            cchSource = strlen(szSource);
        cchToCopy = TEST_MIN((cchTarget - 1), cchSource);

        mbstowcs(szTarget, szSource, cchToCopy);
        szTarget[cchToCopy] = 0;
    }
}

#ifdef STORMLIB_WINDOWS
wchar_t * CopyFormatCharacter(wchar_t * szBuffer, const wchar_t *& szFormat)
{
    static const wchar_t * szStringFormat = _T("%s");
    static const wchar_t * szUint64Format = fmt_I64u_t;

    // String format
    if(szFormat[0] == '%')
    {
        if(szFormat[1] == 's')
        {
            _tcscpy(szBuffer, szStringFormat);
            szFormat += 2;
            return szBuffer + _tcslen(szStringFormat);
        }

        // Replace %I64u with the proper platform-dependent suffix
        if(szFormat[1] == 'I' && szFormat[2] == '6' && szFormat[3] == '4' && szFormat[4] == 'u')
        {
            _tcscpy(szBuffer, szUint64Format);
            szFormat += 5;
            return szBuffer + _tcslen(szUint64Format);
        }
    }

    // Copy the character as-is
    *szBuffer++ = *szFormat++;
    return szBuffer;
}
#endif  // STORMLIB_WINDOWS

char * CopyFormatCharacter(char * szBuffer, const char *& szFormat)
{
    static const char * szStringFormat = "\"%s\"";
    static const char * szUint64Format = fmt_I64u_a;

    // String format
    if(szFormat[0] == '%')
    {
        if(szFormat[1] == 's')
        {
            TestStrCopy(szBuffer, 32, szStringFormat);
            szFormat += 2;
            return szBuffer + strlen(szStringFormat);
        }

        // Replace %I64u with the proper platform-dependent suffix
        if(szFormat[1] == 'I' && szFormat[2] == '6' && szFormat[3] == '4' && szFormat[4] == 'u')
        {
            TestStrCopy(szBuffer, 32, szUint64Format);
            szFormat += 5;
            return szBuffer + strlen(szUint64Format);
        }
    }

    // Copy the character as-is
    *szBuffer++ = *szFormat++;
    return szBuffer;
}

size_t TestStrPrintfV(char * buffer, size_t nCount, const char * format, va_list argList)
{
    return vsnprintf(buffer, nCount, format, argList);
}

size_t TestStrPrintf(char * buffer, size_t nCount, const char * format, ...)
{
    va_list argList;
    size_t length;

    // Start the argument list
    va_start(argList, format);
    length = TestStrPrintfV(buffer, nCount, format, argList);
    va_end(argList);

    return length;
}

size_t TestStrPrintfV(wchar_t * buffer, size_t nCount, const wchar_t * format, va_list argList)
{
#ifdef TEST_PLATFORM_WINDOWS
    return _vsnwprintf(buffer, nCount, format, argList);
#else
    return vswprintf(buffer, nCount, format, argList);
#endif
}

size_t TestStrPrintf(wchar_t * buffer, size_t nCount, const wchar_t * format, ...)
{
    va_list argList;
    size_t length;

    // Start the argument list
    va_start(argList, format);
    length = TestStrPrintfV(buffer, nCount, format, argList);
    va_end(argList);

    return length;
}

//-----------------------------------------------------------------------------
// Definition of the TLogHelper class

class TLogHelper
{
    public:

    //
    //  Constructor and destructor
    //

    TLogHelper(const char * szNewMainTitle = NULL, const TCHAR * szNewSubTitle1 = NULL, const TCHAR * szNewSubTitle2 = NULL)
    {
        // Fill the variables
        memset(this, 0, sizeof(TLogHelper));
        UserString = "";
        TotalFiles = 1;
        UserCount = 1;
        UserTotal = 1;

#ifdef TEST_PLATFORM_WINDOWS
        InitializeCriticalSection(&Locker);
        TickCount = GetTickCount();
#endif

        // Remember the startup time
        SetStartTime();

        // Save all three titles
        szMainTitle = szNewMainTitle;
        szSubTitle1 = szNewSubTitle1;
        szSubTitle2 = szNewSubTitle2;

        // Print the initial information
        if(szNewMainTitle != NULL)
        {
#ifdef __CASCLIB_SELF__
            char szMainTitleT[0x100] = {0};
            size_t nLength;

            nLength = TestStrPrintf(szMainTitleT, _countof(szMainTitleT), "-- \"%s\" --", szNewMainTitle);
            while(nLength < 90)
                szMainTitleT[nLength++] = '-';
            if(nLength < sizeof(szMainTitleT))
                szMainTitleT[nLength++] = 0;

            printf("%s\n", szMainTitleT);
#endif

#ifdef __STORMLIB_SELF__
            TCHAR szMainTitleT[0x100] = {0};

            // Copy the UNICODE main title
            TestStrCopy(szMainTitleT, _countof(szMainTitleT), szMainTitle);

            if(szSubTitle1 != NULL && szSubTitle2 != NULL)
                nPrevPrinted = _tprintf(_T("\rRunning %s (%s+%s) ..."), szMainTitleT, szSubTitle1, szSubTitle2);
            else if(szSubTitle1 != NULL)
                nPrevPrinted = _tprintf(_T("\rRunning %s (%s) ..."), szMainTitleT, szSubTitle1);
            else
                nPrevPrinted = _tprintf(_T("\rRunning %s ..."), szMainTitleT);
#endif
        }
    }

    ~TLogHelper()
    {
        // Print a verdict, if no verdict was printed yet
        if(bMessagePrinted == false)
        {
            PrintVerdict(ERROR_SUCCESS);
        }

#ifdef TEST_PLATFORM_WINDOWS
        DeleteCriticalSection(&Locker);
#endif

#ifdef __CASCLIB_SELF__
        printf("\n");
#endif
    }

    //
    // Measurement of elapsed time
    //

    bool TimeElapsed(DWORD Milliseconds)
    {
        bool bTimeElapsed = false;

#ifdef TEST_PLATFORM_WINDOWS
        if(GetTickCount() > (TickCount + Milliseconds))
        {
            TickCount = GetTickCount();
            if(TestInterlockedIncrement(&TimeTrigger) == 1)
            {
                bTimeElapsed = true;
            }
        }

#endif
        return bTimeElapsed;
    }

    //
    //  Printing functions
    //

    template <typename XCHAR>
    DWORD PrintWithClreol(const XCHAR * szFormat, va_list argList, bool bPrintLastError, bool bPrintEndOfLine)
    {
        char * szBufferPtr;
        char * szBufferEnd;
        size_t nNewPrinted;
        size_t nLength = 0;
        DWORD dwErrCode = Test_GetLastError();
        XCHAR szMessage[0x200];
        char szBuffer[0x200];
        bool bPrintPrefix = TEST_PRINT_PREFIX;

        // Always start the buffer with '\r'
        szBufferEnd = szBuffer + _countof(szBuffer);
        szBufferPtr = szBuffer;
        *szBufferPtr++ = '\r';

        // Print the prefix, if needed
        if(szMainTitle != NULL && bPrintPrefix)
        {
            while(szMainTitle[nLength] != 0)
                *szBufferPtr++ = szMainTitle[nLength++];

            *szBufferPtr++ = ':';
            *szBufferPtr++ = ' ';
        }

        // Construct the message
        nLength = TestStrPrintfV(szMessage, _countof(szMessage), szFormat, argList);
        TestStrCopy(szBufferPtr, (szBufferEnd - szBufferPtr), szMessage);
        szBufferPtr += nLength;

        // Append the last error
        if(bPrintLastError)
        {
            nLength = TestStrPrintf(szBufferPtr, (szBufferEnd - szBufferPtr), " (error code: %u)", dwErrCode);
            szBufferPtr += nLength;
        }

        // Remember how much did we print
        nNewPrinted = (szBufferPtr - szBuffer);

        // Shall we pad the string?
        if((nLength = (szBufferPtr - szBuffer - 1)) < nPrevPrinted)
        {
            size_t nPadding = nPrevPrinted - nLength;

            if((size_t)(nLength + nPadding) > (size_t)(szBufferEnd - szBufferPtr))
                nPadding = (szBufferEnd - szBufferPtr);

            memset(szBufferPtr, ' ', nPadding);
            szBufferPtr += nPadding;
        }

        // Shall we add new line?
        if((bPrintEndOfLine != false) && (szBufferPtr < szBufferEnd))
            *szBufferPtr++ = '\n';
        *szBufferPtr = 0;

        // Remember if we printed a message
        if(bPrintEndOfLine != false)
        {
            bMessagePrinted = true;
            nPrevPrinted = 0;
        }
        else
        {
            nPrevPrinted = nNewPrinted;
        }

        // Finally print the message
        printf("%s", szBuffer);
        nMessageCounter++;
        return dwErrCode;
    }

    template <typename XCHAR>
    void PrintProgress(const XCHAR * szFormat, ...)
    {
        va_list argList;

        // Always reset the time trigger
        TimeTrigger = 0;

        // Only print progress when the cooldown is ready
        if(ProgressReady())
        {
            va_start(argList, szFormat);
            PrintWithClreol(szFormat, argList, false, false);
            va_end(argList);
        }
    }

    template <typename XCHAR>
    void PrintMessage(const XCHAR * szFormat, ...)
    {
        va_list argList;

        va_start(argList, szFormat);
        PrintWithClreol(szFormat, argList, false, true);
        va_end(argList);
    }

    void PrintTotalTime()
    {
        DWORD TotalTime = SetEndTime();

        if(TotalTime != 0)
            PrintMessage("TotalTime: %u.%u second(s)", (TotalTime / 1000), (TotalTime % 1000));
        PrintMessage("Work complete.");
    }

    template <typename XCHAR>
    int PrintErrorVa(const XCHAR * szFormat, ...)
    {
        va_list argList;
        int nResult;

        va_start(argList, szFormat);
        nResult = PrintWithClreol(szFormat, argList, true, true);
        va_end(argList);

        return nResult;
    }

    template <typename XCHAR>
    int PrintError(const XCHAR * szFormat, const XCHAR * szFileName = NULL)
    {
        return PrintErrorVa(szFormat, szFileName);
    }

    // Print final verdict
    DWORD PrintVerdict(DWORD dwErrCode = ERROR_SUCCESS)
    {
        LPCTSTR szSaveSubTitle1 = szSubTitle1;
        LPCTSTR szSaveSubTitle2 = szSubTitle2;
        TCHAR szSaveMainTitle[0x80];

        // Set both to NULL so they won't be printed
        TestStrCopy(szSaveMainTitle, _countof(szSaveMainTitle), szMainTitle);
        szMainTitle = NULL;
        szSubTitle1 = NULL;
        szSubTitle2 = NULL;

        // Print the final information
        if(szSaveMainTitle[0] != 0)
        {
            if(DontPrintResult == false)
            {
                const TCHAR * szVerdict = (dwErrCode == ERROR_SUCCESS) ? _T("succeeded") : _T("failed");

                if(szSaveSubTitle1 != NULL && szSaveSubTitle2 != NULL)
                    PrintMessage(_T("%s (%s+%s) %s."), szSaveMainTitle, szSaveSubTitle1, szSaveSubTitle2, szVerdict);
                else if(szSaveSubTitle1 != NULL)
                    PrintMessage(_T("%s (%s) %s."), szSaveMainTitle, szSaveSubTitle1, szVerdict);
                else
                    PrintMessage(_T("%s %s."), szSaveMainTitle, szVerdict);
            }
            else
            {
                PrintProgress(" ");
                printf("\r");
            }
        }

        // Return the error code so the caller can pass it fuhrter
        return dwErrCode;
    }

    //
    // Locking functions (Windows only)
    //

    void Lock()
    {
#ifdef TEST_PLATFORM_WINDOWS
        EnterCriticalSection(&Locker);
#endif
    }

    void Unlock()
    {
#ifdef TEST_PLATFORM_WINDOWS
        LeaveCriticalSection(&Locker);
#endif
    }

    //
    //  Time functions
    //

    ULONGLONG GetCurrentThreadTime()
    {
#ifdef _WIN32
        ULONGLONG TempTime = 0;

        GetSystemTimeAsFileTime((LPFILETIME)(&TempTime));
        return ((TempTime) / 10 / 1000);

        //ULONGLONG KernelTime = 0;
        //ULONGLONG UserTime = 0;
        //ULONGLONG TempTime = 0;

        //GetThreadTimes(GetCurrentThread(), (LPFILETIME)&TempTime, (LPFILETIME)&TempTime, (LPFILETIME)&KernelTime, (LPFILETIME)&UserTime);
        //return ((KernelTime + UserTime) / 10 / 1000);
#else
        return time(NULL) * 1000;
#endif
    }

    bool ProgressReady()
    {
        time_t dwTickCount = time(NULL);
        bool bResult = false;

        if(dwTickCount > dwPrevTickCount)
        {
            dwPrevTickCount = dwTickCount;
            bResult = true;
        }

        return bResult;
    }

    ULONGLONG SetStartTime()
    {
        StartTime = GetCurrentThreadTime();
        return StartTime;
    }

    DWORD SetEndTime()
    {
        EndTime = GetCurrentThreadTime();
        return (DWORD)(EndTime - StartTime);
    }

    void IncrementTotalBytes(ULONGLONG IncrementValue)
    {
        // For some weird reason, this is measurably faster then InterlockedAdd64
        Lock();
        TotalBytes = TotalBytes + IncrementValue;
        Unlock();
    }

    void FormatTotalBytes(char * szBuffer, size_t ccBuffer)
    {
        ULONGLONG Bytes = TotalBytes;
        ULONGLONG Divider = 1000000000;
        char * szBufferEnd = szBuffer + ccBuffer;
        bool bDividingOn = false;

        while((szBuffer + 4) < szBufferEnd && Divider > 0)
        {
            // Are we already dividing?
            if(bDividingOn)
            {
                szBuffer += TestStrPrintf(szBuffer, ccBuffer, " %03u", (DWORD)(Bytes / Divider));
                Bytes = Bytes % Divider;
            }
            else if(Bytes > Divider)
            {
                szBuffer += TestStrPrintf(szBuffer, ccBuffer, "%u", (DWORD)(Bytes / Divider));
                Bytes = Bytes % Divider;
                bDividingOn = true;
            }
            Divider /= 1000;
        }
    }

#ifdef TEST_PLATFORM_WINDOWS
    CRITICAL_SECTION Locker;
#endif

    ULONGLONG TotalBytes;                           // For user's convenience: Total number of bytes
    ULONGLONG ByteCount;                            // For user's convenience: Current number of bytes
    ULONGLONG StartTime;                            // Start time of an operation, in milliseconds
    ULONGLONG EndTime;                              // End time of an operation, in milliseconds
    const char * UserString;
    DWORD UserCount;
    DWORD UserTotal;
    DWORD TickCount;
    DWORD TimeTrigger;                              // For triggering elapsed timers
    DWORD TotalFiles;                               // For user's convenience: Total number of files
    DWORD FileCount;                                // For user's convenience: Curernt number of files
    DWORD DontPrintResult:1;                        // If true, supress printing result from the destructor

    protected:

    const char  * szMainTitle;                      // Title of the text (usually name)
    const TCHAR * szSubTitle1;                      // Title of the text (can be name of the tested file)
    const TCHAR * szSubTitle2;                      // Title of the text (can be name of the tested file)
    size_t nMessageCounter;
    size_t nPrevPrinted;                            // Length of the previously printed message
    time_t dwPrevTickCount;
    bool bMessagePrinted;
};