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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#pragma once
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <fcntl.h>
#include <sys/stat.h>

#ifndef PLOG_ENABLE_WCHAR_INPUT
#   ifdef _WIN32
#       define PLOG_ENABLE_WCHAR_INPUT 1
#   else
#       define PLOG_ENABLE_WCHAR_INPUT 0
#   endif
#endif

//////////////////////////////////////////////////////////////////////////
// PLOG_CHAR_IS_UTF8 specifies character encoding of `char` type. On *nix
// systems it's set to UTF-8 while on Windows in can be ANSI or UTF-8. It
// automatically detects `/utf-8` command line option in MSVC. Also it can
// be set manually if required.
// This option allows to support http://utf8everywhere.org approach.

#ifndef PLOG_CHAR_IS_UTF8
#   if defined(_WIN32) && !defined(_UTF8)
#       define PLOG_CHAR_IS_UTF8 0
#   else
#       define PLOG_CHAR_IS_UTF8 1
#   endif
#endif

#ifdef _WIN32
#   if defined(PLOG_EXPORT)
#       define PLOG_LINKAGE __declspec(dllexport)
#   elif defined(PLOG_IMPORT)
#       define PLOG_LINKAGE __declspec(dllimport)
#   endif
#   if defined(PLOG_GLOBAL)
#       error "PLOG_GLOBAL isn't supported on Windows"
#   endif
#else
#   if defined(PLOG_GLOBAL)
#       define PLOG_LINKAGE __attribute__ ((visibility ("default")))
#   elif defined(PLOG_LOCAL)
#       define PLOG_LINKAGE __attribute__ ((visibility ("hidden")))
#       define PLOG_LINKAGE_HIDDEN PLOG_LINKAGE
#   endif
#   if defined(PLOG_EXPORT) || defined(PLOG_IMPORT)
#       error "PLOG_EXPORT/PLOG_IMPORT is supported only on Windows"
#   endif
#endif

#ifndef PLOG_LINKAGE
#   define PLOG_LINKAGE
#endif

#ifndef PLOG_LINKAGE_HIDDEN
#   define PLOG_LINKAGE_HIDDEN
#endif

#ifdef _WIN32
#   include <plog/WinApi.h>
#   include <time.h>
#   include <sys/timeb.h>
#   include <io.h>
#   include <share.h>
#else
#   include <unistd.h>
#   include <sys/time.h>
#   if defined(__linux__) || defined(__FreeBSD__)
#       include <sys/syscall.h>
#   elif defined(__rtems__)
#       include <rtems.h>
#   endif
#   if defined(_POSIX_THREADS)
#       include <pthread.h>
#   endif
#   if PLOG_ENABLE_WCHAR_INPUT
#       include <iconv.h>
#   endif
#endif

#ifdef __FREERTOS__ // There is no standard way to know if the code is compiled for FreeRTOS. We expect __FREERTOS__ macro to be defined in such case.
#   include <FreeRTOS.h>
#   include <semphr.h>
#   include <task.h>
#endif

#if PLOG_CHAR_IS_UTF8
#   define PLOG_NSTR(x)    x
#else
#   define _PLOG_NSTR(x)   L##x
#   define PLOG_NSTR(x)    _PLOG_NSTR(x)
#endif

#ifdef _WIN32
#   define PLOG_CDECL      __cdecl
#else
#   define PLOG_CDECL
#endif

#if __cplusplus >= 201103L || defined(_MSC_VER) && _MSC_VER >= 1700
#   define PLOG_OVERRIDE override
#else
#   define PLOG_OVERRIDE
#endif

namespace plog
{
    namespace util
    {
#if PLOG_CHAR_IS_UTF8
        typedef std::string nstring;
        typedef std::ostringstream nostringstream;
        typedef std::istringstream nistringstream;
        typedef std::ostream nostream;
        typedef char nchar;
#else
        typedef std::wstring nstring;
        typedef std::wostringstream nostringstream;
        typedef std::wistringstream nistringstream;
        typedef std::wostream nostream;
        typedef wchar_t nchar;
#endif

        inline void localtime_s(struct tm* t, const time_t* time)
        {
#if defined(_WIN32) && defined(__BORLANDC__)
            ::localtime_s(time, t);
#elif defined(_WIN32) && defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
            *t = *::localtime(time);
#elif defined(_WIN32)
            ::localtime_s(t, time);
#else
            ::localtime_r(time, t);
#endif
        }

