Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2026 BMW AG
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 : #pragma once
11 :
12 : #include <cstdint>
13 : #include <ctime>
14 : #include <iomanip>
15 : #include <iostream>
16 : #include <mutex>
17 : #include <sstream>
18 :
19 : namespace simulation
20 : {
21 :
22 : /**
23 : * Centralized thread-safe simulation logger class
24 : */
25 : class Logger
26 : {
27 : public:
28 : /**
29 : * Log function with cluster ID
30 : * Accepts multiple arguments and builds the message internally
31 : * @param clusterId The cluster ID
32 : * @param args Arguments to be concatenated into the log message
33 : */
34 : template<typename... Args>
35 86 : static void log(uint8_t clusterId, Args&&... args)
36 : {
37 86 : std::ostringstream oss;
38 86 : oss << getTimestamp() << getClusterId(clusterId);
39 86 : ((oss << args), ...); // C++17 fold expression
40 86 : writeLog(oss.str());
41 86 : }
42 :
43 : /**
44 : * Statistics logging function with cluster ID
45 : * Accepts multiple arguments and builds the message internally
46 : * @param clusterId The cluster ID
47 : * @param args Arguments to be concatenated into the log message
48 : */
49 : template<typename... Args>
50 : static void logStats(uint8_t clusterId, Args&&... args)
51 : {
52 : std::ostringstream oss;
53 : oss << getTimestamp() << getClusterId(clusterId);
54 : ((oss << args), ...); // C++17 fold expression
55 : writeLog(oss.str());
56 : }
57 :
58 : /**
59 : * General message logging
60 : * Accepts multiple arguments and builds the message internally
61 : * @param args Arguments to be concatenated into the log message
62 : */
63 : template<typename... Args>
64 13 : static void log(Args&&... args)
65 : {
66 13 : std::ostringstream oss;
67 13 : ((oss << args), ...); // C++17 fold expression
68 13 : writeLog(oss.str());
69 13 : }
70 :
71 : private:
72 : static std::mutex logMutex_;
73 : static void writeLog(std::string const& logLine);
74 : static std::string getTimestamp();
75 : static std::string getClusterId(uint8_t const id);
76 : };
77 :
78 : } // namespace simulation
|