xlogger.h 24.3 KB
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
// Tencent is pleased to support the open source community by making Mars available.
// Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.

// Licensed under the MIT License (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://opensource.org/licenses/MIT

// Unless required by applicable law or agreed to in writing, software distributed under the License is
// distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions and
// limitations under the License.

/*
 ============================================================================
 Name		: xlogger.h
 ============================================================================
 */

#ifndef XLOGGER_H_
#define XLOGGER_H_

#include <assert.h>
#include <stdlib.h>
#include <sys/cdefs.h>
#include <sys/time.h>

#include "mars/comm/string_cast.h"
#include "preprocessor.h"
#include "xloggerbase.h"

#ifdef XLOGGER_DISABLE
#define xlogger_IsEnabledFor(_level) (false)
#define xlogger_AssertP(...) ((void)0)
#define xlogger_Assert(...) ((void)0)
#define xlogger_VPrint(...) ((void)0)
#define xlogger_Print(...) ((void)0)
#define xlogger_Write(...) ((void)0)
#endif

#ifdef __cplusplus
#include <string>

template <bool x>
struct XLOGGER_STATIC_ASSERTION_FAILURE;
template <>
struct XLOGGER_STATIC_ASSERTION_FAILURE<true> {
    enum { value = 1 };
};
template <int x>
struct xlogger_static_assert_test {};

#define XLOGGER_STATIC_ASSERT(...)                                                \
    typedef ::xlogger_static_assert_test<sizeof(                                  \
        ::XLOGGER_STATIC_ASSERTION_FAILURE<((__VA_ARGS__) == 0 ? false : true)>)> \
        PP_CAT(boost_static_assert_typedef_, __LINE__)

#ifdef __APPLE__
using TypeSafeFormat = void*;
#else
const struct TypeSafeFormat {
    TypeSafeFormat() {
    }
} __tsf__;
#endif
const struct XLoggerTag {
    XLoggerTag() {
    }
} __xlogger_tag__;
const struct XLoggerInfoNull {
    XLoggerInfoNull() {
    }
} __xlogger_info_null__;

class XMessage {
 public:
    XMessage() : m_message() {
        m_message.reserve(512);
    }
    XMessage(std::string& _holder) : m_message(_holder) {
    }
    ~XMessage() {
    }

 public:
    const std::string& Message() const {
        return m_message;
    }
    std::string& Message() {
        return m_message;
    }

    const std::string& String() const {
        return m_message;
    }
    std::string& String() {
        return m_message;
    }

#ifdef __GNUC__
    __attribute__((__format__(printf, 2, 0)))
#endif
    XMessage&
    WriteNoFormat(const char* _log) {
        m_message += _log;
        return *this;
    }
#ifdef __GNUC__
    __attribute__((__format__(printf, 3, 0)))
#endif
    XMessage&
    WriteNoFormat(const TypeSafeFormat&, const char* _log) {
        m_message += _log;
        return *this;
    }

    XMessage& operator<<(const string_cast& _value);
    XMessage& operator>>(const string_cast& _value);

    XMessage& operator()() {
        return *this;
    }
    void operator+=(const string_cast& _value) {
        m_message += _value.str();
    }
#ifdef __GNUC__
    __attribute__((__format__(printf, 2, 3)))
#endif
    XMessage&
    operator()(const char* _format, ...);

#ifdef __GNUC__
    __attribute__((__format__(printf, 2, 0)))
#endif
    XMessage&
    VPrintf(const char* _format, va_list _list);

#define XLOGGER_FORMAT_ARGS(n) PP_ENUM_TRAILING_PARAMS(n, const string_cast& a)
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(0));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(1));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(2));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(3));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(4));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(5));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(6));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(7));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(8));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(9));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(10));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(11));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(12));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(13));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(14));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(15));
    XMessage& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(16));
#undef XLOGGER_FORMAT_ARGS

 private:
    void DoTypeSafeFormat(const char* _format, const string_cast** _args);

 private:
    //	  XMessage(const XMessage&);
    //	  XMessage& operator=(const XMessage&);

 private:
    std::string m_message;
};

