1 /*
2  * This file Copyright (C) 2010-2014 Mnemosyne LLC
3  *
4  * It may be used under the GNU GPL versions 2 or 3
5  * or any future license endorsed by Mnemosyne LLC.
6  *
7  */
8 
9 #pragma once
10 
11 #include <stddef.h> /* size_t */
12 
13 #include "file.h" /* tr_sys_file_t */
14 #include "utils.h" /* TR_GNUC_PRINTF, TR_GNUC_NONNULL */
15 
16 #ifdef __cplusplus
17 extern "C"
18 {
19 #endif
20 
21 #define TR_LOG_MAX_QUEUE_LENGTH 10000
22 
23 tr_log_level tr_logGetLevel(void);
24 
tr_logLevelIsActive(tr_log_level level)25 static inline bool tr_logLevelIsActive(tr_log_level level)
26 {
27     return tr_logGetLevel() >= level;
28 }
29 
30 void tr_logAddMessage(char const* file, int line, tr_log_level level, char const* torrent, char const* fmt, ...) \
31     TR_GNUC_PRINTF(5, 6);
32 
33 #define tr_logAddNamed(level, name, ...) \
34     do \
35     { \
36         if (tr_logLevelIsActive(level)) \
37         { \
38             tr_logAddMessage(__FILE__, __LINE__, level, name, __VA_ARGS__); \
39         } \
40     } \
41     while (0)
42 
43 #define tr_logAddNamedError(name, ...) tr_logAddNamed(TR_LOG_ERROR, name, __VA_ARGS__)
44 #define tr_logAddNamedInfo(name, ...) tr_logAddNamed(TR_LOG_INFO, name, __VA_ARGS__)
45 #define tr_logAddNamedDbg(name, ...) tr_logAddNamed(TR_LOG_DEBUG, name, __VA_ARGS__)
46 
47 #define tr_logAddTor(level, tor, ...) tr_logAddNamed(level, tr_torrentName(tor), __VA_ARGS__)
48 
49 #define tr_logAddTorErr(tor, ...) tr_logAddTor(TR_LOG_ERROR, tor, __VA_ARGS__)
50 #define tr_logAddTorInfo(tor, ...) tr_logAddTor(TR_LOG_INFO, tor, __VA_ARGS__)
51 #define tr_logAddTorDbg(tor, ...) tr_logAddTor(TR_LOG_DEBUG, tor, __VA_ARGS__)
52 
53 #define tr_logAdd(level, ...) tr_logAddNamed(level, NULL, __VA_ARGS__)
54 
55 #define tr_logAddError(...) tr_logAdd(TR_LOG_ERROR, __VA_ARGS__)
56 #define tr_logAddInfo(...) tr_logAdd(TR_LOG_INFO, __VA_ARGS__)
57 #define tr_logAddDebug(...) tr_logAdd(TR_LOG_DEBUG, __VA_ARGS__)
58 
59 tr_sys_file_t tr_logGetFile(void);
60 
61 /** @brief return true if deep logging has been enabled by the user; false otherwise */
62 bool tr_logGetDeepEnabled(void);
63 
64 void tr_logAddDeep(char const* file, int line, char const* name, char const* fmt, ...) TR_GNUC_PRINTF(4, 5) \
65     TR_GNUC_NONNULL(1, 4);
66 
67 #define tr_logAddDeepNamed(name, ...) \
68     do \
69     { \
70         if (tr_logGetDeepEnabled()) \
71         { \
72             tr_logAddDeep(__FILE__, __LINE__, name, __VA_ARGS__); \
73         } \
74     } \
75     while (0)
76 
77 /** @brief set the buffer with the current time formatted for deep logging. */
78 char* tr_logGetTimeStr(char* buf, size_t buflen) TR_GNUC_NONNULL(1);
79 
80 #ifdef __cplusplus
81 }
82 #endif
83 
84 /** @} */
85