Преглед на файлове

Merge branch 'cleanup' into header_footer

* cleanup:
  Remove logger_fwd.h
  Remove direct access to format.cxx
  Remove some unhelpful headers. Move logger_impl into the source-code section since it's not a public header.
Sam Jaffe преди 6 години
родител
ревизия
f74b5b3c92

+ 2 - 1
include/logger/c_logger.h

@@ -13,7 +13,7 @@
 #include <memory>
 #include <string>
 
-#include "logger_fwd.h"
+#include "level.h"
 
 #define VA_LOGN(level, size)          \
   do {                                \
@@ -34,6 +34,7 @@
   inline void lvl(std::string const & msg) { log(level::lvl, msg); }
 
 namespace logging {
+  enum class level : int;
   class logger_impl;
   
   class c_logger {

+ 6 - 1
include/logger/detail/appender.h

@@ -1,8 +1,13 @@
 #pragma once
 
-#include "logger/logger_fwd.h"
+#include <iosfwd>
 
 namespace logging {
+  class layout;
+  struct logpacket;
+  class properties;
+  enum class level : int;
+  
   struct appender {
     virtual ~appender() = default;
     virtual std::ostream & stream() = 0;

+ 2 - 2
include/logger/detail/layout.h

@@ -1,10 +1,10 @@
 #pragma once
 
-#include "logger/logger_fwd.h"
-
+#include <iosfwd>
 #include <string>
 
 namespace logging {
+  struct logpacket;
   struct layout {
     virtual ~layout() = default;
     virtual void format(std::ostream & os, logpacket const & pkt) const = 0;

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

@@ -1,21 +0,0 @@
-//
-//  to_json.h
-//  logger
-//
-//  Created by Sam Jaffe on 4/4/19.
-//
-
-#pragma once
-
-#include <string>
-
-#include <json/json.h>
-
-namespace logging { namespace detail {
-  template <typename T> std::string to_string(T const & obj);
-  
-  template <typename T>
-  Json::Value to_json(T const & obj) {
-    return to_string(obj);
-  }
-} }

+ 0 - 25
include/logger/detail/to_string.h

@@ -1,25 +0,0 @@
-//
-//  to_string.h
-//  logger
-//
-//  Created by Sam Jaffe on 4/4/19.
-//
-
-#pragma once
-
-#include <iostream>
-#include <sstream>
-
-namespace logging { namespace detail {
-  template <typename T>
-  void to_stream(T const & obj, std::ostream & os) {
-    os << obj;
-  }
-
-  template <typename T>
-  std::string to_string(T const & obj) {
-    std::stringstream ss;
-    to_stream(obj, ss);
-    return ss.str();
-  }
-} }

+ 23 - 0
include/logger/level.h

@@ -0,0 +1,23 @@
+//
+//  logger_fwd.hpp
+//  logger
+//
+//  Created by Sam Jaffe on 9/3/16.
+//
+
+#pragma once
+
+#include <string>
+
+#define log_here { __FILE__, __LINE__, __FUNCTION__ }
+
+#define LIST_OF_LOGGING_LEVELS \
+  X(trace) X(debug) X(info) X(warning) X(error) X(critical) X(fatal) X(none)
+#define X(token) token,
+namespace logging {
+  enum class level : int {
+    LIST_OF_LOGGING_LEVELS warn = warning
+  };
+  std::ostream & operator<<(std::ostream & os, level l);
+}
+#undef X

+ 8 - 6
include/logger/logger.h

@@ -23,15 +23,16 @@
 #include <memory>
 #include <string>
 
-#include "logger_fwd.h"
-#include "format.h"
+#include "message.h"
 
 #define log_message( logger, lvl, ... )   \
   logger.log(level::lvl, log_here, __VA_ARGS__)
 
 namespace logging {
+  enum class level : int;
+  struct location_info;
   class logger_impl;
-  class manager;
+  struct logpacket;
 
   class logger {
   public:
@@ -39,11 +40,11 @@ namespace logging {
 
     template <typename... Args>
     inline void log(level ll, std::string const & interp, Args && ...args) {
-      log(ll, location_info{}, interp, std::forward<Args>(args)...);
+      log(ll, message(interp, std::forward<Args>(args)...));
     }
     
     template <typename... Args>
-    inline void log(level ll, location_info info,
+    inline void log(level ll, location_info const & info,
                     std::string const & interp, Args && ...args) {
       log(ll, info, message(interp, std::forward<Args>(args)...));
     }
@@ -55,7 +56,8 @@ namespace logging {
     
   private:
     friend class manager;
-    void log(level ll, location_info info, message const&);
+    void log(level ll, message const&);
+    void log(level ll, location_info const & info, message const&);
 
   private:
     std::string const logger_name_;

+ 0 - 47
include/logger/logger_fwd.h

@@ -1,47 +0,0 @@
-//
-//  logger_fwd.hpp
-//  logger
-//
-//  Created by Sam Jaffe on 9/3/16.
-//
-
-#pragma once
-
-#include <string>
-
-#define log_here { __FILE__, __LINE__, __FUNCTION__ }
-
-namespace logging {
-  enum class level : int;
-  struct logpacket;
-  struct properties;
-  // Implementation classes
-  class appender;
-  class layout;
-  using p_appender = std::shared_ptr<appender>;
-  using p_layout = std::shared_ptr<layout>;
-
-  std::ostream & operator<<(std::ostream & os, level l);
-  
-  struct location_info {
-    char const * filename = "???";
-    int line = 0;
-    char const * function = "";
-  };
-  
-#if defined( _WIN32 )
-  constexpr char const * const NEWLINE_TOKEN{"\r\n"};
-#else
-  constexpr char const * const NEWLINE_TOKEN{"\n"};
-#endif
-}
-
-#define LIST_OF_LOGGING_LEVELS \
-  X(trace) X(debug) X(info) X(warning) X(error) X(critical) X(fatal) X(none)
-#define X(token) token,
-namespace logging {
-  enum class level : int {
-    LIST_OF_LOGGING_LEVELS warn = warning
-  };
-}
-#undef X

+ 14 - 2
include/logger/logpacket.h

@@ -1,10 +1,16 @@
 
 #pragma once
 
-#include "logger_fwd.h"
-#include "format.h"
+#include "level.h"
+#include "message.h"
 
 namespace logging {
+  struct location_info {
+    char const * filename = "???";
+    int line = 0;
+    char const * function = "";
+  };
+  
   struct logpacket {
     struct timeval time;
     //    int thread_id;
@@ -13,4 +19,10 @@ namespace logging {
     std::string logger;
     message message;
   };
+  
+#if defined( _WIN32 )
+  constexpr char const * const NEWLINE_TOKEN{"\r\n"};
+#else
+  constexpr char const * const NEWLINE_TOKEN{"\n"};
+#endif
 }

+ 35 - 0
include/logger/message.h

@@ -0,0 +1,35 @@
+//
+//  format.hpp
+//  logger
+//
+//  Created by Sam Jaffe on 8/21/16.
+//
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "wrapper_object.h"
+
+namespace logging {  
+  class message {
+  public:
+    message() = default;
+    message(char const * fmt) : format_code(fmt) {}
+    template <typename... Args>
+    message(std::string const & fmt, Args && ...args);
+    
+    std::string str() const;
+    Json::Value json() const;
+  private:
+    std::string format_code;
+    std::vector<detail::object> objects;
+  };
+  
+  template <typename... Args>
+  message::message(std::string const & fmt, Args && ...args)
+  : format_code(fmt), objects({detail::object(args)...}) {
+    
+  }
+}

+ 18 - 2
include/logger/wrapper_object.h

@@ -9,9 +9,9 @@
 
 #include <iostream>
 #include <string>
+#include <sstream>
 
-#include "detail/to_string.h"
-#include "detail/to_json.h"
+#include <json/json.h>
 
 namespace logging { namespace detail {
   class object {
@@ -33,6 +33,22 @@ namespace logging { namespace detail {
     Json::Value (*to_json_)(void*);
   };
   
+  template <typename T>
+  void to_stream(T const & obj, std::ostream & os) {
+    os << obj;
+  }
+  
+  template <typename T>
+  std::string to_string(T const & obj) {
+    std::stringstream ss;
+    to_stream(obj, ss);
+    return ss.str();
+  }
+  
+  template <typename T>
+  Json::Value to_json(T const & obj) {
+    return to_string(obj);
+  }
   
   template <typename T>
   std::string object::to_string_impl(void * ptr) {

+ 12 - 12
logger.xcodeproj/project.pbxproj

@@ -15,10 +15,9 @@
 		CD1CDE9222543E7E00E5B6B2 /* test_properties.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1CDE9122543E7E00E5B6B2 /* test_properties.cxx */; };
 		CD1CDEAF22556B7E00E5B6B2 /* logger_impl.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1CDEAE22556B7E00E5B6B2 /* logger_impl.cxx */; };
 		CD1CDEB122557FB600E5B6B2 /* default_layout.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1CDEB022557FB600E5B6B2 /* default_layout.cxx */; };
-		CD1CDEB32256B04600E5B6B2 /* format_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1CDEB22256B04600E5B6B2 /* format_test.cxx */; };
 		CD1CDEB52256C94000E5B6B2 /* pattern_layout.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1CDEB42256C94000E5B6B2 /* pattern_layout.cxx */; };
 		CD29739B1D7B401F00E37217 /* logger.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD2973991D7B401F00E37217 /* logger.cxx */; };
-		CD3C80C01D6A2CA300ACC795 /* format.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD3C80BE1D6A2CA300ACC795 /* format.cxx */; };
+		CD3C80C01D6A2CA300ACC795 /* pattern_layout_format.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD3C80BE1D6A2CA300ACC795 /* pattern_layout_format.cxx */; };
 		CD6F73EC225187BE0081ED74 /* logger in Headers */ = {isa = PBXBuildFile; fileRef = CD6F73EA225187A10081ED74 /* logger */; settings = {ATTRIBUTES = (Public, ); }; };
 		CD6F7406225187F40081ED74 /* liblogging.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 0ECAC4AF1BC00AC500FDAE14 /* liblogging.dylib */; };
 		CD6F740C225187FD0081ED74 /* logger_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD6F73FC225187E10081ED74 /* logger_test.cxx */; };
@@ -32,7 +31,7 @@
 		CD88E9572252BDFC00927F40 /* log_manager.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD88E9552252BDFC00927F40 /* log_manager.cxx */; };
 		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 */; };
+		CDA494DE2256D5F40041620C /* pattern_layout_date.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDA494DD2256D5F40041620C /* pattern_layout_date.cxx */; };
 		CDEA62D5225A3B0B00A6FAE0 /* json_layout.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDEA62D4225A3B0B00A6FAE0 /* json_layout.cxx */; };
 /* End PBXBuildFile section */
 
@@ -86,10 +85,9 @@
 		CD1CDE9122543E7E00E5B6B2 /* test_properties.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = test_properties.cxx; sourceTree = "<group>"; };
 		CD1CDEAE22556B7E00E5B6B2 /* logger_impl.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = logger_impl.cxx; sourceTree = "<group>"; };
 		CD1CDEB022557FB600E5B6B2 /* default_layout.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = default_layout.cxx; sourceTree = "<group>"; };
-		CD1CDEB22256B04600E5B6B2 /* format_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = format_test.cxx; sourceTree = "<group>"; };
 		CD1CDEB42256C94000E5B6B2 /* pattern_layout.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = pattern_layout.cxx; sourceTree = "<group>"; };
 		CD2973991D7B401F00E37217 /* logger.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = logger.cxx; sourceTree = "<group>"; };
-		CD3C80BE1D6A2CA300ACC795 /* format.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = format.cxx; sourceTree = "<group>"; };
+		CD3C80BE1D6A2CA300ACC795 /* pattern_layout_format.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = pattern_layout_format.cxx; sourceTree = "<group>"; };
 		CD6F73EA225187A10081ED74 /* logger */ = {isa = PBXFileReference; lastKnownFileType = folder; name = logger; path = include/logger; sourceTree = "<group>"; };
 		CD6F73FC225187E10081ED74 /* logger_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = logger_test.cxx; sourceTree = "<group>"; };
 		CD6F7401225187F40081ED74 /* logger_test.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = logger_test.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -106,7 +104,9 @@
 		CD88E95D2252D3EF00927F40 /* c_logger.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = c_logger.cxx; sourceTree = "<group>"; };
 		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>"; };
+		CDA494DD2256D5F40041620C /* pattern_layout_date.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = pattern_layout_date.cxx; sourceTree = "<group>"; };
+		CDC0E0472267EA30001EDAB7 /* logger_impl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = logger_impl.h; sourceTree = "<group>"; };
+		CDC0E04C2267F8A9001EDAB7 /* pattern_layout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = pattern_layout.h; sourceTree = "<group>"; };
 		CDEA62D4225A3B0B00A6FAE0 /* json_layout.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = json_layout.cxx; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
@@ -158,7 +158,10 @@
 			isa = PBXGroup;
 			children = (
 				CD1CDE862252E5B900E5B6B2 /* properties.cxx */,
+				CDA494DD2256D5F40041620C /* pattern_layout_date.cxx */,
+				CD3C80BE1D6A2CA300ACC795 /* pattern_layout_format.cxx */,
 				CD1CDEB42256C94000E5B6B2 /* pattern_layout.cxx */,
+				CDC0E04C2267F8A9001EDAB7 /* pattern_layout.h */,
 				CDEA62D4225A3B0B00A6FAE0 /* json_layout.cxx */,
 				CD1CDEB022557FB600E5B6B2 /* default_layout.cxx */,
 				CD1CDE8A2252E61800E5B6B2 /* console_appender.cxx */,
@@ -173,12 +176,11 @@
 				CD1CDE812252E54100E5B6B2 /* loggers */,
 				CD88E95D2252D3EF00927F40 /* c_logger.cxx */,
 				CD2973991D7B401F00E37217 /* logger.cxx */,
+				CDC0E0472267EA30001EDAB7 /* logger_impl.h */,
 				CD1CDEAE22556B7E00E5B6B2 /* logger_impl.cxx */,
 				CD88E9552252BDFC00927F40 /* log_manager.cxx */,
 				CD88E9642252D6C700927F40 /* common.h */,
 				CD88E9612252D67A00927F40 /* common.cxx */,
-				CDA494DD2256D5F40041620C /* date_format.cxx */,
-				CD3C80BE1D6A2CA300ACC795 /* format.cxx */,
 			);
 			path = src;
 			sourceTree = "<group>";
@@ -187,7 +189,6 @@
 			isa = PBXGroup;
 			children = (
 				CD6F73FC225187E10081ED74 /* logger_test.cxx */,
-				CD1CDEB22256B04600E5B6B2 /* format_test.cxx */,
 				CD760CB822621776008A62DE /* pattern_layout_test.cxx */,
 				CD760CC822627202008A62DE /* json_layout_test.cxx */,
 				CD760CBE226221F6008A62DE /* console_appender_test.cxx */,
@@ -370,8 +371,8 @@
 				CD29739B1D7B401F00E37217 /* logger.cxx in Sources */,
 				CDEA62D5225A3B0B00A6FAE0 /* json_layout.cxx in Sources */,
 				CD88E9572252BDFC00927F40 /* log_manager.cxx in Sources */,
-				CDA494DE2256D5F40041620C /* date_format.cxx in Sources */,
-				CD3C80C01D6A2CA300ACC795 /* format.cxx in Sources */,
+				CDA494DE2256D5F40041620C /* pattern_layout_date.cxx in Sources */,
+				CD3C80C01D6A2CA300ACC795 /* pattern_layout_format.cxx in Sources */,
 				CD88E95F2252D3EF00927F40 /* c_logger.cxx in Sources */,
 				CD1CDE872252E5B900E5B6B2 /* properties.cxx in Sources */,
 				CD1CDE8B2252E61800E5B6B2 /* console_appender.cxx in Sources */,
@@ -394,7 +395,6 @@
 				CD760CC1226226CC008A62DE /* file_appender_test.cxx in Sources */,
 				CD1CDE9222543E7E00E5B6B2 /* test_properties.cxx in Sources */,
 				CD760CC922627202008A62DE /* json_layout_test.cxx in Sources */,
-				CD1CDEB32256B04600E5B6B2 /* format_test.cxx in Sources */,
 				CD1CDE8D22540D9B00E5B6B2 /* c_logger_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;

+ 1 - 1
src/c_logger.cxx

@@ -8,7 +8,7 @@
 #include "logger/c_logger.h"
 
 #include "common.h"
-#include "logger/detail/logger_impl.h"
+#include "logger_impl.h"
 #include "logger/logpacket.h"
 
 using namespace logging;

+ 1 - 0
src/common.cxx

@@ -11,6 +11,7 @@
 #include <ctime>
 
 #include "logger/exception.h"
+#include "logger/logpacket.h"
 
 namespace logging {
   timeval now() {

+ 1 - 2
src/common.h

@@ -9,9 +9,8 @@
 
 #include <sys/time.h>
 
-#include "logger/logger_fwd.h"
-
 namespace logging {
+  enum class level : int;
   timeval now();
 
   level level_from_string(std::string const & value);

+ 1 - 2
src/log_manager.cxx

@@ -14,12 +14,11 @@
 #include "resource_factory/prototype_factory.hpp"
 
 #include "common.h"
+#include "logger_impl.h"
 #include "logger/c_logger.h"
-#include "logger/detail/appender.h"
 #include "logger/exception.h"
 #include "logger/logger.h"
 #include "logger/properties.h"
-#include "logger_impl.h"
 
 INSTANTIATE_PROTOTYPE_FACTORY_2(logging::appenders);
 INSTANTIATE_PROTOTYPE_FACTORY_2(logging::layouts);

+ 7 - 2
src/logger.cxx

@@ -8,7 +8,7 @@
 #include "logger/logger.h"
 
 #include "common.h"
-#include "logger/detail/logger_impl.h"
+#include "logger_impl.h"
 #include "logger/logpacket.h"
 
 namespace logging {
@@ -20,7 +20,12 @@ namespace logging {
     if (impl_) impl_->flush();
   }
   
-  void logger::log(level ll, location_info info, message const & msg) {
+  void logger::log(level ll, message const & msg) {
+    impl_->write({ now(), ll, {}, logger_name_, msg });
+  }
+  
+  void logger::log(level ll, location_info const & info,
+                   message const & msg) {
     impl_->write({ now(), ll, info, logger_name_, msg });
   }
   

+ 7 - 2
include/logger/detail/logger_impl.h

@@ -1,12 +1,17 @@
 #pragma once
 
-#include "logger/logger_fwd.h"
-
 #include <memory>
 #include <vector>
 #include <utility>
 
 namespace logging {
+  class appender;
+  class layout;
+  enum class level : int;
+  struct logpacket;
+  using p_appender = std::shared_ptr<appender>;
+  using p_layout = std::shared_ptr<layout>;
+
   class log_appender {
   public:
     log_appender(p_appender append, p_layout layout);

+ 1 - 1
src/loggers/pattern_layout.cxx

@@ -8,9 +8,9 @@
 #include "resource_factory/prototype_factory.hpp"
 
 #include "logger/detail/layout.h"
-#include "logger/format.h"
 #include "logger/log_manager.h"
 #include "logger/properties.h"
+#include "pattern_layout.h"
 
 using namespace logging;
 

+ 5 - 28
include/logger/format.h

@@ -1,21 +1,18 @@
 //
-//  format.hpp
+//  pattern_layout.h
 //  logger
 //
-//  Created by Sam Jaffe on 8/21/16.
+//  Created by Sam Jaffe on 4/17/19.
 //
 
 #pragma once
 
 #include <functional>
-#include <sstream>
-#include <string>
+#include <iostream>
 #include <vector>
 
-#include "logger_fwd.h"
-#include "wrapper_object.h"
-
 namespace logging {
+  struct logpacket;
   class format {
   public:
     enum class token_id {
@@ -30,27 +27,7 @@ namespace logging {
   private:
     std::vector<generator> gen;
   };
-    
+  
   using string_generator = std::function<std::string(logpacket const &)>;
   string_generator get_date_formatter(std::string fmt);
-  
-  class message {
-  public:
-    message() = default;
-    message(char const * fmt) : format_code(fmt) {}
-    template <typename... Args>
-    message(std::string const & fmt, Args && ...args);
-    
-    std::string str() const;
-    Json::Value json() const;
-  private:
-    std::string format_code;
-    std::vector<detail::object> objects;
-  };
-  
-  template <typename... Args>
-  message::message(std::string const & fmt, Args && ...args)
-  : format_code(fmt), objects({detail::object(args)...}) {
-    
-  }
 }

+ 2 - 2
src/date_format.cxx

@@ -10,9 +10,9 @@
 #include <string>
 
 #include "logger/exception.h"
-#include "logger/format.h"
-#include "logger/logger_fwd.h"
 #include "logger/logpacket.h"
+#include "logger/message.h"
+#include "pattern_layout.h"
 
 namespace logging {
   std::string fmt_time(struct timeval round, char const * const fmt) {

+ 2 - 2
src/format.cxx

@@ -5,8 +5,6 @@
 //  Created by Sam Jaffe on 8/21/16.
 //
 
-#include "logger/format.h"
-
 #include <cstdint>
 #include <cstring>
 #include <ctime>
@@ -20,6 +18,8 @@
 #include "logger/exception.h"
 #include "logger/logger.h"
 #include "logger/logpacket.h"
+#include "logger/message.h"
+#include "pattern_layout.h"
 
 namespace logging {
   std::string fmt_time_with_milis(struct timeval, std::string const &);

+ 0 - 163
test/format_test.cxx

@@ -1,163 +0,0 @@
-//
-//  format_test.cxx
-//  logger_test
-//
-//  Created by Sam Jaffe on 4/4/19.
-//
-
-#include <gmock/gmock.h>
-
-#include "logger/exception.h"
-#include "logger/format.h"
-#include "logger/logpacket.h"
-
-using namespace logging;
-
-TEST(FormatTest, EmptyFormatterCanParse) {
-  EXPECT_NO_THROW(format::parse_format_string(""));
-}
-
-TEST(FormatTest, ThrowsForEndOfStringAfterPct) {
-  EXPECT_THROW(format::parse_format_string("%"),
-               format_parsing_exception);
-}
-
-TEST(FormatTest, RawStringFmtReturnsSelf) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("TEST STRING");
-  EXPECT_THAT(fmt.process({}), Eq("TEST STRING"));
-}
-
-TEST(FormatTest, NCharReturnsNewLine) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%n");
-  EXPECT_THAT(fmt.process({}), Eq("\n"));
-}
-
-TEST(FormatTest, DoublePctIsLiteral) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%%");
-  EXPECT_THAT(fmt.process({}), Eq("%"));
-}
-
-TEST(FormatTest, CatchesRawContentBeforeFmt) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("TEST%%");
-  EXPECT_THAT(fmt.process({}), Eq("TEST%"));
-}
-
-TEST(FormatTest, CatchesRawContentAfterFmt) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%%TEST");
-  EXPECT_THAT(fmt.process({}), Eq("%TEST"));
-}
-
-// Thursday, April 4, 2019 6:17:20 PM GMT
-namespace {
-  constexpr const int NOW = 1554401840;
-}
-
-TEST(FormatTest, HandlesDateFormatter) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%d");
-  EXPECT_THAT(fmt.process({{NOW,0}}), Eq("2019-04-04 18:17:20,000"));
-}
-
-TEST(FormatTest, FormatsMilliseconds) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%d");
-  EXPECT_THAT(fmt.process({{NOW,123000}}), Eq("2019-04-04 18:17:20,123"));
-}
-
-TEST(FormatTest, ThrowsIfCustomFmtUnterminated) {
-  using testing::Eq;
-  EXPECT_THROW(format::parse_format_string("%d{%"),
-               format_parsing_exception);
-}
-
-TEST(FormatTest, SupportsCustomFormatWithBrace) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%d{%Y}");
-  EXPECT_THAT(fmt.process({{NOW,0}}), Eq("2019"));
-}
-
-TEST(FormatTest, FormatsCustomMilliseconds) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%d{%_ms}");
-  EXPECT_THAT(fmt.process({{NOW,123000}}), Eq("123"));
-}
-
-TEST(FormatTest, SupportsISO8601Format) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%d{ISO8601}");
-  EXPECT_THAT(fmt.process({{NOW,0}}), Eq("2019-04-04T18:17:20.000Z"));
-}
-
-TEST(FormatTest, SupportsSingleDayFormat) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%d{ABSOLUTE}");
-  EXPECT_THAT(fmt.process({{NOW,0}}), Eq("18:17:20,000"));
-}
-
-TEST(FormatTest, SupportsHumanDateFormat) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%d{DATE}");
-  EXPECT_THAT(fmt.process({{NOW,0}}), Eq("04 Apr 2019 18:17:20,000"));
-}
-
-TEST(FormatTest, LoggerIdIsCToken) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%c");
-  EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}),
-              Eq("UNIT_TEST"));
-}
-
-TEST(FormatTest, LogLevelIsPToken) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%p");
-  EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}),
-              Eq("ERROR"));
-}
-
-TEST(FormatTest, LogMessageIsMToken) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%m");
-  EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}),
-              Eq("HELLO"));
-}
-
-TEST(FormatTest, ThrowsOnUnknownToken) {
-  using testing::Eq;
-  EXPECT_THROW(format::parse_format_string("%q"),
-               unknown_format_specifier);
-}
-
-TEST(FormatTest, TokenCanBeTruncatedInFormat) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%.3m");
-  EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}),
-              Eq("HEL"));
-}
-
-TEST(FormatTest, TokenCanBeLeftPadded) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%6m");
-  EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}),
-              Eq(" HELLO"));
-}
-
-TEST(FormatTest, TokenCanBeRightPadded) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%-6m");
-  EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}),
-              Eq("HELLO "));
-}
-
-TEST(FormatTest, TokenCanBeSizeBound) {
-  using testing::Eq;
-  auto fmt = format::parse_format_string("%6.8m");
-  EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}),
-              Eq(" HELLO"));
-  EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO FRIEND"}),
-              Eq("HELLO FR"));
-}

+ 1 - 1
test/mock_logger.h

@@ -14,7 +14,7 @@
 
 #include "logger/detail/appender.h"
 #include "logger/detail/layout.h"
-#include "logger/detail/logger_impl.h"
+#include "logger_impl.h"
 #include "logger/logpacket.h"
 
 namespace logging {

+ 146 - 11
test/pattern_layout_test.cxx

@@ -10,6 +10,7 @@
 #include "resource_factory/prototype_factory.hpp"
 
 #include "logger/detail/layout.h"
+#include "logger/exception.h"
 #include "logger/log_manager.h"
 #include "logger/logpacket.h"
 #include "logger/properties.h"
@@ -19,19 +20,153 @@ namespace {
   constexpr const int NOW = 1554401840;
 }
 
-TEST(PatternLayoutTest, TestInvokesFormatter) {
+std::shared_ptr<logging::layout>
+GetPatternLayout(std::string const & fmt) {
   using namespace logging;
   using namespace logging::property;
-  properties props{_obj({
-    {"pattern", _v("%d{%I:%M:%S.%_ms} [%%] %-5.5p %.36c - %m")}
-  })};
-  auto playout = layouts::instance().get("PatternLayout", props);
-  
+  properties props{_obj({{"pattern", _v(fmt)}})};
+  return layouts::instance().get("PatternLayout", props);
+}
+
+std::string DoFormat(std::string const & fmt,
+                     logging::logpacket const & pkt) {
   std::stringstream ss;
-  playout->format(ss, {{NOW, 0}, level::warning, {}, "UnitTest",
-    "This is a test message"});
-  
+  GetPatternLayout(fmt)->format(ss, pkt);
+  return ss.str();
+}
+
+using namespace logging;
+
+logpacket getpkt(std::string const & msg) {
+  return logpacket{{}, level::error, {}, "UNIT_TEST", msg};
+};
+
+TEST(PatternLayoutTest, EmptyFormatterCanParse) {
+  EXPECT_NO_THROW(GetPatternLayout(""));
+}
+
+TEST(PatternLayoutTest, ThrowsForEndOfStringAfterPct) {
+  EXPECT_THROW(GetPatternLayout("%"),
+               logging::format_parsing_exception);
+}
+
+TEST(PatternLayoutTest, RawStringFmtReturnsSelf) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("TEST STRING", {}), Eq("TEST STRING"));
+}
+
+TEST(PatternLayoutTest, NCharReturnsNewLine) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%n", {}), Eq("\n"));
+}
+
+TEST(PatternLayoutTest, DoublePctIsLiteral) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%%", {}), Eq("%"));
+}
+
+TEST(PatternLayoutTest, CatchesRawContentBeforeFmt) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("TEST%%", {}), Eq("TEST%"));
+}
+
+TEST(PatternLayoutTest, CatchesRawContentAfterFmt) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%%TEST", {}), Eq("%TEST"));
+}
+
+TEST(PatternLayoutTest, HandlesDateFormatter) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%d", {{NOW,0}}),
+              Eq("2019-04-04 18:17:20,000"));
+}
+
+TEST(PatternLayoutTest, FormatsMilliseconds) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%d", {{NOW,123000}}),
+              Eq("2019-04-04 18:17:20,123"));
+}
+
+TEST(PatternLayoutTest, ThrowsIfCustomFmtUnterminated) {
+  using testing::Eq;
+  EXPECT_THROW(GetPatternLayout("%d{%"),
+               logging::format_parsing_exception);
+}
+
+TEST(PatternLayoutTest, SupportsCustomFormatWithBrace) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%d{%Y}", {{NOW,0}}), Eq("2019"));
+}
+
+TEST(PatternLayoutTest, FormatsCustomMilliseconds) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%d{%_ms}", {{NOW,123000}}), Eq("123"));
+}
+
+TEST(PatternLayoutTest, SupportsISO8601Format) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%d{ISO8601}", {{NOW,0}}),
+              Eq("2019-04-04T18:17:20.000Z"));
+}
+
+TEST(PatternLayoutTest, SupportsSingleDayFormat) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%d{ABSOLUTE}", {{NOW,0}}),
+              Eq("18:17:20,000"));
+}
+
+TEST(PatternLayoutTest, SupportsHumanDateFormat) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%d{DATE}", {{NOW,0}}),
+              Eq("04 Apr 2019 18:17:20,000"));
+}
+
+TEST(PatternLayoutTest, LoggerIdIsCToken) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%c", getpkt("HELLO")),
+              Eq("UNIT_TEST"));
+}
+
+TEST(PatternLayoutTest, LogLevelIsPToken) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%p", getpkt("HELLO")),
+              Eq("ERROR"));
+}
+
+TEST(PatternLayoutTest, LogMessageIsMToken) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%m", getpkt("HELLO")),
+              Eq("HELLO"));
+}
+
+TEST(PatternLayoutTest, ThrowsOnUnknownToken) {
+  using testing::Eq;
+  EXPECT_THROW(GetPatternLayout("%q"),
+               logging::unknown_format_specifier);
+}
+
+TEST(PatternLayoutTest, TokenCanBeTruncatedInFormat) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%.3m", getpkt("HELLO")),
+              Eq("HEL"));
+}
+
+TEST(PatternLayoutTest, TokenCanBeLeftPadded) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%6m", getpkt("HELLO")),
+              Eq(" HELLO"));
+}
+
+TEST(PatternLayoutTest, TokenCanBeRightPadded) {
+  using testing::Eq;
+  EXPECT_THAT(DoFormat("%-6m", getpkt("HELLO")),
+              Eq("HELLO "));
+}
+
+TEST(PatternLayoutTest, TokenCanBeSizeBound) {
   using testing::Eq;
-  EXPECT_THAT(ss.str(), Eq("06:17:20.000 [%] WARNI UnitTest -"
-                           " This is a test message"));
+  EXPECT_THAT(DoFormat("%6.8m", getpkt("HELLO")),
+              Eq(" HELLO"));
+  EXPECT_THAT(DoFormat("%6.8m", getpkt("HELLO FRIEND")),
+              Eq("HELLO FR"));
 }