Kaa client C SDK
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
kaa_log.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
26 #ifndef KAA_LOG_H_
27 #define KAA_LOG_H_
28 
29 #include "kaa_error.h"
30 #include <platform/defaults.h>
31 #include <platform/stdio.h>
32 #include <stdint.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 
40 #define KAA_LOG_LEVEL_NONE 0
41 
42 #define KAA_LOG_LEVEL_FATAL 1
43 
44 #define KAA_LOG_LEVEL_ERROR 2
45 
46 #define KAA_LOG_LEVEL_WARN 3
47 
48 #define KAA_LOG_LEVEL_INFO 4
49 
50 #define KAA_LOG_LEVEL_DEBUG 5
51 
52 #define KAA_LOG_LEVEL_TRACE 6
53 
54 #ifndef KAA_MAX_LOG_LEVEL
55 
56 #define KAA_MAX_LOG_LEVEL KAA_LOG_LEVEL_TRACE
57 #endif
58 
59 #define KAA_LOG_LEVEL_FATAL_ENABLED (KAA_MAX_LOG_LEVEL >= KAA_LOG_LEVEL_FATAL)
60 #define KAA_LOG_LEVEL_ERROR_ENABLED (KAA_MAX_LOG_LEVEL >= KAA_LOG_LEVEL_ERROR)
61 #define KAA_LOG_LEVEL_WARN_ENABLED (KAA_MAX_LOG_LEVEL >= KAA_LOG_LEVEL_WARN)
62 #define KAA_LOG_LEVEL_INFO_ENABLED (KAA_MAX_LOG_LEVEL >= KAA_LOG_LEVEL_INFO)
63 #define KAA_LOG_LEVEL_DEBUG_ENABLED (KAA_MAX_LOG_LEVEL >= KAA_LOG_LEVEL_DEBUG)
64 #define KAA_LOG_LEVEL_TRACE_ENABLED (KAA_MAX_LOG_LEVEL >= KAA_LOG_LEVEL_TRACE)
65 
66 
67 struct kaa_logger_t;
68 typedef struct kaa_logger_t kaa_logger_t;
69 
73 typedef uint8_t kaa_log_level_t;
74 
84 kaa_error_t kaa_log_create(kaa_logger_t **logger, size_t buffer_size, kaa_log_level_t max_log_level, FILE* sink);
85 
93 
102 kaa_log_level_t kaa_get_max_log_level(const kaa_logger_t *self);
103 
111 kaa_error_t kaa_set_max_log_level(kaa_logger_t *self, kaa_log_level_t max_log_level);
112 
120 kaa_error_t kaa_log_set_sink(kaa_logger_t *self, FILE *sink);
121 
142 void kaa_log_write(kaa_logger_t *self, const char* source_file, int lineno, kaa_log_level_t log_level
143  , kaa_error_t error_code, const char* format, ...);
144 
145 /*
146  * Shortcut macros for logging at various log levels
147  */
148 #if KAA_LOG_LEVEL_FATAL_ENABLED
149 #define KAA_LOG_FATAL(logger, err, ...) kaa_log_write(logger, __FILE__, __LINE__, KAA_LOG_LEVEL_FATAL, err, __VA_ARGS__)
150 #else
151 #define KAA_LOG_FATAL(logger, err, ...) do { (void)(logger); (void)(err); } while (0)
152 #endif
153 
154 #if KAA_LOG_LEVEL_ERROR_ENABLED
155 #define KAA_LOG_ERROR(logger, err, ...) kaa_log_write(logger, __FILE__, __LINE__, KAA_LOG_LEVEL_ERROR, err, __VA_ARGS__)
156 #else
157 #define KAA_LOG_ERROR(logger, err, ...) do { (void)(logger); (void)(err); } while (0)
158 #endif
159 
160 #if KAA_LOG_LEVEL_WARN_ENABLED
161 #define KAA_LOG_WARN(logger, err, ...) kaa_log_write(logger, __FILE__, __LINE__, KAA_LOG_LEVEL_WARN, err, __VA_ARGS__)
162 #else
163 #define KAA_LOG_WARN(logger, err, ...) do { (void)(logger); (void)(err); } while (0)
164 #endif
165 
166 #if KAA_LOG_LEVEL_INFO_ENABLED
167 #define KAA_LOG_INFO(logger, err, ...) kaa_log_write(logger, __FILE__, __LINE__, KAA_LOG_LEVEL_INFO, err, __VA_ARGS__)
168 #else
169 #define KAA_LOG_INFO(logger, err, ...) do { (void)(logger); (void)(err); } while (0)
170 #endif
171 
172 #if KAA_LOG_LEVEL_DEBUG_ENABLED
173 #define KAA_LOG_DEBUG(logger, err, ...) kaa_log_write(logger, __FILE__, __LINE__, KAA_LOG_LEVEL_DEBUG, err, __VA_ARGS__)
174 #else
175 #define KAA_LOG_DEBUG(logger, err, ...) do { (void)(logger); (void)(err); } while (0)
176 #endif
177 
178 #if KAA_LOG_LEVEL_TRACE_ENABLED
179 #define KAA_LOG_TRACE(logger, err, ...) kaa_log_write(logger, __FILE__, __LINE__, KAA_LOG_LEVEL_TRACE, err, __VA_ARGS__)
180 #else
181 #define KAA_LOG_TRACE(logger, err, ...) do { (void)(logger); (void)(err); } while (0)
182 #endif
183 
184 #if KAA_LOCAL_DBG
185 #define KAA_LOG_TRACE_LDB(logger, err, ...) KAA_LOG_TRACE(logger, err, ...)
186 #else
187 #define KAA_LOG_TRACE_LDB(logger, err, ...) do { (void)(logger); (void)(err); } while (0)
188 #endif
189 
190 #ifdef __cplusplus
191 } /* extern "C" */
192 #endif
193 #endif /* KAA_LOG_H_ */
kaa_error_t kaa_log_destroy(kaa_logger_t *logger)
Deinitializes and destroys the logger instance.
kaa_error_t kaa_log_set_sink(kaa_logger_t *self, FILE *sink)
Sets user sink for log output.
uint8_t kaa_log_level_t
Log level type.
Definition: kaa_log.h:73
kaa_error_t
Definition: kaa_error.h:31
kaa_error_t kaa_set_max_log_level(kaa_logger_t *self, kaa_log_level_t max_log_level)
Sets the maximum log level.
Kaa error codes.
void kaa_log_write(kaa_logger_t *self, const char *source_file, int lineno, kaa_log_level_t log_level, kaa_error_t error_code, const char *format,...)
Compiles a log message and puts it into the sink.
struct kaa_logger_t kaa_logger_t
Definition: kaa_log.h:68
kaa_error_t kaa_log_create(kaa_logger_t **logger, size_t buffer_size, kaa_log_level_t max_log_level, FILE *sink)
Creates and initializes a logger instance.
kaa_log_level_t kaa_get_max_log_level(const kaa_logger_t *self)
Retrieves the current log level.