helics  3.5.2
logger.h
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 <atomic>
22 #include <fstream>
23 #include <memory>
24 #include <mutex>
25 #include <string>
26 #include <utility>
27 
28 namespace helics {
29 class LoggingCore;
30 
31 constexpr int always_log = -100000;
32 constexpr int log_everything = 100;
33 
38 class Logger {
39  private:
40  std::atomic<bool> halted{true};
41  std::mutex fileLock;
42  std::atomic<bool> hasFile{false};
43  std::ofstream outFile;
44  std::shared_ptr<LoggingCore> logCore;
45  int coreIndex = -1;
46  std::atomic<int> consoleLevel{
48  std::atomic<int> fileLevel{log_everything};
49  public:
51  Logger();
53  explicit Logger(std::shared_ptr<LoggingCore> core);
55  ~Logger();
58  void openFile(const std::string& file);
61  void closeFile();
65  void startLogging(int cLevel, int fLevel);
67  void startLogging() { startLogging(consoleLevel, fileLevel); }
69  void haltLogging();
74  void log(int level, std::string logMessage);
78  void log(std::string logMessage) { log(always_log, std::move(logMessage)); }
80  void flush();
82  bool isRunning() const;
86  void changeLevels(int cLevel, int fLevel);
87 
88  private:
90  void logFunction(std::string&& message);
91 };
92 
95  private:
96  std::ofstream outFile;
97  public:
100  public:
105  explicit LoggerNoThread(const std::shared_ptr<LoggingCore>& core);
108  void openFile(const std::string& file);
110  void closeFile();
114  void startLogging(int cLevel, int fLevel);
117  // NOTE:: the interface for log in the noThreadLogging is slightly different
118  // due to the threaded Logger making use of move semantics which isn't that useful in the
119  // noThreadLogger
124  void log(int level, const std::string& logMessage);
128  void log(const std::string& logMessage) { log(always_log, logMessage); }
130  bool isRunning() const;
132  void flush();
136  void changeLevels(int cLevel, int fLevel);
137 };
138 } // namespace helics
Definition: logger.h:94
void flush()
Definition: logger.cpp:164
void closeFile()
Definition: logger.cpp:135
void changeLevels(int cLevel, int fLevel)
Definition: logger.cpp:146
int fileLevel
level below which we need to print to a file
Definition: logger.h:99
void log(const std::string &logMessage)
Definition: logger.h:128
int consoleLevel
level below which we need to print to the console
Definition: logger.h:98
bool isRunning() const
Definition: logger.cpp:172
void log(int level, const std::string &logMessage)
Definition: logger.cpp:152
void startLogging()
Definition: logger.h:116
void openFile(const std::string &file)
Definition: logger.cpp:130
Definition: logger.h:38
Logger()
Definition: logger.cpp:28
void startLogging()
Definition: logger.h:67
void log(int level, std::string logMessage)
Definition: logger.cpp:82
~Logger()
Definition: logger.cpp:39
void closeFile()
Definition: logger.cpp:53
bool isRunning() const
Definition: logger.cpp:96
void log(std::string logMessage)
Definition: logger.h:78
void openFile(const std::string &file)
Definition: logger.cpp:43
void changeLevels(int cLevel, int fLevel)
Definition: logger.cpp:76
void haltLogging()
Definition: logger.cpp:69
void flush()
Definition: logger.cpp:92
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
constexpr int log_everything
level that will log everything
Definition: logger.h:32
constexpr int always_log
level that will always log
Definition: logger.h:31