CombineHarvester
Logging.cc
Go to the documentation of this file.
2 #include <string>
3 #include <iostream>
4 #include "boost/lexical_cast.hpp"
5 
6 namespace ch {
7 
8 std::string GetQualififedName(std::string const& str) {
9  std::size_t bpos = str.find_first_of('(');
10  if (bpos == std::string::npos) return str;
11  std::size_t apos = bpos;
12  bool break_cond = false;
13  bool in_clause = false;
14  while (!break_cond && apos > 0) {
15  --apos;
16  if (str[apos] == '>') in_clause = true;
17  if (str[apos] == '<') in_clause = false;
18  if (str[apos] == ' ' && !in_clause) {
19  break_cond = true;
20  ++apos;
21  }
22  }
23  std::string cpy = str.substr(apos, bpos - apos);
24  return cpy;
25 }
26 
27 void LogLine(std::ostream& stream, std::string const& func,
28  std::string const& message) {
29  stream << "[" << func << "] " << message << "\n";
30 }
31 
32 std::string FnError(std::string const& message, std::string const& file,
33  unsigned line, std::string const& fn) {
34  std::string res;
35  std::string banner(79, '*');
36  res += "\n" + banner;
37  res += "\nContext: Function " + GetQualififedName(fn) + " at ";
38  res += "\n " + file + ":" + boost::lexical_cast<std::string>(line);
39  res += "\nProblem: " + message;
40  res += "\n" + banner;
41  res +=
42  "\nPlease report issues at\n "
43  "https://github.com/cms-analysis/CombineHarvester/issues";
44  res += "\n" + banner;
45  return res;
46 }
47 
48 // Implementation of FnTimer ("Function Timer") class
49 FnTimer::FnTimer(std::string name) : name_(name), calls_(0), elapsed_(0.) {}
51  printf(
52  "[Timer] %-40s Calls: %-20u Total [s]: %-20.5g Per-call [s]: %-20.5g\n",
53  name_.c_str(), calls_, elapsed_, elapsed_ / double(calls_));
54 }
56  ++calls_;
57  return Token(this);
58 }
59 void FnTimer::StartTimer() { start_ = std::chrono::system_clock::now(); }
61  end_ = std::chrono::system_clock::now();
62  elapsed_ += std::chrono::duration<double>(end_ - start_).count();
63 }
64 FnTimer::Token::Token(FnTimer* src) : src_(src) { src_->StartTimer(); }
65 FnTimer::Token::~Token() { src_->StopTimer(); }
66 }
Token(FnTimer *src)
Definition: Logging.cc:64
Determine the total amount of time spent in a function.
Definition: Logging.h:85
void StopTimer()
Definition: Logging.cc:60
Token Inc()
Definition: Logging.cc:55
void StartTimer()
Definition: Logging.cc:59
FnTimer(std::string name)
Definition: Logging.cc:49
Definition: Algorithm.h:10
std::string GetQualififedName(std::string const &str)
Extracts the fully-qualified function name from a complete function signature.
Definition: Logging.cc:8
void LogLine(std::ostream &stream, std::string const &func, std::string const &message)
Writes a logging message to a given ostream.
Definition: Logging.cc:27
std::string FnError(std::string const &message, std::string const &file, unsigned line, std::string const &fn)
Generates an error message which includes the name of the calling function and the filename and line ...
Definition: Logging.cc:32