1 /*
2  * twemproxy - A fast and lightweight proxy for memcached protocol.
3  * Copyright (C) 2011 Twitter, Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _NC_LOG_H_
19 #define _NC_LOG_H_
20 
21 #include <nc_util.h>
22 
23 struct logger {
24     const char *name; /* log file name */
25     int  level;       /* log level */
26     int  fd;          /* log file descriptor */
27     int  nerror;      /* # log error */
28 };
29 
30 #define LOG_EMERG   0   /* system in unusable */
31 #define LOG_ALERT   1   /* action must be taken immediately */
32 #define LOG_CRIT    2   /* critical conditions */
33 #define LOG_ERR     3   /* error conditions */
34 #define LOG_WARN    4   /* warning conditions */
35 #define LOG_NOTICE  5   /* normal but significant condition (default) */
36 #define LOG_INFO    6   /* informational */
37 #define LOG_DEBUG   7   /* debug messages */
38 #define LOG_VERB    8   /* verbose messages */
39 #define LOG_VVERB   9   /* verbose messages on crack */
40 #define LOG_VVVERB  10  /* verbose messages on ganga */
41 #define LOG_PVERB   11  /* periodic verbose messages on crack */
42 
43 #define LOG_MAX_LEN 256 /* max length of log message */
44 
45 /*
46  * log_stderr   - log to stderr
47  * loga         - log always
48  * loga_hexdump - log hexdump always
49  * log_error    - error log messages
50  * log_warn     - warning log messages
51  * log_panic    - log messages followed by a panic
52  * ...
53  * log_debug    - debug log messages based on a log level
54  * log_hexdump  - hexadump -C of a log buffer
55  */
56 #ifdef NC_DEBUG_LOG
57 
58 #define log_debug(_level, ...) do {                                         \
59     if (log_loggable(_level) != 0) {                                        \
60         _log(__FILE__, __LINE__, 0, __VA_ARGS__);                           \
61     }                                                                       \
62 } while (0)
63 
64 #define log_hexdump(_level, _data, _datalen, ...) do {                      \
65     if (log_loggable(_level) != 0) {                                        \
66         _log(__FILE__, __LINE__, 0, __VA_ARGS__);                           \
67         _log_hexdump(__FILE__, __LINE__, (char *)(_data), (int)(_datalen),  \
68                      __VA_ARGS__);                                          \
69     }                                                                       \
70 } while (0)
71 
72 #else
73 
74 #define log_debug(_level, ...)
75 #define log_hexdump(_level, _data, _datalen, ...)
76 
77 #endif
78 
79 #define log_stderr(...) do {                                                \
80     _log_stderr(__VA_ARGS__);                                               \
81 } while (0)
82 
83 #define log_safe(...) do {                                                  \
84     _log_safe(__VA_ARGS__);                                                 \
85 } while (0)
86 
87 #define log_stderr_safe(...) do {                                           \
88     _log_stderr_safe(__VA_ARGS__);                                          \
89 } while (0)
90 
91 #define loga(...) do {                                                      \
92     _log(__FILE__, __LINE__, 0, __VA_ARGS__);                               \
93 } while (0)
94 
95 #define loga_hexdump(_data, _datalen, ...) do {                             \
96     _log(__FILE__, __LINE__, 0, __VA_ARGS__);                               \
97     _log_hexdump(__FILE__, __LINE__, (char *)(_data), (int)(_datalen),      \
98                  __VA_ARGS__);                                              \
99 } while (0)                                                                 \
100 
101 #define log_error(...) do {                                                 \
102     if (log_loggable(LOG_ALERT) != 0) {                                     \
103         _log(__FILE__, __LINE__, 0, __VA_ARGS__);                           \
104     }                                                                       \
105 } while (0)
106 
107 #define log_warn(...) do {                                                  \
108     if (log_loggable(LOG_WARN) != 0) {                                      \
109         _log(__FILE__, __LINE__, 0, __VA_ARGS__);                           \
110     }                                                                       \
111 } while (0)
112 
113 #define log_panic(...) do {                                                 \
114     if (log_loggable(LOG_EMERG) != 0) {                                     \
115         _log(__FILE__, __LINE__, 1, __VA_ARGS__);                           \
116     }                                                                       \
117 } while (0)
118 
119 
120 int log_init(int level, const char *filename);
121 void log_deinit(void);
122 void log_level_up(void);
123 void log_level_down(void);
124 void log_level_set(int level);
125 void log_stacktrace(void);
126 void log_reopen(void);
127 int log_loggable(int level);
128 void _log(const char *file, int line, int panic, const char *fmt, ...) NC_ATTRIBUTE_FORMAT(printf, 4, 5);
129 void _log_stderr(const char *fmt, ...) NC_ATTRIBUTE_FORMAT(printf, 1, 2);
130 void _log_safe(const char *fmt, ...) NC_ATTRIBUTE_FORMAT(printf, 1, 2);
131 void _log_stderr_safe(const char *fmt, ...) NC_ATTRIBUTE_FORMAT(printf, 1, 2);
132 void _log_hexdump(const char *file, int line, const char *data, int datalen, const char *fmt, ...);
133 
134 #endif
135