Sam Jaffe 6 gadi atpakaļ
vecāks
revīzija
872d7eb8d0

+ 30 - 0
include/logger/detail/to_json.h

@@ -0,0 +1,30 @@
+//
+//  to_json.h
+//  logger
+//
+//  Created by Sam Jaffe on 4/4/19.
+//
+
+#pragma once
+
+#if defined(LOGGING_USE_JSON)
+#if defined(LOGGING_JSON_SJAFFE)
+#include "json/json.hpp"
+namespace logging {
+  typedef json::value json;
+}
+#elif defined(LOGGING_JSON_VALUE)
+#include "json/json_fwd.h"
+namespace logging {
+  typedef Json::Value json;
+}
+#endif
+namespace logging { namespace detail {
+  template <typename T>
+  json to_json(T const & obj);
+} }
+#else
+namespace logging { namespace detail {
+  template <typename T> void to_json(T const &) {}
+} }
+#endif

+ 3 - 3
include/logger/format.h

@@ -34,13 +34,13 @@ namespace logging {
   using string_generator = std::function<std::string(logpacket const &)>;
   string_generator get_date_formatter(std::string fmt);
     
-  void format_msg(std::ostream & os, std::string const & interp,
-                  size_t pos, size_t idx, std::vector<object> const & objs);
+  void format_msg(std::ostream & os, std::string const & interp, size_t pos,
+                  std::vector<detail::object> const & objs, size_t idx);
   
   template <typename... Args>
   std::string format_msg(std::string const & interp, Args && ...args) {
     std::stringstream msg;
-    format_msg(msg, interp, 0, 0, {object(args)...});
+    format_msg(msg, interp, 0, {detail::object(args)...}, 0);
     return msg.str();
   }
 }

+ 12 - 12
include/logger/wrapper_object.h

@@ -7,37 +7,37 @@
 
 #pragma once
 
-#include <iosfwd>
+#include <iostream>
 #include <string>
 
 #include "detail/to_string.h"
+#include "detail/to_json.h"
 
-namespace logging {
+namespace logging { namespace detail {
   class object {
   public:
-    template <typename T>
-    explicit object(T & object);
-    
-    std::string to_string() const;
+    template <typename T> explicit object(T & object);
     
   private:
-    template <typename> static std::string to_string(void * ptr);
+    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);
     
   private:
     void * ptr_;
     std::string (*to_string_)(void*);
   };
   
-  std::ostream & operator<<(std::ostream &, object const &);
   
   template <typename T>
-  std::string object::to_string(void * ptr) {
-    return logging::detail::to_string(*static_cast<T*>(ptr));
+  std::string object::to_string_impl(void * ptr) {
+    return to_string(*static_cast<T*>(ptr));
   }
   
   template <typename T>
   object::object(T & object)
-  : ptr_(&object), to_string_(&object::to_string<T>) {
+  : ptr_(&object), to_string_(&object::to_string_impl<T>) {
     
   }
-}
+} }

+ 0 - 4
logger.xcodeproj/project.pbxproj

@@ -27,7 +27,6 @@
 		CD88E95F2252D3EF00927F40 /* c_logger.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD88E95D2252D3EF00927F40 /* c_logger.cxx */; };
 		CD88E9632252D67A00927F40 /* common.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD88E9612252D67A00927F40 /* common.cxx */; };
 		CDA494DE2256D5F40041620C /* date_format.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDA494DD2256D5F40041620C /* date_format.cxx */; };
-		CDA494E62256D8090041620C /* wrapper_object.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDA494E42256D8090041620C /* wrapper_object.cxx */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -96,7 +95,6 @@
 		CD88E9612252D67A00927F40 /* common.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = common.cxx; sourceTree = "<group>"; };
 		CD88E9642252D6C700927F40 /* common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = common.h; sourceTree = "<group>"; };
 		CDA494DD2256D5F40041620C /* date_format.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = date_format.cxx; sourceTree = "<group>"; };
-		CDA494E42256D8090041620C /* wrapper_object.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = wrapper_object.cxx; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -165,7 +163,6 @@
 				CD88E9612252D67A00927F40 /* common.cxx */,
 				CDA494DD2256D5F40041620C /* date_format.cxx */,
 				CD3C80BE1D6A2CA300ACC795 /* format.cxx */,
-				CDA494E42256D8090041620C /* wrapper_object.cxx */,
 			);
 			path = src;
 			sourceTree = "<group>";
@@ -355,7 +352,6 @@
 				CD3C80C01D6A2CA300ACC795 /* format.cxx in Sources */,
 				CD88E95F2252D3EF00927F40 /* c_logger.cxx in Sources */,
 				CD1CDE872252E5B900E5B6B2 /* properties.cxx in Sources */,
-				CDA494E62256D8090041620C /* wrapper_object.cxx in Sources */,
 				CD1CDE8B2252E61800E5B6B2 /* console_appender.cxx in Sources */,
 				CD1CDEAF22556B7E00E5B6B2 /* logger_impl.cxx in Sources */,
 				CD1CDE892252E60900E5B6B2 /* file_appender.cxx in Sources */,

+ 4 - 4
src/format.cxx

@@ -156,16 +156,16 @@ namespace logging {
     return ss.str();
   }
 
-  void format_msg(std::ostream & os, std::string const & interp,
-                  size_t pos, size_t idx, std::vector<object> const & objs) {
+  void format_msg(std::ostream & os, std::string const & interp, size_t pos,
+                  std::vector<detail::object> const & objs, size_t idx) {
     size_t next = interp.find("{}", pos);
     os << interp.substr(pos, next);
     if (next == std::string::npos) {
       return; // throw?
     } else if (!strncmp(interp.c_str() + next - 1, "{{}}", 4)) {
-      format_msg(os, interp, next+2, idx, objs);
+      format_msg(os, interp, next+2, objs, idx);
     } else {
-      format_msg(os << objs[idx], interp, next+2, idx+1, objs);
+      format_msg(os << objs[idx], interp, next+2, objs, idx+1);
     }
   }
 }

+ 0 - 19
src/wrapper_object.cxx

@@ -1,19 +0,0 @@
-//
-//  wrapper_object.cxx
-//  logging
-//
-//  Created by Sam Jaffe on 4/4/19.
-//
-
-#include "logger/wrapper_object.h"
-
-using namespace logging;
-
-std::string object::to_string() const {
-  return to_string_(ptr_);
-}
-
-// TODO: Call to_stream(os)
-std::ostream & logging::operator<<(std::ostream & os, object const & obj) {
-  return os << obj.to_string();
-}