]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil.core/native/FMILibrary/src/Util/src/JM/jm_callbacks.c
Switch to full JavaSE-11+ compatibility
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / src / Util / src / JM / jm_callbacks.c
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 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdarg.h>
20 #include <assert.h>
21
22 #include "JM/jm_callbacks.h"
23 #include "JM/jm_portability.h"
24
25 static const char* jm_log_level_str[] = 
26 {
27         "NOTHING",
28         "FATAL",
29         "ERROR",
30         "WARNING",
31         "INFO",
32         "VERBOSE",
33         "DEBUG",
34         "ALL"
35 };
36
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];
41         else {
42                 assert(0);
43                 return "UNEXPECTED";
44         }
45 }
46
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);
49 }
50
51 void jm_log(jm_callbacks* cb, const char* module, jm_log_level_enu_t log_level, const char* fmt, ...) {
52         va_list args;
53         if(log_level > cb->log_level) return;
54     va_start (args, fmt);
55     jm_log_v(cb, module, log_level, fmt, args);
56     va_end (args);
57 }
58
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);
62         if(cb->logger) {
63                 cb->logger(cb,module, log_level, cb->errMessageBuffer);
64         }
65 }
66
67 #define CREATE_LOG_FUNCTIONS(log_level) \
68 void jm_log_ ## log_level(jm_callbacks* cb, const char* module, const char* fmt, ...) { \
69         va_list args; \
70     va_start (args, fmt); \
71         jm_log_v(cb, module, jm_log_level_ ## log_level, fmt, args); \
72     va_end (args); \
73 } \
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); \
76 }
77
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)
85 #endif
86
87
88 jm_callbacks jm_standard_callbacks;
89
90 jm_callbacks* jm_standard_callbacks_ptr = 0;
91
92 jm_callbacks* jm_default_callbacks = 0;
93
94 void jm_set_default_callbacks(jm_callbacks* c) {
95         if(c)
96                 jm_default_callbacks = c;
97         else
98                 jm_default_callbacks = jm_standard_callbacks_ptr;
99 }
100
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;
112
113                         jm_standard_callbacks_ptr = &jm_standard_callbacks;
114                 }
115                 jm_default_callbacks = jm_standard_callbacks_ptr;
116         }
117         return jm_default_callbacks; 
118 }