// // file_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/appender.h" #include "logger/log_manager.h" #include "logger/properties.h" using namespace logging; class file_appender : public appender { public: static std::shared_ptr create(properties const&); explicit file_appender(const std::string& fname, bool append); void write(std::string const & msg) override; void flush() override; private: bool flush_immediately_{false}; std::ofstream out_; }; static bool is_append(properties const & props) { return props.contains("append") && props["append"]; } std::shared_ptr file_appender::create(properties const & props) { file_appender app(props["file"], is_append(props)); return std::make_shared(std::move(app)); } static std::ios_base::openmode mode(bool append) { return (append ? std::ios_base::app : 0) | std::ios_base::out; } file_appender::file_appender(const std::string& fname, bool append) : out_(fname, mode(append)) {} void file_appender::write(std::string const & msg) { out_ << msg; if (flush_immediately_) { flush(); } } void file_appender::flush() { out_.flush(); } namespace { bool _ = appenders::instance().bind("File", file_appender::create); }