OMCompiler/SimulationRuntime/cpp/Core/Utils/extension/logger.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | /* | ||
| 29 | * logger.cpp | ||
| 30 | * | ||
| 31 | * Created on: 04.06.2015 | ||
| 32 | * Author: marcus and rfranke | ||
| 33 | */ | ||
| 34 | #include <Core/ModelicaDefine.h> | ||
| 35 | #include <Core/Modelica.h> | ||
| 36 | #include <Core/Utils/extension/FactoryExport.h> | ||
| 37 | #include <Core/Utils/extension/logger.hpp> | ||
| 38 | |||
| 39 |
1/2✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
|
15 | static std::string encode_char_entities(const std::string& s) |
| 40 | { | ||
| 41 | std::string r; | ||
| 42 |
1/2✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
|
15 | r.reserve(s.size()); |
| 43 |
2/2✓ Branch 0 taken 577 times.
✓ Branch 1 taken 15 times.
|
592 | for (char c : s) { |
| 44 |
1/6✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 577 times.
|
577 | switch (c) { |
| 45 | case '<': r += "<"; break; | ||
| 46 | case '>': r += ">"; break; | ||
| 47 | case '&': r += "&"; break; | ||
| 48 | case '"': r += """; break; | ||
| 49 | case '\'': r += "'"; break; | ||
| 50 |
1/2✓ Branch 1 taken 577 times.
✗ Branch 2 not taken.
|
577 | default: r += c; break; |
| 51 | } | ||
| 52 | } | ||
| 53 | 15 | return r; | |
| 54 | } | ||
| 55 | |||
| 56 | Logger* Logger::_instance = NULL; | ||
| 57 | |||
| 58 | 82 | Logger::Logger(LogSettings settings, bool enabled) | |
| 59 | : _logSettings(settings) | ||
| 60 | 164 | , _isEnabled(enabled) | |
| 61 | { | ||
| 62 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 42 times.
|
82 | if (_instance != NULL) |
| 63 | 40 | delete _instance; | |
| 64 | 82 | _instance = NULL; | |
| 65 | 82 | _startTime = _endTime = 0.0; | |
| 66 | 82 | } | |
| 67 | |||
| 68 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
159 | Logger::~Logger() |
| 69 | { | ||
| 70 | 159 | } | |
| 71 | |||
| 72 | 81 | void Logger::initialize(LogSettings settings) | |
| 73 | { | ||
| 74 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 1 time.
|
81 | switch (settings.format) { |
| 75 | case LF_TXT: | ||
| 76 |
3/4✓ Branch 1 taken 80 times.
✓ Branch 4 taken 80 times.
✓ Branch 6 taken 80 times.
✗ Branch 7 not taken.
|
80 | _instance = new Logger(settings, true); |
| 77 | 80 | break; | |
| 78 | default: | ||
| 79 |
3/6✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
2 | _instance = new LoggerXML(settings, true); |
| 80 | } | ||
| 81 | 81 | } | |
| 82 | |||
| 83 | 40 | void Logger::finalize() | |
| 84 | { | ||
| 85 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | if (_instance != NULL) |
| 86 | 40 | delete _instance; | |
| 87 | 40 | _instance = NULL; | |
| 88 | 40 | } | |
| 89 | |||
| 90 | ✗ | void Logger::writeInternal(std::string msg, LogCategory cat, LogLevel lvl, | |
| 91 | LogStructure ls) | ||
| 92 | { | ||
| 93 | ✗ | if (ls != LS_END) { | |
| 94 | ✗ | std::string catStr = getCategory(cat); | |
| 95 | ✗ | std::ostream &stream = lvl <= 1? std::cerr: std::cout; | |
| 96 | ✗ | stream << getPrefix(cat, lvl) << catStr.append(6 - catStr.length(), ' ') | |
| 97 | << ": " << msg << std::endl; | ||
| 98 | } | ||
| 99 | ✗ | } | |
| 100 | |||
| 101 | 340499 | void Logger::statusInternal(const char *, double, double) | |
| 102 | { | ||
| 103 | 340499 | } | |
| 104 | |||
| 105 | 41 | void Logger::setEnabledInternal(bool enabled) | |
| 106 | { | ||
| 107 | 41 | _isEnabled = enabled; | |
| 108 | 41 | } | |
| 109 | |||
| 110 | 40 | bool Logger::isEnabledInternal() | |
| 111 | { | ||
| 112 | 40 | return _isEnabled; | |
| 113 | } | ||
| 114 | |||
| 115 | ✗ | std::string Logger::getPrefix(LogCategory cat, LogLevel lvl) const | |
| 116 | { | ||
| 117 | ✗ | switch (lvl) { | |
| 118 | case(LL_DEBUG): | ||
| 119 | ✗ | return "DEBUG : "; | |
| 120 | case(LL_ERROR): | ||
| 121 | ✗ | return "ERROR : "; | |
| 122 | case(LL_INFO): | ||
| 123 | ✗ | return "INFO : "; | |
| 124 | case(LL_WARNING): | ||
| 125 | ✗ | return "WARNING: "; | |
| 126 | default: | ||
| 127 | ✗ | return ""; | |
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | 15 | std::string Logger::getCategory(LogCategory cat) const | |
| 132 | { | ||
| 133 |
2/8✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
|
15 | switch (cat) { |
| 134 | case(LC_INIT): | ||
| 135 | ✗ | return "init"; | |
| 136 | case(LC_NLS): | ||
| 137 | ✗ | return "nls"; | |
| 138 | case(LC_LS): | ||
| 139 | ✗ | return "ls"; | |
| 140 | case(LC_SOLVER): | ||
| 141 | 14 | return "solver"; | |
| 142 | case(LC_OUTPUT): | ||
| 143 | ✗ | return "output"; | |
| 144 | case(LC_EVENTS): | ||
| 145 | ✗ | return "events"; | |
| 146 | case(LC_MODEL): | ||
| 147 | ✗ | return "model"; | |
| 148 | case(LC_OTHER): | ||
| 149 | default: | ||
| 150 | 1 | return "other"; | |
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | 15 | std::string Logger::getLevel(LogLevel lvl) const | |
| 155 | { | ||
| 156 |
2/5✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
|
15 | switch(lvl) { |
| 157 | case(LL_ERROR): | ||
| 158 | ✗ | return "error"; | |
| 159 | case(LL_WARNING): | ||
| 160 | 1 | return "warning"; | |
| 161 | case(LL_DEBUG): | ||
| 162 | ✗ | return "debug"; | |
| 163 | case(LL_INFO): | ||
| 164 | 14 | return "info"; | |
| 165 | default: | ||
| 166 | ✗ | return "unknown"; | |
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | 1 | LoggerXML::LoggerXML(LogSettings settings, bool enabled, std::ostream &stream) | |
| 171 | : Logger(settings, enabled) | ||
| 172 |
1/2✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 | , _stream(stream) |
| 173 | { | ||
| 174 | 1 | } | |
| 175 | |||
| 176 | 2 | LoggerXML::~LoggerXML() | |
| 177 | { | ||
| 178 | 2 | } | |
| 179 | |||
| 180 | 16 | void LoggerXML::writeInternal(std::string msg, LogCategory cat, LogLevel lvl, | |
| 181 | LogStructure ls) | ||
| 182 | { | ||
| 183 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 time.
|
16 | if (ls != LS_END) { |
| 184 | 15 | _stream << "<message stream=\"" << getCategory(cat) << "\" " | |
| 185 |
2/6✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
30 | << "type=\"" << getLevel(lvl) << "\" " |
| 186 |
3/8✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 15 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
45 | << "text=\"" << encode_char_entities(msg) << "\""; |
| 187 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 14 times.
|
15 | if (ls == LS_BEGIN) |
| 188 | 1 | _stream << " >" << std::endl; | |
| 189 | else | ||
| 190 | 14 | _stream << " />" << std::endl; | |
| 191 | } | ||
| 192 | else { | ||
| 193 | 1 | _stream << "</message>" << std::endl; | |
| 194 | } | ||
| 195 | 16 | } | |
| 196 |