class XLogger {
 public:
    XLogger(TLogLevel _level,
            const char* _tag,
            const char* _file,
            const char* _func,
            int _line,
            bool _trace = false,
            bool (*_hook)(XLoggerInfo& _info, std::string& _log) = NULL);
    ~XLogger();

 public:
    XLogger& Assert(const char* _exp);

    bool Empty() const {
        return !m_isassert && m_message.empty();
    }
    const std::string& Message() const {
        return m_message;
    }
    void ForwardToSysTrace() {
        m_info.traceLog = 1;
    }
    void Clear() {
        m_message.clear();
    }

#ifdef __GNUC__
    __attribute__((__format__(printf, 2, 0)))
#endif
    XLogger&
    WriteNoFormat(const char* _log) {
        m_message += _log;
        return *this;
    }
#ifdef __GNUC__
    __attribute__((__format__(printf, 3, 0)))
#endif
    XLogger&
    WriteNoFormat(const TypeSafeFormat&, const char* _log) {
        m_message += _log;
        return *this;
    }

    XLogger& operator<<(const string_cast& _value);
    XLogger& operator>>(const string_cast& _value);

    void operator>>(XLogger& _xlogger);

    void operator<<(XLogger& _xlogger);

    XLogger& operator()() {
        return *this;
    }
    XLogger& operator()(const XLoggerInfoNull&) {
        m_isinfonull = true;
        return *this;
    }
    XLogger& operator()(const XLoggerTag&, const char* _tag) {
        m_info.tag = _tag;
        return *this;
    }
#ifdef __GNUC__
    __attribute__((__format__(printf, 2, 3)))
#endif
    XLogger&
    operator()(const char* _format, ...);

#ifdef __GNUC__
    __attribute__((__format__(printf, 2, 0)))
#endif
    XLogger&
    VPrintf(const char* _format, va_list _list);

#define XLOGGER_FORMAT_ARGS(n) PP_ENUM_TRAILING_PARAMS(n, const string_cast& a)
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(0));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(1));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(2));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(3));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(4));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(5));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(6));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(7));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(8));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(9));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(10));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(11));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(12));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(13));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(14));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(15));
    XLogger& operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(16));
#undef XLOGGER_FORMAT_ARGS

 private:
    void DoTypeSafeFormat(const char* _format, const string_cast** _args);

 private:
    XLogger(const XLogger&);
    XLogger& operator=(const XLogger&);

 private:
    XLoggerInfo m_info = XLOGGER_INFO_INITIALIZER;
    std::string m_message;
    bool m_isassert;
    const char* m_exp;
    bool (*m_hook)(XLoggerInfo& _info, std::string& _log);
    bool m_isinfonull;
};

class XScopeTracer {
 public:
    XScopeTracer(TLogLevel _level,
                 const char* _tag,
                 const char* _name,
                 const char* _file,
                 const char* _func,
                 int _line,
                 const char* _log);

    ~XScopeTracer();

    void Exit(const std::string& _exitmsg) {
        m_exitmsg += _exitmsg;
    }

 private:
    XScopeTracer(const XScopeTracer&);
    XScopeTracer& operator=(const XScopeTracer&);

 private:
    bool m_enable;
    XLoggerInfo m_info = XLOGGER_INFO_INITIALIZER;
    char m_name[128];
    timeval m_tv;

    std::string m_exitmsg;
};

#define XLOGGER_FORMAT_ARGS(n) PP_ENUM_TRAILING_PARAMS(n, const string_cast& a)
#define XLOGGER_VARIANT_ARGS(n) PP_ENUM_PARAMS(n, &a)
#define XLOGGER_VARIANT_ARGS_NULL(n) PP_ENUM(n, NULL)
#define XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(n, m)                                                                \
    inline XMessage& XMessage::operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(n)) { \
        if (_format != NULL) {                                                                                 \
            const string_cast* args[16] = {XLOGGER_VARIANT_ARGS(n) PP_COMMA_IF(PP_AND(n, m))                   \
                                               XLOGGER_VARIANT_ARGS_NULL(m)};                                  \
            DoTypeSafeFormat(_format, args);                                                                   \
        }                                                                                                      \
        return *this;                                                                                          \
    }

XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(0, 16)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(1, 15)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(2, 14)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(3, 13)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(4, 12)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(5, 11)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(6, 10)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(7, 9)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(8, 8)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(9, 7)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(10, 6)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(11, 5)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(12, 4)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(13, 3)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(14, 2)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(15, 1)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(16, 0)

#undef XLOGGER_FORMAT_ARGS
#undef XLOGGER_VARIANT_ARGS
#undef XLOGGER_VARIANT_ARGS_NULL
#undef XLOGGER_TYPESAFE_FORMAT_IMPLEMENT

#define XLOGGER_FORMAT_ARGS(n) PP_ENUM_TRAILING_PARAMS(n, const string_cast& a)
#define XLOGGER_VARIANT_ARGS(n) PP_ENUM_PARAMS(n, &a)
#define XLOGGER_VARIANT_ARGS_NULL(n) PP_ENUM(n, NULL)
#define XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(n, m)                                                              \
    inline XLogger& XLogger::operator()(const TypeSafeFormat&, const char* _format XLOGGER_FORMAT_ARGS(n)) { \
        if (_format != NULL) {                                                                               \
            const string_cast* args[16] = {XLOGGER_VARIANT_ARGS(n) PP_COMMA_IF(PP_AND(n, m))                 \
                                               XLOGGER_VARIANT_ARGS_NULL(m)};                                \
            DoTypeSafeFormat(_format, args);                                                                 \
        }                                                                                                    \
        return *this;                                                                                        \
    }

XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(0, 16)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(1, 15)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(2, 14)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(3, 13)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(4, 12)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(5, 11)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(6, 10)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(7, 9)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(8, 8)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(9, 7)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(10, 6)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(11, 5)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(12, 4)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(13, 3)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(14, 2)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(15, 1)
XLOGGER_TYPESAFE_FORMAT_IMPLEMENT(16, 0)

#undef XLOGGER_FORMAT_ARGS
#undef XLOGGER_VARIANT_ARGS
#undef XLOGGER_VARIANT_ARGS_NULL
#undef XLOGGER_TYPESAFE_FORMAT_IMPLEMENT

#endif  // cpp

#define __CONCAT_IMPL__(x, y) x##y
#define __CONCAT__(x, y) __CONCAT_IMPL__(x, y)
#define __ANONYMOUS_VARIABLE__(x) __CONCAT__(x, __LINE__)

#define __XFILE__ (__FILE__)

#ifndef _MSC_VER
//#define __XFUNCTION__		  __PRETTY_FUNCTION__
#define __XFUNCTION__ __FUNCTION__
#else
// Definitely, VC6 not support this feature!
#if _MSC_VER > 1200
//#define __XFUNCTION__	__FUNCSIG__
#define __XFUNCTION__ __FUNCTION__
#else
#define __XFUNCTION__ "N/A"
#warning " is not supported by this compiler"
#endif
#endif

// xlogger define

#ifndef XLOGGER_TAG
#define XLOGGER_TAG ""
#endif

/* tips: this code replace or change the tag in source file
static const char* __my_xlogger_tag = "prefix_"XLOGGER_TAG"_suffix";
#undef XLOGGER_TAG
#define XLOGGER_TAG __my_xlogger_tag
*/

#define xdump xlogger_dump
#define XLOGGER_ROUTER_OUTPUT(op1, op, ...) \
    PP_IF(PP_NUM_PARAMS(__VA_ARGS__), PP_IF(PP_DEC(PP_NUM_PARAMS(__VA_ARGS__)), op, op1), )

#if !defined(__cplusplus)

#ifdef __GNUC__
__attribute__((__format__(printf, 2, 3)))
#endif
__inline void
__xlogger_c_write(const XLoggerInfo* _info, const char* _log, ...) {
    xlogger_Write(_info, _log);
}

