format.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // format.hpp
  3. // logger
  4. //
  5. // Created by Sam Jaffe on 8/21/16.
  6. //
  7. #pragma once
  8. #include <functional>
  9. #include <sstream>
  10. #include <string>
  11. #include <vector>
  12. #include "logger_fwd.h"
  13. #include "wrapper_object.h"
  14. namespace logging {
  15. class format {
  16. public:
  17. enum class token_id {
  18. DATE, PRIORITY, CATEGORY, MESSAGE, STRING, NEWLINE
  19. };
  20. using generator = std::function<void(logpacket const &, std::ostream &)>;
  21. static format parse_format_string(std::string const &);
  22. void process(logpacket const & pkt, std::ostream & os) const;
  23. std::string process(logpacket const & pkt) const;
  24. private:
  25. std::vector<generator> gen;
  26. };
  27. using string_generator = std::function<std::string(logpacket const &)>;
  28. string_generator get_date_formatter(std::string fmt);
  29. void format_msg(std::ostream & os, std::string const & interp, size_t pos,
  30. std::vector<detail::object> const & objs, size_t idx);
  31. template <typename... Args>
  32. std::string format_msg(std::string const & interp, Args && ...args) {
  33. std::stringstream msg;
  34. format_msg(msg, interp, 0, {detail::object(args)...}, 0);
  35. return msg.str();
  36. }
  37. }