Repository navigation
Expand file tree
/
Copy pathLogSystem.cpp
More file actions
159 lines (133 loc) · 5.76 KB
/
Copy pathLogSystem.cpp
File metadata and controls
159 lines (133 loc) · 5.76 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
===========================================================================
Daemon BSD Source Code
Copyright (c) 2013-2016, Daemon Developers
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daemon developers nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
===========================================================================
*/
#include <common/FileSystem.h>
#include "qcommon/q_shared.h"
#include "qcommon/qcommon.h"
#include "LogSystem.h"
namespace Log {
static Target* targets[MAX_TARGET_ID];
//TODO make me reentrant // or check it is actually reentrant when using for (Event e : events) do stuff
//TODO think way more about thread safety
void Dispatch(Log::Event event, int targetControl) {
static std::vector<Log::Event> buffers[MAX_TARGET_ID];
static std::recursive_mutex bufferLocks[MAX_TARGET_ID];
if (Sys::IsProcessTerminating()) {
return;
}
for (int i = 0; i < MAX_TARGET_ID; i++) {
if ((targetControl >> i) & 1) {
std::lock_guard<std::recursive_mutex> guard(bufferLocks[i]);
auto& buffer = buffers[i];
buffer.push_back(event);
bool processed = false;
if (targets[i]) {
processed = targets[i]->Process(buffer);
}
if (processed || buffer.size() > 512) {
buffer.clear();
}
}
}
}
void RegisterTarget(TargetId id, Target* target) {
targets[id] = target;
}
Target::Target() = default;
void Target::Register(TargetId id) {
Log::RegisterTarget(id, this);
}
//Log Targets
//TODO: move them in their respective modules
//TODO this one isn't multithreaded at all, need a rewrite of the consoles
class TTYTarget : public Target {
public:
TTYTarget() {
this->Register(TTY_CONSOLE);
}
virtual bool Process(const std::vector<Log::Event>& events) override {
for (auto& event : events) {
CON_Print(event.text.c_str());
CON_Print("\n");
}
return true;
}
};
static TTYTarget tty;
//TODO add a Callback on these that will make the logFile open a new file or something?
static Cvar::Cvar<bool> useLogFile("logs.logFile.active", "are the logs sent in the logfile", Cvar::NONE, true);
static Cvar::Cvar<std::string> logFileName("logs.logFile.filename", "the name of the logfile", Cvar::INIT | Cvar::TEMPORARY, "daemon.log");
static Cvar::Cvar<bool> overwrite("logs.logFile.overwrite", "if true the logfile is deleted at each run else the logs are just appended", Cvar::INIT | Cvar::TEMPORARY, true);
static Cvar::Cvar<bool> forceFlush("logs.logFile.forceFlush", "are all the logs flushed immediately (more accurate but slower)", Cvar::INIT | Cvar::TEMPORARY, false);
class LogFileTarget: public Target {
public:
LogFileTarget() {
this->Register(LOGFILE);
}
virtual bool Process(const std::vector<Log::Event>& events) override {
//If we have no log file drop the events
if (not useLogFile.Get()) {
return true;
}
if (logFile) {
for (auto& event : events) {
logFile.Printf("%s\n", event.text);
}
return true;
} else {
return false;
}
}
FS::File logFile;
};
static LogFileTarget logfile;
void OpenLogFile() {
//If we have no log file do nothing here
if (not useLogFile.Get()) {
return;
}
try {
if (overwrite.Get()) {
logfile.logFile = FS::HomePath::OpenWrite(logFileName.Get());
} else {
logfile.logFile = FS::HomePath::OpenAppend(logFileName.Get());
}
if (forceFlush.Get()) {
logfile.logFile.SetLineBuffered(true);
}
} catch (std::system_error& err) {
Sys::Error("Could not open log file %s: %s", logFileName.Get(), err.what());
}
}
void FlushLogFile() {
std::error_code err;
logfile.logFile.Flush(err);
if (err) {
Log::Warn("Error flushing log file");
}
}
}