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
#pragma once
#include <cstdarg>
#include <plog/Severity.h>
#include <plog/Util.h>

#ifdef __cplusplus_cli
#include <vcclr.h>  // For PtrToStringChars
#endif

namespace plog
{
    namespace detail
    {
#if !defined(_MSC_VER) || _MSC_VER > 1400 // MSVC 2005 doesn't understand `enableIf`, so drop all `meta`
        namespace meta
        {
            template<class T>
            inline T& declval()
            {
#ifdef __INTEL_COMPILER
#    pragma warning(suppress: 327) // NULL reference is not allowed
#endif
                return *reinterpret_cast<T*>(0);<--- Null pointer dereference
            }

            template<bool B, class T = void>
            struct enableIf {};

            template<class T>
            struct enableIf<true, T> { typedef T type; };

            struct No  { char a[1]; };
            struct Yes { char a[2]; };

            template <class From, class To>
            struct isConvertible
            {
                // `+ sizeof(U*)` is required for GCC 4.5-4.7
                template<class U>
                static typename enableIf<!!(sizeof(static_cast<To>(meta::declval<U>())) + sizeof(U*)), Yes>::type test(int);

                template<class U>
                static No test(...);

                enum { value = sizeof(test<From>(0)) == sizeof(Yes) };
            };

            template <class T>
            struct isConvertibleToString : isConvertible<T, std::string> {};

#if PLOG_ENABLE_WCHAR_INPUT
            template <class T>
            struct isConvertibleToWString : isConvertible<T, std::wstring> {};
#endif

            template <class T>
            struct isContainer
            {
                template<class U>
                static typename meta::enableIf<!!(sizeof(
#if defined(_MSC_VER) && _MSC_VER < 1700 // MSVC 2010 doesn't understand `typename T::const_iterator`
                    meta::declval<U>().begin()) + sizeof(meta::declval<U>().end()
#else
                    typename U::const_iterator
#endif
                    )), Yes>::type test(int);

                template<class U>
                static No test(...);

                enum { value = sizeof(test<T>(0)) == sizeof(Yes) };
            };

            // Detects `std::filesystem::path` and `boost::filesystem::path`. They look like containers
            // but we don't want to treat them as containers, so we use this detector to filter them out.
            template <class T>
            struct isFilesystemPath
            {
                template<class U>
                static typename meta::enableIf<!!(sizeof(meta::declval<U>().preferred_separator)), Yes>::type test(int);

                template<class U>
                static No test(...);

                enum { value = sizeof(test<T>(0)) == sizeof(Yes) };
            };
        }
#endif

        //////////////////////////////////////////////////////////////////////////
        // Stream output operators as free functions

#if PLOG_ENABLE_WCHAR_INPUT
        inline void operator<<(util::nostringstream& stream, const wchar_t* data)
        {
            data = data ? data : L"(null)";

#   ifdef _WIN32
#       if PLOG_CHAR_IS_UTF8
            std::operator<<(stream, util::toNarrow(data, codePage::kUTF8));
#       else
            std::operator<<(stream, data);
#       endif
#   else
            std::operator<<(stream, util::toNarrow(data));
#   endif
        }

        inline void operator<<(util::nostringstream& stream, wchar_t* data)
        {
            plog::detail::operator<<(stream, const_cast<const wchar_t*>(data));
        }

        inline void operator<<(util::nostringstream& stream, const std::wstring& data)
        {
            plog::detail::operator<<(stream, data.c_str());
        }
#endif

        inline void operator<<(util::nostringstream& stream, const char* data)
        {
            data = data ? data : "(null)";

#if defined(_WIN32) && defined(__BORLANDC__)
#   if PLOG_CHAR_IS_UTF8
            stream << data;
#   else
            stream << util::toWide(data);
#   endif
#elif defined(_WIN32)
#   if PLOG_CHAR_IS_UTF8
            std::operator<<(stream, data);
#   else
            std::operator<<(stream, util::toWide(data));
#   endif
#else
            std::operator<<(stream, data);
#endif
        }

        inline void operator<<(util::nostringstream& stream, char* data)
        {
            plog::detail::operator<<(stream, const_cast<const char*>(data));
        }

