Loading...
Searching...
No Matches
as_log.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2024 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
23extern "C" {
24#endif
25
26//---------------------------------
27// Types
28//---------------------------------
29
30/**
31 * Log Level
32 */
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(as_log_level level, const char * func, const char * file, uint32_t line, const char * fmt, ...)
47 * {
48 * char msg[1024] = {0};
49 *
50 * va_list ap;
51 * va_start(ap, fmt);
52 * vsnprintf(msg, 1024, fmt, ap);
53 * msg[1023] = '\0';
54 * va_end(ap);
55 *
56 * fprintf(stderr, "[%s:%d][%s] %d - %s\n", file, line, func, level, msg);
57 *
58 * return true;
59 * }
60 * ~~~~~~~~~~
61 *
62 * The function should return true on success.
63 *
64 * @param level The log level of the message.
65 * @param func The function where the message was logged.
66 * @param file The file where the message was logged.
67 * @param line The line where the message was logged.
68 * @param fmt The format string used.
69 * @param ... The format argument.
70 *
71 * @return true if the message was logged. Otherwise false.
72 *
73 * @relates as_log
74 */
75typedef bool (* as_log_callback)(
76 as_log_level level, const char * func, const char * file, uint32_t line,
77 const char * fmt, ...);
78
79/**
80 * Aerospike Client exposed logging functionality including:
81 * - Ability to control the verbosity of log messages.
82 * - Direct where log messages are sent to.
83 *
84 * ## Setting Log Level
85 *
86 * To set the log level for the aerospike client, simply use
87 * as_log_set_level() and pass in the client log to set.
88 *
89 * ~~~~~~~~~~{.c}
90 * as_log_set_level(AS_LOG_LEVEL_INFO);
91 * ~~~~~~~~~~
92 *
93 * ## Redirecting Log Output
94 *
95 * By default, the logger is not enabled.
96 *
97 * To enable where log messages are sent, simply define a new @ref as_log_callback,
98 * and set it for the client using as_log_set_callback():
99 *
100 * ~~~~~~~~~~{.c}
101 * as_log_set_callback(my_log_callback);
102 * ~~~~~~~~~~
103 *
104 * Where the `my_log_callback` could be defined as
105 *
106 * ~~~~~~~~~~{.c}
107 * bool my_log_callback(as_log_level level, const char * func, const char * file, uint32_t line, const char * fmt, ...)
108 * {
109 * char msg[1024] = {0};
110 * va_list ap;
111 * va_start(ap, fmt);
112 * vsnprintf(msg, 1024, fmt, ap);
113 * msg[1023] = '\0';
114 * va_end(ap);
115 * fprintf(stderr, "[%s:%d][%s] %d - %s\n", file, line, func, level, msg);
116 * return true;
117 * }
118 * ~~~~~~~~~~
119 *
120 * @ingroup client_objects
121 */
122typedef struct as_log_s {
123
124 /**
125 * Log Level
126 */
128
129 /**
130 * Was callback explicitly set.
131 */
133
134 /**
135 * Logging Callback
136 */
137 as_log_callback callback;
138
139} as_log;
140
141//---------------------------------
142// Globals
143//---------------------------------
144
146AS_EXTERN 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 */
159static inline void
161{
162 g_as_log.level = level;
163}
164
165/**
166 * Set logging callback for the global client log.
167 * To silence the log, set callback to NULL.
168 *
169 * @param callback The log callback.
170 *
171 * @relates as_log
172 */
173static inline void
174as_log_set_callback(as_log_callback callback)
175{
176 g_as_log.callback = callback;
177 g_as_log.callback_set = true;
178}
179
180/**
181 * Convert log level to a string.
182 *
183 * @param level The log level.
184 *
185 * @relates as_log
186 */
187static inline const char*
189{
190 return as_log_level_strings[level];
191}
192
193#ifdef __cplusplus
194} // end extern "C"
195#endif
AS_EXTERN const char * as_log_level_strings[]
AS_EXTERN as_log g_as_log
as_log_level
Definition as_log.h:33
@ AS_LOG_LEVEL_INFO
Definition as_log.h:36
@ AS_LOG_LEVEL_TRACE
Definition as_log.h:38
@ AS_LOG_LEVEL_ERROR
Definition as_log.h:34
@ AS_LOG_LEVEL_WARN
Definition as_log.h:35
@ AS_LOG_LEVEL_DEBUG
Definition as_log.h:37
#define AS_EXTERN
Definition as_std.h:25
static const char * as_log_level_tostring(as_log_level level)
Definition as_log.h:188
bool callback_set
Definition as_log.h:132
as_log_callback callback
Definition as_log.h:137
static void as_log_set_level(as_log_level level)
Definition as_log.h:160
static void as_log_set_callback(as_log_callback callback)
Definition as_log.h:174
as_log_level level
Definition as_log.h:127