        inline void gmtime_s(struct tm* t, const time_t* time)
        {
#if defined(_WIN32) && defined(__BORLANDC__)
            ::gmtime_s(time, t);
#elif defined(_WIN32) && defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
            *t = *::gmtime(time);
#elif defined(_WIN32)
            ::gmtime_s(t, time);
#else
            ::gmtime_r(time, t);
#endif
        }

#ifdef _WIN32
        typedef timeb Time;

        inline void ftime(Time* t)
        {
            ::ftime(t);
        }
#else
        struct Time
        {
            time_t time;
            unsigned short millitm;
        };

        inline void ftime(Time* t)
        {
            timeval tv;
            ::gettimeofday(&tv, NULL);

            t->time = tv.tv_sec;
            t->millitm = static_cast<unsigned short>(tv.tv_usec / 1000);
        }
#endif

        inline unsigned int gettid()
        {
#if defined(__FREERTOS__) && defined(INCLUDE_xTaskGetCurrentTaskHandle)
            return static_cast<unsigned int>(reinterpret_cast<uintptr_t>(xTaskGetCurrentTaskHandle()));
#elif defined(_WIN32)
            return GetCurrentThreadId();
#elif defined(__linux__)
            return static_cast<unsigned int>(::syscall(__NR_gettid));
#elif defined(__FreeBSD__)
            long tid;
            syscall(SYS_thr_self, &tid);
            return static_cast<unsigned int>(tid);
#elif defined(__rtems__)
            return rtems_task_self();
#elif defined(__APPLE__)
            uint64_t tid64;
            pthread_threadid_np(NULL, &tid64);
            return static_cast<unsigned int>(tid64);
#else
            return 0;
#endif
        }

#ifndef _GNU_SOURCE
    inline int vasprintf(char** strp, const char* format, va_list ap)
    {
        va_list ap_copy;
#if defined(_MSC_VER) && _MSC_VER <= 1600
        ap_copy = ap; // there is no va_copy on Visual Studio 2010
#else
        va_copy(ap_copy, ap);
#endif
#ifndef __STDC_SECURE_LIB__
        int charCount = vsnprintf(NULL, 0, format, ap_copy);
#else
        int charCount = _vscprintf(format, ap_copy);
#endif
        va_end(ap_copy);
        if (charCount < 0)
        {
            return -1;
        }

        size_t bufferCharCount = static_cast<size_t>(charCount) + 1;

        char* str = static_cast<char*>(malloc(bufferCharCount));
        if (!str)
        {
            return -1;
        }

#ifndef __STDC_SECURE_LIB__
        int retval = vsnprintf(str, bufferCharCount, format, ap);
#else
        int retval = vsnprintf_s(str, bufferCharCount, static_cast<size_t>(-1), format, ap);
#endif
        if (retval < 0)
        {
            free(str);
            return -1;
        }

        *strp = str;
        return retval;
    }
#endif

#ifdef _WIN32
    inline int vaswprintf(wchar_t** strp, const wchar_t* format, va_list ap)
    {
#if defined(__BORLANDC__)
        int charCount = 0x1000; // there is no _vscwprintf on Borland/Embarcadero
#else
        int charCount = _vscwprintf(format, ap);
        if (charCount < 0)
        {
            return -1;
        }
#endif

        size_t bufferCharCount = static_cast<size_t>(charCount) + 1;

        wchar_t* str = static_cast<wchar_t*>(malloc(bufferCharCount * sizeof(wchar_t)));
        if (!str)
        {
            return -1;
        }

#if defined(__BORLANDC__)
        int retval = vsnwprintf_s(str, bufferCharCount, format, ap);
#elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
        int retval = _vsnwprintf(str, bufferCharCount, format, ap);
#else
        int retval = _vsnwprintf_s(str, bufferCharCount, charCount, format, ap);
#endif
        if (retval < 0)
        {
            free(str);
            return -1;
        }

        *strp = str;
        return retval;
    }
#endif

#ifdef _WIN32
        inline std::wstring toWide(const char* str, UINT cp = codePage::kChar)
        {
            size_t len = ::strlen(str);
            std::wstring wstr(len, 0);

            if (!wstr.empty())
            {
                int wlen = MultiByteToWideChar(cp, 0, str, static_cast<int>(len), &wstr[0], static_cast<int>(wstr.size()));
                wstr.resize(wlen);
            }

            return wstr;
        }

        inline std::wstring toWide(const std::string& str, UINT cp = codePage::kChar)
        {
            return toWide(str.c_str(), cp);
        }

        inline const std::wstring& toWide(const std::wstring& str) // do nothing for already wide string
        {
            return str;
        }