        inline void operator<<(util::nostringstream& stream, const std::string& data)
        {
            plog::detail::operator<<(stream, data.c_str());
        }

#ifdef __cpp_char8_t
        inline void operator<<(util::nostringstream& stream, const char8_t* data)
        {
#   if PLOG_CHAR_IS_UTF8
            plog::detail::operator<<(stream, reinterpret_cast<const char*>(data));
#   else
            plog::detail::operator<<(stream, util::toWide(reinterpret_cast<const char*>(data), codePage::kUTF8));
#   endif
        }
#endif //__cpp_char8_t

        // Print `std::pair`
        template<class T1, class T2>
        inline void operator<<(util::nostringstream& stream, const std::pair<T1, T2>& data)
        {
            stream << data.first;
            stream << ":";
            stream << data.second;
        }

#if defined(__clang__) || !defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__) >= 405 // skip for GCC < 4.5 due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38600
#if !defined(_MSC_VER) || _MSC_VER > 1400 // MSVC 2005 doesn't understand `enableIf`, so drop all `meta`
        // Print data that can be casted to `std::string`
        template<class T>
        inline typename meta::enableIf<meta::isConvertibleToString<T>::value, void>::type operator<<(util::nostringstream& stream, const T& data)
        {
            plog::detail::operator<<(stream, static_cast<std::string>(data));
        }

#if PLOG_ENABLE_WCHAR_INPUT
        // Print data that can be casted to `std::wstring`
        template<class T>
        inline typename meta::enableIf<meta::isConvertibleToWString<T>::value, void>::type operator<<(util::nostringstream& stream, const T& data)
        {
            plog::detail::operator<<(stream, static_cast<std::wstring>(data));
        }
#endif

        // Print std containers
        template<class T>
        inline typename meta::enableIf<meta::isContainer<T>::value &&
            !meta::isConvertibleToString<T>::value &&
#if PLOG_ENABLE_WCHAR_INPUT
            !meta::isConvertibleToWString<T>::value &&
#endif
            !meta::isFilesystemPath<T>::value, void>::type operator<<(util::nostringstream& stream, const T& data)
        {
            stream << "[";

            for (typename T::const_iterator it = data.begin(); it != data.end();)
            {
                stream << *it;

                if (++it == data.end())
                {
                    break;
                }

                stream << ", ";
            }

            stream << "]";
        }
#endif
#endif

#ifdef __cplusplus_cli
        // Print managed C++ `System::String^`
        inline void operator<<(util::nostringstream& stream, System::String^ data)
        {
            cli::pin_ptr<const System::Char> ptr = PtrToStringChars(data);
            plog::detail::operator<<(stream, static_cast<const wchar_t*>(ptr));
        }
#endif

#if PLOG_ENABLE_WCHAR_INPUT && (!defined(_MSC_VER) || _MSC_VER > 1400) // MSVC 2005 doesn't understand `enableIf`, so drop all `meta`
        namespace meta
        {
            template<bool Value>
            struct valueType { enum { value = Value }; };

            template<class T, class Stream>
            inline No operator<<(Stream&, const T&);

            template<class T, class Stream>
            struct isStreamable : valueType<sizeof(operator<<(meta::declval<Stream>(), meta::declval<const T>())) != sizeof(No)> {};

            template<class Stream>
            struct isStreamable<std::ios_base& PLOG_CDECL (std::ios_base&), Stream> : valueType<true> {};

            template<class Stream, size_t N>
            struct isStreamable<wchar_t[N], Stream> : valueType<false> {};

            template<class Stream, size_t N>
            struct isStreamable<const wchar_t[N], Stream> : valueType<false> {};

            // meta doesn't work well for deleted functions and C++20 has `operator<<(std::ostream&, const wchar_t*) = delete` so explicitly define it
            template<>
            struct isStreamable<const wchar_t*, std::ostream> : valueType<false> {};

#   ifdef __cpp_char8_t
            // meta doesn't work well for deleted functions and C++20 has `operator<<(std::ostream&, const char8_t*) = delete` so explicitly define it
            template<class Stream, size_t N>
            struct isStreamable<char8_t[N], Stream> : valueType<false> {};

