Prechádzať zdrojové kódy

Move some formatting functions partially into another file to support access from other contexts.

Sam Jaffe 5 rokov pred
rodič
commit
bfc3835675

+ 37 - 0
include/logger/detail/data_accessors.h

@@ -0,0 +1,37 @@
+#pragma once
+
+#include <string>
+#include <vector>
+
+namespace logging {
+  struct logpacket;
+}
+
+namespace logging { namespace detail {
+  class thread_info_helper {
+  public:
+    std::string thread_id() const;
+    std::string thread_name() const;
+  };
+  
+  class calling_class_helper {
+  private:
+    size_t max_components_;
+    
+  public:
+    calling_class_helper(size_t max_components = 0)
+      : max_components_(max_components) {}
+    
+    std::vector<std::string> full_name(logpacket const & lp) const;
+    std::string full_name(logpacket const & lp, std::string const & join) const;
+  };
+  
+  class time_elapsed_helper {
+  private:
+    long long created_;
+    
+  public:
+    time_elapsed_helper();
+    std::string elapsed(logpacket const & packet) const;
+  };
+}}

+ 4 - 0
logger.xcodeproj/project.pbxproj

@@ -19,6 +19,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 */; };
+		CD90C1DA24E5B988008A1402 /* data_accessors.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD90C1D924E5B988008A1402 /* data_accessors.cxx */; };
 		CDA494DE2256D5F40041620C /* pattern_layout_date.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDA494DD2256D5F40041620C /* pattern_layout_date.cxx */; };
 		CDC0E0512269378E001EDAB7 /* message.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDC0E0502269378E001EDAB7 /* message.cxx */; };
 		CDCB3C5924E480FC0029B771 /* libjsoncpp.1.9.2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CDCB3C5724E480E70029B771 /* libjsoncpp.1.9.2.dylib */; };
@@ -106,6 +107,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>"; };
+		CD90C1D924E5B988008A1402 /* data_accessors.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = data_accessors.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>"; };
@@ -193,6 +195,7 @@
 				CDC0E0502269378E001EDAB7 /* message.cxx */,
 				CD88E9642252D6C700927F40 /* common.h */,
 				CD88E9612252D67A00927F40 /* common.cxx */,
+				CD90C1D924E5B988008A1402 /* data_accessors.cxx */,
 			);
 			path = src;
 			sourceTree = "<group>";
@@ -397,6 +400,7 @@
 				CD1CDE8B2252E61800E5B6B2 /* console_appender.cxx in Sources */,
 				CD1CDEAF22556B7E00E5B6B2 /* logger_impl.cxx in Sources */,
 				CD1CDE892252E60900E5B6B2 /* file_appender.cxx in Sources */,
+				CD90C1DA24E5B988008A1402 /* data_accessors.cxx in Sources */,
 				CD1CDEB52256C94000E5B6B2 /* pattern_layout.cxx in Sources */,
 				CD88E9632252D67A00927F40 /* common.cxx in Sources */,
 				CD1CDEB122557FB600E5B6B2 /* default_layout.cxx in Sources */,

+ 101 - 0
src/data_accessors.cxx