#define xlogger2(level, tag, file, func, line, ...)                                                                   \
    if ((!xlogger_IsEnabledFor(level)))                                                                               \
        ;                                                                                                             \
    else {                                                                                                            \
        XLoggerInfo info = {level, tag, file, func, line, {0, 0}, -1, -1, -1, false};                                 \
        \ gettimeofday(&info.m_tv, NULL);                                                                             \
        XLOGGER_ROUTER_OUTPUT(__xlogger_c_write(&info, __VA_ARGS__), xlogger_Print(&info, __VA_ARGS__), __VA_ARGS__); \
    }

#define xlogger2_if(exp, level, tag, file, func, line, ...)                                                           \
    if (!(exp) || !xlogger_IsEnabledFor(level))                                                                       \
        ;                                                                                                             \
    else {                                                                                                            \
        XLoggerInfo info = {level, tag, file, func, line, {0, 0}, -1, -1, -1, false};                                 \
        gettimeofday(&info.timeval, NULL);                                                                            \
        XLOGGER_ROUTER_OUTPUT(__xlogger_c_write(&info, __VA_ARGS__), xlogger_Print(&info, __VA_ARGS__), __VA_ARGS__); \
    }

#define __xlogger_c_impl(level, ...) xlogger2(level, XLOGGER_TAG, __XFILE__, __XFUNCTION__, __LINE__, __VA_ARGS__)
#define __xlogger_c_impl_if(level, exp, ...) \
    xlogger2_if(exp, level, XLOGGER_TAG, __XFILE__, __XFUNCTION__, __LINE__, __VA_ARGS__)

#define xverbose2(...) __xlogger_c_impl(kLevelVerbose, __VA_ARGS__)
#define xdebug2(...) __xlogger_c_impl(kLevelDebug, __VA_ARGS__)
#define xinfo2(...) __xlogger_c_impl(kLevelInfo, __VA_ARGS__)
#define xwarn2(...) __xlogger_c_impl(kLevelWarn, __VA_ARGS__)
#define xerror2(...) __xlogger_c_impl(kLevelError, __VA_ARGS__)
#define xfatal2(...) __xlogger_c_impl(kLevelFatal, __VA_ARGS__)

#define xverbose2_if(exp, ...) __xlogger_c_impl_if(kLevelVerbose, exp, __VA_ARGS__)
#define xdebug2_if(exp, ...) __xlogger_c_impl_if(kLevelDebug, exp, __VA_ARGS__)
#define xinfo2_if(exp, ...) __xlogger_c_impl_if(kLevelInfo, exp, __VA_ARGS__)
#define xwarn2_if(exp, ...) __xlogger_c_impl_if(kLevelWarn, exp, __VA_ARGS__)
#define xerror2_if(exp, ...) __xlogger_c_impl_if(kLevelError, exp, __VA_ARGS__)
#define xfatal2_if(exp, ...) __xlogger_c_impl_if(kLevelFatal, exp, __VA_ARGS__)