            template<class Stream>
            struct isStreamable<const char8_t*, Stream> : valueType<false> {};
#   endif //__cpp_char8_t
        }

#   if PLOG_CHAR_IS_UTF8
        // Print types that can be streamed into `std::owstringstream` but not into `std::ostringstream` when we use UTF-8 on Windows
        template<class T>
        inline typename meta::enableIf<meta::isStreamable<T, std::wostream>::value &&
            !meta::isStreamable<T, std::ostream>::value &&
            !meta::isConvertibleToWString<T>::value, void>::type operator<<(std::ostringstream& stream, const T& data)
        {
            std::wostringstream ss;
            ss << data;
            stream << ss.str();
        }
#   else
        // Print types that can be streamed into `std::ostringstream` but not into `std::owstringstream` when we use `wchar_t` on Windows
        template<class T>
        inline typename meta::enableIf<meta::isStreamable<T, std::ostream>::value &&
            !meta::isStreamable<T, std::wostream>::value &&
            !meta::isConvertibleToString<T>::value, void>::type operator<<(std::wostringstream& stream, const T& data)
        {
            std::ostringstream ss;
            ss << data;
            stream << ss.str();
        }
#   endif
#endif
    }

    class Record
    {
    public:
        Record(Severity severity, const char* func, size_t line, const char* file, const void* object, int instanceId)
            : m_severity(severity), m_tid(util::gettid()), m_object(object), m_line(line), m_func(func), m_file(file), m_instanceId(instanceId)
        {
            util::ftime(&m_time);
        }

        Record& ref()
        {
            return *this;
        }

        //////////////////////////////////////////////////////////////////////////
        // Stream output operators

        Record& operator<<(char data)
        {
            char str[] = { data, 0 };<--- Variable 'str' can be declared as const array
            return *this << str;
        }

#if PLOG_ENABLE_WCHAR_INPUT
        Record& operator<<(wchar_t data)
        {
            wchar_t str[] = { data, 0 };
            return *this << str;
        }
#endif

        Record& operator<<(util::nostream& (PLOG_CDECL *data)(util::nostream&))
        {
            m_message << data;
            return *this;
        }

#ifdef QT_VERSION
        Record& operator<<(const QString& data)
        {
#   if PLOG_CHAR_IS_UTF8
            return *this << data.toStdString();
#   else
            return *this << data.toStdWString();
#   endif
        }

#   if QT_VERSION < 0x060000
        Record& operator<<(const QStringRef& data)
        {
            return *this << data.toString();
        }
#   endif

#   ifdef QSTRINGVIEW_H
        Record& operator<<(QStringView data)
        {
            return *this << data.toString();
        }
#   endif
#endif

        template<typename T>
        Record& operator<<(const T& data)
        {
            using namespace plog::detail;

            m_message << data;
            return *this;
        }

#ifndef __cplusplus_cli
        Record& printf(const char* format, ...)<--- The function 'printf' is never used.
        {
            using namespace util;

            char* str = NULL;
            va_list ap;

            va_start(ap, format);
            int len = vasprintf(&str, format, ap);
            static_cast<void>(len);
            va_end(ap);

            *this << str;
            free(str);

            return *this;
        }

#ifdef _WIN32
        Record& printf(const wchar_t* format, ...)
        {
            using namespace util;

            wchar_t* str = NULL;
            va_list ap;

            va_start(ap, format);
            int len = vaswprintf(&str, format, ap);
            static_cast<void>(len);
            va_end(ap);

            *this << str;
            free(str);

            return *this;
        }
#endif
#endif //__cplusplus_cli

        //////////////////////////////////////////////////////////////////////////
        // Getters

        virtual const util::Time& getTime() const
        {
            return m_time;
        }

        virtual Severity getSeverity() const
        {
            return m_severity;
        }

        virtual unsigned int getTid() const
        {
            return m_tid;
        }

        virtual const void* getObject() const
        {
            return m_object;
        }

        virtual size_t getLine() const
        {
            return m_line;
        }

        virtual const util::nchar* getMessage() const
        {
            m_messageStr = m_message.str();
            return m_messageStr.c_str();
        }

        virtual const char* getFunc() const
        {
            m_funcStr = util::processFuncName(m_func);
            return m_funcStr.c_str();
        }

        virtual const char* getFile() const
        {
            return m_file;
        }

        virtual ~Record() // virtual destructor to satisfy -Wnon-virtual-dtor warning
        {
        }

        virtual int getInstanceId() const
        {
            return m_instanceId;
        }

    private:
        util::Time              m_time;
        const Severity          m_severity;
        const unsigned int      m_tid;
        const void* const       m_object;
        const size_t            m_line;
        util::nostringstream    m_message;
        const char* const       m_func;
        const char* const       m_file;
        const int               m_instanceId;
        mutable std::string     m_funcStr;
        mutable util::nstring   m_messageStr;
    };
}