All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
src/include/aerospike/as_logger.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2008-2013 by Aerospike.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  *****************************************************************************/
22 #pragma once
23 
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 
28 /*****************************************************************************
29  * MACROS
30  *****************************************************************************/
31 
32 /**
33  * Test if logging of AS_LOG_TRACE message is enabled
34  */
35 #define as_logger_trace_enabled(__logger) \
36  as_logger_enabled(__logger, AS_LOGGER_LEVEL_TRACE)
37 
38 /**
39  * Test if logging of AS_LOG_DEBUG message is enabled
40  */
41 #define as_logger_debug_enabled(__logger) \
42  as_logger_enabled(__logger, AS_LOGGER_LEVEL_DEBUG)
43 
44 /**
45  * Test if logging of AS_LOG_INFO message is enabled
46  */
47 #define as_logger_info_enabled(__logger) \
48  as_logger_enabled(__logger, AS_LOGGER_LEVEL_INFO)
49 
50 /**
51  * Test if logging of AS_LOG_WARN message is enabled
52  */
53 #define as_logger_warn_enabled(__logger) \
54  as_logger_enabled(__logger, AS_LOGGER_LEVEL_WARN)
55 
56 /**
57  * Test if logging of AS_LOG_ERROR message is enabled
58  */
59 #define as_logger_error_enabled(__logger) \
60  as_logger_enabled(__logger, AS_LOGGER_LEVEL_ERROR)
61 
62 
63 /**
64  * Log an AS_LOG_ERROR message
65  */
66 #define as_logger_trace(__logger, __message, __args...) \
67  as_logger_log(__logger, AS_LOGGER_LEVEL_TRACE, __FILE__, __LINE__, __message, ##__args)
68 
69 /**
70  * Log an AS_LOG_DEBUG message
71  */
72 #define as_logger_debug(__logger, __message, __args...) \
73  as_logger_log(__logger, AS_LOGGER_LEVEL_DEBUG, __FILE__, __LINE__, __message, ##__args)
74 
75 /**
76  * Log an AS_LOG_INFO message
77  */
78 #define as_logger_info(__logger, __message, __args...) \
79  as_logger_log(__logger, AS_LOGGER_LEVEL_INFO, __FILE__, __LINE__, __message, ##__args)
80 
81 /**
82  * Log an AS_LOG_WARN message
83  */
84 #define as_logger_warn(__logger, __message, __args...) \
85  as_logger_log(__logger, AS_LOGGER_LEVEL_WARN, __FILE__, __LINE__, __message, ##__args)
86 
87 /**
88  * Log an AS_LOG_ERROR message
89  */
90 #define as_logger_error(__logger, __message, __args...) \
91  as_logger_log(__logger, AS_LOGGER_LEVEL_ERROR, __FILE__, __LINE__, __message, ##__args)
92 
93 /*****************************************************************************
94  * TYPES
95  *****************************************************************************/
96 
97 /**
98  * The supported logging levels
99  */
100 typedef enum as_logger_level_e {
107 
108 struct as_logger_hooks_s;
109 
110 /**
111  * Logger handle
112  */
113 typedef struct as_logger_s {
114  bool free;
115  void * source;
116  const struct as_logger_hooks_s * hooks;
117 } as_logger;
118 
119 
120 /**
121  * The interface which all loggers should implement.
122  */
123 typedef struct as_logger_hooks_s {
124 
125  /**
126  * The destroy should free resources associated with the logger's source.
127  * The destroy should not free the logger itself.
128  */
129  int (* destroy)(as_logger *);
130 
131  /**
132  * Test if the log level is enabled for the logger.
133  */
134  int (* enabled)(const as_logger *, const as_logger_level);
135 
136  /**
137  * Get the current log level of the logger.
138  */
139  as_logger_level (* level)(const as_logger *);
140 
141  /**
142  * Log a message using the logger.
143  */
144  int (* log)(const as_logger *, const as_logger_level, const char *, const int, const char *, va_list);
145 
147 
148 
149 
150 
151 /*****************************************************************************
152  * FUNCTIONS
153  *****************************************************************************/
154 
155 /**
156  * Initialize a stack allocated logger
157  */
158 as_logger * as_logger_init(as_logger * logger, void * source, const as_logger_hooks * hooks);
159 
160 /**
161  * Heap allocate and initialize a logger
162  */
163 as_logger * as_logger_new(void * source, const as_logger_hooks * hooks);
164 
165 /**
166  * Release resources associated with the logger.
167  * Calls logger->destroy. If success and if this is a heap allocated
168  * logger, then it will be freed.
169  */
170 int as_logger_destroy(as_logger * logger);
171 
172 
173 /**
174  * Test if the log level is enabled for the logger.
175  *
176  * For most purposes, you should use the macros:
177  * - as_logger_trace_enabled(logger)
178  * - as_logger_debug_enabled(logger)
179  * - as_logger_info_enabled(logger)
180  * - as_logger_warn_enabled(logger)
181  * - as_logger_error_enabled(logger)
182  *
183  * Usage:
184  * if ( as_logger_enabled(logger, AS_LOG_DEBUG) ) {
185  * char * foo = tostring(x);
186  * as_logger_debug(logger, "foo = %s", foo);
187  * cf_free(foo);
188  * }
189  *
190  */
191 bool as_logger_is_enabled(const as_logger * logger, const as_logger_level level);
192 
193 /**
194  * Get the current log level for the logger.
195  */
197 
198 /**
199  * Log a message using the logger.
200  *
201  * For most purposes, you should use the macros:
202  * - as_logger_trace(logger, message, ...)
203  * - as_logger_debug(logger, message, ...)
204  * - as_logger_info_(logger, message, ...)
205  * - as_logger_warn_(logger, message, ...)
206  * - as_logger_error(logger, message, ...)
207  *
208  * Usage:
209  * as_logger_log(logger, AS_LOG_DEBUG, __FILE__, __LINE__, "Hello %s", "Bob");
210  *
211  */
212 int as_logger_log(const as_logger * logger, const as_logger_level level, const char * file, const int line, const char * format, ...);
as_logger * as_logger_init(as_logger *logger, void *source, const as_logger_hooks *hooks)
struct as_logger_hooks_s * hooks
bool as_logger_is_enabled(const as_logger *logger, const as_logger_level level)
as_logger_level as_logger_get_level(const as_logger *logger)
int as_logger_destroy(as_logger *logger)
as_logger * as_logger_new(void *source, const as_logger_hooks *hooks)
int as_logger_log(const as_logger *logger, const as_logger_level level, const char *file, const int line, const char *format,...)