1 /*****************************************************************************
2  *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
3  *                                                                           *
4  *   This program is free software; you can redistribute it and/or modify    *
5  *   it under the terms of the GNU Lesser General Public License as          *
6  *   published by the Free Software Foundation; either version 2.1 of the    *
7  *   License, or (at your option) version 3, or any later version accepted   *
8  *   by the membership of KDE e.V. (or its successor approved by the         *
9  *   membership of KDE e.V.), which shall act as a proxy defined in          *
10  *   Section 6 of version 3 of the license.                                  *
11  *                                                                           *
12  *   This program is distributed in the hope that it will be useful,         *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
15  *   Lesser General Public License for more details.                         *
16  *                                                                           *
17  *   You should have received a copy of the GNU Lesser General Public        *
18  *   License along with this library. If not,                                *
19  *   see <http://www.gnu.org/licenses/>.                                     *
20  *****************************************************************************/
21 
22 #ifndef _QTC_UTILS_LOG_H_
23 #define _QTC_UTILS_LOG_H_
24 
25 #include "utils.h"
26 #include <stdarg.h>
27 
28 namespace QtCurve {
29 
30 void backtrace();
31 enum class LogLevel {
32     Debug,
33     Info,
34     Warn,
35     Error,
36     Force
37 };
38 
39 namespace Log {
40 
41 LogLevel level();
42 
43 __attribute__((format(printf, 5, 0)))
44 void logv(LogLevel level, const char *fname, int line,
45           const char *func, const char *fmt, va_list ap);
46 
47 __attribute__((format(printf, 5, 6)))
48 void log(LogLevel level, const char *fname, int line, const char *func,
49          const char *fmt, ...);
50 
51 static inline bool
checkLevel(LogLevel _level)52 checkLevel(LogLevel _level)
53 {
54     return qtcUnlikely(_level <= LogLevel::Force && _level >= level());
55 }
56 
57 }
58 }
59 
60 #define qtcLog(__level, fmt, args...)                                   \
61     do {                                                                \
62         using namespace QtCurve;                                        \
63         LogLevel level = (__level);                                     \
64         if (!Log::checkLevel(level)) {                                  \
65             break;                                                      \
66         }                                                               \
67         Log::log(level, __FILE__, __LINE__, __FUNCTION__, fmt, ##args); \
68     } while (0)
69 
70 #define qtcDebug(fmt, args...) qtcLog(LogLevel::Debug, fmt, ##args)
71 #define qtcInfo(fmt, args...) qtcLog(LogLevel::Info, fmt, ##args)
72 #define qtcWarn(fmt, args...) qtcLog(LogLevel::Warn, fmt, ##args)
73 #define qtcError(fmt, args...) qtcLog(LogLevel::Error, fmt, ##args)
74 #define qtcForceLog(fmt, args...) qtcLog(LogLevel::Force, fmt, ##args)
75 
76 #endif
77