ealogger
ealogger is a c++ library that provides a blazing fast and easy to use logging mechanism
utility.h
Go to the documentation of this file.
1 // ealogger is a simple, asynchronous and powerful logger library for c++
2 // Copyright 2013 - 2016 Christian Rapp
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef UTILITY_H
17 #define UTILITY_H
18 
24 #ifndef _WIN32
25 #include <unistd.h>
26 #endif
27 #ifdef _WIN32
28 #include <winsock2.h>
29 #pragma comment(lib, "Ws2_32.lib")
30 #endif
31 #include <algorithm>
32 #include <ctime>
33 #include <regex>
34 #include <string>
35 // Check for backtrace function
36 #ifdef __GNUC__
37 #include <cxxabi.h>
38 #include <execinfo.h>
39 #endif
40 
41 namespace ealogger
42 {
47 namespace utility
48 {
53 inline std::string get_hostname()
54 {
55  std::string hname = "UNKNOWN";
56  char hchar[1024] = "\0";
57  if (gethostname(hchar, sizeof(hchar)) == 0) {
58  hname = std::string(hchar);
59  }
60  return hname;
61 }
62 
68 inline std::string get_file_name(const std::string &absolute_path)
69 {
70  const auto pos = absolute_path.find_last_of("/\\");
71  return (absolute_path.substr(pos + 1));
72 }
73 
83 inline void stack_trace(unsigned int size,
84  std::vector<std::string> &stack_msg_vec)
85 {
86 #ifdef __GLIBC__
87  void *addrlist[size + 1];
88 
89  size_t no_of_stack_addresses =
90  backtrace(addrlist, sizeof(addrlist) / sizeof(void *));
91  char **temp_symbols = backtrace_symbols(addrlist, no_of_stack_addresses);
92 
93  // initialize a vector of string with the char**
94  std::vector<std::string> symbollist(temp_symbols,
95  temp_symbols + no_of_stack_addresses);
96  // temp_symbols has to be freed
97  free(temp_symbols);
98 
99  std::regex rgx("(^.*\\()(.*\\+)(0[xX][0-9a-fA-F]+\\))");
100 
101  // TODO: This always prints stack_trace as first symbol. Should we omit
102  // this?
103  for (const auto &symbol : symbollist) {
104  std::smatch match;
105  // if there is no match with our regex we have to continue and use the
106  // original symbol
107  if (!std::regex_search(symbol, match, rgx)) {
108  stack_msg_vec.push_back(symbol);
109  continue;
110  }
111  // get the regex matches and create the 3 strings we need
112  std::string file = match[1].str();
113  file = file.substr(0, file.size() - 1);
114  std::string mangled_name = match[2].str();
115  mangled_name = mangled_name.substr(0, mangled_name.size() - 1);
116  std::string caller = match[3].str();
117  caller = caller.substr(0, caller.size() - 1);
118 
119  // demangle status
120  int status = 0;
121  // must be freed
122  char *realname =
123  abi::__cxa_demangle(mangled_name.c_str(), 0, 0, &status);
124  if (status == 0) {
125  stack_msg_vec.emplace_back(file + " : " + std::string(realname) +
126  "+" + caller);
127  } else {
128  stack_msg_vec.emplace_back(file + " : " + mangled_name + "()" + "+" +
129  caller);
130  }
131  free(realname);
132  }
133 #endif
134 }
135 
145 inline std::string format_time_to_string(std::time_t t,
146  const std::string &time_format)
147 {
148  // time struct
149  struct tm *timeinfo;
150  // buffer where we store the formatted time string
151  char buffer[80];
152 
153  // FIXME: This is not threadsafe. Use localtime_r instead
154  timeinfo = std::localtime(&t);
155  // localtime_r
156 
157  std::strftime(buffer, 80, time_format.c_str(), timeinfo);
158  return (std::string(buffer));
159 }
160 
173 inline std::string format_time_to_string(const std::string &time_format)
174 {
175  // get raw time
176  time_t rawtime;
177  std::time(&rawtime);
178  return utility::format_time_to_string(std::move(rawtime), time_format);
179 }
180 }
181 }
182 
183 #endif /* ifndef UTILITY_H */
Main namespace for ealogger.
Definition: conversion_pattern.h:28
void stack_trace(unsigned int size, std::vector< std::string > &stack_msg_vec)
Print a demangled stacktrace.
Definition: utility.h:83
std::string format_time_to_string(std::time_t t, const std::string &time_format)
Get a formatted time string based on.
Definition: utility.h:145
std::string get_hostname()
Get local hostname.
Definition: utility.h:53
std::string get_file_name(const std::string &absolute_path)
Get the last element of a path.
Definition: utility.h:68
Namespace for ealogger utility functions.