2 Copyright (C) 2012 Modelon AB
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the BSD style license.
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.
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>.
22 #include "JM/jm_callbacks.h"
23 #include "JM/jm_portability.h"
25 static const char* jm_log_level_str[] =
37 /** \brief Convert log level into a string */
38 const char* jm_log_level_to_string(jm_log_level_enu_t level) {
39 if((level >= jm_log_level_nothing) && (level <= jm_log_level_all))
40 return jm_log_level_str[level];
47 void jm_default_logger(jm_callbacks* c, jm_string module, jm_log_level_enu_t log_level, jm_string message) {
48 fprintf(stderr, "[%s][%s] %s\n", jm_log_level_to_string(log_level), module, message);
51 void jm_log(jm_callbacks* cb, const char* module, jm_log_level_enu_t log_level, const char* fmt, ...) {
53 if(log_level > cb->log_level) return;
55 jm_log_v(cb, module, log_level, fmt, args);
59 void jm_log_v(jm_callbacks* cb, const char* module, jm_log_level_enu_t log_level, const char* fmt, va_list ap) {
60 if(log_level > cb->log_level) return;
61 jm_vsnprintf(cb->errMessageBuffer, JM_MAX_ERROR_MESSAGE_SIZE, fmt, ap);
63 cb->logger(cb,module, log_level, cb->errMessageBuffer);
67 #define CREATE_LOG_FUNCTIONS(log_level) \
68 void jm_log_ ## log_level(jm_callbacks* cb, const char* module, const char* fmt, ...) { \
70 va_start (args, fmt); \
71 jm_log_v(cb, module, jm_log_level_ ## log_level, fmt, args); \
74 void jm_log_ ## log_level ## _v(jm_callbacks* cb, const char* module, const char* fmt, va_list ap) { \
75 jm_log_v(cb, module, jm_log_level_ ## log_level, fmt, ap); \
78 CREATE_LOG_FUNCTIONS(fatal)
79 CREATE_LOG_FUNCTIONS(error)
80 CREATE_LOG_FUNCTIONS(warning)
81 CREATE_LOG_FUNCTIONS(info)
82 CREATE_LOG_FUNCTIONS(verbose)
83 #ifdef FMILIB_ENABLE_LOG_LEVEL_DEBUG
84 CREATE_LOG_FUNCTIONS(debug)
88 jm_callbacks jm_standard_callbacks;
90 jm_callbacks* jm_standard_callbacks_ptr = 0;
92 jm_callbacks* jm_default_callbacks = 0;
94 void jm_set_default_callbacks(jm_callbacks* c) {
96 jm_default_callbacks = c;
98 jm_default_callbacks = jm_standard_callbacks_ptr;
101 jm_callbacks* jm_get_default_callbacks() {
102 if(!jm_default_callbacks) {
103 if(!jm_standard_callbacks_ptr) {
104 jm_standard_callbacks.calloc = calloc;
105 jm_standard_callbacks.malloc = malloc;
106 jm_standard_callbacks.realloc = realloc;
107 jm_standard_callbacks.free = free;
108 jm_standard_callbacks.logger = jm_default_logger;
109 jm_standard_callbacks.log_level = jm_log_level_info;
110 jm_standard_callbacks.context = 0;
111 jm_standard_callbacks.errMessageBuffer[0] = 0;
113 jm_standard_callbacks_ptr = &jm_standard_callbacks;
115 jm_default_callbacks = jm_standard_callbacks_ptr;
117 return jm_default_callbacks;