|
|
@@ -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_);
|
|
|
+ }
|
|
|
+
|
|
|
+}}
|