/
ArtT
/
Netology
Обзор
Документация
Войти
/
ArtT
/
Netology
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Part6/Task_6_3_2/Task_6_3_2.cpp
119 строк
3 KB
ArtT
Добавил задание 6_3
16 июл 2026, 12:47
16 июл 2026, 12:47
ddf6ffa
Код
Авторство
О чём код?
#include <iostream> #include <fstream> #include <vector> #include <memory> #include <algorithm> #include <string> class Observer { public: virtual void onWarning(const std::string& message) {} virtual void onError(const std::string& message) {} virtual void onFatalError(const std::string& message) {} virtual ~Observer() = default; }; class Observable { public: void AddObserver(const std::weak_ptr<Observer>& observer) { cleanup(); observers_.push_back(observer); } void warning(const std::string& message) const { for (const auto& weak_observer : observers_) { if (auto observer = weak_observer.lock()) { observer->onWarning(message); } } } void error(const std::string& message) const { for (const auto& weak_observer : observers_) { if (auto observer = weak_observer.lock()) { observer->onError(message); } } } void fatalError(const std::string& message) const { for (const auto& weak_observer : observers_) { if (auto observer = weak_observer.lock()) { observer->onFatalError(message); } } } private: std::vector<std::weak_ptr<Observer>> observers_; void cleanup() { observers_.erase( std::remove_if(observers_.begin(), observers_.end(), [](const std::weak_ptr<Observer>& w) { return w.expired(); }), observers_.end()); } }; class WarningObserver : public Observer { public: void onWarning(const std::string& message) override { std::cout << "[WARNING] " << message << std::endl; } }; class ErrorObserver : public Observer { public: explicit ErrorObserver(const std::string& file_path) : file_path_(file_path) { } void onError(const std::string& message) override { std::ofstream out(file_path_, std::ios::app); if (!out.is_open()) { throw std::runtime_error("Cannot open file: " + file_path_); } out << "[ERROR] " << message << std::endl; } private: std::string file_path_; }; class FatalErrorObserver : public Observer { public: explicit FatalErrorObserver(const std::string& file_path) : file_path_(file_path) { } void onFatalError(const std::string& message) override { std::cout << "[FATAL ERROR] " << message << std::endl; std::ofstream out(file_path_, std::ios::app); if (!out.is_open()) { throw std::runtime_error("Cannot open file: " + file_path_); } out << "[FATAL ERROR] " << message << std::endl; } private: std::string file_path_; }; int main() { Observable logger; auto warning_observer = std::make_shared<WarningObserver>(); auto error_observer = std::make_shared<ErrorObserver>("errors.log"); auto fatal_observer = std::make_shared<FatalErrorObserver>("fatal.log"); logger.AddObserver(warning_observer); logger.AddObserver(error_observer); logger.AddObserver(fatal_observer); logger.warning("Low disk space"); logger.error("Failed to open connection"); logger.fatalError("Segmentation fault"); warning_observer.reset(); logger.warning("This warning will simply be skipped"); return EXIT_SUCCESS; }