StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
reportingUtils.h
1 //
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:: $: revision of last commit
24 // $Author: jeromel $: author of last commit
25 // $Date: 2014/06/25 15:01:39 $: 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: "
65 #define printWarn std::cerr << "??? " << __PRETTY_FUNCTION__ << " [" << __FILE__ << ":" << __LINE__ << "]: warning: "
66 #define printInfo std::cout << ">>> " << getClassMethod__(__PRETTY_FUNCTION__) << "(): info: "
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
79 printSvnVersion()
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
96 printCompilerInfo()
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 // various stuff
139 
140 // indicates progess by printing relative or absolute progress in regular intervals
141 inline
142 std::ostream&
143 progressIndicator(const unsigned int currentPos,
144  const unsigned int nmbTotal,
145  const bool absolute = false,
146  const unsigned int fieldWidth = 3,
147  const unsigned int nmbSteps = 10,
148  std::ostream& out = std::cout)
149 {
150  const double step = nmbTotal / (double)nmbSteps;
151  if ((int)(currentPos / step) - (int)((currentPos - 1) / step) != 0) {
152  if (absolute)
153  out << " " << std::setw(fieldWidth) << currentPos << " of " << nmbTotal << std::endl;
154  else
155  out << " " << std::setw(fieldWidth) << (int)(currentPos / step) * nmbSteps << " %" << std::endl;
156  }
157  return out;
158 }
159 
160 
161 // converts bool to "true"/"false" string
162 inline
163 std::string trueFalse(const bool val)
164 {
165  if (val)
166  return "true";
167  else
168  return "false";
169 }
170 
171 // converts bool to "yes"/"no" string
172 inline
173 std::string yesNo(const bool val)
174 {
175  if (val)
176  return "yes";
177  else
178  return "no";
179 }
180 
181 // converts bool to "on"/"off" string
182 inline
183 std::string onOff(const bool val)
184 {
185  if (val)
186  return "on";
187  else
188  return "off";
189 }
190 
191 // converts bool to "enabled"/"disabled" string
192 inline
193 std::string enDisabled(const bool val)
194 {
195  if (val)
196  return "enabled";
197  else
198  return "disabled";
199 }
200 
201 
202 #endif // REPORTINGUTILS_H