瀏覽代碼

Write out string fallback code for to_json.

Sam Jaffe 6 年之前
父節點
當前提交
6cefb871d9

+ 1 - 1
include/logger/detail/to_json.h

@@ -43,6 +43,6 @@ namespace logging { namespace detail {
   
   template <typename T>
   std::string to_json(T const & obj, bool compact = false) {
-    return to_string(obj);
+    return "\"" + to_string(obj) + "\"";
   }
 } }

+ 1 - 1
include/logger/format.h

@@ -42,7 +42,7 @@ namespace logging {
     message(std::string const & fmt, Args && ...args);
     
     std::string str() const;
-    
+    std::string json() const;
   private:
     std::string format_code;
     std::vector<detail::object> objects;

+ 12 - 2
include/logger/wrapper_object.h

@@ -18,15 +18,19 @@ namespace logging { namespace detail {
   public:
     template <typename T> explicit object(T & object);
     
+    std::string json() const { return to_json_(ptr_); }
+    
   private:
     friend std::ostream & operator<<(std::ostream & os, object const & obj) {
       return os << obj.to_string_(obj.ptr_);
     }
     template <typename> static std::string to_string_impl(void * ptr);
+    template <typename> static std::string to_json_impl(void * ptr);
 
   private:
     void * ptr_;
     std::string (*to_string_)(void*);
+    std::string (*to_json_)(void*);
   };
   
   
@@ -34,11 +38,17 @@ namespace logging { namespace detail {
   std::string object::to_string_impl(void * ptr) {
     return to_string(*static_cast<T*>(ptr));
   }
-  
+
+  template <typename T>
+  std::string object::to_json_impl(void * ptr) {
+    return to_json(*static_cast<T*>(ptr));
+  }
+
   template <typename T>
   object::object(T & object)
   : ptr_(&object),
-  to_string_(&object::to_string_impl<T>) {
+  to_string_(&object::to_string_impl<T>),
+  to_json_(&object::to_json_impl<T>) {
     
   }
 } }

+ 4 - 0
src/format.cxx

@@ -164,4 +164,8 @@ namespace logging {
     format_msg(ss, format_code, 0, objects, 0);
     return ss.str();
   }
+  
+  std::string message::json() const {
+    return objects[0].json();
+  }
 }

+ 5 - 1
src/loggers/json_layout.cxx

@@ -101,7 +101,11 @@ void json_layout::format(std::ostream & os, logpacket const & pkt) const {
   writer.write_key("loggerName");
   os << "\"" << pkt.logger << "\",";
   writer.write_key("message");
-  os << "\"" << pkt.message.str() << "\"";
+  if (log_as_json_) {
+    os << pkt.message.json();
+  } else {
+    os << "\"" << pkt.message.str() << "\"";
+  }
   --writer.indent;
   writer.write_indent();
   os << '}';

+ 12 - 9
test/json_layout_test.cxx

@@ -101,15 +101,15 @@ std::ostream & operator<<(std::ostream & os, example const & ex) {
 }
 
 std::string const struct_json_output = R"({
-"instant": {
-  "epochSecond": 1554401840,
-  "nanoOfSecond": 123456000
-},
-"level": "warning",
-"loggerName": "UnitTest",
-"message": {
-  "content":225
-}
+  "instant": {
+    "epochSecond": 1554401840,
+    "nanoOfSecond": 123456000
+  },
+  "level": "warning",
+  "loggerName": "UnitTest",
+  "message": {
+    "content":225
+  }
 })";
 
 struct json_example {
@@ -118,6 +118,9 @@ struct json_example {
 std::ostream & operator<<(std::ostream & os, json_example const & ex) {
   return os << '{' << ex.content << '}';
 }
+std::string to_json(json_example const & ex) {
+  return "{\"content\":" + std::to_string(ex.content) + "}";
+}
 
 TEST(JsonLayoutTest, MessageCanBeLoggedAsJSON) {
   using namespace logging;