#define xassert2(exp, ...)                                                                                            \
    if (((exp) || !xlogger_IsEnabledFor(kLevelFatal)))                                                                \
        ;                                                                                                             \
    else {                                                                                                            \
        XLoggerInfo info = {kLevelFatal, XLOGGER_TAG, __XFILE__, __XFUNCTION__, __LINE__, {0, 0}, -1, -1, -1, false}; \
        gettimeofday(&info.m_tv, NULL);                                                                               \
        xlogger_AssertP(&info, #exp, __VA_ARGS__);                                                                    \
    }
//"##__VA_ARGS__" remove "," if NULL
#else

#ifndef XLOGGER_HOOK
#define XLOGGER_HOOK NULL
#endif

#define xlogger(level, tag, file, func, line, ...)                 \
    if ((!xlogger_IsEnabledFor(level)))                            \
        ;                                                          \
    else                                                           \
        XLogger(level, tag, file, func, line, false, XLOGGER_HOOK) \
            XLOGGER_ROUTER_OUTPUT(.WriteNoFormat(TSF __VA_ARGS__), (TSF __VA_ARGS__), __VA_ARGS__)

#define xlogger2(level, tag, file, func, line, ...)                \
    if ((!xlogger_IsEnabledFor(level)))                            \
        ;                                                          \
    else                                                           \
        XLogger(level, tag, file, func, line, false, XLOGGER_HOOK) \
            XLOGGER_ROUTER_OUTPUT(.WriteNoFormat(__VA_ARGS__), (__VA_ARGS__), __VA_ARGS__)

#define xlogger2_if(exp, level, tag, file, func, line, ...)        \
    if ((!(exp) || !xlogger_IsEnabledFor(level)))                  \
        ;                                                          \
    else                                                           \
        XLogger(level, tag, file, func, line, false, XLOGGER_HOOK) \
            XLOGGER_ROUTER_OUTPUT(.WriteNoFormat(__VA_ARGS__), (__VA_ARGS__), __VA_ARGS__)

#define xlogger_trace(level, tag, file, func, line, ...)          \
    if ((!xlogger_IsEnabledFor(level)))                           \
        ;                                                         \
    else                                                          \
        XLogger(level, tag, file, func, line, true, XLOGGER_HOOK) \
            XLOGGER_ROUTER_OUTPUT(.WriteNoFormat(__VA_ARGS__), (__VA_ARGS__), __VA_ARGS__)

#define __xlogger_cpp_impl2(level, ...) xlogger2(level, XLOGGER_TAG, __XFILE__, __XFUNCTION__, __LINE__, __VA_ARGS__)
#define __xlogger_cpp_impl_if(level, exp, ...) \
    xlogger2_if(exp, level, XLOGGER_TAG, __XFILE__, __XFUNCTION__, __LINE__, __VA_ARGS__)
#define __xlogger_cpp_impl_trace(level, ...) \
    xlogger_trace(level, XLOGGER_TAG, __XFILE__, __XFUNCTION__, __LINE__, __VA_ARGS__)

#define xverbose2(...) __xlogger_cpp_impl2(kLevelVerbose, __VA_ARGS__)
#define xdebug2(...) __xlogger_cpp_impl2(kLevelDebug, __VA_ARGS__)
#define xinfo2(...) __xlogger_cpp_impl2(kLevelInfo, __VA_ARGS__)
#define xwarn2(...) __xlogger_cpp_impl2(kLevelWarn, __VA_ARGS__)
#define xerror2(...) __xlogger_cpp_impl2(kLevelError, __VA_ARGS__)
#define xfatal2(...) __xlogger_cpp_impl2(kLevelFatal, __VA_ARGS__)
#define xlog2(level, ...) __xlogger_cpp_impl2(level, __VA_ARGS__)

#define xverbose2_if(exp, ...) __xlogger_cpp_impl_if(kLevelVerbose, exp, __VA_ARGS__)
#define xdebug2_if(exp, ...) __xlogger_cpp_impl_if(kLevelDebug, exp, __VA_ARGS__)
#define xinfo2_if(exp, ...) __xlogger_cpp_impl_if(kLevelInfo, exp, __VA_ARGS__)
#define xwarn2_if(exp, ...) __xlogger_cpp_impl_if(kLevelWarn, exp, __VA_ARGS__)
#define xerror2_if(exp, ...) __xlogger_cpp_impl_if(kLevelError, exp, __VA_ARGS__)
#define xfatal2_if(exp, ...) __xlogger_cpp_impl_if(kLevelFatal, exp, __VA_ARGS__)
#define xlog2_if(level, ...) __xlogger_cpp_impl_if(level, __VA_ARGS__)

#define xverbose_trace(...) __xlogger_cpp_impl_trace(kLevelVerbose, __VA_ARGS__)
#define xdebug_trace(...) __xlogger_cpp_impl_trace(kLevelDebug, __VA_ARGS__)
#define xinfo_trace(...) __xlogger_cpp_impl_trace(kLevelInfo, __VA_ARGS__)
#define xwarn_trace(...) __xlogger_cpp_impl_trace(kLevelWarn, __VA_ARGS__)
#define xerror_trace(...) __xlogger_cpp_impl_trace(kLevelError, __VA_ARGS__)
#define xfatal_trace(...) __xlogger_cpp_impl_trace(kLevelFatal, __VA_ARGS__)

#define xgroup2_define(group) XLogger group(kLevelAll, XLOGGER_TAG, __XFILE__, __XFUNCTION__, __LINE__, XLOGGER_HOOK)
#define xgroup2(...) \
    XLogger(kLevelAll, XLOGGER_TAG, __XFILE__, __XFUNCTION__, __LINE__, false, XLOGGER_HOOK)(__VA_ARGS__)
#define xgroup2_if(exp, ...) \
    if ((!(exp)))            \
        ;                    \
    else                     \
        XLogger(kLevelAll, XLOGGER_TAG, __XFILE__, __XFUNCTION__, __LINE__, false, XLOGGER_HOOK)(__VA_ARGS__)

#define xassert2(exp, ...)                                                                         \
    if (((exp) || !xlogger_IsEnabledFor(kLevelFatal)))                                             \
        ;                                                                                          \
    else                                                                                           \
        XLogger(kLevelFatal, XLOGGER_TAG, __XFILE__, __XFUNCTION__, __LINE__, false, XLOGGER_HOOK) \
            .Assert(#exp) XLOGGER_ROUTER_OUTPUT(.WriteNoFormat(__VA_ARGS__), (__VA_ARGS__), __VA_ARGS__)

#define xmessage2_define(name, ...) \
    XMessage name;                  \
    name XLOGGER_ROUTER_OUTPUT(.WriteNoFormat(__VA_ARGS__), (__VA_ARGS__), __VA_ARGS__)
#define xmessage2(...) XMessage() XLOGGER_ROUTER_OUTPUT(.WriteNoFormat(__VA_ARGS__), (__VA_ARGS__), __VA_ARGS__)

#define XLOGGER_SCOPE_MESSAGE(...) PP_IF(PP_NUM_PARAMS(__VA_ARGS__), xmessage2(__VA_ARGS__).String().c_str(), NULL)
#define __xscope_impl(level, name, ...)  \
    XScopeTracer __ANONYMOUS_VARIABLE__( \
        _tracer_)(level, XLOGGER_TAG, name, __XFILE__, __XFUNCTION__, __LINE__, XLOGGER_SCOPE_MESSAGE(__VA_ARGS__))

#define xverbose_scope(name, ...) __xscope_impl(kLevelVerbose, name, __VA_ARGS__)
#define xdebug_scope(name, ...) __xscope_impl(kLevelDebug, name, __VA_ARGS__)
#define xinfo_scope(name, ...) __xscope_impl(kLevelInfo, name, __VA_ARGS__)

#define __xfunction_scope_impl(level, name, ...) \
    XScopeTracer __ANONYMOUS_VARIABLE__(         \
        _xfunction_)(level, XLOGGER_TAG, name, __XFILE__, __XFUNCTION__, __LINE__, XLOGGER_SCOPE_MESSAGE(__VA_ARGS__))

#define xverbose_function(...) __xfunction_scope_impl(kLevelVerbose, __FUNCTION__, __VA_ARGS__)
#define xdebug_function(...) __xfunction_scope_impl(kLevelDebug, __FUNCTION__, __VA_ARGS__)
#define xinfo_function(...) __xfunction_scope_impl(kLevelInfo, __FUNCTION__, __VA_ARGS__)

#ifdef __APPLE__
#define TSF __builtin_return_address(0),
#else
#define TSF __tsf__,
#endif
#define XTAG __xlogger_tag__,
#define XNULL __xlogger_info_null__
#define XENDL "\n"
#define XTHIS "@%p, ", this

#endif
#endif /* XLOGGER_H_ */