File: FileSink.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (334 lines) | stat: -rw-r--r-- 8,099 bytes parent folder | download | duplicates (3)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "FileSink.h"
#include "Backend.h"
#include "FramePrefixer.h"
#include "Level.h" // for LOG_LEVEL_*
#include "System/MainDefines.h"
#include "System/Log/ILog.h"
#include "System/Log/Level.h"

#include <cassert>
#include <cstdio>
#include <string>

#include <algorithm>
#include <vector>


namespace log_file {
	struct LogFileDetails {
		LogFileDetails(
			FILE* outStream = nullptr,
			const std::string& sections = "",
			int minLevel = LOG_LEVEL_ALL,
			int flushLevel = LOG_LEVEL_ERROR
		)
			: outStream(outStream)
			, sections(sections)
			, minLevel(minLevel)
			, flushLevel(flushLevel)
		{}

		FILE* GetOutStream() const {
			return outStream;
		}

		bool IsLogging(int level, const char* section) const {
			return ((level >= minLevel) && (sections.empty() || (sections.find("," + std::string(section) + ",") != std::string::npos)));
		}
		bool FlushOnWrite(int level) const {
			return (level >= flushLevel);
		}

	private:
		FILE* outStream;
		std::string sections;
		int minLevel;
		int flushLevel;
	};

	/**
	 * This is only used to check whether some code tries to access the
	 * log-files container after it got deleted.
	 */
	bool validTracker = true;


	/**
	 * This class allows us to stop logging cleanly, when the application exits,
	 * and while the container is still valid (not deleted yet).
	 */
	struct LogFilesContainer {
	public:
		typedef std::pair<std::string, LogFileDetails> LogFilePair;
		typedef std::vector<LogFilePair> LogFilesMap;

		~LogFilesContainer() {
			log_file_removeAllLogFiles();
			validTracker = false;
		}
		LogFilesMap& GetLogFiles() {
			return logFiles;
		}

	private:
		std::vector< std::pair<std::string, LogFileDetails> > logFiles;
	};

	using LogFilePair = LogFilesContainer::LogFilePair;
	using LogFilesMap = LogFilesContainer::LogFilesMap;


	inline LogFilesMap& getLogFiles() {
		static LogFilesContainer logFilesContainer;

		assert(validTracker);
		return (logFilesContainer.GetLogFiles());
	}


	/**
	 * This class allows us to stop logging cleanly, when the application exits,
	 * and while the container is still valid (not deleted yet).
	 */
	struct LogRecord {
		LogRecord(int level, const std::string& section, const std::string& record)
			: level(level)
			, section(section)
			, record(record)
		{}

		int GetLevel() const { return level; }
		const std::string& GetSection() const { return section; }
		const std::string& GetRecord() const { return record; }

	private:
		int level;
		std::string section;
		std::string record;
	};
	typedef std::vector<LogRecord> logRecords_t;


	inline logRecords_t& getRecordBuffer() {
		static logRecords_t buffer;
		return buffer;
	}

	inline bool isActivelyLogging() {
		return (!getLogFiles().empty());
	}

	void writeToFile(FILE* outStream, const char* record, bool flush) {
		char framePrefix[128] = {'\0'};
		log_framePrefixer_createPrefix(framePrefix, sizeof(framePrefix));

		FPRINTF(outStream, "%s%s\n", framePrefix, record);

		if (flush)
			fflush(outStream);
	}

	/**
	 * Writes to the individual log files, if they do want to log the section.
	 */
	void writeToFiles(int level, const char* section, const char* record)
	{
		const auto& logFiles = getLogFiles();

		for (const auto& p: logFiles) {
			if (!p.second.IsLogging(level, section))
				continue;
			if (p.second.GetOutStream() == nullptr)
				continue;

			writeToFile(p.second.GetOutStream(), record, p.second.FlushOnWrite(level));
		}
	}

	/**
	 * Flushes the buffers of the individual log files.
	 */
	void flushFiles() {
		const auto& logFiles = getLogFiles();

		for (const auto& p: logFiles) {
			if (p.second.GetOutStream() == nullptr)
				continue;

			fflush(p.second.GetOutStream());
		}
	}
	/**

	 * Writes the content of the buffer to all the currently registered log
	 * files.
	 */
	void writeBufferToFiles() {
		logRecords_t& logRecords = getRecordBuffer();

		for (LogRecord& logRec: logRecords) {
			writeToFiles(logRec.GetLevel(), logRec.GetSection().c_str(), logRec.GetRecord().c_str());
		}

		logRecords.clear();
	}

