This repository was archived by the owner on May 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathomc_error.c
More file actions
522 lines (471 loc) · 15.5 KB
/
omc_error.c
File metadata and controls
522 lines (471 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
* GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the OSMC (Open Source Modelica Consortium)
* Public License (OSMC-PL) are obtained from OSMC, either from the above
* address, from the URLs: http://www.openmodelica.org or
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
* distribution. GNU version 3 is obtained from:
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
* http://www.opensource.org/licenses/BSD-3-Clause.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
* EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
* CONDITIONS OF OSMC-PL.
*
*/
#include "setjmp.h"
#include <stdio.h>
#include "omc_error.h"
/* For MMC_THROW, so we can end this thing */
#include "meta/meta_modelica.h"
const int firstOMCErrorStream = 3;
const char *LOG_STREAM_NAME[SIM_LOG_MAX] = {
"LOG_UNKNOWN",
"stdout",
"assert",
"LOG_DASSL",
"LOG_DASSL_STATES",
"LOG_DEBUG",
"LOG_DSS",
"LOG_DSS_JAC",
"LOG_DT",
"LOG_EVENTS",
"LOG_EVENTS_V",
"LOG_INIT",
"LOG_IPOPT",
"LOG_IPOPT_FULL",
"LOG_IPOPT_JAC",
"LOG_IPOPT_HESSE",
"LOG_IPOPT_ERROR",
"LOG_JAC",
"LOG_LS",
"LOG_LS_V",
"LOG_NLS",
"LOG_NLS_V",
"LOG_NLS_HOMOTOPY",
"LOG_NLS_JAC",
"LOG_NLS_JAC_TEST",
"LOG_NLS_RES",
"LOG_NLS_EXTRAPOLATE",
"LOG_RES_INIT",
"LOG_RT",
"LOG_SIMULATION",
"LOG_SOLVER",
"LOG_SOLVER_CONTEXT",
"LOG_SOTI",
"LOG_STATS",
"LOG_STATS_V",
#ifdef USE_DEBUG_TRACE
"LOG_TRACE",
#endif
"LOG_UTIL",
"LOG_ZEROCROSSINGS",
};
const char *LOG_STREAM_DESC[SIM_LOG_MAX] = {
"unknown",
"this stream is always active", /* LOG_STDOUT */
"this stream is always active", /* LOG_ASSERT */
"additional information about dassl solver", /* LOG_DASSL */
"outputs the states at every dassl call", /* LOG_DASSL_STATES */
"additional debug information", /* LOG_DEBUG */
"outputs information about dynamic state selection", /* LOG_DSS */
"outputs jacobian of the dynamic state selection", /* LOG_DSS_JAC */
"additional information about dynamic tearing", /* LOG_DT */
"additional information during event iteration", /* LOG_EVENTS */
"verbose logging of event system", /* LOG_EVENTS_V */
"additional information during initialization", /* LOG_INIT */
"information from Ipopt", /* LOG_IPOPT */
"more information from Ipopt", /* LOG_IPOPT_FULL*/
"check jacobian matrix with Ipopt", /* LOG_IPOPT_JAC*/
"check hessian matrix with Ipopt", /* LOG_IPOPT_HESSE*/
"print max error in the optimization", /* LOG_IPOPT_ERROR*/
"outputs the jacobian matrix used by dassl", /* LOG_JAC */
"logging for linear systems", /* LOG_LS */
"verbose logging of linear systems", /* LOG_LS_V */
"logging for nonlinear systems", /* LOG_NLS */
"verbose logging of nonlinear systems", /* LOG_NLS_V */
"logging of homotopy solver for nonlinear systems", /* LOG_NLS_HOMOTOPY */
"outputs the jacobian of nonlinear systems", /* LOG_NLS_JAC */
"tests the analytical jacobian of nonlinear systems", /* LOG_NLS_JAC_TEST */
"outputs every evaluation of the residual function", /* LOG_NLS_RES */
"outputs debug information about extrapolate process", /* LOG_NLS_EXTRAPOLATE */
"outputs residuals of the initialization", /* LOG_RES_INIT */
"additional information regarding real-time processes",/* LOG_RT */
"additional information about simulation process", /* LOG_SIMULATION */
"additional information about solver process", /* LOG_SOLVER */
"context information during the solver process", /* LOG_SOLVER_CONTEXT" */
"final solution of the initialization", /* LOG_SOTI */
"additional statistics about timer/events/solver", /* LOG_STATS */
"additional statistics for LOG_STATS", /* LOG_STATS_V */
#ifdef USE_DEBUG_TRACE
"enables additional output to trace call stack", /* LOG_TRACE */
#endif
"???", /* LOG_UTIL*/
"additional information about the zerocrossings" /* LOG_ZEROCROSSINGS */
};
static const char *LOG_TYPE_DESC[LOG_TYPE_MAX] = {
"unknown",
"info",
"warning",
"error",
"assert",
"debug"
};
int useStream[SIM_LOG_MAX];
int level[SIM_LOG_MAX];
int lastType[SIM_LOG_MAX];
int lastStream = LOG_UNKNOWN;
int showAllWarnings = 0;
#ifdef USE_DEBUG_TRACE
int DEBUG_TRACE_PUSH_HELPER(const char* pFnc, const char* pFile, const long ln){if(useStream[LOG_TRACE]) printf("TRACE: push %s (%s:%d)\n", pFnc, pFile, ln); return 0;}
int DEBUG_TRACE_POP_HELPER(int traceID){if(useStream[LOG_TRACE]) printf("TRACE: pop\n"); return 0;}
#endif
void initDumpSystem()
{
int i;
for(i=0; i<SIM_LOG_MAX; ++i)
{
useStream[i] = 0;
level[i] = 0;
lastType[i] = 0;
}
useStream[LOG_STDOUT] = 1;
useStream[LOG_ASSERT] = 1;
}
void printInfo(int stream, FILE_INFO info)
{
infoStreamPrint(stream, 0, "[%s:%d:%d-%d:%d:%s]", info.filename, info.lineStart, info.colStart, info.lineEnd, info.colEnd, info.readonly ? "readonly" : "writable");
}
void omc_assert_function(threadData_t* threadData, FILE_INFO info, const char *msg, ...)
{
va_list ap;
va_start(ap,msg);
printInfo(LOG_ASSERT, info);
errorStreamPrint(LOG_ASSERT, 0, "Modelica Assert: ");
va_errorStreamPrint(LOG_ASSERT, 0, msg, ap);
va_end(ap);
fflush(NULL);
if (threadData) {
MMC_THROW_INTERNAL();
} else {
MMC_THROW();
}
}
void omc_assert_warning_function(FILE_INFO info, const char *msg, ...)
{
va_list ap;
va_start(ap,msg);
printInfo(LOG_ASSERT, info);
warningStreamPrint(LOG_ASSERT, 0, "Warning, assertion triggered: ");
va_warningStreamPrint(LOG_ASSERT, 0, msg, ap);
va_end(ap);
fflush(NULL);
}
void omc_throw_function(threadData_t *threadData)
{
if (threadData) {
MMC_THROW_INTERNAL();
} else {
MMC_THROW();
}
}
void omc_terminate_function(FILE_INFO info, const char *msg, ...)
{
va_list ap;
va_start(ap,msg);
printInfo(LOG_STDOUT, info);
infoStreamPrint(LOG_STDOUT, 0, "Modelica Terminate: ");
va_infoStreamPrint(LOG_STDOUT, 0, msg, ap);
va_end(ap);
fflush(NULL);
MMC_THROW();
}
static void printEscapedXML(const char *msg)
{
while (*msg) {
if (*msg == '&') fputs("&", stdout);
else if (*msg == '<') fputs("<", stdout);
else if (*msg == '>') fputs(">", stdout);
else if (*msg == '"') fputs(""", stdout);
else fputc(*msg, stdout);
msg++;
}
}
void messageText(int type, int stream, int indentNext, char *msg, int subline, const int *indexes)
{
int i;
int len;
printf("%-17s | ", (subline || (lastStream == stream && level[stream] > 0)) ? "|" : LOG_STREAM_NAME[stream]);
printf("%-7s | ", (subline || (lastStream == stream && lastType[stream] == type && level[stream] > 0)) ? "|" : LOG_TYPE_DESC[type]);
lastType[stream] = type;
lastStream = stream;
for(i=0; i<level[stream]; ++i)
printf("| ");
for(i=0; msg[i]; i++)
{
if(msg[i] == '\n')
{
msg[i] = '\0';
printf("%s\n", msg);
if (msg[i+1]) {
messageText(type, stream, 0, &msg[i+1], 1, indexes);
}
return;
}
}
len = strlen(msg);
if (len>0 && msg[len-1]=='\n') {
printf("%s", msg);
} else {
printf("%s\n", msg);
}
fflush(NULL);
if (indentNext) level[stream]++;
}
static int printXMLrootNode = 0;
static void messagesXMLroot()
{
fputs("<messages>\n", stdout);
fflush(stdout);
}
void messagesCloseXMLroot()
{
if (printXMLrootNode){
fputs("</messages>\n", stdout);
fflush(stdout);
}
}
void messageXML(int type, int stream, int indentNext, char *msg, int subline, const int *indexes)
{
/* print xml root node */
if(!printXMLrootNode){
messagesXMLroot();
printXMLrootNode = 1;
}
printf("<message stream=\"%s\" type=\"%s\" text=\"", LOG_STREAM_NAME[stream], LOG_TYPE_DESC[type]);
printEscapedXML(msg);
if (indexes) {
int i;
printf("\">\n");
for (i=1; i<=*indexes; i++) {
printf("<used index=\"%d\" />\n", indexes[i]);
}
if (!indentNext) {
fputs("</message>\n",stdout);
}
} else {
fputs(indentNext ? "\">\n" : "\" />\n", stdout);
}
fflush(stdout);
}
static void messageCloseText(int stream)
{
if(ACTIVE_STREAM(stream))
level[stream]--;
}
static void messageCloseXML(int stream)
{
if(ACTIVE_STREAM(stream))
{
fputs("</message>\n", stdout);
fflush(stdout);
}
}
static void messageCloseTextWarning(int stream)
{
if(ACTIVE_WARNING_STREAM(stream))
level[stream]--;
}
static void messageCloseXMLWarning(int stream)
{
if(ACTIVE_WARNING_STREAM(stream))
{
fputs("</message>\n", stdout);
fflush(stdout);
}
}
static void (*messageFunction)(int type, int stream, int indentNext, char *msg, int subline, const int *indexes) = messageText;
void (*messageClose)(int stream) = messageCloseText;
void (*messageCloseWarning)(int stream) = messageCloseTextWarning;
void setStreamPrintXML(int isXML)
{
if (isXML) {
messageFunction = messageXML;
messageClose = messageCloseXML;
messageCloseWarning = messageCloseXMLWarning;
} else {
messageFunction = messageText;
messageClose = messageCloseText;
messageCloseWarning = messageCloseTextWarning;
}
}
#define SIZE_LOG_BUFFER 2048
void va_infoStreamPrint(int stream, int indentNext, const char *format, va_list args)
{
if (useStream[stream]) {
char logBuffer[SIZE_LOG_BUFFER];
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
messageFunction(LOG_TYPE_INFO, stream, indentNext, logBuffer, 0, NULL);
}
}
void infoStreamPrintWithEquationIndexes(int stream, int indentNext, const int *indexes, const char *format, ...)
{
if (useStream[stream]) {
char logBuffer[SIZE_LOG_BUFFER];
va_list args;
va_start(args, format);
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
va_end(args);
messageFunction(LOG_TYPE_INFO, stream, indentNext, logBuffer, 0, indexes);
}
}
void infoStreamPrint(int stream, int indentNext, const char *format, ...)
{
if (useStream[stream]) {
char logBuffer[SIZE_LOG_BUFFER];
va_list args;
va_start(args, format);
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
va_end(args);
messageFunction(LOG_TYPE_INFO, stream, indentNext, logBuffer, 0, NULL);
}
}
void warningStreamPrintWithEquationIndexes(int stream, int indentNext, const int *indexes, const char *format, ...)
{
if (ACTIVE_WARNING_STREAM(stream)) {
char logBuffer[SIZE_LOG_BUFFER];
va_list args;
va_start(args, format);
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
va_end(args);
messageFunction(LOG_TYPE_WARNING, stream, indentNext, logBuffer, 0, indexes);
}
}
void warningStreamPrint(int stream, int indentNext, const char *format, ...)
{
if (ACTIVE_WARNING_STREAM(stream)) {
char logBuffer[SIZE_LOG_BUFFER];
va_list args;
va_start(args, format);
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
va_end(args);
messageFunction(LOG_TYPE_WARNING, stream, indentNext, logBuffer, 0, NULL);
}
}
void va_warningStreamPrintWithEquationIndexes(int stream, int indentNext, const int *indexes, const char *format, va_list args)
{
if (ACTIVE_WARNING_STREAM(stream)) {
char logBuffer[SIZE_LOG_BUFFER];
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
messageFunction(LOG_TYPE_WARNING, stream, indentNext, logBuffer, 0, indexes);
}
}
void va_warningStreamPrint(int stream, int indentNext, const char *format, va_list args)
{
if (ACTIVE_WARNING_STREAM(stream)) {
char logBuffer[SIZE_LOG_BUFFER];
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
messageFunction(LOG_TYPE_WARNING, stream, indentNext, logBuffer, 0, NULL);
}
}
void errorStreamPrint(int stream, int indentNext, const char *format, ...)
{
char logBuffer[SIZE_LOG_BUFFER];
va_list args;
va_start(args, format);
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
va_end(args);
messageFunction(LOG_TYPE_ERROR, stream, indentNext, logBuffer, 0, NULL);
}
void va_errorStreamPrint(int stream, int indentNext, const char *format, va_list args)
{
char logBuffer[SIZE_LOG_BUFFER];
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
messageFunction(LOG_TYPE_ERROR, stream, indentNext, logBuffer, 0, NULL);
}
void va_errorStreamPrintWithEquationIndexes(int stream, int indentNext, const int *indexes, const char *format, va_list args)
{
char logBuffer[SIZE_LOG_BUFFER];
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
messageFunction(LOG_TYPE_ERROR, stream, indentNext, logBuffer, 0, indexes);
}
#ifdef USE_DEBUG_OUTPUT
void debugStreamPrint(int stream, int indentNext, const char *format, ...)
{
if (useStream[stream]) {
char logBuffer[SIZE_LOG_BUFFER];
va_list args;
va_start(args, format);
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
va_end(args);
messageFunction(LOG_TYPE_DEBUG, stream, indentNext, logBuffer, 0, NULL);
}
}
void debugStreamPrintWithEquationIndexes(int stream, int indentNext, const int *indexes, const char *format, ...)
{
if (useStream[stream]) {
char logBuffer[SIZE_LOG_BUFFER];
va_list args;
va_start(args, format);
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
va_end(args);
messageFunction(LOG_TYPE_DEBUG, stream, indentNext, logBuffer, 0, indexes);
}
}
#endif
static inline jmp_buf* getBestJumpBuffer(threadData_t *threadData)
{
switch (threadData->currentErrorStage) {
case ERROR_EVENTSEARCH:
case ERROR_SIMULATION:
case ERROR_NONLINEARSOLVER:
case ERROR_INTEGRATOR:
case ERROR_OPTIMIZE:
#ifndef OMC_EMCC
return threadData->simulationJumpBuffer;
#endif
case ERROR_EVENTHANDLING:
default:
return threadData->globalJumpBuffer ? threadData->globalJumpBuffer : threadData->mmc_jumper;
}
}
void va_throwStreamPrint(threadData_t *threadData, const char *format, va_list args)
{
char logBuffer[SIZE_LOG_BUFFER];
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
messageFunction(LOG_TYPE_DEBUG, LOG_ASSERT, 0, logBuffer, 0, NULL);
threadData = threadData ? threadData : (threadData_t*)pthread_getspecific(mmc_thread_data_key);
longjmp(*getBestJumpBuffer(threadData), 1);
}
void throwStreamPrint(threadData_t *threadData, const char *format, ...)
{
va_list args;
va_start(args, format);
va_throwStreamPrint(threadData, format, args);
va_end(args);
}
void throwStreamPrintWithEquationIndexes(threadData_t *threadData, const int *indexes, const char *format, ...)
{
char logBuffer[SIZE_LOG_BUFFER];
va_list args;
va_start(args, format);
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
va_end(args);
messageFunction(LOG_TYPE_DEBUG, LOG_ASSERT, 0, logBuffer, 0, indexes);
threadData = threadData ? threadData : (threadData_t*)pthread_getspecific(mmc_thread_data_key);
longjmp(*getBestJumpBuffer(threadData), 1);
}