| helics 3.6.1

LogManager.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2025,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8
14 #include "../common/LogBuffer.hpp"
15 #include "../helics_enums.h"
16 #include "FederateIdExtra.hpp"
17
18 #include <atomic>
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 namespace spdlog {
26 class logger;
27 }
28
29 namespace helics {
30 class LogBuffer;
31 class helicsCLI11App;
32 class LogBuffer;
33 class ActionMessage;
34
35 class LogManager {
36 private:
37 std::string logIdentifier;
38 std::atomic<int32_t> maxLogLevel{HELICS_LOG_LEVEL_WARNING};
40 int32_t consoleLogLevel{HELICS_LOG_LEVEL_WARNING};
42 int32_t fileLogLevel{HELICS_LOG_LEVEL_WARNING};
44 std::vector<std::pair<GlobalFederateId, std::int32_t>> remoteTargets;
46 std::shared_ptr<spdlog::logger> consoleLogger;
48 std::shared_ptr<spdlog::logger> fileLogger;
49 std::atomic<bool> initialized{false};
50 mutable LogBuffer mLogBuffer;
52 std::function<void(int, std::string_view, std::string_view)> loggerFunction;
53 std::function<void(ActionMessage&& mm)> mTransmit;
54 std::string logFile;
55
56 public:
58 std::atomic<bool> forceLoggingFlush{false};
59
60 ~LogManager();
61
62 void initializeLogging(const std::string& identifier);
63
67 bool sendToLogger(int logLevel,
68 std::string_view header,
69 std::string_view message,
70 bool disableRemote = false) const;
72 void addLoggingCLI(std::shared_ptr<helicsCLI11App>& app);
73
75 void setLogLevel(int32_t level);
80 void setLogLevels(int32_t consoleLevel, int32_t fileLevel);
82 void logFlush();
83
89 void setLoggerFunction(
90 std::function<void(int level, std::string_view identifier, std::string_view message)>
91 logFunction);
92
93 void setTransmitCallback(std::function<void(ActionMessage&& mm)> transmit)
94
95 {
96 mTransmit = std::move(transmit);
97 }
98 int getMaxLevel() const { return maxLogLevel.load(); }
99 int getFileLevel() const { return fileLogLevel; }
100 int getConsoleLevel() const { return consoleLogLevel; }
101 void setLoggingFile(std::string_view lfile, const std::string& identifier);
102 LogBuffer& getLogBuffer() { return mLogBuffer; }
103 void updateRemote(GlobalFederateId destination, int level);
104
105 private:
106 void updateMaxLogLevel();
107 };
108
109 } // namespace helics
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::LogBuffer
Definition: LogBuffer.hpp:22
helics::LogManager
Definition: LogManager.hpp:35
helics::LogManager::forceLoggingFlush
std::atomic< bool > forceLoggingFlush
force the log to flush after every message
Definition: LogManager.hpp:58
helics::LogManager::logFlush
void logFlush()
Definition: LogManager.cpp:215
helics::LogManager::setLogLevel
void setLogLevel(int32_t level)
Definition: LogManager.cpp:210
helics::LogManager::addLoggingCLI
void addLoggingCLI(std::shared_ptr< helicsCLI11App > &app)
Definition: LogManager.cpp:155
helics::LogManager::setLogLevels
void setLogLevels(int32_t consoleLevel, int32_t fileLevel)
Definition: LogManager.cpp:225
helics::LogManager::setLoggerFunction
void setLoggerFunction(std::function< void(int level, std::string_view identifier, std::string_view message)> logFunction)
Definition: LogManager.cpp:204
helics::LogManager::sendToLogger
bool sendToLogger(int logLevel, std::string_view header, std::string_view message, bool disableRemote=false) const
Definition: LogManager.cpp:92
HELICS_LOG_LEVEL_WARNING
@ HELICS_LOG_LEVEL_WARNING
Definition: helics_enums.h:212
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14