1 /*
2  *  This file is part of GNOME Twitch - 'Enjoy Twitch on your GNU/Linux desktop'
3  *  Copyright © 2017 Vincent Szolnoky <vinszent@vinszent.com>
4  *
5  *  GNOME Twitch is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  GNOME Twitch is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with GNOME Twitch. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef _GT_LOG_H
20 #define _GT_LOG_H
21 
22 #include <glib.h>
23 
24 typedef enum
25 {
26     // Report most mundane info like http responses, irc responses, etc.
27     // Generally reporting raw data.
28     GT_LOG_LEVEL_TRACE    = 1 << (G_LOG_LEVEL_USER_SHIFT + 6),
29     // 'Spammy' info like cache hits/misses, interesting object references, http requests
30     GT_LOG_LEVEL_DEBUG    = 1 << (G_LOG_LEVEL_USER_SHIFT + 5),
31     // Less 'spammy' info but not useful to user.
32     // Init of stuff, finalisation of stuff, etc.
33     GT_LOG_LEVEL_INFO     = 1 << (G_LOG_LEVEL_USER_SHIFT + 4),
34     // Info that the end user might find useful. Things that tend to only happen
35     // a handful of times. Setting signing in, favouriting channels, etc.
36     GT_LOG_LEVEL_MESSAGE  = 1 << (G_LOG_LEVEL_USER_SHIFT + 3),
37     // Warning mainly to alert devs of potential errors but GT can still
38     // function like normal. Failed http requests, opening of settings files, etc.
39     // Can show error to user.
40     GT_LOG_LEVEL_WARNING  = 1 << (G_LOG_LEVEL_USER_SHIFT + 2),
41     // Like warning but more critical. Should definitely show error to user.
42     // GT might not function like normal, but will still run.
43     GT_LOG_LEVEL_CRITICAL = 1 << (G_LOG_LEVEL_USER_SHIFT + 1),
44     // Boom, crash.
45     GT_LOG_LEVEL_ERROR    = 1 << G_LOG_LEVEL_USER_SHIFT,
46 } GtLogLevelFlags;
47 
48 #ifndef TAG
49 #error Tag not defined
50 #else
51 #define LOG(lvl, msg, ...) g_log(NULL, (GLogLevelFlags) lvl, "{%s:%d %s} %s", TAG, __LINE__, __FUNCTION__, msg, ##__VA_ARGS__)
52 #define LOGF(lvl, fmt, ...) g_log(NULL, (GLogLevelFlags) lvl, "{%s:%d %s} " fmt, TAG, __LINE__, __FUNCTION__, ##__VA_ARGS__)
53 #define FATAL(msg, ...) LOGF(GT_LOG_LEVEL_WARNING, msg, ##__VA_ARGS__)
54 #define FATALF(fmt, ...) LOGF(GT_LOG_LEVEL_WARNING, fmt, __VA_ARGS__)
55 #define ERROR(msg, ...) LOGF(GT_LOG_LEVEL_ERROR, msg, ##__VA_ARGS__)
56 #define ERRORF(fmt, ...) LOGF(GT_LOG_LEVEL_ERROR, fmt, __VA_ARGS__)
57 #define CRITICAL(msg, ...) LOGF(GT_LOG_LEVEL_CRITICAL, msg, ##__VA_ARGS__)
58 #define CRITICALF(fmt, ...) LOGF(GT_LOG_LEVEL_CRITICAL, fmt, __VA_ARGS__)
59 #define WARNING(msg, ...) LOGF(GT_LOG_LEVEL_WARNING, msg, ##__VA_ARGS__)
60 #define WARNINGF(fmt, ...) LOGF(GT_LOG_LEVEL_WARNING, fmt, __VA_ARGS__)
61 #define MESSAGE(msg, ...) LOGF(GT_LOG_LEVEL_MESSAGE, msg, ##__VA_ARGS__)
62 #define MESSAGEF(fmt, ...) LOGF(GT_LOG_LEVEL_MESSAGE, fmt, __VA_ARGS__)
63 #define INFO(msg, ...) LOGF(GT_LOG_LEVEL_INFO, msg, ##__VA_ARGS__)
64 #define INFOF(fmt, ...) LOGF(GT_LOG_LEVEL_INFO, fmt, __VA_ARGS__)
65 #define DEBUG(msg, ...) LOGF(GT_LOG_LEVEL_DEBUG, msg, ##__VA_ARGS__)
66 #define DEBUGF(fmt, ...) LOGF(GT_LOG_LEVEL_DEBUG, fmt, __VA_ARGS__)
67 #define TRACE(msg, ...) LOGF(GT_LOG_LEVEL_TRACE, msg, ##__VA_ARGS__)
68 #define TRACEF(fmt, ...) LOGF(GT_LOG_LEVEL_TRACE, fmt, __VA_ARGS__)
69 
70 
71 #define RETURN_IF_FAIL(expr)                                    \
72     G_STMT_START                                                \
73     {                                                           \
74         if (!(expr))                                            \
75         {                                                       \
76             CRITICAL("Expression '%s' should be TRUE", #expr);  \
77             return;                                             \
78         }                                                       \
79     } G_STMT_END
80 #define RETURN_VAL_IF_FAIL(expr, val)                           \
81     G_STMT_START                                                \
82     {                                                           \
83         if (!(expr))                                            \
84         {                                                       \
85             CRITICAL("Expression '%s' should be TRUE", #expr);  \
86             return val;                                         \
87         }                                                       \
88     } G_STMT_END
89 #define RETURN_IF_REACHED()                                     \
90     G_STMT_START                                                \
91     {                                                           \
92         {                                                       \
93             CRITICAL("This expression should not be reached");  \
94             return;                                             \
95         }                                                       \
96     } G_STMT_END
97 #define RETURN_VAL_IF_REACHED(val)                              \
98     G_STMT_START                                                \
99     {                                                           \
100         {                                                       \
101             CRITICAL("This expression should not be reached");  \
102             return val;                                         \
103         }                                                       \
104     } G_STMT_END
105 #define RETURN_IF_ERROR(err)                                            \
106     G_STMT_START                                                        \
107     {                                                                   \
108         if (err)                                                        \
109         {                                                               \
110             CRITICAL("Error should be NULL, message was '%s'", err->message); \
111             return;                                                     \
112         }                                                               \
113     } G_STMT_END
114 #endif
115 
116 #endif
117