@@ -0,0 +1,101 @@
+//
+//  data_accessors.cpp
+//  logging
+//
+//  Created by Sam Jaffe on 8/13/20.
+//
+
+#include "logger/detail/data_accessors.h"
+
+#include <sstream>
+#include <thread>
+
+#include "common.h"
+#include "logger/logpacket.h"
+
+namespace {
+  void erase_prefix(std::string & str, char const * prefix) {
+    size_t size = strlen(prefix);
+    if (str.find(prefix, 0, size) == 0) {
+      str.erase(0, size);
+    }
+  }
+  
+  void trim_upto_prefix_alpha(std::string & str, char const * prefix) {
+    size_t size = strlen(prefix);
+    for (size_t i = str.find(prefix); i != std::string::npos;
+         i = str.find(prefix, i + 1)) {
+      if (isalpha(str[i + size])) {
+        str.erase(0, i + size);
+        return;
+      }
+    }
+  }
+  
+  long long millis(timeval tv) {
+    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
+  }
+}
+
+namespace logging { namespace detail {
+  
+  std::string thread_info_helper::thread_id() const {
+    std::stringstream ss;
+    ss << std::this_thread::get_id();
+    return ss.str();
+  }
+  
+//  std::string thread_info_helper::thread_name() const;
+  
+  std::vector<std::string>
+  calling_class_helper::full_name(logpacket const & packet) const {
+    std::string pf = packet.info.pretty_function;
+    std::string func = std::string(packet.info.function) + "(";
+    // If this is a function in the global namespace, return just the empty str
+    if (pf[pf.find(func)-1] == ' ') {
+      return {""};
+    }
+    // Delete the function name, and everything after
+    pf.erase(pf.find(func));
+    // Since this may be a class function, delete the ending bit
+    if (pf.rfind("::", pf.size()) != std::string::npos) {
+      pf.erase(pf.size() - 2);
+    }
+    // Erase virtual and static prefixes
+    erase_prefix(pf, "virtual ");
+    erase_prefix(pf, "static ");
+    // This gets rid of any template or reference return types, whose typenames
+    // might have spaces in then.
+    trim_upto_prefix_alpha(pf, "> ");
+    trim_upto_prefix_alpha(pf, "&");
+    // Erase any remaining return type information
+    pf.erase(0, pf.find(" ") + 1);
+    std::vector<std::string> rval;
+    size_t i = 0, n = pf.find("::");
+    rval.emplace_back(pf.substr(i, n));
+    for (i = n, n = pf.find("::", i+2); i != std::string::npos;
+         i = n, n = pf.find("::", i+2)) {
+      rval.emplace_back(pf.substr(i+2, n));
+    }
+    if (max_components_ && max_components_ < rval.size()) {
+      rval.erase(rval.begin(), rval.end() - max_components_);
+    }
+    return rval;
+  }
+  
+  std::string calling_class_helper::full_name(logpacket const & packet,
+                                              std::string const & join) const {
+    auto tokens = full_name(packet);
+    std::stringstream ss;
+    ss << tokens[0];
+    auto append = [&](auto & value) { ss << join << value; };
+    std::for_each(tokens.begin() + 1, tokens.end(), append);
+    return ss.str();
+  }
+  
+  time_elapsed_helper::time_elapsed_helper() : created_(millis(now())) {}
+  std::string time_elapsed_helper::elapsed(logpacket const & packet) const {
+    return std::to_string(millis(packet.time) - created_);
+  }
+
+}}

+ 15 - 52
src/loggers/pattern_layout_format.cxx

@@ -15,6 +15,7 @@
 #include <string>
 
 #include "common.h"
+#include "logger/detail/data_accessors.h"
 #include "logger/exception.h"
 #include "logger/logger.h"
 #include "logger/logpacket.h"
@@ -31,14 +32,10 @@ namespace logging { namespace {
     }
   };
 
-  struct time_elapsed_gen : public format::generator_t {
-    long long created;
-    time_elapsed_gen() : created(millis(now())) {}
-    static long long millis(timeval tv) {
-      return tv.tv_sec * 1000 + tv.tv_usec / 1000;
-    }
+  struct time_elapsed_gen : public detail::time_elapsed_helper,
+                            public format::generator_t {
     std::string str(logpacket const & lp) const override {
-      return std::to_string(millis(lp.time) - created);
+      return elapsed(lp);
     }
   };
 
@@ -48,55 +45,21 @@ namespace logging { namespace {
     }
   };
   
-  void erase_prefix(std::string & str, char const * prefix) {
-    size_t size = strlen(prefix);
-    if (str.find(prefix, 0, size) == 0) {
-      str.erase(0, size);
-    }
-  }
-  
-  void trim_upto_prefix_alpha(std::string & str, char const * prefix) {
-    size_t size = strlen(prefix);
-    for (size_t i = str.find(prefix); i != std::string::npos;
-         i = str.find(prefix, i + 1)) {
-      if (isalpha(str[i + size])) {
-        str.erase(0, i + size);
-        return;
-      }
-    }
-  }
-  
-  struct class_info_gen : public format::generator_t {
-    size_t components;
-    class_info_gen(char const * token) : components(0) {
+  struct class_info_gen : public detail::calling_class_helper,
+                          public format::generator_t {
+    static size_t components(char const * token) {
       if (token && *token == '{') {
-        components = atol(token + 1);
+        return atol(token + 1);
+      } else {
+        return 0;
       }
     }
+
+    class_info_gen(char const * token)
+        : detail::calling_class_helper(components(token)) {}
+
     std::string str(logpacket const & lp) const override {
-      std::string pf = lp.info.pretty_function;
-      std::string func = std::string(lp.info.function) + "(";
-      if (pf[pf.find(func)-1] == ' ') {
-        return "";
-      }
-      pf.erase(pf.find(func));
-      if (pf.rfind("::", pf.size()) != std::string::npos) {
-        pf.erase(pf.size() - 2);
-      }
-      // Erase virtual prefix
-      erase_prefix(pf, "virtual ");
-      erase_prefix(pf, "static ");
-      trim_upto_prefix_alpha(pf, "> ");
-      trim_upto_prefix_alpha(pf, "&");
-      pf.erase(0, pf.find(" ") + 1);
-      for (size_t i = pf.rfind("::"), c = 1;
-           i != std::string::npos && c <= components;
-           i = pf.rfind("::", i-1), ++c) {
-        if (c == components) {
-          pf.erase(0, i + 2);
-        }
-      }
-      return pf;
+      return full_name(lp, "::");
     }
   };