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 <aerospike/as_status.h>
20 
21 #include <citrusleaf/cf_atomic.h>
22 
23 #include <stdarg.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 /******************************************************************************
31  * TYPES
32  *****************************************************************************/
33 
34 /**
35  * Log Level
36  */
37 typedef enum as_log_level_e {
44 } as_log_level;
45 
46 /**
47  * Callback function for as_log related logging calls.
48  *
49  * The following is a simple log callback:
50  * ~~~~~~~~~~{.c}
51  * bool my_log_callback(
52  * as_log_level level, const char * func, const char * file, uint32_t line,
53  * const char * fmt, ...)
54  * {
55  * char msg[1024] = {0};
56  *
57  * va_list ap;
58  * va_start(ap, fmt);
59  * vsnprintf(msg, 1024, fmt, ap);
60  * msg[1023] = '\0';
61  * va_end(ap);
62  *
63  * fprintf(stderr, "[%s:%d][%s] %d - %s\n", file, line, func, level, msg);
64  *
65  * return true;
66  * }
67  * ~~~~~~~~~~
68  *
69  * The function should return true on success.
70  *
71  *
72  * @param level The log level of the message.
73  * @param func The function where the message was logged.
74  * @param file The file where the message was logged.
75  * @param line The line where the message was logged.
76  * @param fmt The format string used.
77  * @param ... The format argument.
78  *
79  * @return true if the message was logged. Otherwise false.
80  *
81  * @ingroup as_log_object
82  */
83 typedef bool (* as_log_callback)(
84  as_log_level level, const char * func, const char * file, uint32_t line,
85  const char * fmt, ...);
86 
87 /**
88  * Aerospike Client exposed logging functionality including:
89  * - Ability to control the verbosity of log messages.
90  * - Direct where log messages are sent to.
91  *
92  * Each @ref aerospike contains its own as_log instance: aerospike.log.
93  *
94  * ## Setting Log Level
95  *
96  * To set the log level for the aerospike client, simply use
97  * as_log_set_level() and pass in the client log to set.
98  *
99  * ~~~~~~~~~~{.c}
100  * as_log_set_level(&as->log, AS_LOG_LEVEL_INFO);
101  * ~~~~~~~~~~
102  *
103  * ## Redirecting Log Output
104  *
105  * By default, the logger sends log messages to STDERR.
106  *
107  * To change where log messages are sent, simply define a new @ref as_log_callback,
108  * and set it for the client using as_log_set_callback():
109  *
110  * ~~~~~~~~~~{.c}
111  * as_log_set_callback(&as->log, my_log_callback);
112  * ~~~~~~~~~~
113  *
114  * Where the `my_log_callback` could be defined as
115  *
116  * ~~~~~~~~~~{.c}
117  * bool my_log_callback(
118  * as_log_level level, const char * func, const char * file, uint32_t line,
119  * const char * fmt, ...)
120  * {
121  * char msg[1024] = {0};
122  * va_list ap;
123  * va_start(ap, fmt);
124  * vsnprintf(msg, 1024, fmt, ap);
125  * msg[1023] = '\0';
126  * va_end(ap);
127  * fprintf(stderr, "[%s:%d][%s] %d - %s\n", file, line, func, level, msg);
128  * return true;
129  * }
130  * ~~~~~~~~~~
131  *
132  * @ingroup client_objects
133  */
134 typedef struct as_log_s {
135 
136  /**
137  * Log Level
138  */
139  cf_atomic32 level;
140 
141  /**
142  * Logging Callback
143  */
144  cf_atomic_p callback;
145 
146 } as_log;
147 
148 /******************************************************************************
149  * FUNCTIONS
150  *****************************************************************************/
151 
152 /**
153  * Initialize Log Context
154  *
155  * @relates as_log
156  */
157 as_log * as_log_init(as_log * log);
158 
159 /**
160  * Set the level for the given log.
161  *
162  * @param log The log context.
163  * @param level The log level.
164  *
165  * @return true on success. Otherwise false.
166  *
167  * @relates as_log
168  */
169 bool as_log_set_level(as_log * log, as_log_level level);
170 
171 /**
172  * Set the callback for the given log
173  *
174  * @param log The log context.
175  * @param callback The log callback.
176  *
177  * @return true on success. Otherwise false.
178  *
179  * @relates as_log
180  */
181 bool as_log_set_callback(as_log * log, as_log_callback callback);