| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- //
- // format.hpp
- // logger
- //
- // Created by Sam Jaffe on 8/21/16.
- //
- #pragma once
- #include <functional>
- #include <sstream>
- #include <string>
- #include <vector>
- #include "logger_fwd.h"
- namespace logging {
- class format {
- public:
- enum class token_id {
- DATE, PRIORITY, CATEGORY, MESSAGE, STRING, NEWLINE
- };
-
- using generator = std::function<void(logpacket const &, std::ostream &)>;
-
- 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<generator> gen;
- };
-
- inline void format_msg(std::stringstream & msg, std::string const & interp,
- size_t pos) {
- msg << interp.substr(pos);
- }
-
- struct format_point_t {
- size_t start;
- size_t end;
- };
-
- format_point_t get_next_format_specifier(std::string const & interp,
- size_t pos );
-
- template <typename Arg0, typename... Args>
- inline void format_msg(std::stringstream & msg, std::string const & interp,
- size_t pos, Arg0 && arg0, Args && ...args) {
- format_point_t next = get_next_format_specifier( interp, pos );
- msg << interp.substr(pos, next.start);
- if (next.start == std::string::npos) {
- return; // throw?
- } else if (!strncmp(interp.c_str() + next.start - 1, "{{}}", 4)) {
- format_msg(msg, interp, next.end, std::forward<Arg0>(arg0),
- std::forward<Args>(args)... );
- } else {
- msg << arg0;
- format_msg(msg, interp, next.end, std::forward<Args>(args)... );
- }
- }
-
- template <typename... Args>
- inline std::string format_msg(std::string const & interp,
- Args && ...args) {
- std::stringstream msg;
- format_msg<Args...>( msg, interp, 0, std::forward<Args>(args)... );
- return msg.str();
- }
- }
|