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-2017 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 <stdarg.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /******************************************************************************
31  * TYPES
32  *****************************************************************************/
33 
34 /**
35  * Log Level
36  */
37 typedef enum as_log_level_e {
43 } as_log_level;
44 
45 /**
46  * Callback function for as_log related logging calls.
47  *
48  * The following is a simple log callback:
49  * ~~~~~~~~~~{.c}
50  * bool my_log_callback(
51  * as_log_level level, const char * func, const char * file, uint32_t line,
52  * const char * fmt, ...)
53  * {
54  * char msg[1024] = {0};
55  *
56  * va_list ap;
57  * va_start(ap, fmt);
58  * vsnprintf(msg, 1024, fmt, ap);
59  * msg[1023] = '\0';
60  * va_end(ap);
61  *
62  * fprintf(stderr, "[%s:%d][%s] %d - %s\n", file, line, func, level, msg);
63  *
64  * return true;
65  * }
66  * ~~~~~~~~~~
67  *
68  * The function should return true on success.
69  *
70  *
71  * @param level The log level of the message.
72  * @param func The function where the message was logged.
73  * @param file The file where the message was logged.
74  * @param line The line where the message was logged.
75  * @param fmt The format string used.
76  * @param ... The format argument.
77  *
78  * @return true if the message was logged. Otherwise false.
79  *
80  * @ingroup as_log_object
81  */
82 typedef bool (* as_log_callback)(
83  as_log_level level, const char * func, const char * file, uint32_t line,
84  const char * fmt, ...);
85 
86 /**
87  * Aerospike Client exposed logging functionality including:
88  * - Ability to control the verbosity of log messages.
89  * - Direct where log messages are sent to.
90  *
91  * ## Setting Log Level
92  *
93  * To set the log level for the aerospike client, simply use
94  * as_log_set_level() and pass in the client log to set.
95  *
96  * ~~~~~~~~~~{.c}
97  * as_log_set_level(AS_LOG_LEVEL_INFO);
98  * ~~~~~~~~~~
99  *
100  * ## Redirecting Log Output
101  *
102  * By default, the logger is not enabled.
103  *
104  * To enable where log messages are sent, simply define a new @ref as_log_callback,
105  * and set it for the client using as_log_set_callback():
106  *
107  * ~~~~~~~~~~{.c}
108  * as_log_set_callback(my_log_callback);
109  * ~~~~~~~~~~
110  *
111  * Where the `my_log_callback` could be defined as
112  *
113  * ~~~~~~~~~~{.c}
114  * bool my_log_callback(
115  * as_log_level level, const char * func, const char * file, uint32_t line,
116  * const char * fmt, ...)
117  * {
118  * char msg[1024] = {0};
119  * va_list ap;
120  * va_start(ap, fmt);
121  * vsnprintf(msg, 1024, fmt, ap);
122  * msg[1023] = '\0';
123  * va_end(ap);
124  * fprintf(stderr, "[%s:%d][%s] %d - %s\n", file, line, func, level, msg);
125  * return true;
126  * }
127  * ~~~~~~~~~~
128  *
129  * @ingroup client_objects
130  */
131 typedef struct as_log_s {
132 
133  /**
134  * Log Level
135  */
137 
138  /**
139  * Logging Callback
140  */
142 
143 } as_log;
144 
145 /******************************************************************************
146  * GLOBAL VARIABLES
147  *****************************************************************************/
148 
149 extern as_log g_as_log;
150 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  *
172  * @param callback The log callback.
173  *
174  * @relates as_log
175  */
176 static inline void
178 {
179  g_as_log.callback = callback;
180 }
181 
182 /**
183  * Convert log level to a string.
184  *
185  * @param level The log level.
186  *
187  * @relates as_log
188  */
189 static inline const char*
191 {
192  return as_log_level_strings[level];
193 }
194 
195 #ifdef __cplusplus
196 } // end extern "C"
197 #endif