EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
reportingUtils.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file reportingUtils.h
1 
2 //
3 // Copyright 2010
4 //
5 // This file is part of starlight.
6 //
7 // starlight is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // starlight is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with starlight. If not, see <http://www.gnu.org/licenses/>.
19 //
21 //
22 // File and Version Information:
23 // $Rev:: 272 $: revision of last commit
24 // $Author:: jnystrand $: author of last commit
25 // $Date:: 2016-07-08 17:47:36 +0100 #$: date of last commit
26 //
27 // Description:
28 // some simple streams for reporting plus some stream operators
29 // for common STL classes
30 //
31 //
33 
34 
35 #ifndef REPORTINGUTILS_H
36 #define REPORTINGUTILS_H
37 
38 
39 #include <iostream>
40 #include <iomanip>
41 #include <string>
42 #include <vector>
43 
44 
46 // macros for printing errors, warnings, and infos
47 
48 // cuts out block "className::methodName" from __PRETTY_FUNCTION__ output
49 inline
50 std::string
51 getClassMethod__(std::string prettyFunction)
52 {
53  size_t pos = prettyFunction.find("(");
54  if (pos == std::string::npos)
55  return prettyFunction; // something is not right
56  prettyFunction.erase(pos); // cut away signature
57  pos = prettyFunction.rfind(" ");
58  if (pos == std::string::npos)
59  return prettyFunction; // something is not right
60  prettyFunction.erase(0, pos + 1); // cut away return type
61  return prettyFunction;
62 }
63 
64 #define printErr std::cerr << "!!! " << __PRETTY_FUNCTION__ << " [" << __FILE__ << ":" << __LINE__ << "]: ERROR: " << std::flush
65 #define printWarn std::cerr << ">>> " << __PRETTY_FUNCTION__ << " [" << __FILE__ << ":" << __LINE__ << "]: "<<std::endl<<"Warning: " << std::flush
66 #define printInfo std::cout << ">>> " << getClassMethod__(__PRETTY_FUNCTION__) << "(): Info: " << std::flush
67 
68 
70 // functions to print version and compilation info
71 
72 #ifndef SVN_VERSION // SVN_VERSION set by Makefile
73 #define SVN_VERSION "undefined"
74 #endif
75 inline std::string svnVersion() { return SVN_VERSION; }
76 
77 inline
78 void
80 {
81  const std::string ver = svnVersion();
82  if (ver == "")
83  printInfo << "subversion repository revision is unknown." << std::endl;
84  else
85  printInfo << "subversion repository revision is '" << ver << "'" << std::endl;
86 }
87 
88 
89 #ifndef CMAKE_SOURCE_DIR // CMAKE_SOURCE_DIR set by Makefile
90 #define CMAKE_SOURCE_DIR "undefined"
91 #endif
92 inline std::string compileDir() { return CMAKE_SOURCE_DIR; }
93 
94 inline
95 void
97 {
98  const std::string date = __DATE__;
99  const std::string time = __TIME__;
100  const std::string ver = __VERSION__;
101  const std::string dir = compileDir();
102  printInfo << "this executable was compiled in ";
103  if (dir != "")
104  std::cout << "'" << dir << "'";
105  else
106  std::cout << "unknown directory";
107  std::cout << " on " << date << " " << time << " by compiler " << ver << std::endl;
108 }
109 
110 
112 // simple stream operators for some STL classes
113 
114 template<typename T1, typename T2>
115 inline
116 std::ostream&
117 operator << (std::ostream& out,
118  const std::pair<T1, T2>& pair)
119 {
120  return out << "(" << pair.first << ", " << pair.second << ")";
121 }
122 
123 
124 template<typename T>
125 inline
126 std::ostream&
127 operator << (std::ostream& out,
128  const std::vector<T>& vec)
129 {
130  out << "{";
131  for (unsigned int i = 0; i < (vec.size() - 1); ++i)
132  out << "[" << i << "] = " << vec[i] << ", ";
133  return out << "[" << vec.size() - 1 << "] = " << vec[vec.size() - 1] << "}";
134 }
135 
136 
138 // indicates progess by printing relative or absolute progress in regular intervals
139 inline
140 std::ostream&
141 progressIndicator(const unsigned int currentPos,
142  const unsigned int nmbTotal,
143  const bool absolute = false,
144  const unsigned int fieldWidth = 3,
145  const unsigned int nmbSteps = 10,
146  std::ostream& out = std::cout)
147 {
148  const double step = nmbTotal / (double)nmbSteps;
149  if ((int)(currentPos / step) - (int)((currentPos - 1) / step) != 0) {
150  if (absolute)
151  out << " " << std::setw(fieldWidth) << currentPos << " of " << nmbTotal << std::endl;
152  else
153  out << " " << std::setw(fieldWidth) << (int)(currentPos / step) * nmbSteps << " %" << std::endl;
154  }
155  return out;
156 }
157 
158 
159 // converts bool to "true"/"false" string
160 inline
161 std::string trueFalse(const bool val)
162 {
163  if (val)
164  return "true";
165  else
166  return "false";
167 }
168 
169 // converts bool to "yes"/"no" string
170 inline
171 std::string yesNo(const bool val)
172 {
173  if (val)
174  return "yes";
175  else
176  return "no";
177 }
178 
179 // converts bool to "on"/"off" string
180 inline
181 std::string onOff(const bool val)
182 {
183  if (val)
184  return "on";
185  else
186  return "off";
187 }
188 
189 // converts bool to "enabled"/"disabled" string
190 inline
191 std::string enDisabled(const bool val)
192 {
193  if (val)
194  return "enabled";
195  else
196  return "disabled";
197 }
198 
199 
200 #endif // REPORTINGUTILS_H