helics  3.3.0
LogManager.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2022,
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;
51 
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::LogManager
Definition: LogManager.hpp:35
helics::SUMMARY
@ SUMMARY
print/log summary information
Definition: logging.hpp:26
helics::LogManager::setLogLevel
void setLogLevel(int32_t level)
Definition: LogManager.cpp:208
helics::ERROR_LEVEL
@ ERROR_LEVEL
only print errors
Definition: logging.hpp:23
helics::DUMPLOG
@ DUMPLOG
only for dumplog
Definition: logging.hpp:21
HELICS_LOG_LEVEL_WARNING
@ HELICS_LOG_LEVEL_WARNING
Definition: helics_enums.h:192
LogManager.hpp
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::LogManager::logFlush
void logFlush()
Definition: LogManager.cpp:213
HELICS_LOG_LEVEL_DUMPLOG
@ HELICS_LOG_LEVEL_DUMPLOG
Definition: helics_enums.h:184
helics::LogBuffer
Definition: LogBuffer.hpp:24
helics::LogManager::sendToLogger
bool sendToLogger(int logLevel, std::string_view header, std::string_view message, bool disableRemote=false) const
Definition: LogManager.cpp:90
helics::LogManager::addLoggingCLI
void addLoggingCLI(std::shared_ptr< helicsCLI11App > &app)
Definition: LogManager.cpp:153
helics::LogManager::setLogLevels
void setLogLevels(int32_t consoleLevel, int32_t fileLevel)
Definition: LogManager.cpp:223
helics::ActionMessage::payload
SmallBuffer payload
buffer to contain the data payload
Definition: ActionMessage.hpp:48
helics::ActionMessage::dest_id
GlobalFederateId dest_id
20 fed_id for a targeted message
Definition: ActionMessage.hpp:39
helics::TIMING
@ TIMING
print interfaces+timing(exec/grant/disconnect)
Definition: logging.hpp:31
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::LogManager::forceLoggingFlush
std::atomic< bool > forceLoggingFlush
force the log to flush after every message
Definition: LogManager.hpp:58
helics::LogManager::setLoggerFunction
void setLoggerFunction(std::function< void(int level, std::string_view identifier, std::string_view message)> logFunction)
Definition: LogManager.cpp:202
helics::TRACE
@ TRACE
trace level printing (all processed messages)
Definition: logging.hpp:34
helics::WARNING
@ WARNING
print/log warning and errors
Definition: logging.hpp:25
helics::PROFILING
@ PROFILING
profiling log level
Definition: logging.hpp:24
helics::FED
@ FED
special logging command for message coming from a fed
Definition: logging.hpp:35