FMI Library: part of JModelica.org
jm_callbacks.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 Modelon AB
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the BSD style license.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  FMILIB_License.txt file for more details.
11 
12  You should have received a copy of the FMILIB_License.txt file
13  along with this program. If not, contact Modelon AB <http://www.modelon.com>.
14 */
15 
16 #ifndef JM_CALLBACKS_H
17 #define JM_CALLBACKS_H
18 #include <stddef.h>
19 #include <stdarg.h>
20 
21 #include <fmilib_config.h>
22 
23 #include "jm_types.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
37 typedef struct jm_callbacks jm_callbacks;
38 
45 typedef jm_voidp (*jm_malloc_f)(size_t size);
46 
48 typedef jm_voidp (*jm_realloc_f)(void *ptr, size_t size);
49 
51 typedef jm_voidp (*jm_calloc_f)(size_t numitems, size_t itemsize);
52 
54 typedef void (*jm_free_f)(jm_voidp p);
68 typedef void (*jm_logger_f)(jm_callbacks* c, jm_string module, jm_log_level_enu_t log_level, jm_string message);
69 
71 #define JM_MAX_ERROR_MESSAGE_SIZE 2000
72 
74 struct jm_callbacks {
82  jm_free_f free;
91 };
92 
100 
104 static void jm_clear_last_error(jm_callbacks* cb) { cb->errMessageBuffer[0] = 0; }
105 
114 
121 
126 void jm_default_logger(jm_callbacks* c, jm_string module, jm_log_level_enu_t log_level, jm_string message);
127 
136 void jm_log(jm_callbacks* cb, const char* module, jm_log_level_enu_t log_level, const char* fmt, ...);
137 
142 void jm_log_v(jm_callbacks* cb, const char* module, jm_log_level_enu_t log_level, const char* fmt, va_list ap);
143 
147 void jm_log_fatal_v(jm_callbacks* cb, const char* module, const char* fmt, va_list ap);
151 void jm_log_fatal(jm_callbacks* cb, const char* module, const char* fmt, ...);
152 
156 void jm_log_error_v(jm_callbacks* cb, const char* module, const char* fmt, va_list ap);
157 
161 void jm_log_error(jm_callbacks* cb, const char* module, const char* fmt, ...);
162 
166 void jm_log_warning_v(jm_callbacks* cb, const char* module, const char* fmt, va_list ap);
167 
171 void jm_log_warning(jm_callbacks* cb, const char* module, const char* fmt, ...);
172 
176 void jm_log_info_v(jm_callbacks* cb, const char* module, const char* fmt, va_list ap);
180 void jm_log_info(jm_callbacks* cb, const char* module, const char* fmt, ...);
181 
185 void jm_log_verbose_v(jm_callbacks* cb, const char* module, const char* fmt, va_list ap);
189 void jm_log_verbose(jm_callbacks* cb, const char* module, const char* fmt, ...);
190 
191 #ifdef FMILIB_ENABLE_LOG_LEVEL_DEBUG
192 
197 void jm_log_debug_v(jm_callbacks* cb, const char* module, const char* fmt, va_list ap);
203 void jm_log_debug(jm_callbacks* cb, const char* module, const char* fmt, ...);
204 #else
205 
209 static void jm_log_debug_v(jm_callbacks* cb, const char* module, const char* fmt, va_list ap) {}
214 static void jm_log_debug(jm_callbacks* cb, const char* module, const char* fmt, ...) {}
215 #endif
216 
217 
218 /* @}
219 */
220 
221 #ifdef __cplusplus
222 }
223 #endif
224 /* JM_CONTEXT_H */
225 #endif
FMILIB_EXPORT void jm_log_info_v(jm_callbacks *cb, const char *module, const char *fmt, va_list ap)
Send an info message to the logger function. See jm_log() for details.
jm_calloc_f calloc
Allocate zero initialized memory.
Definition: jm_callbacks.h:77
Library configuration file generated by the build system.
FMILIB_EXPORT void jm_log_warning_v(jm_callbacks *cb, const char *module, const char *fmt, va_list ap)
Send a warning message to the logger function. See jm_log() for details.
size_t jm_callbacks * c
FMILIB_EXPORT jm_callbacks * jm_get_default_callbacks(void)
Get default callbacks. The function never returns NULL.
void * jm_voidp
A void pointer.
Definition: jm_types.h:35
jm_voidp(* jm_realloc_f)(void *ptr, size_t size)
Re-allocation function type.
Definition: jm_callbacks.h:48
static void jm_clear_last_error(jm_callbacks *cb)
Clear the last generated log message.
Definition: jm_callbacks.h:103
static void jm_log_debug_v(jm_callbacks *cb, const char *module, const char *fmt, va_list ap)
Send a debug message to the logger function. See jm_log() for details.
Definition: jm_callbacks.h:208
FMILIB_EXPORT void jm_log_v(jm_callbacks *cb, const char *module, jm_log_level_enu_t log_level, const char *fmt, va_list ap)
Send a message to the logger function.
char errMessageBuffer[JM_MAX_ERROR_MESSAGE_SIZE]
The buffer used along with jm_get_last_error()
Definition: jm_callbacks.h:89
FMILIB_EXPORT void jm_log(jm_callbacks *cb, const char *module, jm_log_level_enu_t log_level, const char *fmt,...)
Send a message to the logger function.
jm_voidp(* jm_calloc_f)(size_t numitems, size_t itemsize)
Zero-initialized allocation function type.
Definition: jm_callbacks.h:51
jm_log_level_enu_t
Log levels supported via the logger functions in jm_callbacks.
Definition: jm_types.h:51
v size
jm_log_level_enu_t log_level
Logging level.
Definition: jm_callbacks.h:85
FMILIB_EXPORT void jm_log_fatal_v(jm_callbacks *cb, const char *module, const char *fmt, va_list ap)
Send a fatal error message to the logger function. See jm_log() for details.
#define FMILIB_EXPORT
Used to declare the public API of the library needed for dynamic linking.
static jm_string jm_get_last_error(jm_callbacks *cb)
Get the last log message produced by the library.
Definition: jm_callbacks.h:98
FMILIB_EXPORT void jm_log_warning(jm_callbacks *cb, const char *module, const char *fmt,...)
Send a warning message to the logger function. See jm_log() for details.
FMILIB_EXPORT void jm_set_default_callbacks(jm_callbacks *c)
Set the structure to be returned by jm_get_default_callbacks().
FMILIB_EXPORT void jm_log_fatal(jm_callbacks *cb, const char *module, const char *fmt,...)
Send a fatal error message to the logger function. See jm_log() for details.
jm_voidp(* jm_malloc_f)(size_t size)
Allocation function type.
Definition: jm_callbacks.h:45
FMILIB_EXPORT void jm_log_error_v(jm_callbacks *cb, const char *module, const char *fmt, va_list ap)
Send a error message to the logger function. See jm_log() for details.
jm_malloc_f malloc
Allocate non-initialized memory.
Definition: jm_callbacks.h:75
jm_voidp context
Arbitrary context pointer passed to the logger function.
Definition: jm_callbacks.h:87
const char * jm_string
A constant string.
Definition: jm_types.h:33
FMILIB_EXPORT void jm_log_info(jm_callbacks *cb, const char *module, const char *fmt,...)
Send an info message to the logger function. See jm_log() for details.
FMILIB_EXPORT void jm_default_logger(jm_callbacks *c, jm_string module, jm_log_level_enu_t log_level, jm_string message)
The default logger implementation prints messages to stderr.
The callbacks struct is sent to all the modules in the library.
Definition: jm_callbacks.h:73
void(* jm_logger_f)(jm_callbacks *c, jm_string module, jm_log_level_enu_t log_level, jm_string message)
Logger callback type.
Definition: jm_callbacks.h:67
FMILIB_EXPORT void jm_log_verbose_v(jm_callbacks *cb, const char *module, const char *fmt, va_list ap)
Send a verbose message to the logger function. See jm_log() for details.
#define JM_MAX_ERROR_MESSAGE_SIZE
Maximum message size that can be stored in the jm_callbacks struct.
Definition: jm_callbacks.h:70
jm_realloc_f realloc
Re-allocate memory.
Definition: jm_callbacks.h:79
jm_free_f free
Free-allocated memory.
Definition: jm_callbacks.h:81
FMILIB_EXPORT void jm_log_verbose(jm_callbacks *cb, const char *module, const char *fmt,...)
Send a verbose message to the logger function. See jm_log() for details.
jm_logger_f logger
Logging callback.
Definition: jm_callbacks.h:83
FMILIB_EXPORT void jm_log_error(jm_callbacks *cb, const char *module, const char *fmt,...)
Send a error message to the logger function. See jm_log() for details.
static void jm_log_debug(jm_callbacks *cb, const char *module, const char *fmt,...)
Send a debug message to the logger function. See jm_log() for details.
Definition: jm_callbacks.h:213
void(* jm_free_f)(jm_voidp p)
Free memory function type.
Definition: jm_callbacks.h:54