	inline void writeToBuffer(int level, const std::string& section, const std::string& record)
	{
		logRecords_t& logRecords = getRecordBuffer();

		if (logRecords.empty())
			logRecords.reserve(1024);

		logRecords.emplace_back(level, section, record);
	}
}


#ifdef __cplusplus
extern "C" {
#endif

void log_file_addLogFile(
	const char* filePath,
	const char* sections,
	int minLevel,
	int flushLevel
) {
	assert(filePath != nullptr);

	auto& logFiles = log_file::getLogFiles();

	const std::string sectionsStr = (sections == nullptr) ? "" : sections;
	const std::string filePathStr = filePath;

	const auto pred = [](const log_file::LogFilePair& a, const log_file::LogFilePair& b) { return (a.first < b.first); };
	const auto iter = std::lower_bound(logFiles.begin(), logFiles.end(), log_file::LogFilePair{filePathStr, nullptr}, pred);

	// we are already logging to this file
	if (iter != logFiles.end() && iter->first == filePathStr)
		return;

	FILE* tmpStream = fopen(filePath, "w");

	if (tmpStream == nullptr) {
		LOG_L(L_ERROR, "[%s] failed to open log file \"%s\" for writing", __func__, filePath);
		return;
	}

	setvbuf(tmpStream, nullptr, _IOFBF, std::min(BUFSIZ, 8192)); // limit buffer to 8kB

	logFiles.emplace_back(filePathStr, log_file::LogFileDetails(tmpStream, sectionsStr, minLevel, flushLevel));

	// swap into position; only a handful of files are ever added
	for (size_t i = logFiles.size() - 1; i > 0; i--) {
		if (logFiles[i - 1].first < logFiles[i].first)
			break;

		std::swap(logFiles[i - 1], logFiles[i]);
	}
}

void log_file_removeLogFile(const char* filePath) {
	assert(filePath != nullptr);

	auto& logFiles = log_file::getLogFiles();

	const auto pred = [](const log_file::LogFilePair& a, const log_file::LogFilePair& b) { return (a.first < b.first); };
	const auto iter = std::lower_bound(logFiles.begin(), logFiles.end(), log_file::LogFilePair{filePath, nullptr}, pred);

	// we are not logging to this file
	if (iter == logFiles.end() || strcmp(iter->first.c_str(), filePath) != 0)
		return;

	// turn off logging to this file
	fclose(iter->second.GetOutStream());

	// erase entry
	for (size_t i = iter - logFiles.begin(), n = logFiles.size() - 1; i < n; i++) {
		logFiles[i].first  = std::move(logFiles[i + 1].first );
		logFiles[i].second = std::move(logFiles[i + 1].second);
	}

	logFiles.pop_back();
}

void log_file_removeAllLogFiles() {
	auto& logFiles = log_file::getLogFiles();

	for (auto& logFilePair: logFiles) {
		fclose(logFilePair.second.GetOutStream());
	}

	logFiles.clear();
}


FILE* log_file_getLogFileStream(const char* filePath) {
	const auto& logFiles = log_file::getLogFiles();

	for (const auto& p: logFiles) {
		if (strcmp((p.first).c_str(), filePath) != 0)
			continue;

		return ((p.second).GetOutStream());
	}

	return nullptr;
}



/**
 * @name logging_sink_file
 * ILog.h sink implementation.
 */
///@{

/// Records a log entry
static void log_sink_record_file(int level, const char* section, const char* record)
{
	if (log_file::validTracker && log_file::isActivelyLogging()) {
		// write buffer to log file
		log_file::writeBufferToFiles();

		// write current record to log file
		log_file::writeToFiles(level, section, record);
	} else {
		// buffer until a log file is ready for output
		log_file::writeToBuffer(level, section, record);
	}
}

/// Cleans up all log streams, by flushing them.
static void log_sink_cleanup_file() {
	if (!log_file::isActivelyLogging())
		return;

	// flush the log buffers to files
	log_file::flushFiles();
}

///@}


namespace {
	/// Auto-registers the sink defined in this file before main() is called
	struct FileSinkRegistrator {
		FileSinkRegistrator() {
			log_backend_registerSink(&log_sink_record_file);
			log_backend_registerCleanup(&log_sink_cleanup_file);
		}
		~FileSinkRegistrator() {
			log_backend_unregisterSink(&log_sink_record_file);
			log_backend_unregisterCleanup(&log_sink_cleanup_file);
		}
	} fileSinkRegistrator;
}

#ifdef __cplusplus
} // extern "C"
#endif