// // format.hpp // logger // // Created by Sam Jaffe on 8/21/16. // #pragma once #include #include #include #include #include "logger_fwd.h" namespace logging { class format { public: enum class token_id { DATE, PRIORITY, CATEGORY, MESSAGE, STRING, NEWLINE }; using generator = std::function; static format parse_format_string(std::string const &); void process(logpacket const & pkt, std::ostream & os) const; std::string process(logpacket const & pkt) const; private: std::vector gen; }; inline void format_msg(std::stringstream & msg, std::string const & interp, size_t pos) { msg << interp.substr(pos); } template inline void format_msg(std::stringstream & msg, std::string const & interp, size_t pos, Arg0 && arg0, Args && ...args) { size_t next = interp.find("{}", pos); msg << interp.substr(pos, next); if (next == std::string::npos) { return; // throw? } else if (!strncmp(interp.c_str() + next - 1, "{{}}", 4)) { format_msg(msg, interp, next+2, std::forward(arg0), std::forward(args)... ); } else { msg << arg0; format_msg(msg, interp, next+2, std::forward(args)... ); } } template inline std::string format_msg(std::string const & interp, Args && ...args) { std::stringstream msg; format_msg( msg, interp, 0, std::forward(args)... ); return msg.str(); } }