format.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. class message {
  30. public:
  31. message() = default;
  32. message(char const * fmt) : format_code(fmt) {}
  33. template <typename... Args>
  34. message(std::string const & fmt, Args && ...args);
  35. std::string str() const;
  36. Json::Value json() const;
  37. private:
  38. std::string format_code;
  39. std::vector<detail::object> objects;
  40. };
  41. template <typename... Args>
  42. message::message(std::string const & fmt, Args && ...args)
  43. : format_code(fmt), objects({detail::object(args)...}) {
  44. }
  45. }