Skip to content

Commit 038fa39

Browse files
Fix logging (#70)
* Fix private logging Signed-off-by: ClemensLinnhoff <[email protected]> * Add OSMPVersion and MODELVERSION for modelDescription.xml Signed-off-by: ClemensLinnhoff <[email protected]> * Fix public logging Signed-off-by: Marcel Kettelgerdes <[email protected]> --------- Signed-off-by: ClemensLinnhoff <[email protected]> Signed-off-by: Marcel Kettelgerdes <[email protected]> Co-authored-by: Marcel Kettelgerdes <[email protected]>
1 parent 91fcff5 commit 038fa39

10 files changed

+94
-24
lines changed

.clang-tidy

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ CheckOptions:
66
- key: readability-identifier-naming.AbstractClassCases
77
value: CamelCase
88
- key: readability-identifier-naming.AbstractClassPrefix
9-
value: I
9+
value: ''
1010
- key: readability-identifier-naming.AbstractClassSuffix
1111
value: ''
1212
- key: readability-identifier-naming.ClassCase
1313
value: CamelCase
1414
- key: readability-identifier-naming.ClassConstantCase
1515
value: CamelCase
1616
- key: readability-identifier-naming.ClassConstantPrefix
17-
value: k
17+
value: ''
1818
- key: readability-identifier-naming.ClassConstantSuffix
1919
value: ''
2020
- key: readability-identifier-naming.ClassMemberCase
2121
value: lower_case
2222
- key: readability-identifier-naming.ClassMemberPrefix
23-
value: g_
23+
value: ''
2424
- key: readability-identifier-naming.ClassMemberSuffix
2525
value: ''
2626
- key: readability-identifier-naming.ClassMethodCase
@@ -74,7 +74,7 @@ CheckOptions:
7474
- key: readability-identifier-naming.EnumConstantCase
7575
value: aNy_CasE
7676
- key: readability-identifier-naming.EnumConstantPrefix
77-
value: k
77+
value: ''
7878
- key: readability-identifier-naming.EnumConstantSuffix
7979
value: ''
8080
- key: readability-identifier-naming.EnumPrefix
@@ -90,7 +90,7 @@ CheckOptions:
9090
- key: readability-identifier-naming.GlobalConstantCase
9191
value: CamelCase
9292
- key: readability-identifier-naming.GlobalConstantPrefix
93-
value: k
93+
value: ''
9494
- key: readability-identifier-naming.GlobalConstantSuffix
9595
value: ''
9696
- key: readability-identifier-naming.GlobalFunctionCase
@@ -102,11 +102,11 @@ CheckOptions:
102102
- key: readability-identifier-naming.GlobalVariableCase
103103
value: lower_case
104104
- key: readability-identifier-naming.GlobalVariablePrefix
105-
value: g_
105+
value: ''
106106
- key: readability-identifier-naming.GlobalVariableSuffix
107107
value: ''
108108
- key: readability-identifier-naming.IgnoreFailedSplit
109-
value: '0'
109+
value: ''
110110
- key: readability-identifier-naming.InlineNamespaceCase
111111
value: lower_case
112112
- key: readability-identifier-naming.InlineNamespacePrefix

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ get_directory_property(OSI_VERSION_MAJOR DIRECTORY lib/open-simulation-interface
88
get_directory_property(OSI_VERSION_MINOR DIRECTORY lib/open-simulation-interface DEFINITION VERSION_MINOR)
99
get_directory_property(OSI_VERSION_PATCH DIRECTORY lib/open-simulation-interface DEFINITION VERSION_PATCH)
1010
set(OSIVERSION "${OSI_VERSION_MAJOR}.${OSI_VERSION_MINOR}.${OSI_VERSION_PATCH}")
11+
set(OSMPVERSION "1.4.0" CACHE STRING "OSMP Version String")
12+
set(MODELVERSION "0.5.0" CACHE STRING "Model Version String")
1113

1214
include_directories(lib/fmi2/headers)
1315

src/CMakeLists.txt

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
set(CMAKE_CXX_STANDARD 11)
22
set(CMAKE_CXX_STANDARD_REQUIRED ON)
33
set(LINK_WITH_SHARED_OSI OFF CACHE BOOL "Link FMU with shared OSI library instead of statically linking")
4-
set(PUBLIC_LOGGING OFF CACHE BOOL "Enable logging via FMI logger")
5-
set(PRIVATE_LOGGING OFF CACHE BOOL "Enable private logging to file")
4+
set(PUBLIC_LOGGING ON CACHE BOOL "Enable logging via FMI logger")
5+
set(PRIVATE_LOGGING ON CACHE BOOL "Enable private logging to file")
6+
if(WIN32)
7+
set(PRIVATE_LOG_PATH "C:/TEMP/sl-1-0-sensor-model-repository-template.log" CACHE FILEPATH "Path to write private log file to")
8+
else()
9+
set(PRIVATE_LOG_PATH "/tmp/sl-1-0-sensor-model-repository-template.log" CACHE FILEPATH "Path to write private log file to")
10+
endif()
11+
if(PRIVATE_LOGGING)
12+
file(TO_NATIVE_PATH ${PRIVATE_LOG_PATH} PRIVATE_LOG_PATH_NATIVE)
13+
string(REPLACE "\\" "\\\\" PRIVATE_LOG_PATH ${PRIVATE_LOG_PATH_NATIVE})
14+
endif()
15+
set(VERBOSE_FMI_LOGGING OFF CACHE BOOL "Enable detailed FMI function logging")
616

717
string(TIMESTAMP FMUTIMESTAMP UTC)
818
string(MD5 FMUGUID modelDescription.in.xml)
919
configure_file(modelDescription.in.xml modelDescription.xml @ONLY)
20+
configure_file(OSMPConfig.in.h OSMPConfig.h)
1021

1122
find_package(Protobuf 2.6.1 REQUIRED)
1223
add_library(sl-1-0-sensor-model-repository-template SHARED
@@ -22,6 +33,8 @@ else()
2233
target_link_libraries(sl-1-0-sensor-model-repository-template open_simulation_interface_pic)
2334
endif()
2435

36+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
37+
2538
if(WIN32)
2639
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
2740
set(FMI_BINARIES_PLATFORM "win64")
@@ -51,6 +64,7 @@ add_custom_command(TARGET sl-1-0-sensor-model-repository-template
5164
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/modelDescription.xml" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu"
5265
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/OSMP.cpp" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/sources/"
5366
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/OSMP.h" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/sources/"
67+
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/OSMPConfig.h" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/sources/"
5468
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/MySensorModel.cpp" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/sources/"
5569
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/MySensorModel.h" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/sources/"
5670
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/../sl-1-0-sensor-model-repository-template.srmd" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/extra/net.pmsf.ssp.srmd/resourceMetaData.srmd"

src/MySensorModel.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,20 @@
1313
#include "osi_sensordata.pb.h"
1414
#include "osi_sensorview.pb.h"
1515

16-
void MySensorModel::Init(double nominal_range_in)
16+
#ifdef PRIVATE_LOG_PATH
17+
ofstream MySensorModel::private_log_file;
18+
#endif
19+
20+
void MySensorModel::Init(double nominal_range_in, string theinstance_name, fmi2CallbackFunctions thefunctions, bool thelogging_on)
1721
{
22+
logging_categories_.clear();
23+
logging_categories_.insert("FMI");
24+
logging_categories_.insert("OSMP");
25+
logging_categories_.insert("OSI");
26+
instance_name_ = theinstance_name;
27+
functions_ = thefunctions;
28+
logging_on_ = thelogging_on;
29+
1830
nominal_range_ = nominal_range_in;
1931
}
2032

src/MySensorModel.h

+29-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@
66
//
77

88
#pragma once
9+
#include <cstdarg>
10+
#include <fstream>
11+
#include <iostream>
12+
#include <set>
13+
#include <string>
914

15+
#include "OSMPConfig.h"
16+
#include "fmi2Functions.h"
1017
#include "osi_sensordata.pb.h"
1118

19+
using namespace std;
20+
1221
class MySensorModel
1322
{
1423
public:
15-
void Init(double nominal_range_in);
24+
void Init(double nominal_range_in, string theinstance_name, fmi2CallbackFunctions thefunctions, bool thelogging_on);
25+
1626
osi3::SensorData Step(osi3::SensorView current_in, double time);
1727

1828
static void RotatePointXYZ(double x, double y, double z, double yaw, double pitch, double roll, double& rx, double& ry, double& rz);
@@ -40,6 +50,11 @@ class MySensorModel
4050
double mounting_position_roll);
4151

4252
private:
53+
string instance_name_;
54+
bool logging_on_;
55+
set<string> logging_categories_;
56+
fmi2CallbackFunctions functions_;
57+
4358
double nominal_range_;
4459

4560
/* Private File-based Logging just for Debugging */
@@ -55,7 +70,9 @@ class MySensorModel
5570
va_start(ap, format);
5671
char buffer[1024];
5772
if (!private_log_file.is_open())
73+
{
5874
private_log_file.open(PRIVATE_LOG_PATH, ios::out | ios::app);
75+
}
5976
if (private_log_file.is_open())
6077
{
6178
#ifdef _WIN32
@@ -82,17 +99,23 @@ class MySensorModel
8299
#endif
83100
#ifdef PRIVATE_LOG_PATH
84101
if (!private_log_file.is_open())
102+
{
85103
private_log_file.open(PRIVATE_LOG_PATH, ios::out | ios::app);
104+
}
86105
if (private_log_file.is_open())
87106
{
88-
private_log_file << "OSMPDummySensor"
89-
<< "::" << instanceName << "<" << ((void*)this) << ">:" << category << ": " << buffer << endl;
107+
private_log_file << "MySensorModel"
108+
<< "::"
109+
<< "template"
110+
<< "<" << ((void*)this) << ">:" << category << ": " << buffer << endl;
90111
private_log_file.flush();
91112
}
92113
#endif
93114
#ifdef PUBLIC_LOGGING
94-
if (loggingOn && loggingCategories.count(category))
95-
functions.logger(functions.componentEnvironment, instanceName.c_str(), fmi2OK, category, buffer);
115+
if (logging_on_ && (logging_categories_.count(category) != 0U))
116+
{
117+
functions_.logger(functions_.componentEnvironment, instance_name_.c_str(), fmi2OK, category, buffer);
118+
}
96119
#endif
97120
#endif
98121
}
@@ -113,7 +136,7 @@ class MySensorModel
113136
#if defined(PRIVATE_LOG_PATH) || defined(PUBLIC_LOGGING)
114137
va_list ap;
115138
va_start(ap, format);
116-
internal_log(category, format, ap);
139+
InternalLog(category, format, ap);
117140
va_end(ap);
118141
#endif
119142
}

src/OSMP.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
using namespace std;
4848

4949
#ifdef PRIVATE_LOG_PATH
50-
ofstream COSMPDummySensor::private_log_file;
50+
ofstream OSMP::private_log_file;
5151
#endif
5252

5353
/*
@@ -262,7 +262,7 @@ fmi2Status OSMP::DoExitInitializationMode()
262262
}
263263

264264
// initialize sensor model
265-
my_sensor_model_.Init(FmiNominalRange());
265+
my_sensor_model_.Init(FmiNominalRange(), instance_name_, functions_, logging_on_);
266266

267267
return fmi2OK;
268268
}

src/OSMP.h

+12-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef FMU_SHARED_OBJECT
1010
#define FMI2_FUNCTION_PREFIX OSMPDummySensor_
1111
#endif
12+
#include "OSMPConfig.h"
1213
#include "fmi2Functions.h"
1314

1415
/*
@@ -168,17 +169,23 @@ class OSMP
168169
#endif
169170
#ifdef PRIVATE_LOG_PATH
170171
if (!private_log_file.is_open())
172+
{
171173
private_log_file.open(PRIVATE_LOG_PATH, ios::out | ios::app);
174+
}
172175
if (private_log_file.is_open())
173176
{
174-
private_log_file << "OSMPDummySensor"
175-
<< "::" << instanceName << "<" << ((void*)this) << ">:" << category << ": " << buffer << endl;
177+
private_log_file << "OSMP"
178+
<< "::"
179+
<< "model"
180+
<< "<" << ((void*)this) << ">:" << category << ": " << buffer << endl;
176181
private_log_file.flush();
177182
}
178183
#endif
179184
#ifdef PUBLIC_LOGGING
180-
if (loggingOn && loggingCategories.count(category))
181-
functions.logger(functions.componentEnvironment, instanceName.c_str(), fmi2OK, category, buffer);
185+
if (logging_on_ && (logging_categories_.count(category) != 0U))
186+
{
187+
functions_.logger(functions_.componentEnvironment, instance_name_.c_str(), fmi2OK, category, buffer);
188+
}
182189
#endif
183190
#endif
184191
}
@@ -199,7 +206,7 @@ class OSMP
199206
#if defined(PRIVATE_LOG_PATH) || defined(PUBLIC_LOGGING)
200207
va_list ap;
201208
va_start(ap, format);
202-
internal_log(category, format, ap);
209+
InternalLog(category, format, ap);
203210
va_end(ap);
204211
#endif
205212
}

src/OSMPConfig.in.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// Copyright 2016 -- 2018 PMSF IT Consulting Pierre R. Mai
3+
// Copyright 2023 BMW AG
4+
// SPDX-License-Identifier: MPL-2.0
5+
//
6+
7+
#cmakedefine PUBLIC_LOGGING
8+
#cmakedefine PRIVATE_LOG_PATH "@PRIVATE_LOG_PATH@"
9+
#cmakedefine VERBOSE_FMI_LOGGING
10+
#cmakedefine DEBUG_BREAKS
11+
#define FMU_GUID "@FMUGUID@"

src/modelDescription.in.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
guid="@FMUGUID@"
66
description="Demonstration C++ Sensor FMU for OSI Sensor Model Packaging within OpenMSL"
77
author="Persival GmbH"
8-
version="@OSMPVERSION@"
8+
version="@MODELVERSION@"
99
generationTool="Based on OSMPDummySensor"
1010
generationDateAndTime="@FMUTIMESTAMP@"
1111
variableNamingConvention="structured">

test/unit/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ project(UnitTests)
33
# 'lib' is the folder with Google Test sources
44
add_subdirectory(lib)
55
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
6+
include_directories(${CMAKE_CURRENT_BINARY_DIR}/../../src)
67

78
# 'Google_Tests_run' is the target name
89
# 'test1.cpp tests2.cpp' are source files with tests

0 commit comments

Comments
 (0)