<commit_before>#include <x0/DebugLogger.h>
#include <x0/Tokenizer.h>
#include <x0/Buffer.h>
#include <cstdlib>
#include <cstdarg>

namespace x0 {

DebugLogger::Instance::Instance(DebugLogger* logger, const std::string& tag) :
	logger_(logger),
	tag_(tag),
	enabled_(false),
	verbosity_(1),
	codes_(),
	pre_(),
	post_()
{
}

DebugLogger::Instance::~Instance()
{
}

void DebugLogger::Instance::enable()
{
	printf("Instance(%s).enable()\n", tag_.c_str());
	enabled_ = true;
}

void DebugLogger::Instance::disable()
{
	printf("Instance(%s).disable()\n", tag_.c_str());
	enabled_ = false;
}

void DebugLogger::Instance::setPreference(const std::string& value)
{
	static const struct {
		int value;
		const std::string name;
	} codes[] = {
		{ 1, "bold" },
		{ 2, "faint"} ,
		{ 3, "italic" },
		{ 4, "underline" },
		{ 5, "blink" },

		{ 11, "font1" },
		{ 12, "font2" },

		{ 30, "black" },
		{ 31, "red" },
		{ 32, "green" },
		{ 33, "yellow" },
		{ 34, "blue" },
		{ 35, "magenta" },
		{ 36, "cyan" },
		{ 37, "white" },

		{ 40, "bg-black" },
		{ 41, "bg-red" },
		{ 42, "bg-green" },
		{ 43, "bg-yellow" },
		{ 44, "bg-blue" },
		{ 45, "bg-magenta" },
		{ 46, "bg-cyan" },
		{ 47, "bg-white" },
	};

	bool found = false;
	for (const auto& code: codes) {
		if (value == code.name) {
			codes_.push_back(code.value);
			found = true;
			break;
		}
	}

	if (!found)
		return;

	// update ansii code strings
	Buffer buf;
	buf.push_back("\033[");
	for (size_t i = 0; i < codes_.size(); ++i) {
		if (likely(i))
			buf.push_back(';');

		buf.push_back(codes_[i]);
	}
	buf.push_back("m");
	pre_ += buf.str();

	if (post_.empty())
		post_ = "\033[0m";
}

void DebugLogger::Instance::log(int level, const char* fmt, ...)
{
	if (!enabled_)
		return;

	if (level > verbosity_)
		return;

	char msg[1024];
	va_list va;
	va_start(va, fmt);
	vsnprintf(msg, sizeof(msg), fmt, va);
	va_end(va);

	if (logger_->colored()) {
		printf("%s[%s:%d] %s%s\n", pre_.c_str(), tag_.c_str(), level, msg, post_.c_str());
	} else {
		printf("[%s:%d] %s\n", tag_.c_str(), level, msg);
	}
}<commit_msg>[base] DebugLogger: fixes untagged logging to go through the onLogWrite message-redirector, too<commit_after>#include <x0/DebugLogger.h>
#include <x0/Tokenizer.h>
#include <x0/Buffer.h>
#include <cstdlib>
#include <cstdarg>

namespace x0 {

DebugLogger::Instance::Instance(DebugLogger* logger, const std::string& tag) :
	logger_(logger),
	tag_(tag),
	enabled_(false),
	verbosity_(1),
	codes_(),
	pre_(),
	post_()
{
}

DebugLogger::Instance::~Instance()
{
}

void DebugLogger::Instance::enable()
{
	printf("Instance(%s).enable()\n", tag_.c_str());
	enabled_ = true;
}

void DebugLogger::Instance::disable()
{
	printf("Instance(%s).disable()\n", tag_.c_str());
	enabled_ = false;
}

void DebugLogger::Instance::setPreference(const std::string& value)
{
	static const struct {
		int value;
		const std::string name;
	} codes[] = {
		{ 1, "bold" },
		{ 2, "faint"} ,
		{ 3, "italic" },
		{ 4, "underline" },
		{ 5, "blink" },

		{ 11, "font1" },
		{ 12, "font2" },

		{ 30, "black" },
		{ 31, "red" },
		{ 32, "green" },
		{ 33, "yellow" },
		{ 34, "blue" },
		{ 35, "magenta" },
		{ 36, "cyan" },
		{ 37, "white" },

		{ 40, "bg-black" },
		{ 41, "bg-red" },
		{ 42, "bg-green" },
		{ 43, "bg-yellow" },
		{ 44, "bg-blue" },
		{ 45, "bg-magenta" },
		{ 46, "bg-cyan" },
		{ 47, "bg-white" },
	};

	bool found = false;
	for (const auto& code: codes) {
		if (value == code.name) {
			codes_.push_back(code.value);
			found = true;
			break;
		}
	}

	if (!found)
		return;

	// update ansii code strings
	Buffer buf;
	buf.push_back("\033[");
	for (size_t i = 0; i < codes_.size(); ++i) {
		if (likely(i))
			buf.push_back(';');

		buf.push_back(codes_[i]);
	}
	buf.push_back("m");
	pre_ += buf.str();

	if (post_.empty())
		post_ = "\033[0m";
}

void DebugLogger::Instance::log(int level, const char* fmt, ...)
{
	if (!enabled_)
		return;

	if (level > verbosity_)
		return;

	char msg[1024];
	va_list va;
	va_start(va, fmt);
	vsnprintf(msg, sizeof(msg), fmt, va);
	va_end(va);

	if (logger_->colored()) {
		Buffer buf;
		buf.printf("%s[%s:%d] %s%s", pre_.c_str(), tag_.c_str(), level, msg, post_.c_str());
		logger_->onLogWrite(buf.c_str(), buf.size());
	} else {
		Buffer buf;
		buf.printf("[%s:%d] %s", tag_.c_str(), level, msg);
		logger_->onLogWrite(buf.c_str(), buf.size());
	}
}
{'fraction_non_alphanumeric': 0.2265409383624655, 'fraction_numerical': 0.027598896044158234, 'mean_word_length': 3.7118093174431204, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, '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': '481609', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1804, 'n_words': 375}