Ver código fonte

Separate out date parsing code into its own file

Sam Jaffe 6 anos atrás
pai
commit
d951dbf35d
4 arquivos alterados com 78 adições e 54 exclusões
  1. 3 0
      include/logger/format.h
  2. 4 0
      logger.xcodeproj/project.pbxproj
  3. 67 0
      src/date_format.cxx
  4. 4 54
      src/format.cxx

+ 3 - 0
include/logger/format.h

@@ -30,6 +30,9 @@ namespace logging {
     std::vector<generator> gen;
   };
   
+  using string_generator = std::function<std::string(logpacket const &)>;
+  string_generator get_date_formatter(std::string fmt);
+  
   inline void format_msg(std::stringstream & msg, std::string const & interp,
                          size_t pos) {
     msg << interp.substr(pos);

+ 4 - 0
logger.xcodeproj/project.pbxproj

@@ -26,6 +26,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 */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -93,6 +94,7 @@
 		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>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -160,6 +162,7 @@
 				CD88E95D2252D3EF00927F40 /* c_logger.cxx */,
 				CD88E9552252BDFC00927F40 /* log_manager.cxx */,
 				CD3C80BE1D6A2CA300ACC795 /* format.cxx */,
+				CDA494DD2256D5F40041620C /* date_format.cxx */,
 			);
 			path = src;
 			sourceTree = "<group>";
@@ -345,6 +348,7 @@
 			files = (
 				CD29739B1D7B401F00E37217 /* logger.cxx in Sources */,
 				CD88E9572252BDFC00927F40 /* log_manager.cxx in Sources */,
+				CDA494DE2256D5F40041620C /* date_format.cxx in Sources */,
 				CD3C80C01D6A2CA300ACC795 /* format.cxx in Sources */,
 				CD88E95F2252D3EF00927F40 /* c_logger.cxx in Sources */,
 				CD1CDE872252E5B900E5B6B2 /* properties.cxx in Sources */,

+ 67 - 0
src/date_format.cxx

@@ -0,0 +1,67 @@
+//
+//  date_format.cxx
+//  logging
+//
+//  Created by Sam Jaffe on 4/4/19.
+//
+
+#include <ctime>
+#include <functional>
+#include <string>
+
+#include "logger/exception.h"
+#include "logger/format.h"
+#include "logger/logger_fwd.h"
+
+namespace logging {
+  std::string fmt_time(struct timeval round, char const * const fmt) {
+    struct tm time;
+    // Supports localtime when requested, but you can't really test that
+    if (strstr(fmt, "%z") || strstr(fmt, "%Z")) {
+      localtime_r(&round.tv_sec, &time);
+    } else {
+      gmtime_r(&round.tv_sec, &time);
+    }
+    char buf[64] = {'\0'};
+    std::strftime(buf, sizeof(buf), fmt, &time);
+    return buf;
+  }
+  
+  std::string fmt_time(struct timeval round, std::string const & fmt) {
+    return fmt_time(round, fmt.c_str());
+  }
+  
+  std::string fmt_time_with_milis(struct timeval round,
+                                  std::string const & fmt) {
+    char buf[64] = {'\0'};
+    snprintf(buf, sizeof(buf), fmt.c_str(), round.tv_usec/1000);
+    return fmt_time(round, buf);
+  }
+  
+  string_generator get_date_formatter(std::string fmt) {
+    if (fmt.find("%_ms") != std::string::npos) {
+      size_t pos = 0;
+      while ((pos = fmt.find("%", pos)) != std::string::npos) {
+        fmt.replace(pos, 1, "%%");
+        pos += 2;
+      }
+      fmt.replace(fmt.find("%%_ms"), 5, "%.03d");
+      return [=](logpacket const & lp) {
+        return fmt_time_with_milis(lp.time, fmt);
+      };
+    } else {
+      return [=](logpacket const & lp) {
+        return fmt_time(lp.time, fmt);
+      };
+    }
+  }
+  
+  string_generator parse_date_format_string(char const * str) {
+    char const * const end = strchr(str, '}');
+    if (end == nullptr) {
+      std::string error_msg{"expected date-format specifier to terminate"};
+      throw format_parsing_exception{error_msg};
+    }
+    return get_date_formatter(std::string(str, end));
+  }
+}

+ 4 - 54
src/format.cxx

@@ -27,64 +27,14 @@
 #endif
 
 namespace logging {
-  namespace {
-    std::string fmt_time(struct timeval round, char const * const fmt) {
-      struct tm time;
-      // Supports localtime when requested, but you can't really test that
-      if (strstr(fmt, "%z") || strstr(fmt, "%Z")) {
-        localtime_r(&round.tv_sec, &time);
-      } else {
-        gmtime_r(&round.tv_sec, &time);
-      }
-      char buf[64] = {'\0'};
-      std::strftime(buf, sizeof(buf), fmt, &time);
-      return buf;
-    }
-    
-    std::string fmt_time(struct timeval round, std::string const & fmt) {
-      return fmt_time(round, fmt.c_str());
-    }
-    
-    std::string fmt_time_with_milis(struct timeval round,
-                                    std::string const & fmt) {
-      char buf[64] = {'\0'};
-      snprintf(buf, sizeof(buf), fmt.c_str(), round.tv_usec/1000);
-      return fmt_time(round, buf);
-    }
-  }
-  using string_generator = std::function<std::string(logpacket const &)>;
-  
-  string_generator parse_date_format_string( char const * str ) {
-    char const * const end = strchr(str, '}');
-    if (end == nullptr) {
-      std::string error_msg{"expected date-format specifier to terminate"};
-      throw format_parsing_exception{error_msg};
-    }
-
-    char const * const has_millis = strstr( str, "%_ms");
-    std::string fmtstring{str, end};
-    if ( has_millis && has_millis < end ) {
-      size_t pos = 0;
-      while ( (pos = fmtstring.find("%", pos)) != std::string::npos ) {
-        fmtstring.replace(pos, 1, "%%");
-        pos += 2;
-      }
-      fmtstring.replace(fmtstring.find("%%_ms"), 5, "%.03d");
-      return [=](logpacket const & lp) {
-        return fmt_time_with_milis(lp.time, fmtstring);
-      };
-    } else {
-      return [=](logpacket const & lp) {
-        return fmt_time(lp.time, fmtstring);
-      };
-    }
-  }
+  std::string fmt_time_with_milis(struct timeval, std::string const &);
+  string_generator parse_date_format_string(char const *);
   
 #define is( chr ) *next == chr
 #define is_string( str ) ! strncmp(next, str, strlen(str))
   string_generator date_token(char const * next) {
     std::string predef_format = "%%Y-%%m-%%d %%H:%%M:%%S,%.03d";
-    if ( is_string("{ISO8601}")) {
+    if (is_string("{ISO8601}")) {
       predef_format = "%%Y-%%m-%%dT%%H:%%M:%%S.%.03dZ";
     } else if (is_string("{ABSOLUTE}")) {
       predef_format = "%%H:%%M:%%S,%.03d";
@@ -97,7 +47,7 @@ namespace logging {
       return fmt_time_with_milis(lp.time, predef_format);
     };
   }
-  
+
   string_generator string_token(std::string str) {
     return [=]( logpacket const & ){
       return str;