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-2014 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 /******************************************************************************
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  *
67  * @param level The log level of the message.
68  * @param func The function where the message was logged.
69  * @param file The file where the message was logged.
70  * @param line The line where the message was logged.
71  * @param fmt The format string used.
72  * @param ... The format argument.
73  *
74  * @return true if the message was logged. Otherwise false.
75  *
76  * @ingroup as_log_object
77  */
78 typedef bool (* as_log_callback)(
79  as_log_level level, const char * func, const char * file, uint32_t line,
80  const char * fmt, ...);
81 
82 /**
83  * Aerospike Client exposed logging functionality including:
84  * - Ability to control the verbosity of log messages.
85  * - Direct where log messages are sent to.
86  *
87  * ## Setting Log Level
88  *
89  * To set the log level for the aerospike client, simply use
90  * as_log_set_level() and pass in the client log to set.
91  *
92  * ~~~~~~~~~~{.c}
93  * as_log_set_level(AS_LOG_LEVEL_INFO);
94  * ~~~~~~~~~~
95  *
96  * ## Redirecting Log Output
97  *
98  * By default, the logger is not enabled.
99  *
100  * To enable where log messages are sent, simply define a new @ref as_log_callback,
101  * and set it for the client using as_log_set_callback():
102  *
103  * ~~~~~~~~~~{.c}
104  * as_log_set_callback(my_log_callback);
105  * ~~~~~~~~~~
106  *
107  * Where the `my_log_callback` could be defined as
108  *
109  * ~~~~~~~~~~{.c}
110  * bool my_log_callback(
111  * as_log_level level, const char * func, const char * file, uint32_t line,
112  * const char * fmt, ...)
113  * {
114  * char msg[1024] = {0};
115  * va_list ap;
116  * va_start(ap, fmt);
117  * vsnprintf(msg, 1024, fmt, ap);
118  * msg[1023] = '\0';
119  * va_end(ap);
120  * fprintf(stderr, "[%s:%d][%s] %d - %s\n", file, line, func, level, msg);
121  * return true;
122  * }
123  * ~~~~~~~~~~
124  *
125  * @ingroup client_objects
126  */
127 typedef struct as_log_s {
128 
129  /**
130  * Log Level
131  */
133 
134  /**
135  * Logging Callback
136  */
138 
139 } as_log;
140 
141 /******************************************************************************
142  * GLOBAL VARIABLES
143  *****************************************************************************/
144 
145 extern as_log g_as_log;
146 extern const char* as_log_level_strings[];
147 
148 /******************************************************************************
149  * FUNCTIONS
150  *****************************************************************************/
151 
152 /**
153  * Set logging level for the global client log.
154  *
155  * @param level The log level.
156  *
157  * @relates as_log
158  */
159 static inline void
161 {
162  g_as_log.level = level;
163 }
164 
165 /**
166  * Set logging callback for the global client log.
167  *
168  * @param callback The log callback.
169  *
170  * @relates as_log
171  */
172 static inline void
174 {
175  g_as_log.callback = callback;
176 }
177 
178 /**
179  * Convert log level to a string.
180  *
181  * @param level The log level.
182  *
183  * @relates as_log
184  */
185 static inline const char*
187 {
188  return as_log_level_strings[level];
189 }