OMCompiler/SimulationRuntime/c/moo/streamlog.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 | #ifndef MOO_OM_LOG_H | ||
| 29 | #define MOO_OM_LOG_H | ||
| 30 | |||
| 31 | #include "util/omc_error.h" | ||
| 32 | |||
| 33 | #include "streamlog.h" | ||
| 34 | |||
| 35 | |||
| 36 | namespace OpenModelica { | ||
| 37 | |||
| 38 | // TODO: how to make the distinction between OMEdit target and console / .mos file target? | ||
| 39 | |||
| 40 | ✗ | void create_set_logger() { | |
| 41 | // auto logger = std::make_unique<StreamLogger>(); | ||
| 42 | // Log::set_global_logger(std::move(logger)); | ||
| 43 | auto logger = std::make_unique<OMEditStreamLogger>(); | ||
| 44 | ✗ | Log::set_global_logger(std::move(logger)); | |
| 45 | ✗ | } | |
| 46 | |||
| 47 | ✗ | void StreamLogger::log(LogLevel lvl, std::string msg) { | |
| 48 | std::string formatted; | ||
| 49 | ✗ | switch (lvl) { | |
| 50 | case LogLevel::Info: | ||
| 51 | ✗ | formatted = fmt::format("{}\n", msg); | |
| 52 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "%s", formatted.c_str()); | |
| 53 | break; | ||
| 54 | case LogLevel::Success: | ||
| 55 | ✗ | formatted = fmt::format("{}\n", msg); | |
| 56 | ✗ | infoStreamPrint(OMC_LOG_SUCCESS, 0, "%s", formatted.c_str()); | |
| 57 | break; | ||
| 58 | case LogLevel::Warning: | ||
| 59 | ✗ | formatted = fmt::format("{}\n", msg); | |
| 60 | ✗ | warningStreamPrint(OMC_LOG_MOO, 0, "%s", formatted.c_str()); | |
| 61 | break; | ||
| 62 | case LogLevel::Error: | ||
| 63 | ✗ | formatted = fmt::format("{}\n", msg); | |
| 64 | ✗ | errorStreamPrint(OMC_LOG_MOO, 0, "%s", formatted.c_str()); | |
| 65 | break; | ||
| 66 | } | ||
| 67 | ✗ | } | |
| 68 | |||
| 69 | // split the message into lines, preserving trailing newline as an empty line | ||
| 70 | ✗ | void OMEditStreamLogger::log(LogLevel lvl, std::string msg) { | |
| 71 | std::vector<std::string> lines; | ||
| 72 | size_t start = 0; | ||
| 73 | size_t pos = 0; | ||
| 74 | ✗ | while ((pos = msg.find('\n', start)) != std::string::npos) { | |
| 75 | ✗ | lines.push_back(msg.substr(start, pos - start)); | |
| 76 | ✗ | start = pos + 1; | |
| 77 | } | ||
| 78 | |||
| 79 | // if msg ends with '\n', push an empty line to preserve the newline | ||
| 80 | ✗ | if (start <= msg.size()) lines.push_back(msg.substr(start)); | |
| 81 | |||
| 82 | // print each line, first line normally, rest as sub-lines | ||
| 83 | ✗ | for (auto& line : lines) { | |
| 84 | ✗ | switch (lvl) { | |
| 85 | case LogLevel::Info: | ||
| 86 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "%s", line.c_str()); | |
| 87 | break; | ||
| 88 | case LogLevel::Success: | ||
| 89 | ✗ | infoStreamPrint(OMC_LOG_SUCCESS, 0, "%s", line.c_str()); | |
| 90 | break; | ||
| 91 | case LogLevel::Warning: | ||
| 92 | ✗ | warningStreamPrint(OMC_LOG_MOO, 0, "%s", line.c_str()); | |
| 93 | break; | ||
| 94 | case LogLevel::Error: | ||
| 95 | ✗ | errorStreamPrint(OMC_LOG_MOO, 0, "%s", line.c_str()); | |
| 96 | break; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | ✗ | } | |
| 100 | |||
| 101 | } // namespace OpenModelica | ||
| 102 | |||
| 103 | #endif // MOO_OM_LOG_H | ||
| 104 |