// // console_logger.cxx // logging // // Created by Sam Jaffe on 4/1/19. // #include #include "../../../../paradigm/declarative/expect/include/expect/expect.hpp" #include "../../../../types/resource_factory/include/resource_factory/prototype_factory.hpp" #include "logger/detail/logger_impl.h" #include "logger/log_manager.h" #include "logger/properties.h" using namespace logging; static std::string validate_or_throw(logging::properties const& props) { expects(props.type == properties::OBJECT, "expected properties to be a map"); auto it = props.obj.find("target"); expects(it != props.obj.end(), "expected a console target entry"); expects(it->second.type == properties::STRING, "expected properties[\"target\"] to be a string"); return it->second.value; } class console_logger : public logger_impl { public: static std::shared_ptr create(properties const& props); explicit console_logger(std::ostream& os); void write(logpacket const & pkt) override; void flush() override; private: std::ostream& out_; }; std::shared_ptr console_logger::create(properties const& props) { const std::string target = validate_or_throw(props); if (target == "SYSTEM_OUT") { return std::make_shared(std::cout); } else if (target == "SYSTEM_ERR") { return std::make_shared(std::cerr); } else { throw std::logic_error{target + " is not a valid console"}; } } console_logger::console_logger(std::ostream& os) : out_(os) {} void console_logger::write(logpacket const & pkt) { out_ << pkt.message; } void console_logger::flush() { out_.flush(); } namespace { bool _ = impl_factory::instance().bind("Console", console_logger::create); }