libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
Output.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef OUTPUT_H_
32 #define OUTPUT_H_
33 
34 #include "../Exception.h"
35 
36 #include <string>
37 #include <memory>
38 #include <vector>
39 #include <map>
40 #include <chrono>
41 #include <ctime>
42 #include <mutex>
43 #include <functional>
44 #include <atomic>
45 
46 namespace BaseLib
47 {
48 class SharedObjects;
49 
54 class Output
55 {
56 public:
61  Output();
62 
67  virtual ~Output();
68 
75  void init(SharedObjects* baseLib);
76 
82  std::string getPrefix();
83 
89  void setPrefix(const std::string& prefix);
90 
94  void enableStdOutput();
95 
99  void disableStdOutput();
100 
104  void setOutputCallback(std::function<void(int32_t, const std::string&)> value);
105 
110  static std::string getTimeString(int64_t time = 0);
111 
120  void printEx(const std::string& file, uint32_t line, const std::string& function, const std::string& what = "");
121 
132  void printCritical(const std::string& message);
133 
144  void printError(const std::string& message);
145 
156  void printWarning(const std::string& message);
157 
168  void printInfo(const std::string& message);
169 
181  void printDebug(const std::string& message, int32_t minDebugLevel = 5);
182 
195  void printMessage(const std::string& message, int32_t minDebugLevel = 0, bool errorLog = false);
196 
200 private:
201 
205  BaseLib::SharedObjects* _bl = nullptr;
206 
210  std::string _prefix;
211 
215  std::atomic_bool _stdOutput{ true };
216 
220  static std::mutex _outputMutex;
221 
225  std::function<void(int32_t, const std::string&)> _outputCallback;
226 
227  Output(const Output&);
228  Output& operator=(const Output&);
229 };
230 }
231 #endif
void setPrefix(const std::string &prefix)
Sets a string, which will be used to prefix all output.
Definition: Output.cpp:47
void printError(const std::string &message)
Prints an error message (debug level < 2).
Definition: Output.cpp:149
void init(SharedObjects *baseLib)
Initializes the object.
Definition: Output.cpp:71
This is the base library main class.
Definition: BaseLib.h:95
Definition: BaseLib.cpp:34
void printWarning(const std::string &message)
Prints a warning message (debug level < 3).
Definition: Output.cpp:166
Class to print output of different kinds to the standard and error output.
Definition: Output.h:54
void printMessage(const std::string &message, int32_t minDebugLevel=0, bool errorLog=false)
Prints a message regardless of the current debug level.
Definition: Output.cpp:213
PVariable value
Definition: UiElements.h:217
void printCritical(const std::string &message)
Prints a critical error message (debug level < 1).
Definition: Output.cpp:132
void printDebug(const std::string &message, int32_t minDebugLevel=5)
Prints a debug message (debug level < 5).
Definition: Output.cpp:198
static std::string getTimeString(int64_t time=0)
Returns a time string like "08/27/14 14:13:53.471".
Definition: Output.cpp:76
void setOutputCallback(std::function< void(int32_t, const std::string &)> value)
Sets a callback function which will be called for all messages.
Definition: Output.cpp:62
void disableStdOutput()
Disables standard output and standard error.
Definition: Output.cpp:57
std::string getPrefix()
Returns the prefix previously defined with setPrefix.
Definition: Output.cpp:42
Output()
The main constructor.
virtual ~Output()
The destructor.
void printInfo(const std::string &message)
Prints a info message (debug level < 4).
Definition: Output.cpp:183
void printEx(const std::string &file, uint32_t line, const std::string &function, const std::string &what="")
Prints an error message with filename, line number and function name.
Definition: Output.cpp:101
void enableStdOutput()
Enables standard output and standard error.
Definition: Output.cpp:52