Debian Codegraph @

include/ilogwriter.h

raw
1 #pragma once
2
3 #include <string>
4 #include <mutex>
5
6 namespace applog
7 {
8
9 enum class LogLevel
10 {
11 Verbose = 0,
12 Standard,
13 Warning,
14 Error,
15 };
16
17 constexpr LogLevel AllLogLevels[] = {
18 LogLevel::Verbose,
19 LogLevel::Standard,
20 LogLevel::Warning,
21 LogLevel::Error
22 };
23
24 /**
25 * greebo: A LogDevice is a class which is able to take log output.
26 *
27 * Examples of LogDevices are the Console and the DarkRadiant logfile.
28 * Note: Use the LogWriter::attach() method to register a class for logging.
29 */
30 class ILogDevice
31 {
32 public:
33 virtual ~ILogDevice() {}
34
35 /**
36 * greebo: This method gets called by the ILogWriter with
37 * a log string as argument.
38 */
39 virtual void writeLog(const std::string& outputStr, LogLevel level) = 0;
40
41 /**
42 * If this device is a console, it will be filled with the log output
43 * that has been collected before it has been attached.
44 * This is supposed to make it easier to inspect the startup phase opening
45 * the console viewer in the UI.
46 */
47 virtual bool isConsole() const
48 {
49 return false;
50 }
51 };
52
53 /**
54 * Central logging hub, dispatching any incoming log messages
55 * to all attached ILogDevices.
56 *
57 * Also offers the stream references to set up the rMessage/rWarning streams
58 * in every module.
59 */
60 class ILogWriter
61 {
62 public:
63 virtual ~ILogWriter()
64 {}
65
66 /**
67 * greebo: Writes the given buffer p with the given length to the
68 * various output devices (i.e. Console and Log file).
69 */
70 virtual void write(const char* p, std::size_t length, LogLevel level) = 0;
71
72 /**
73 * Returns the stream reference for the given log level. These are used
74 * to initialise the global rMessage/rWarning streams used by the application.
75 */
76 virtual std::ostream& getLogStream(LogLevel level) = 0;
77
78 /**
79 * Returns the synchronization object for writing to the streams.
80 */
81 virtual std::mutex& getStreamLock() = 0;
82
83 /**
84 * greebo: Use these methods to attach/detach a log device from the
85 * writer class. After attaching a device, all log output
86 * will be written to it.
87 */
88 virtual void attach(ILogDevice* device) = 0;
89 virtual void detach(ILogDevice* device) = 0;
90 };
91
92 }