        inline std::string toNarrow(const std::wstring& wstr, long page)
        {
            int len = WideCharToMultiByte(page, 0, wstr.c_str(), static_cast<int>(wstr.size()), 0, 0, 0, 0);
            std::string str(len, 0);

            if (!str.empty())
            {
                WideCharToMultiByte(page, 0, wstr.c_str(), static_cast<int>(wstr.size()), &str[0], len, 0, 0);
            }

            return str;
        }
#elif PLOG_ENABLE_WCHAR_INPUT
        inline std::string toNarrow(const wchar_t* wstr)
        {
            size_t wlen = ::wcslen(wstr);
            std::string str(wlen * sizeof(wchar_t), 0);

            if (!str.empty())
            {
                const char* in = reinterpret_cast<const char*>(&wstr[0]);
                char* out = &str[0];
                size_t inBytes = wlen * sizeof(wchar_t);
                size_t outBytes = str.size();

                iconv_t cd = ::iconv_open("UTF-8", "WCHAR_T");
                ::iconv(cd, const_cast<char**>(&in), &inBytes, &out, &outBytes);
                ::iconv_close(cd);

                str.resize(str.size() - outBytes);
            }

            return str;
        }
#endif

        inline std::string processFuncName(const char* func)
        {
#if (defined(_WIN32) && !defined(__MINGW32__)) || defined(__OBJC__)
            return std::string(func);
#else
            const char* funcBegin = func;
            const char* funcEnd = ::strchr(funcBegin, '(');
            int foundTemplate = 0;

            if (!funcEnd)
            {
                return std::string(func);
            }

            for (const char* i = funcEnd - 1; i >= funcBegin; --i) // search backwards for the first space char
            {
                if (*i == '>')
                {
                    foundTemplate++;
                }
                else if (*i == '<')
                {
                    foundTemplate--;
                }
                else if (*i == ' ' && foundTemplate == 0)
                {
                    funcBegin = i + 1;
                    break;
                }
            }

            return std::string(funcBegin, funcEnd);
#endif
        }

        inline const nchar* findExtensionDot(const nchar* fileName)
        {
#if PLOG_CHAR_IS_UTF8
            return std::strrchr(fileName, '.');
#else
            return std::wcsrchr(fileName, L'.');
#endif
        }

        inline void splitFileName(const nchar* fileName, nstring& fileNameNoExt, nstring& fileExt)<--- The function 'splitFileName' is never used.
        {
            const nchar* dot = findExtensionDot(fileName);

            if (dot)
            {
                fileNameNoExt.assign(fileName, dot);
                fileExt.assign(dot + 1);
            }
            else
            {
                fileNameNoExt.assign(fileName);
                fileExt.clear();
            }
        }

        class PLOG_LINKAGE NonCopyable
        {
        protected:
            NonCopyable()
            {
            }

        private:
            NonCopyable(const NonCopyable&);
            NonCopyable& operator=(const NonCopyable&);
        };

        class PLOG_LINKAGE_HIDDEN File : NonCopyable
        {
        public:
            File() : m_file(-1)
            {
            }

            ~File()
            {
                close();
            }

