-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.h
90 lines (76 loc) · 2.43 KB
/
Logger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef LOGGER_H
#define LOGGER_H
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <ctime>
//! constants for logger output prefixes, use with logger.out(prefix)
//! INFO: operation successfull, just informing the user
#define LOGGER_INFO 0
//! WARNING: operation error, but recoverable - possibly something, that just may imply some error somewhere
#define LOGGER_WARNING 1
//! ERROR: operation error, unrecoverable - program should end without finishing computation
#define LOGGER_ERROR 2
class Logger {
private:
//! helper class: LoggerStream just uses a LoggerBuffer
class LoggerStream : public std::ostream {
//! helper class: buffer that prefixes each line with current time
class LoggerBuffer : public std::stringbuf {
std::ostream& m_out;
Logger* m_parentLogger;
public:
LoggerBuffer(Logger* parentLogger, std::ostream& out) : m_out(out), m_parentLogger(parentLogger) {}
virtual int sync ();
};
LoggerBuffer buffer;
public:
LoggerStream(Logger* parentLogger, std::ostream& stream) : std::ostream(&buffer), buffer(parentLogger, stream) {}
};
//! is logging enabled?
bool m_logging;
//! is logging to fileset via logger interface?
bool m_using_file;
//! stream to send logs to
LoggerStream* m_out;
/** get current time formatted into string
* @return time
*/
std::string getTime() const;
/**
* get current date formatted into string
* @return date
*/
std::string getDate() const;
public:
Logger();
~Logger();
/** makes stream for logs available
* @return stream
*/
std::ostream& out();
/** makes stream for logs available with common prefix
* @param prefix prefix constant
* @return stream
*/
std::ostream& out(int prefix);
/** set stream for logs
* @param outStream new stream for logging
*/
void setOutputStream(std::ostream& outStream = std::clog);
/** set logging into file (if existing, new logs are appended)
* @param filePath file for logs
*/
void setOutputFile(const std::string filePath = "default_log.txt");
/** is logging enabled?
* @return logging enabled?
*/
bool getLogging();
/** enable/disable logging
* @param state
*/
void setlogging(bool state);
};
#endif // LOGGER_H