bump
This commit is contained in:
+207
-87
@@ -12,70 +12,131 @@
|
|||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <optional>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include <boost/format.hpp>
|
#include <boost/format.hpp>
|
||||||
|
|
||||||
|
// Forward declarations
|
||||||
|
class DaemonLogger;
|
||||||
|
|
||||||
|
// The Severity enum is defined outside the class to be available to the template helper,
|
||||||
|
// resolving the incomplete type compilation error.
|
||||||
|
enum class Severity { Debug, Info, Warning, Error, _Count };
|
||||||
|
|
||||||
|
|
||||||
|
// Вспомогательный шаблонный класс для управления каскадными свойствами
|
||||||
|
template <typename T>
|
||||||
|
class CascadingPropertyResolver {
|
||||||
|
private:
|
||||||
|
std::mutex mtx_;
|
||||||
|
T global_default_;
|
||||||
|
std::unordered_map<std::string, T> category_defaults_;
|
||||||
|
std::unordered_map<std::string, std::unordered_map<Severity, T>> severity_specific_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void setGlobal(T value) {
|
||||||
|
std::lock_guard<std::mutex> lock(mtx_);
|
||||||
|
global_default_ = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setForCategory(const std::string& category, T value) {
|
||||||
|
std::lock_guard<std::mutex> lock(mtx_);
|
||||||
|
category_defaults_[category] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setForSeverity(const std::string& category, Severity severity, T value) {
|
||||||
|
std::lock_guard<std::mutex> lock(mtx_);
|
||||||
|
severity_specific_[category][severity] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
T resolve(const std::string& category, Severity severity) {
|
||||||
|
std::lock_guard<std::mutex> lock(mtx_);
|
||||||
|
// Правило 1: Ищем [категория, уровень]
|
||||||
|
if (auto cat_it = severity_specific_.find(category); cat_it != severity_specific_.end()) {
|
||||||
|
if (auto sev_it = cat_it->second.find(severity); sev_it != cat_it->second.end()) {
|
||||||
|
return sev_it->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Правило 2: Ищем [категория]
|
||||||
|
if (auto cat_it = category_defaults_.find(category); cat_it != category_defaults_.end()) {
|
||||||
|
return cat_it->second;
|
||||||
|
}
|
||||||
|
// Правило 3: Используем глобальное значение
|
||||||
|
return global_default_;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class DaemonLogger {
|
class DaemonLogger {
|
||||||
public:
|
public:
|
||||||
enum class Severity {
|
// Re-expose Severity enum inside the class for clarity and namespacing,
|
||||||
Debug, // 0
|
// while the actual definition is outside.
|
||||||
Info, // 1
|
using Severity = ::Severity;
|
||||||
Warning, // 2
|
|
||||||
Error, // 3
|
|
||||||
_Count // 4 - для размера массива
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Обертка для потокобезопасной записи в один ostream
|
// Обертка для потокобезопасной записи в один ostream
|
||||||
struct SynchronizedStream {
|
struct SynchronizedStream {
|
||||||
std::ostream* stream;
|
std::ostream* stream;
|
||||||
std::mutex mtx;
|
std::mutex mtx;
|
||||||
|
|
||||||
SynchronizedStream(std::ostream& s) : stream(&s) {}
|
SynchronizedStream(std::ostream& s) : stream(&s) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
// Контекст, передаваемый каждому функтору формата
|
||||||
// --- Глобальная конфигурация маршрутизации ---
|
struct LogContext {
|
||||||
|
const std::string& category;
|
||||||
|
Severity severity;
|
||||||
|
const std::string& location;
|
||||||
|
const std::string& user_message;
|
||||||
|
// Кэш для временной метки, чтобы не вычислять ее многократно
|
||||||
|
mutable std::optional<std::string> time_cache;
|
||||||
|
};
|
||||||
|
|
||||||
// Устанавливает поток по умолчанию, если другие правила не совпали
|
// Определяем типы для скомпилированного формата
|
||||||
|
using LogPieceFormatter = std::function<void(std::ostream&, const LogContext&)>;
|
||||||
|
using CompiledFormat = std::vector<LogPieceFormatter>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
// --- Конфигурация маршрутизации потоков ---
|
||||||
static void setGlobalOutputStream(std::ostream& stream) {
|
static void setGlobalOutputStream(std::ostream& stream) {
|
||||||
std::lock_guard<std::mutex> lock(config_mutex_);
|
stream_resolver_.setGlobal(get_or_create_synced_stream(stream));
|
||||||
global_stream_ = get_or_create_synced_stream(stream);
|
|
||||||
reconfigureAllLoggers();
|
reconfigureAllLoggers();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Устанавливает поток для всех уровней в указанной категории
|
|
||||||
static void setCategoryOutputStream(const std::string& category, std::ostream& stream) {
|
static void setCategoryOutputStream(const std::string& category, std::ostream& stream) {
|
||||||
std::lock_guard<std::mutex> lock(config_mutex_);
|
stream_resolver_.setForCategory(category, get_or_create_synced_stream(stream));
|
||||||
category_defaults_[category] = get_or_create_synced_stream(stream);
|
reconfigureLogger(category);
|
||||||
if (auto it = registry_.find(category); it != registry_.end()) {
|
|
||||||
it->second->resolveOutputStreams();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Устанавливает поток для конкретного уровня в конкретной категории
|
|
||||||
static void setSeverityOutputStream(const std::string& category, Severity severity, std::ostream& stream) {
|
static void setSeverityOutputStream(const std::string& category, Severity severity, std::ostream& stream) {
|
||||||
std::lock_guard<std::mutex> lock(config_mutex_);
|
stream_resolver_.setForSeverity(category, severity, get_or_create_synced_stream(stream));
|
||||||
severity_specific_[category][severity] = get_or_create_synced_stream(stream);
|
reconfigureLogger(category);
|
||||||
if (auto it = registry_.find(category); it != registry_.end()) {
|
|
||||||
it->second->resolveOutputStreams();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Получить логгер по категории
|
// --- Конфигурация форматов ---
|
||||||
|
static void setGlobalFormat(const std::string& format) {
|
||||||
|
format_resolver_.setGlobal(compileFormat(format));
|
||||||
|
reconfigureAllLoggers();
|
||||||
|
}
|
||||||
|
static void setCategoryFormat(const std::string& category, const std::string& format) {
|
||||||
|
format_resolver_.setForCategory(category, compileFormat(format));
|
||||||
|
reconfigureLogger(category);
|
||||||
|
}
|
||||||
|
static void setSeverityFormat(const std::string& category, Severity severity, const std::string& format) {
|
||||||
|
format_resolver_.setForSeverity(category, severity, compileFormat(format));
|
||||||
|
reconfigureLogger(category);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Получение и настройка логгера ---
|
||||||
static DaemonLogger& get(const std::string& category) {
|
static DaemonLogger& get(const std::string& category) {
|
||||||
std::lock_guard<std::mutex> lock(registry_mutex_);
|
std::lock_guard<std::mutex> lock(registry_mutex_);
|
||||||
auto it = registry_.find(category);
|
if (auto it = registry_.find(category); it != registry_.end()) {
|
||||||
if (it == registry_.end()) {
|
return *(it->second);
|
||||||
|
}
|
||||||
auto logger = std::unique_ptr<DaemonLogger>(new DaemonLogger(category));
|
auto logger = std::unique_ptr<DaemonLogger>(new DaemonLogger(category));
|
||||||
logger->resolveOutputStreams(); // Разрешаем маршруты при создании
|
logger->resolveProperties();
|
||||||
auto [inserted_it, _] = registry_.try_emplace(category, std::move(logger));
|
auto [inserted_it, _] = registry_.try_emplace(category, std::move(logger));
|
||||||
return *(inserted_it->second);
|
return *(inserted_it->second);
|
||||||
}
|
}
|
||||||
return *(it->second);
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Настройка экземпляра логгера ---
|
|
||||||
void setMinSeverity(Severity level) { min_severity_ = level; }
|
void setMinSeverity(Severity level) { min_severity_ = level; }
|
||||||
void suppress() { suppressed_ = true; }
|
void suppress() { suppressed_ = true; }
|
||||||
void unsuppress() { suppressed_ = false; }
|
void unsuppress() { suppressed_ = false; }
|
||||||
@@ -83,17 +144,15 @@ public:
|
|||||||
// --- Основной метод логирования ---
|
// --- Основной метод логирования ---
|
||||||
template<class... Args>
|
template<class... Args>
|
||||||
void log(Severity severity, const std::string& source_location, const std::string& format, Args... args) const {
|
void log(Severity severity, const std::string& source_location, const std::string& format, Args... args) const {
|
||||||
if (suppressed_ || severity < min_severity_) {
|
if (suppressed_ || severity < min_severity_) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1. Быстрое получение нужного потока (O(1))
|
size_t severity_idx = static_cast<size_t>(severity);
|
||||||
auto& target_synced_stream = resolved_streams_[static_cast<size_t>(severity)];
|
auto& target_stream = resolved_streams_[severity_idx];
|
||||||
if (!target_synced_stream || !target_synced_stream->stream) {
|
auto& compiled_format = resolved_compiled_formats_[severity_idx];
|
||||||
return; // Маршрут не настроен / ведет в никуда
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Форматирование сообщения (до блокировки мьютекса)
|
if (!target_stream || !target_stream->stream || !compiled_format || compiled_format->empty()) return;
|
||||||
|
|
||||||
|
// 1. Форматируем только пользовательскую часть сообщения
|
||||||
std::string user_message;
|
std::string user_message;
|
||||||
try {
|
try {
|
||||||
user_message = (boost::format(format) % ... % args).str();
|
user_message = (boost::format(format) % ... % args).str();
|
||||||
@@ -101,48 +160,117 @@ public:
|
|||||||
user_message = "!!! LOG FORMATTING ERROR: " + std::string(e.what()) + " !!!";
|
user_message = "!!! LOG FORMATTING ERROR: " + std::string(e.what()) + " !!!";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostringstream final_message;
|
// 2. Создаем контекст
|
||||||
final_message << currentTime() << " [" << severityToString(severity) << "]"
|
LogContext context{category_, severity, source_location, user_message, std::nullopt};
|
||||||
<< " [" << category_ << "]"
|
|
||||||
<< " [" << source_location << "] "
|
|
||||||
<< user_message;
|
|
||||||
|
|
||||||
// 3. Потокобезопасная запись в целевой поток
|
// 3. Быстро собираем итоговое сообщение, исполняя скомпилированный план
|
||||||
|
std::ostringstream final_message_stream;
|
||||||
|
for (const auto& formatter : *compiled_format) {
|
||||||
|
formatter(final_message_stream, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Потокобезопасно выводим результат
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(target_synced_stream->mtx);
|
std::lock_guard<std::mutex> lock(target_stream->mtx);
|
||||||
*target_synced_stream->stream << final_message.str() << std::endl;
|
*target_stream->stream << final_message_stream.str() << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit DaemonLogger(std::string category) : category_(std::move(category)) {}
|
explicit DaemonLogger(std::string category) : category_(std::move(category)) {}
|
||||||
|
|
||||||
// Разрешает и кэширует маршруты для этого экземпляра логгера
|
void resolveProperties() {
|
||||||
void resolveOutputStreams() {
|
|
||||||
std::lock_guard<std::mutex> lock(config_mutex_);
|
|
||||||
for (size_t i = 0; i < static_cast<size_t>(Severity::_Count); ++i) {
|
for (size_t i = 0; i < static_cast<size_t>(Severity::_Count); ++i) {
|
||||||
Severity s = static_cast<Severity>(i);
|
Severity s = static_cast<Severity>(i);
|
||||||
// Правило 1: Ищем [категория, уровень]
|
resolved_streams_[i] = stream_resolver_.resolve(category_, s);
|
||||||
if (auto cat_it = severity_specific_.find(category_); cat_it != severity_specific_.end()) {
|
resolved_compiled_formats_[i] = format_resolver_.resolve(category_, s);
|
||||||
if (auto sev_it = cat_it->second.find(s); sev_it != cat_it->second.end()) {
|
}
|
||||||
resolved_streams_[i] = sev_it->second;
|
}
|
||||||
continue;
|
|
||||||
}
|
// "Компилятор" строки формата в набор функторов
|
||||||
}
|
static std::shared_ptr<const CompiledFormat> compileFormat(const std::string& format) {
|
||||||
// Правило 2: Ищем [категория]
|
static std::mutex compile_mutex;
|
||||||
if (auto cat_it = category_defaults_.find(category_); cat_it != category_defaults_.end()) {
|
static std::unordered_map<std::string, std::shared_ptr<const CompiledFormat>> cache;
|
||||||
resolved_streams_[i] = cat_it->second;
|
|
||||||
continue;
|
std::lock_guard<std::mutex> lock(compile_mutex);
|
||||||
}
|
if (auto it = cache.find(format); it != cache.end()) {
|
||||||
// Правило 3: Используем глобальный поток
|
return it->second;
|
||||||
resolved_streams_[i] = global_stream_;
|
}
|
||||||
|
|
||||||
|
auto compiled = std::make_shared<CompiledFormat>();
|
||||||
|
size_t last_pos = 0;
|
||||||
|
size_t brace_pos = 0;
|
||||||
|
|
||||||
|
while ((brace_pos = format.find('{', last_pos)) != std::string::npos) {
|
||||||
|
// Добавляем литерал перед плейсхолдером
|
||||||
|
if (brace_pos > last_pos) {
|
||||||
|
compiled->emplace_back(
|
||||||
|
[literal = format.substr(last_pos, brace_pos - last_pos)](auto& os, const auto&) {
|
||||||
|
os << literal;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t end_brace_pos = format.find('}', brace_pos);
|
||||||
|
if (end_brace_pos == std::string::npos) {
|
||||||
|
// Если нет закрывающей скобки, то оставшаяся часть строки, включая '{',
|
||||||
|
// будет обработана как литерал в конце функции.
|
||||||
|
last_pos = brace_pos;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string placeholder = format.substr(brace_pos + 1, end_brace_pos - brace_pos - 1);
|
||||||
|
if (placeholder == "date") {
|
||||||
|
compiled->emplace_back([](auto& os, const auto& ctx) {
|
||||||
|
if (!ctx.time_cache) ctx.time_cache = currentTime();
|
||||||
|
os << *ctx.time_cache;
|
||||||
|
});
|
||||||
|
} else if (placeholder == "cat") {
|
||||||
|
compiled->emplace_back([](auto& os, const auto& ctx) { os << ctx.category; });
|
||||||
|
} else if (placeholder == "severity") {
|
||||||
|
compiled->emplace_back([](auto& os, const auto& ctx) { os << severityToString(ctx.severity); });
|
||||||
|
} else if (placeholder == "loc") {
|
||||||
|
compiled->emplace_back([](auto& os, const auto& ctx) { os << ctx.location; });
|
||||||
|
} else if (placeholder == "umsg") {
|
||||||
|
compiled->emplace_back([](auto& os, const auto& ctx) { os << ctx.user_message; });
|
||||||
|
} else {
|
||||||
|
// Неизвестный плейсхолдер - выводим как есть, включая скобки
|
||||||
|
compiled->emplace_back(
|
||||||
|
[literal = format.substr(brace_pos, end_brace_pos - brace_pos + 1)](auto& os, const auto&) {
|
||||||
|
os << literal;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
last_pos = end_brace_pos + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Добавляем оставшийся хвост строки
|
||||||
|
if (last_pos < format.length()) {
|
||||||
|
compiled->emplace_back(
|
||||||
|
[literal = format.substr(last_pos)](auto& os, const auto&) {
|
||||||
|
os << literal;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
cache[format] = compiled;
|
||||||
|
return compiled;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reconfigureAllLoggers() {
|
||||||
|
std::lock_guard<std::mutex> lock(registry_mutex_);
|
||||||
|
for (auto const& [key, val] : registry_) val->resolveProperties();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reconfigureLogger(const std::string& category) {
|
||||||
|
std::lock_guard<std::mutex> lock(registry_mutex_);
|
||||||
|
if (auto it = registry_.find(category); it != registry_.end()) {
|
||||||
|
it->second->resolveProperties();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Статические хелперы и данные ---
|
|
||||||
static std::shared_ptr<SynchronizedStream> get_or_create_synced_stream(std::ostream& stream) {
|
static std::shared_ptr<SynchronizedStream> get_or_create_synced_stream(std::ostream& stream) {
|
||||||
auto it = stream_registry_.find(&stream);
|
static std::mutex stream_reg_mutex;
|
||||||
if (it != stream_registry_.end()) {
|
std::lock_guard<std::mutex> lock(stream_reg_mutex);
|
||||||
|
if (auto it = stream_registry_.find(&stream); it != stream_registry_.end()) {
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
auto synced_stream = std::make_shared<SynchronizedStream>(stream);
|
auto synced_stream = std::make_shared<SynchronizedStream>(stream);
|
||||||
@@ -150,21 +278,17 @@ private:
|
|||||||
return synced_stream;
|
return synced_stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reconfigureAllLoggers() {
|
|
||||||
std::lock_guard<std::mutex> lock(registry_mutex_);
|
|
||||||
for (auto const& [key, val] : registry_) {
|
|
||||||
val->resolveOutputStreams();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string currentTime() {
|
static std::string currentTime() {
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
auto now_c = std::chrono::system_clock::to_time_t(now);
|
auto now_c = std::chrono::system_clock::to_time_t(now);
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
|
// std::localtime is not thread-safe on all platforms, but is often an acceptable trade-off.
|
||||||
|
// For a fully thread-safe solution, platform-specific alternatives (localtime_r, localtime_s) would be needed.
|
||||||
oss << std::put_time(std::localtime(&now_c), "%Y-%m-%d %H:%M:%S");
|
oss << std::put_time(std::localtime(&now_c), "%Y-%m-%d %H:%M:%S");
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
static std::string severityToString(Severity severity) {
|
|
||||||
|
static const char* severityToString(Severity severity) {
|
||||||
switch (severity) {
|
switch (severity) {
|
||||||
case Severity::Debug: return "DEBUG";
|
case Severity::Debug: return "DEBUG";
|
||||||
case Severity::Info: return "INFO ";
|
case Severity::Info: return "INFO ";
|
||||||
@@ -179,20 +303,16 @@ private:
|
|||||||
Severity min_severity_ = Severity::Debug;
|
Severity min_severity_ = Severity::Debug;
|
||||||
bool suppressed_ = false;
|
bool suppressed_ = false;
|
||||||
std::array<std::shared_ptr<SynchronizedStream>, static_cast<size_t>(Severity::_Count)> resolved_streams_;
|
std::array<std::shared_ptr<SynchronizedStream>, static_cast<size_t>(Severity::_Count)> resolved_streams_;
|
||||||
|
std::array<std::shared_ptr<const CompiledFormat>, static_cast<size_t>(Severity::_Count)> resolved_compiled_formats_;
|
||||||
|
|
||||||
// --- Статические данные класса ---
|
// --- Статические данные класса ---
|
||||||
inline static std::unordered_map<std::string, std::unique_ptr<DaemonLogger>> registry_;
|
inline static std::unordered_map<std::string, std::unique_ptr<DaemonLogger>> registry_;
|
||||||
inline static std::mutex registry_mutex_;
|
inline static std::mutex registry_mutex_;
|
||||||
|
|
||||||
inline static std::mutex config_mutex_;
|
|
||||||
// Хранилище правил маршрутизации
|
|
||||||
inline static std::shared_ptr<SynchronizedStream> global_stream_;
|
|
||||||
inline static std::unordered_map<std::string, std::shared_ptr<SynchronizedStream>> category_defaults_;
|
|
||||||
inline static std::unordered_map<std::string, std::unordered_map<Severity, std::shared_ptr<SynchronizedStream>>> severity_specific_;
|
|
||||||
// Реестр ostream'ов, чтобы не создавать дубликаты SynchronizedStream для одного и того же потока
|
|
||||||
inline static std::unordered_map<std::ostream*, std::shared_ptr<SynchronizedStream>> stream_registry_;
|
inline static std::unordered_map<std::ostream*, std::shared_ptr<SynchronizedStream>> stream_registry_;
|
||||||
};
|
|
||||||
|
|
||||||
|
inline static CascadingPropertyResolver<std::shared_ptr<SynchronizedStream>> stream_resolver_;
|
||||||
|
inline static CascadingPropertyResolver<std::shared_ptr<const CompiledFormat>> format_resolver_;
|
||||||
|
};
|
||||||
|
|
||||||
// --- Макросы для удобного вызова ---
|
// --- Макросы для удобного вызова ---
|
||||||
#define __LOG_IMPL(category, severity, format, ...) \
|
#define __LOG_IMPL(category, severity, format, ...) \
|
||||||
|
|||||||
@@ -1,49 +1,80 @@
|
|||||||
#include "glogger.hpp"
|
#include "glogger.hpp"
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
// Создаем файлы для логов
|
// Создаем потоки для файлов, которые будут жить в течение всей программы
|
||||||
std::ofstream network_log_file("network.log");
|
std::ofstream general_log_file("general.log");
|
||||||
std::ofstream critical_errors_file("critical.log");
|
std::ofstream critical_errors_file("critical.log");
|
||||||
|
std::ofstream network_log_file("network.log");
|
||||||
|
|
||||||
|
void worker_thread_func(int thread_id) {
|
||||||
|
LOG_INFO("General", "Worker thread %1% started.", thread_id);
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||||
|
LOG_DEBUG("Network", "heartbeat from thread %1%", thread_id);
|
||||||
|
if (thread_id % 2 == 0) {
|
||||||
|
LOG_ERROR("General", "Simulated failure in even thread %1%", thread_id);
|
||||||
|
}
|
||||||
|
LOG_INFO("General", "Worker thread %1% finished.", thread_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// --- Настраиваем сложную маршрутизацию ---
|
// --- 1. Настройка маршрутизации и форматов ---
|
||||||
|
|
||||||
// 1. По умолчанию все логи идут в стандартный вывод (консоль)
|
// Глобальное правило по умолчанию: все, что не подошло под другие правила,
|
||||||
|
// идет в консоль в простом формате.
|
||||||
DaemonLogger::setGlobalOutputStream(std::cout);
|
DaemonLogger::setGlobalOutputStream(std::cout);
|
||||||
|
DaemonLogger::setGlobalFormat("[{severity}] {cat}: {umsg}");
|
||||||
|
|
||||||
// 2. Все логи категории "Database", кроме ошибок, идут в /dev/null (игнорируются)
|
// Правило для категории "General": пишем в файл general.log с полным форматом.
|
||||||
// Для этого создадим "пустой" поток
|
DaemonLogger::setCategoryOutputStream("General", general_log_file);
|
||||||
std::ofstream null_stream;
|
DaemonLogger::setCategoryFormat("General", "{date} [{severity}] <{loc}> {umsg}");
|
||||||
null_stream.setstate(std::ios_base::badbit); // Делаем поток невалидным
|
|
||||||
DaemonLogger::setCategoryOutputStream("Database", null_stream);
|
|
||||||
|
|
||||||
// 3. Все логи категории "Network" идут в свой файл network.log
|
// Правило для категории "Network": пишем в свой файл network.log.
|
||||||
|
// Формат очень короткий, т.к. предполагается большой объем логов.
|
||||||
DaemonLogger::setCategoryOutputStream("Network", network_log_file);
|
DaemonLogger::setCategoryOutputStream("Network", network_log_file);
|
||||||
|
DaemonLogger::setCategoryFormat("Network", "{date} -> {umsg}");
|
||||||
|
|
||||||
// 4. Но ОШИБКИ из "Network" И ОШИБКИ из "Database" идут в специальный файл critical.log
|
// Самое специфичное правило: ошибки из категории "General" дублируются
|
||||||
DaemonLogger::setSeverityOutputStream("Network", DaemonLogger::Severity::Error, critical_errors_file);
|
// в отдельный файл critical.log с особым, кричащим форматом.
|
||||||
DaemonLogger::setSeverityOutputStream("Database", DaemonLogger::Severity::Error, critical_errors_file);
|
DaemonLogger::setSeverityOutputStream("General", DaemonLogger::Severity::Error, critical_errors_file);
|
||||||
|
DaemonLogger::setSeverityFormat("General", DaemonLogger::Severity::Error, "!!! CRITICAL [{cat}] {date} !!! {umsg} @ {loc}");
|
||||||
|
|
||||||
// --- Начинаем логирование ---
|
// --- 2. Тестирование одиночных логов ---
|
||||||
|
|
||||||
LOG_INFO("Main", "Система логирования настроена. Запускаем приложение.");
|
LOG_INFO("Main", "Система логирования настроена. Запускаем тесты."); // Попадет в std::cout
|
||||||
|
LOG_DEBUG("General", "Это отладочное сообщение для general.log"); // Попадет в general.log
|
||||||
|
LOG_WARNING("General", "Это предупреждение для general.log"); // Попадет в general.log
|
||||||
|
|
||||||
// Логирование из разных категорий
|
LOG_ERROR("General", "Критическая ошибка, которая пойдет в critical.log"); // Попадет в critical.log
|
||||||
LOG_DEBUG("Network", "Инициализация сетевого стека..."); // Пойдет в network.log
|
|
||||||
LOG_INFO("Network", "Соединение с %1% установлено.", "192.168.1.1"); // Пойдет в network.log
|
|
||||||
|
|
||||||
LOG_INFO("Database", "Подключение к базе данных..."); // Пойдет в null_stream (никуда)
|
LOG_INFO("Network", "New connection from 192.168.1.100"); // Попадет в network.log
|
||||||
LOG_WARNING("Database", "Медленный запрос обнаружен."); // Пойдет в null_stream (никуда)
|
LOG_INFO("Network", "Packet loss detected: 5%%"); // Попадет в network.log
|
||||||
|
|
||||||
LOG_ERROR("Network", "Таймаут соединения с хостом %1%", "example.com"); // Пойдет в critical.log
|
// --- 3. Тестирование изменения настроек на лету ---
|
||||||
LOG_ERROR("Database", "Не удалось выполнить транзакцию."); // Пойдет в critical.log
|
LOG_INFO("Main", "Меняем уровень логирования для 'Network' на Warning");
|
||||||
|
|
||||||
LOG_INFO("Scheduler", "Задача #%1% выполнена.", 123); // Правил нет, пойдет в std::cout
|
|
||||||
LOG_ERROR("Scheduler", "Не удалось запустить задачу."); // Правил нет, пойдет в std::cout
|
|
||||||
|
|
||||||
// Установка минимального уровня для категории
|
|
||||||
DaemonLogger::get("Network").setMinSeverity(DaemonLogger::Severity::Warning);
|
DaemonLogger::get("Network").setMinSeverity(DaemonLogger::Severity::Warning);
|
||||||
LOG_INFO("Network", "Эта информация больше не будет записана."); // Не пройдет из-за min_severity
|
LOG_INFO("Network", "Эта запись НЕ должна появиться в network.log");
|
||||||
|
LOG_WARNING("Network", "А эта запись-предупреждение ДОЛЖНА появиться.");
|
||||||
|
|
||||||
|
// Возвращаем обратно для многопоточного теста
|
||||||
|
DaemonLogger::get("Network").setMinSeverity(DaemonLogger::Severity::Debug);
|
||||||
|
|
||||||
|
// --- 4. Тестирование многопоточной записи ---
|
||||||
|
LOG_INFO("Main", "Запускаем 5 потоков для стресс-теста...");
|
||||||
|
|
||||||
|
std::vector<std::thread> threads;
|
||||||
|
for (int i = 0; i < 5; ++i) {
|
||||||
|
threads.emplace_back(worker_thread_func, i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& t : threads) {
|
||||||
|
t.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INFO("Main", "Все потоки завершили работу. Проверьте файлы general.log, critical.log и network.log");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user