Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JetScapeLogger.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JetScapeLogger.cc
1 /*******************************************************************************
2  * Copyright (c) The JETSCAPE Collaboration, 2018
3  *
4  * Modular, task-based framework for simulating all aspects of heavy-ion collisions
5  *
6  * For the list of contributors see AUTHORS.
7  *
8  * Report issues at https://github.com/JETSCAPE/JETSCAPE/issues
9  *
10  * or via email to bugs.jetscape@gmail.com
11  *
12  * Distributed under the GNU General Public License 3.0 (GPLv3 or later).
13  * See COPYING for details.
14  ******************************************************************************/
15 
16 #include <stddef.h>
17 #include <fstream>
18 
19 #include "JetScapeLogger.h"
20 
21 using namespace std;
22 
23 // For Linux systems (also Mac)
24 // only shows max usage throughtout the program
25 
26 #include <sys/time.h>
27 #include <sys/resource.h>
28 
30  struct rusage usage;
31  // NOTE: Reported in kB on BSD/Linux, bytes in Mac/Darwin
32  // Could try to explicitly catch __linux__ as well
33  float mbsize = 1024;
34 #ifdef __MACH__
35  mbsize = 1024 * 1024;
36 #endif
37 
38  if (0 == getrusage(RUSAGE_SELF, &usage))
39  return usage.ru_maxrss / mbsize;
40  else
41  return 0;
42 }
43 
44 /*
45 //For mac os x only ...
46 // shows current usage
47 #include <mach/mach.h>
48 #include <mach/task.h>
49 
50 int getMemoryUsage()
51 {
52  struct mach_task_basic_info info;
53  mach_msg_type_number_t size = MACH_TASK_BASIC_INFO_COUNT;
54  kern_return_t kerr = task_info(mach_task_self(),
55  MACH_TASK_BASIC_INFO,
56  (task_info_t)&info,
57  &size);
58  if( kerr == KERN_SUCCESS )
59  return info.resident_size/1024./1024.;
60  else {
61  cout<<"Error with task_info(): "<<mach_error_string(kerr)<<endl;
62  return -1;}
63 }
64  */
65 
66 // Just some definition of colors for colored terminal log output
67 // Resetting to default color in LogStreamer!
68 
69 #define BLACK "\033[30m" /* Black */
70 #define RED "\033[31m" /* Red */
71 #define GREEN "\033[32m" //\033[7;30m" bkg /* Green */
72 #define YELLOW "\033[33m" /* Yellow */
73 #define BLUE "\033[34m" /* Blue */
74 #define MAGENTA "\033[35m" /* Magenta */
75 #define CYAN "\033[36m" /* Cyan */
76 #define WHITE "\033[37m" /* White */
77 #define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
78 #define BOLDRED "\033[1m\033[31m" /* Bold Red */
79 #define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
80 #define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
81 #define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
82 #define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
83 #define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
84 #define BOLDWHITE "\033[1m\033[37m" /* Bold White */
85 
86 #define CLEAR "\033[2J" // clear screen escape code
87 
88 namespace Jetscape {
89 
90 std::ostringstream null;
91 
92 SafeOstream safe_cout(std::cout);
93 SafeOstream safe_cerr(std::cerr);
95 
96 JetScapeLogger *JetScapeLogger::m_pInstance = NULL;
97 
98 JetScapeLogger *JetScapeLogger::Instance() {
99  if (!m_pInstance)
100  m_pInstance = new JetScapeLogger();
101 
102  return m_pInstance;
103 }
104 
105 LogStreamer JetScapeLogger::Warn() {
106  string s = "[Warning] ";
107  //s << __PRETTY_FUNCTION__ <<":"<<__LINE__<<" ";
108  //return LogStreamer(std::cout<<s<<__PRETTY_FUNCTION__ <<":"<<__LINE__<<" ");
109  return LogStreamer(std::cout << BOLDRED << s);
110 }
111 
112 LogStreamerThread JetScapeLogger::DebugThread() {
113  if (debug) {
114  string s = "[Debug Thread] ";
115  //s << __PRETTY_FUNCTION__ <<":"<<__LINE__<<" ";
116  s += to_string(getMemoryUsage());
117  s += "MB ";
118  return (LogStreamerThread(safe_cout) << BLUE << s);
119  } else {
120  // check if it is not written in some system log files ...
121  //safe_null.setstate(std::ios_base::failbit);
123  }
124 }
125 
126 LogStreamer JetScapeLogger::Debug() {
127  if (debug) {
128  string s = "[Debug] ";
129  //s << __PRETTY_FUNCTION__ <<":"<<__LINE__<<" ";
130  s += to_string(getMemoryUsage());
131  s += "MB ";
132  return LogStreamer(std::cout << BLUE << s);
133  } else {
134  null.setstate(std::ios_base::failbit);
135  return LogStreamer(null);
136  }
137 }
138 
139 LogStreamer JetScapeLogger::Info() {
140  string s = "[Info] ";
141  // s << __PRETTY_FUNCTION__ <<":"<<__LINE__<<" ";
142  if (info) {
143  string s = "[Info] ";
144  // s << __PRETTY_FUNCTION__ <<":"<<__LINE__<<" ";
145  s += to_string(getMemoryUsage());
146  s += "MB ";
147  return LogStreamer(std::cout << s);
148  } else {
149  null.setstate(std::ios_base::failbit);
150  return LogStreamer(null);
151  }
152 }
153 
154 LogStreamer JetScapeLogger::InfoNice() {
155  string s = "[Info] ";
156  // s << __PRETTY_FUNCTION__ <<":"<<__LINE__<<" ";
157  //s += to_string(getMemoryUsage()); s+="MB ";
158  if (info) {
159  return LogStreamer(std::cout << s);
160  } else {
161  null.setstate(std::ios_base::failbit);
162  return LogStreamer(null);
163  }
164 }
165 
166 LogStreamer JetScapeLogger::Remark() {
167  if (remark) {
168  string s = "[REMARK] ";
169  return LogStreamer(std::cout << BOLDMAGENTA << s);
170  } else {
171  null.setstate(std::ios_base::failbit);
172  return LogStreamer(null);
173  }
174 }
175 
176 LogStreamer JetScapeLogger::Verbose(unsigned short m_vlevel) {
177  if (m_vlevel < vlevel) // or if (m_vlevel==vlevel)
178  {
179  string s = "[Verbose][";
180  s += std::to_string(m_vlevel);
181  s += "] ";
182  s += to_string(getMemoryUsage());
183  s += "MB ";
184  return LogStreamer(std::cout << GREEN << s);
185  } else {
186  null.setstate(std::ios_base::failbit);
187  return LogStreamer(null);
188  }
189 }
190 
191 LogStreamer JetScapeLogger::VerboseShower(unsigned short m_vlevel) {
192  if (m_vlevel < vlevel) // or if (m_vlevel==vlevel)
193  {
194  string s = "[Verbose][";
195  s += std::to_string(m_vlevel);
196  s += "] ";
197  s += to_string(getMemoryUsage());
198  s += "MB ";
199  return LogStreamer(std::cout << BOLDCYAN << s);
200  } else {
201  null.setstate(std::ios_base::failbit);
202  return LogStreamer(null);
203  }
204 }
205 
206 LogStreamer JetScapeLogger::VerboseParton(unsigned short m_vlevel, Parton &p) {
207  if (m_vlevel < vlevel) // or if (m_vlevel==vlevel)
208  {
209  string s = "[Verbose][";
210  s += std::to_string(m_vlevel);
211  s += "] Parton: ";
212  return LogStreamer(std::cout << GREEN << s << " " << p << endl);
213  } else {
214  null.setstate(std::ios_base::failbit);
215  return LogStreamer(null);
216  }
217 }
218 
219 LogStreamer JetScapeLogger::VerboseVertex(unsigned short m_vlevel, Vertex &v) {
220  if (m_vlevel < vlevel) // or if (m_vlevel==vlevel)
221  {
222  string s = "[Verbose][";
223  s += std::to_string(m_vlevel);
224  s += "] Vertex: ";
225  return LogStreamer(std::cout << GREEN << s << " " << v << endl);
226  } else {
227  null.setstate(std::ios_base::failbit);
228  return LogStreamer(null);
229  }
230 }
231 
232 } // end namespace Jetscape