            size_t open(const nstring& fileName)
            {
#if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__))
                m_file = ::_wsopen(toWide(fileName).c_str(), _O_CREAT | _O_WRONLY | _O_BINARY | _O_NOINHERIT, SH_DENYWR, _S_IREAD | _S_IWRITE);
#elif defined(_WIN32)
                ::_wsopen_s(&m_file, toWide(fileName).c_str(), _O_CREAT | _O_WRONLY | _O_BINARY | _O_NOINHERIT, _SH_DENYWR, _S_IREAD | _S_IWRITE);
#elif defined(O_CLOEXEC)
                m_file = ::open(fileName.c_str(), O_CREAT | O_APPEND | O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
#else
                m_file = ::open(fileName.c_str(), O_CREAT | O_APPEND | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
#endif
                return seek(0, SEEK_END);
            }

            size_t write(const void* buf, size_t count)
            {
                return m_file != -1 ? static_cast<size_t>(
#ifdef _WIN32
                    ::_write(m_file, buf, static_cast<unsigned int>(count))
#else
                    ::write(m_file, buf, count)
#endif
                    ) : static_cast<size_t>(-1);
            }

            template<class CharType>
            size_t write(const std::basic_string<CharType>& str)
            {
                return write(str.data(), str.size() * sizeof(CharType));
            }

            size_t seek(size_t offset, int whence)
            {
                return m_file != -1 ? static_cast<size_t>(
#if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__))
                    ::_lseek(m_file, static_cast<off_t>(offset), whence)
#elif defined(_WIN32)
                    ::_lseeki64(m_file, static_cast<off_t>(offset), whence)
#else
                    ::lseek(m_file, static_cast<off_t>(offset), whence)
#endif
                    ) : static_cast<size_t>(-1);
            }

            void close()
            {
                if (m_file != -1)
                {
#ifdef _WIN32
                    ::_close(m_file);
#else
                    ::close(m_file);
#endif
                    m_file = -1;
                }
            }

            static int unlink(const nstring& fileName)
            {
#ifdef _WIN32
                return ::_wunlink(toWide(fileName).c_str());
#else
                return ::unlink(fileName.c_str());
#endif
            }

            static int rename(const nstring& oldFilename, const nstring& newFilename)
            {
#ifdef _WIN32
                return MoveFileW(toWide(oldFilename).c_str(), toWide(newFilename).c_str());
#else
                return ::rename(oldFilename.c_str(), newFilename.c_str());
#endif
            }

        private:
            int m_file;
        };

        class PLOG_LINKAGE_HIDDEN Mutex : NonCopyable
        {
        public:
            Mutex()
            {
#ifdef __FREERTOS__
                m_sync = xSemaphoreCreateBinary();
                xSemaphoreGive(m_sync);
#elif defined(_WIN32)
                InitializeCriticalSection(&m_sync);
#elif defined(__rtems__)
                rtems_semaphore_create(0, 1,
                            RTEMS_PRIORITY |
                            RTEMS_BINARY_SEMAPHORE |
                            RTEMS_INHERIT_PRIORITY, 1, &m_sync);
#elif defined(_POSIX_THREADS)
                ::pthread_mutex_init(&m_sync, 0);
#endif
            }

            ~Mutex()
            {
#ifdef __FREERTOS__
                vSemaphoreDelete(m_sync);
#elif defined(_WIN32)
                DeleteCriticalSection(&m_sync);
#elif defined(__rtems__)
                rtems_semaphore_delete(m_sync);
#elif defined(_POSIX_THREADS)
                ::pthread_mutex_destroy(&m_sync);
#endif
            }

            friend class MutexLock;

        private:
            void lock()
            {
#ifdef __FREERTOS__
                xSemaphoreTake(m_sync, portMAX_DELAY);
#elif defined(_WIN32)
                EnterCriticalSection(&m_sync);
#elif defined(__rtems__)
                rtems_semaphore_obtain(m_sync, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
#elif defined(_POSIX_THREADS)
                ::pthread_mutex_lock(&m_sync);
#endif
            }

            void unlock()
            {
#ifdef __FREERTOS__
                xSemaphoreGive(m_sync);
#elif defined(_WIN32)
                LeaveCriticalSection(&m_sync);
#elif defined(__rtems__)
                rtems_semaphore_release(m_sync);
#elif defined(_POSIX_THREADS)
                ::pthread_mutex_unlock(&m_sync);
#endif
            }

        private:
#ifdef __FREERTOS__
            SemaphoreHandle_t m_sync;
#elif defined(_WIN32)
            CRITICAL_SECTION m_sync;
#elif defined(__rtems__)
            rtems_id m_sync;
#elif defined(_POSIX_THREADS)
            pthread_mutex_t m_sync;
#endif
        };

        class PLOG_LINKAGE_HIDDEN MutexLock : NonCopyable
        {
        public:
            MutexLock(Mutex& mutex) : m_mutex(mutex)
            {
                m_mutex.lock();
            }

            ~MutexLock()
            {
                m_mutex.unlock();
            }

        private:
            Mutex& m_mutex;
        };

        template<class T>
#ifdef _WIN32
        class Singleton : NonCopyable
#else
        class PLOG_LINKAGE Singleton : NonCopyable
#endif
        {
        public:
#if (defined(__clang__) || defined(__GNUC__) && __GNUC__ >= 8) && !defined(__BORLANDC__)
            // This constructor is called before the `T` object is fully constructed, and
            // pointers are not dereferenced anyway, so UBSan shouldn't check vptrs.
            __attribute__((no_sanitize("vptr")))
#endif
            Singleton()
            {
                assert(!m_instance);
                m_instance = static_cast<T*>(this);
            }

            ~Singleton()
            {
                assert(m_instance);
                m_instance = 0;
            }

            static T* getInstance()
            {
                return m_instance;
            }

        private:
            static T* m_instance;
        };

        template<class T>
        T* Singleton<T>::m_instance = NULL;
    }
}