MicroModelicaCCompiler  4.5.3
debug.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #include "debug.hpp"
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 namespace MicroModelica {
27 
28 namespace Util {
29 
30 static const char *enableFlags = nullptr; // controls which DEBUG messages are printed
31 
32 void debugInit(const char *flagList) { enableFlags = flagList; }
33 
34 bool debugIsEnabled(char flag)
35 {
36  if (enableFlags != nullptr)
37  return (strchr(enableFlags, flag) != 0) || (strchr(enableFlags, '+') != 0);
38  else
39  return false;
40 }
41 
42 void DEBUG(char flag, const char *format, ...)
43 {
44  if (debugIsEnabled(flag)) {
45  va_list ap;
46  va_start(ap, format);
47  vfprintf(stdout, format, ap);
48  fflush(stdout);
49  va_end(ap);
50  }
51 }
52 
53 bool isDebugParam(char *param)
54 {
55  if (strcmp(param, "c") == 0) {
56  return true;
57  }
58  if (strcmp(param, "a") == 0) {
59  return true;
60  }
61  if (strcmp(param, "s") == 0) {
62  return true;
63  }
64  return false;
65 }
66 
67 void ERROR(const char *format, ...)
68 {
69  va_list ap;
70  va_start(ap, format);
71  char *error_string = "Error: ";
72  char *new_format = (char *)malloc(sizeof(char) * strlen(error_string) + (strlen(format)));
73  strcpy(new_format, error_string);
74  strcat(new_format, format);
75  vfprintf(stderr, new_format, ap);
76  fflush(stderr);
77  va_end(ap);
78  exit(EXIT_FAILURE);
79 }
80 
81 void ERROR_UNLESS(bool condition, const char *format, ...)
82 {
83  if (!condition) {
84  va_list ap;
85  va_start(ap, format);
86  ERROR(format, ap);
87  va_end(ap);
88  }
89 }
90 
91 } // namespace Util
92 } // namespace MicroModelica
MicroModelica::Util::ERROR
void ERROR(const char *format,...)
Definition: debug.cpp:101
MicroModelica::Util::enableFlags
static const char * enableFlags
Definition: debug.cpp:64
MicroModelica::Util::isDebugParam
bool isDebugParam(char *param)
Definition: debug.cpp:87
MicroModelica::Util::debugInit
void debugInit(const char *flagList)
Definition: debug.cpp:66
MicroModelica::Util::debugIsEnabled
bool debugIsEnabled(char flag)
Definition: debug.cpp:68
MicroModelica::Util::DEBUG
void DEBUG(char flag, const char *format,...)
Definition: debug.cpp:76
MicroModelica::Util::ERROR_UNLESS
void ERROR_UNLESS(bool condition, const char *format,...)
Definition: debug.cpp:115
debug.hpp
MicroModelica
Definition: files.cpp:45