OMSimulatorLib
The OMSimulator project is a FMI-based co-simulation environment that supports ordinary (i.e., non-delayed) and TLM connections.
Logging.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenModelica.
3  *
4  * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
5  * c/o Linköpings universitet, Department of Computer and Information Science,
6  * SE-58183 Linköping, Sweden.
7  *
8  * All rights reserved.
9  *
10  * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
11  * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12  * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13  * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14  * ACCORDING TO RECIPIENTS CHOICE.
15  *
16  * The OpenModelica software and the Open Source Modelica
17  * Consortium (OSMC) Public License (OSMC-PL) are obtained
18  * from OSMC, either from the above address,
19  * from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20  * http://www.openmodelica.org, and in the OpenModelica distribution.
21  * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22  *
23  * This program is distributed WITHOUT ANY WARRANTY; without
24  * even the implied warranty of MERCHANTABILITY or FITNESS
25  * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26  * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27  *
28  * See the full OSMC Public License conditions for more details.
29  *
30  */
31 
32 #ifndef _OMS_LOGGING_H_
33 #define _OMS_LOGGING_H_
34 
35 #include "OMSimulator/Types.h"
36 
37 #include <string>
38 #include <fstream>
39 #include <mutex>
40 
41 #ifndef __FUNCTION_NAME__
42  #ifdef WIN32 //WINDOWS
43  #define __FUNCTION_NAME__ __FUNCTION__
44  #else //*NIX
45  #define __FUNCTION_NAME__ __func__
46  #endif
47 #endif
48 
49 class Log
50 {
51 public:
52  static void Info(const std::string& msg);
53  static oms_status_enu_t Warning(const std::string& msg);
54  static oms_status_enu_t Error(const std::string& msg, const std::string& function);
55  static void Debug(const std::string& msg);
56  static void Trace(const std::string& function, const std::string& file, const long line);
57 
58  static void ProgressBar(double start, double stop, double value);
59  static void TerminateBar();
60 
61  static oms_status_enu_t setLogFile(const std::string& filename);
62  static void setMaxLogFileSize(const unsigned long size) {getInstance().limit=1024*1024*size;}
63 
64  static void setLoggingCallback(void (*cb)(oms_message_type_enu_t type, const char* message)) {getInstance().cb = cb;}
65  static oms_status_enu_t setLoggingLevel(int logLevel);
66  static const int getLoggingLevel();
67 
68  static bool DebugEnabled();
69  static bool TraceEnabled();
70 
71 private:
72  Log();
73  ~Log();
74 
75  static Log& getInstance();
76  void printStringToStream(std::ostream& stream, const std::string& type, const std::string& msg);
77 
78  // stop the compiler generating methods copying the object
79  Log(Log const& copy);
80  Log& operator=(Log const& copy);
81 
82 private:
83  int logLevel;
84  std::string filename;
85  std::ofstream logFile;
86  std::mutex m;
87  unsigned int numWarnings;
88  unsigned int numErrors;
89  unsigned int numMessages;
90 
91  unsigned long limit = 1024*1024*50;
92  unsigned long size = 0;
93 
94  bool progress = false;
95  int percent;
96 
97  void (*cb)(oms_message_type_enu_t type, const char* message);
98 };
99 
100 #define logInfo(msg) Log::Info(msg)
101 #define logWarning(msg) Log::Warning(msg)
102 #define logError(msg) Log::Error(msg, __func__)
103 
104 #if !defined(NDEBUG)
105  // In case some preparation is required
106  #define logDebugEnabled() Log::DebugEnabled()
107  #define logTraceEnabled() Log::TraceEnabled()
108 
109  #define logDebug(msg) Log::Debug(msg)
110  #define logTrace() Log::Trace(__FUNCTION_NAME__, __FILE__, __LINE__)
111 #else
112  #define logDebugEnabled() (0)
113  #define logTraceEnabled() (0)
114 
115  #define logDebug(msg) ((void)0)
116  #define logTrace() ((void)0)
117 #endif
118 
119 // common error messages
120 #define logError_AlreadyInScope(cref) logError("\"" + std::string(cref) + "\" already exists in the scope")
121 #define logError_BusAndConnectorNotSameModel(bus, connector) logError("Bus \"" + std::string(bus) + "\" and connector \"" + std::string(connector) + "\" do not belong to same model")
122 #define logError_BusAndConnectorNotSameSystem(bus, connector) logError("Bus \"" + std::string(bus) + "\" and connector \"" + std::string(connector) + "\" do not belong to same system")
123 #define logError_BusNotInComponent(cref, component) logError("Bus connector \"" + std::string(cref) + "\" not found in component \"" + std::string(component->getFullCref()) + "\"")
124 #define logError_BusNotInSystem(cref, system) logError("Bus connector \"" + std::string(cref) + "\" not found in system \"" + std::string(system->getFullCref()) + "\"")
125 #define logError_ComponentNotInSystem(system, component) logError("System \"" + std::string(system->getFullCref()) + "\" does not contain component \"" + std::string(component) + "\"")
126 #define logError_ConnectionExistsAlready(crefA, crefB, system) logError("Connection <\"" + std::string(crefA) + "\", \"" + std::string(crefB) + "\"> exists already in system \"" + std::string(system->getFullCref()) + "\"")
127 #define logError_ConnectionNotInSystem(crefA, crefB, system) logError("Connection <\"" + std::string(crefA) + "\", \"" + std::string(crefB) + "\"> not found in system \"" + std::string(system->getFullCref()) + "\"")
128 #define logError_ConnectorNotInComponent(cref, component) logError("Connector \"" + std::string(cref) + "\" not found in component \"" + std::string(component->getFullCref()) + "\"")
129 #define logError_ConnectorNotInSystem(cref, system) logError("Connector \"" + std::string(cref) + "\" not found in system \"" + std::string(system->getFullCref()) + "\"")
130 #define logError_FMUCall(call, fmu) logError(std::string(call) + " failed for FMU \"" + std::string(fmu->getFullCref()) + "\"")
131 #define logError_Initialization(system) logError("Initialization of system \"" + std::string(system) + "\" failed")
132 #define logError_InternalError logError("internal error")
133 #define logError_InvalidIdent(cref) logError("\"" + std::string(cref) + "\" is not a valid ident")
134 #define logError_InvalidIdent(cref) logError("\"" + std::string(cref) + "\" is not a valid ident")
135 #define logError_ModelInWrongState(cref) logError("Model \"" + std::string(cref) + "\" is in wrong model state")
136 #define logError_ModelNotInScope(cref) logError("Model \"" + std::string(cref) + "\" does not exist in the scope")
137 #define logError_NoConnectorsInTLMBus(cref) logError("No connectors in TLM bus: \"" + std::string(cref) + "\"")
138 #define logError_NotForExternalModels logError("Not available for external models")
139 #define logError_NotForScSystem logError("Not available for strongly coupled systems")
140 #define logError_NotForTlmSystem logError("Not available for TLM systems")
141 #define logError_NotImplemented logError("Not implemented")
142 #define logError_OnlyForExternalModels logError("Only available for TLM sub models (aka external models)")
143 #define logError_OnlyForModel logError("Only implemented for model identifiers")
144 #define logError_OnlyForRealInputs(cref) logError("Signal \"" + std::string(cref) + "\" is not a real input signal")
145 #define logError_OnlyForSystemTLM logError("Only available for TLM systems")
146 #define logError_OnlyForSystemWC logError("Only available for WC systems")
147 #define logError_ResetFailed(system) logError("failed to reset system \"" + std::string(system) + "\" to instantiation mode")
148 #define logError_SubSystemNotInSystem(system, subsystem) logError("System \"" + std::string(system) + "\" does not contain subsystem \"" + std::string(subsystem) + "\"")
149 #define logError_SystemNotInModel(model, system) logError("Model \"" + std::string(model) + "\" does not contain system \"" + std::string(system) + "\"")
150 #define logError_Termination(system) logError("Termination of system \"" + std::string(system) + "\" failed")
151 #define logError_TlmBusNotInComponent(cref, component) logError("TLM bus connector \"" + std::string(cref) + "\" not found in component \"" + std::string(component->getFullCref()) + "\"")
152 #define logError_TlmBusNotInSystem(cref, system) logError("TLM bus connector \"" + std::string(cref) + "\" not found in system \"" + std::string(system->getFullCref()) + "\"")
153 #define logError_UnknownSignal(cref) logError("Unknown signal \"" + std::string(cref) + "\"")
154 #define logError_UnknownTLMVariableType(vartype) logError("Unknown TLM variable type: \""+vartype+"\"")
155 #define logError_VariableTypeAlreadyInTLMBus(cref,vartype) logError("TLM bus connector \"" + std::string(cref) + "\" already contains a variable with type \"" + vartype + "\"")
156 #define logError_WrongSchema(name) logError("Wrong xml schema detected. Unexpected tag \"" + name + "\"")
157 #define logWarning_deprecated logWarning("Wrong/deprecated content detected but successfully loaded. Please re-export the SSP file to avoid this message.")
158 
159 #endif
Log::numWarnings
unsigned int numWarnings
Definition: Logging.h:87
Log::filename
std::string filename
Definition: Logging.h:84
Log::logFile
std::ofstream logFile
Definition: Logging.h:85
Log::Trace
static void Trace(const std::string &function, const std::string &file, const long line)
Definition: Logging.cpp:215
Log::ProgressBar
static void ProgressBar(double start, double stop, double value)
Definition: Logging.cpp:294
Log::m
std::mutex m
Definition: Logging.h:86
Log::getInstance
static Log & getInstance()
Definition: Logging.cpp:70
Log::TerminateBar
static void TerminateBar()
Definition: Logging.cpp:324
Log::Info
static void Info(const std::string &msg)
Definition: Logging.cpp:141
Log::setLoggingCallback
static void setLoggingCallback(void(*cb)(oms_message_type_enu_t type, const char *message))
Definition: Logging.h:64
Log::Debug
static void Debug(const std::string &msg)
Definition: Logging.cpp:193
Log::numErrors
unsigned int numErrors
Definition: Logging.h:88
Version.h
Log
Definition: Logging.h:49
Log::progress
bool progress
Definition: Logging.h:94
Log::size
unsigned long size
Definition: Logging.h:92
Log::printStringToStream
void printStringToStream(std::ostream &stream, const std::string &type, const std::string &msg)
Definition: Logging.cpp:77
Log::TraceEnabled
static bool TraceEnabled()
Definition: Logging.cpp:209
Log::getLoggingLevel
static const int getLoggingLevel()
Definition: Logging.cpp:288
Log::setLoggingLevel
static oms_status_enu_t setLoggingLevel(int logLevel)
Definition: Logging.cpp:272
Log::setLogFile
static oms_status_enu_t setLogFile(const std::string &filename)
Definition: Logging.cpp:233
Log::DebugEnabled
static bool DebugEnabled()
Definition: Logging.cpp:187
Log::cb
void(* cb)(oms_message_type_enu_t type, const char *message)
Definition: Logging.h:97
Log::logLevel
int logLevel
Definition: Logging.h:83
Log::~Log
~Log()
Definition: Logging.cpp:64
Log::setMaxLogFileSize
static void setMaxLogFileSize(const unsigned long size)
Definition: Logging.h:62
Log::Log
Log()
Definition: Logging.cpp:56
Log::Error
static oms_status_enu_t Error(const std::string &msg, const std::string &function)
Definition: Logging.cpp:170
logError
#define logError(msg)
Definition: Logging.h:102
std
Definition: ComRef.h:88
Log::operator=
Log & operator=(Log const &copy)
not implemented
oms_git_version
const char * oms_git_version
Log::percent
int percent
Definition: Logging.h:95
Logging.h
Log::numMessages
unsigned int numMessages
Definition: Logging.h:89
Log::limit
unsigned long limit
Definition: Logging.h:91
TimeStr
std::string TimeStr()
Definition: Logging.cpp:44
Log::Warning
static oms_status_enu_t Warning(const std::string &msg)
Definition: Logging.cpp:154