All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_log.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2022 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at 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, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #include <aerospike/as_std.h>
20 #include <stdarg.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /******************************************************************************
27  * TYPES
28  *****************************************************************************/
29 
30 /**
31  * Log Level
32  */
33 typedef enum as_log_level_e {
39 } as_log_level;
40 
41 /**
42  * Callback function for as_log related logging calls.
43  *
44  * The following is a simple log callback:
45  * ~~~~~~~~~~{.c}
46  * bool my_log_callback(
47  * as_log_level level, const char * func, const char * file, uint32_t line,
48  * const char * fmt, ...)
49  * {
50  * char msg[1024] = {0};
51  *
52  * va_list ap;
53  * va_start(ap, fmt);
54  * vsnprintf(msg, 1024, fmt, ap);
55  * msg[1023] = '\0';
56  * va_end(ap);
57  *
58  * fprintf(stderr, "[%s:%d][%s] %d - %s\n", file, line, func, level, msg);
59  *
60  * return true;
61  * }
62  * ~~~~~~~~~~
63  *
64  * The function should return true on success.
65  *
66  * @param level The log level of the message.
67  * @param func The function where the message was logged.
68  * @param file The file where the message was logged.
69  * @param line The line where the message was logged.
70  * @param fmt The format string used.
71  * @param ... The format argument.
72  *
73  * @return true if the message was logged. Otherwise false.
74  *
75  * @ingroup as_log_object
76  */
77 typedef bool (* as_log_callback)(
78  as_log_level level, const char * func, const char * file, uint32_t line,
79  const char * fmt, ...);
80 
81 /**
82  * Aerospike Client exposed logging functionality including:
83  * - Ability to control the verbosity of log messages.
84  * - Direct where log messages are sent to.
85  *
86  * ## Setting Log Level
87  *
88  * To set the log level for the aerospike client, simply use
89  * as_log_set_level() and pass in the client log to set.
90  *
91  * ~~~~~~~~~~{.c}
92  * as_log_set_level(AS_LOG_LEVEL_INFO);
93  * ~~~~~~~~~~
94  *
95  * ## Redirecting Log Output
96  *
97  * By default, the logger is not enabled.
98  *
99  * To enable where log messages are sent, simply define a new @ref as_log_callback,
100  * and set it for the client using as_log_set_callback():
101  *
102  * ~~~~~~~~~~{.c}
103  * as_log_set_callback(my_log_callback);
104  * ~~~~~~~~~~
105  *
106  * Where the `my_log_callback` could be defined as
107  *
108  * ~~~~~~~~~~{.c}
109  * bool my_log_callback(
110  * as_log_level level, const char * func, const char * file, uint32_t line,
111  * const char * fmt, ...)
112  * {
113  * char msg[1024] = {0};
114  * va_list ap;
115  * va_start(ap, fmt);
116  * vsnprintf(msg, 1024, fmt, ap);
117  * msg[1023] = '\0';
118  * va_end(ap);
119  * fprintf(stderr, "[%s:%d][%s] %d - %s\n", file, line, func, level, msg);
120  * return true;
121  * }
122  * ~~~~~~~~~~
123  *
124  * @ingroup client_objects
125  */
126 typedef struct as_log_s {
127 
128  /**
129  * Log Level
130  */
132 
133  /**
134  * Was callback explicitly set.
135  */
137 
138  /**
139  * Logging Callback
140  */
142 
143 } as_log;
144 
145 /******************************************************************************
146  * GLOBAL VARIABLES
147  *****************************************************************************/
148 
149 AS_EXTERN extern as_log g_as_log;
150 AS_EXTERN extern const char* as_log_level_strings[];
151 
152 /******************************************************************************
153  * FUNCTIONS
154  *****************************************************************************/
155 
156 /**
157  * Set logging level for the global client log.
158  *
159  * @param level The log level.
160  *
161  * @relates as_log
162  */
163 static inline void
165 {
166  g_as_log.level = level;
167 }
168 
169 /**
170  * Set logging callback for the global client log.
171  * To silence the log, set callback to NULL.
172  *
173  * @param callback The log callback.
174  *
175  * @relates as_log
176  */
177 static inline void
179 {
180  g_as_log.callback = callback;
181  g_as_log.callback_set = true;
182 }
183 
184 /**
185  * Convert log level to a string.
186  *
187  * @param level The log level.
188  *
189  * @relates as_log
190  */
191 static inline const char*
193 {
194  return as_log_level_strings[level];
195 }
196 
197 #ifdef __cplusplus
198 } // end extern "C"
199 #endif
as_log_level
Definition: as_log.h:33
as_log_callback callback
Definition: as_log.h:141
Definition: as_log.h:126
static const char * as_log_level_tostring(as_log_level level)
Definition: as_log.h:192
AS_EXTERN const char * as_log_level_strings[]
#define AS_EXTERN
Definition: as_std.h:25
as_log_level level
Definition: as_log.h:131
AS_EXTERN as_log g_as_log
bool(* as_log_callback)(as_log_level level, const char *func, const char *file, uint32_t line, const char *fmt,...)
Definition: as_log.h:77
static void as_log_set_level(as_log_level level)
Definition: as_log.h:164
bool callback_set
Definition: as_log.h:136
static void as_log_set_callback(as_log_callback callback)
Definition: as_log.h:178