<commit_before>#include "framework/logger_file.h"
#include "framework/logger.h"
#include "framework/options.h"

#include <fstream>

namespace OpenApoc
{

namespace
{

LogFunction previousFunction; // To allow chaining log functions

LogLevel fileLogLevel = LogLevel::Nothing;
LogLevel backtraceLogLevel = LogLevel::Nothing;
std::ofstream logFile;

void FileLogFunction(LogLevel level, UString prefix, const UString &text)
{
	previousFunction(level, prefix, text);
	if (level <= fileLogLevel)
	{
		UString levelPrefix;
		switch (level)
		{
			case LogLevel::Error:
				levelPrefix = "E";
				break;
			case LogLevel::Warning:
				levelPrefix = "W";
				break;
			case LogLevel::Info:
				levelPrefix = "I";
				break;
			case LogLevel::Debug:
				levelPrefix = "D";
				break;
			default:
				levelPrefix = "U";
				break;
		}
		auto message = OpenApoc::format("%s %s: %s", levelPrefix, prefix, text);
		logFile << message << std::endl;
	}

	if (level <= backtraceLogLevel)
	{
		auto backtrace = getBacktrace();
		for (const auto &frame : backtrace)
		{
			logFile << frame << std::endl;
		}
	}
}

} // namespace

void enableFileLogger(const char *outputFile)
{
	LogAssert(outputFile);
	logFile.open(outputFile);
	if (!logFile.good())
	{
		LogError("File logger failed to open file \"%s\"", outputFile);
	}
	fileLogLevel = (LogLevel)Options::fileLogLevelOption.get();
	backtraceLogLevel = (LogLevel)Options::backtraceLogLevelOption.get();
	previousFunction = getLogCallback();
	setLogCallback(FileLogFunction);
}

} // namespace OpenApoc
<commit_msg>Flush stream in file_logger on warning and above<commit_after>#include "framework/logger_file.h"
#include "framework/logger.h"
#include "framework/options.h"

#include <fstream>

namespace OpenApoc
{

namespace
{

LogFunction previousFunction; // To allow chaining log functions

LogLevel fileLogLevel = LogLevel::Nothing;
LogLevel backtraceLogLevel = LogLevel::Nothing;
std::ofstream logFile;

void FileLogFunction(LogLevel level, UString prefix, const UString &text)
{
	previousFunction(level, prefix, text);
	auto flush = false;
	if (level <= fileLogLevel)
	{
		UString levelPrefix;
		switch (level)
		{
			case LogLevel::Error:
				levelPrefix = "E";
				flush = true;
				break;
			case LogLevel::Warning:
				levelPrefix = "W";
				flush = true;
				break;
			case LogLevel::Info:
				levelPrefix = "I";
				break;
			case LogLevel::Debug:
				levelPrefix = "D";
				break;
			default:
				levelPrefix = "U";
				break;
		}
		const auto message = OpenApoc::format("%s %s: %s", levelPrefix, prefix, text);
		logFile << message << std::endl;
	}

	if (level <= backtraceLogLevel)
	{
		const auto backtrace = getBacktrace();
		for (const auto &frame : backtrace)
		{
			logFile << frame << std::endl;
		}
		flush = true;
	}
	if (flush)
		logFile.flush();
}

} // namespace

void enableFileLogger(const char *outputFile)
{
	LogAssert(outputFile);
	logFile.open(outputFile);
	if (!logFile.good())
	{
		LogError("File logger failed to open file \"%s\"", outputFile);
	}
	fileLogLevel = (LogLevel)Options::fileLogLevelOption.get();
	backtraceLogLevel = (LogLevel)Options::backtraceLogLevelOption.get();
	previousFunction = getLogCallback();
	setLogCallback(FileLogFunction);
}

} // namespace OpenApoc

{'fraction_non_alphanumeric': 0.13050169282856264, 'fraction_numerical': 0.0, 'mean_word_length': 4.354200988467875, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7188414', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1068, 'n_words': 259}