helics  3.5.2
loggerCore.hpp
1 /*
2 Copyright (c) 2017-2020,
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 /*
8  * LLNS Copyright Start
9  * Copyright (c) 2014-2018, Lawrence Livermore National Security
10  * This work was performed under the auspices of the U.S. Department
11  * of Energy by Lawrence Livermore National Laboratory in part under
12  * Contract W-7405-Eng-48 and in part under Contract DE-AC52-07NA27344.
13  * Produced at the Lawrence Livermore National Laboratory.
14  * All rights reserved.
15  * For details, see the LICENSE file.
16  * LLNS Copyright End
17  */
18 
19 #pragma once
20 
21 #include "gmlc/concurrency/TripWire.hpp"
22 #include "gmlc/containers/BlockingQueue.hpp"
23 
24 #include <atomic>
25 #include <functional>
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include <thread>
30 #include <utility>
31 #include <vector>
32 
33 namespace helics {
35 class LoggingCore {
36  private:
37  static std::atomic<bool> fastShutdown; // set to true to enable a fast shutdown
38  std::thread
39  loggingThread;
40  std::vector<std::function<void(std::string&& message)>>
41  functions;
42  std::mutex functionLock;
43  gmlc::containers::BlockingQueue<std::pair<int32_t, std::string>>
44  loggingQueue;
45  gmlc::concurrency::TripWireDetector tripDetector;
46 
47  public:
49  LoggingCore();
51  ~LoggingCore();
54  void addMessage(const std::string& message);
57  void addMessage(std::string&& message);
62  void addMessage(int index, const std::string& message);
67  void addMessage(int index, std::string&& message);
71  int addFileProcessor(std::function<void(std::string&& message)> newFunction);
73  void haltOperations(int loggerIndex);
75  void updateProcessingFunction(int index,
76  std::function<void(std::string&& message)> newFunction);
78  static void setFastShutdown();
79 
80  private:
81  void processingLoop();
82 };
83 
86  private:
87  static std::map<std::string, std::shared_ptr<LoggerManager>>
88  loggers;
89  std::string name;
90  std::shared_ptr<LoggingCore> loggingControl;
91  explicit LoggerManager(const std::string& loggerName);
92 
93  public:
95  static std::shared_ptr<LoggerManager> getLoggerManager(const std::string& loggerName = "");
97  static std::shared_ptr<LoggingCore> getLoggerCore(const std::string& loggerName = "");
101  static void closeLogger(const std::string& loggerName = "");
103  static void logMessage(std::string message);
104 
105  /*destructor*/
106  virtual ~LoggerManager();
108  const std::string& getName() const { return name; }
109 };
110 } // namespace helics
Definition: loggerCore.hpp:85
static void logMessage(std::string message)
Definition: loggerCore.cpp:210
static std::shared_ptr< LoggingCore > getLoggerCore(const std::string &loggerName="")
Definition: loggerCore.cpp:196
static void closeLogger(const std::string &loggerName="")
Definition: loggerCore.cpp:201
static std::shared_ptr< LoggerManager > getLoggerManager(const std::string &loggerName="")
Definition: loggerCore.cpp:180
const std::string & getName() const
Definition: loggerCore.hpp:108
Definition: loggerCore.hpp:35
void updateProcessingFunction(int index, std::function< void(std::string &&message)> newFunction)
Definition: loggerCore.cpp:100
~LoggingCore()
Definition: loggerCore.cpp:36
static void setFastShutdown()
Definition: loggerCore.cpp:86
void haltOperations(int loggerIndex)
Definition: loggerCore.cpp:91
void addMessage(const std::string &message)
Definition: loggerCore.cpp:58
LoggingCore()
Definition: loggerCore.cpp:31
int addFileProcessor(std::function< void(std::string &&message)> newFunction)
Definition: loggerCore.cpp:72
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14