Grid 0.7.0
Log.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/Log.h
6
7 Copyright (C) 2015
8
9 Author: Antonin Portelli <antonin.portelli@me.com>
10 Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
11 Author: Peter Boyle <paboyle@ph.ed.ac.uk>
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License along
24 with this program; if not, write to the Free Software Foundation, Inc.,
25 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26
27 See the full license in the file "LICENSE" in the top level distribution directory
28*************************************************************************************/
29/* END LEGAL */
30
31#include <map>
32
33#ifndef GRID_LOG_H
34#define GRID_LOG_H
35
36#ifdef HAVE_EXECINFO_H
37#include <execinfo.h>
38#endif
39
41
43// Dress the output; use std::chrono for time stamping via the StopWatch class
45
46class Colours{
47protected:
49public:
50 std::map<std::string, std::string> colour;
51
52 Colours(bool activate=false){
53 Active(activate);
54 };
55
56 void Active(bool activate){
57 is_active=activate;
58 if (is_active){
59 colour["BLACK"] ="\033[30m";
60 colour["RED"] ="\033[31m";
61 colour["GREEN"] ="\033[32m";
62 colour["YELLOW"] ="\033[33m";
63 colour["BLUE"] ="\033[34m";
64 colour["PURPLE"] ="\033[35m";
65 colour["CYAN"] ="\033[36m";
66 colour["WHITE"] ="\033[37m";
67 colour["NORMAL"] ="\033[0;39m";
68 } else {
69 colour["BLACK"] ="";
70 colour["RED"] ="";
71 colour["GREEN"] ="";
72 colour["YELLOW"]="";
73 colour["BLUE"] ="";
74 colour["PURPLE"]="";
75 colour["CYAN"] ="";
76 colour["WHITE"] ="";
77 colour["NORMAL"]="";
78 }
79 };
80};
81
82
83class Logger {
84protected:
86 int active;
88 int topWidth{-1}, chanWidth{-1};
89 static int timestamp;
90 std::string name, topName;
91 std::string COLOUR;
92
93public:
97 static std::ostream devnull;
98
99 std::string background() {return Painter.colour["NORMAL"];}
100 std::string evidence() {return Painter.colour["YELLOW"];}
101 std::string colour() {return Painter.colour[COLOUR];}
102
103 Logger(std::string topNm, int on, std::string nm, Colours& col_class, std::string col) : active(on),
104 name(nm),
105 topName(topNm),
106 Painter(col_class),
107 timing_mode(0),
108 COLOUR(col)
109 {
111 };
112
113 void Active(int on) {active = on;};
114 int isActive(void) {return active;};
115 static void Timestamp(int on) {timestamp = on;};
116 void Reset(void) {
117 StopWatch->Reset();
118 StopWatch->Start();
119 }
120 void TimingMode(int on) {
121 timing_mode = on;
122 if(on) {
124 Reset();
125 }
126 }
127 void setTopWidth(const int w) {topWidth = w;}
128 void setChanWidth(const int w) {chanWidth = w;}
129
130 friend std::ostream& operator<< (std::ostream& stream, Logger& log){
131
132 if ( log.active ) {
133 std::ios_base::fmtflags f(stream.flags());
134
135 stream << log.background()<< std::left;
136 if (log.topWidth > 0)
137 {
138 stream << std::setw(log.topWidth);
139 }
140 stream << log.topName << log.background()<< " : ";
141 // stream << log.colour() << std::left;
142 stream << std::left;
143 if (log.chanWidth > 0)
144 {
145 stream << std::setw(log.chanWidth);
146 }
147 stream << log.name << log.background() << " : ";
148 if ( log.timestamp ) {
149 log.StopWatch->Stop();
150 GridTime now = log.StopWatch->Elapsed();
151
152 if ( log.timing_mode==1 ) log.StopWatch->Reset();
153 log.StopWatch->Start();
154 stream << log.evidence()
155 << now << log.background() << " : " ;
156 }
157 // stream << log.colour();
158 stream << std::right;
159 stream.flags(f);
160 return stream;
161 } else {
162 return devnull;
163 }
164 }
165
166};
167
168class GridLogger: public Logger {
169public:
170 GridLogger(int on, std::string nm, Colours&col_class, std::string col_key = "NORMAL"):
171 Logger("Grid", on, nm, col_class, col_key){};
172};
173
174void GridLogConfigure(std::vector<std::string> &logstreams);
175
176extern GridLogger GridLogMG;
191
192std::string demangle(const char* name) ;
193
194template<typename... Args>
195inline std::string sjoin(Args&&... args) noexcept {
196 std::ostringstream msg;
197 (msg << ... << args);
198 return msg.str();
199}
200
202template <typename... Args>
203inline void Grid_log(Args&&... args) {
204 std::string msg = sjoin(std::forward<Args>(args)...);
205 std::cout << GridLogMessage << msg << std::endl;
206}
207
209template <typename... Args>
210inline void Grid_warn(Args&&... args) {
211 std::string msg = sjoin(std::forward<Args>(args)...);
212 std::cout << "\033[33m" << GridLogWarning << msg << "\033[0m" << std::endl;
213}
214
216template <typename... Args>
217inline void Grid_error(Args&&... args) {
218 std::string msg = sjoin(std::forward<Args>(args)...);
219 std::cout << "\033[31m" << GridLogError << msg << "\033[0m" << std::endl;
220}
221
223template <typename... Args>
224inline void Grid_pass(Args&&... args) {
225 std::string msg = sjoin(std::forward<Args>(args)...);
226 std::cout << "\033[32m" << GridLogMessage << msg << "\033[0m" << std::endl;
227}
228
229#define _NBACKTRACE (256)
231
232#define BACKTRACEFILE() { \
233 char string[20]; \
234 std::sprintf(string,"backtrace.%d",CartesianCommunicator::RankWorld()); \
235 std::FILE * fp = std::fopen(string,"w"); \
236 BACKTRACEFP(fp) \
237 std::fclose(fp); \
238 }
239
240
241#ifdef HAVE_EXECINFO_H
242#define BACKTRACEFP(fp) { \
243 int symbols = backtrace (Grid_backtrace_buffer,_NBACKTRACE); \
244 char **strings = backtrace_symbols(Grid_backtrace_buffer,symbols); \
245 for (int i = 0; i < symbols; i++){ \
246 std::fprintf (fp,"BackTrace Strings: %d %s\n",i, demangle(strings[i]).c_str()); std::fflush(fp); \
247 } \
248 }
249#else
250#define BACKTRACEFP(fp) { \
251 std::fprintf (fp,"BT %d %lx\n",0, __builtin_return_address(0)); std::fflush(fp); \
252 std::fprintf (fp,"BT %d %lx\n",1, __builtin_return_address(1)); std::fflush(fp); \
253 std::fprintf (fp,"BT %d %lx\n",2, __builtin_return_address(2)); std::fflush(fp); \
254 std::fprintf (fp,"BT %d %lx\n",3, __builtin_return_address(3)); std::fflush(fp); \
255 }
256#endif
257
258#define BACKTRACE() BACKTRACEFP(stdout)
259
261
262#endif
accelerator_inline Grid_simd< S, V > log(const Grid_simd< S, V > &r)
GridLogger GridLogIterative(1, "Iterative", GridLogColours, "BLUE")
GridLogger GridLogError(1, "Error", GridLogColours, "RED")
GridLogger GridLogIRL(1, "IRL", GridLogColours, "NORMAL")
GridLogger GridLogPerformance(1, "Performance", GridLogColours, "GREEN")
GridLogger GridLogDebug(1, "Debug", GridLogColours, "PURPLE")
GridLogger GridLogMG(1, "MG", GridLogColours, "NORMAL")
GridLogger GridLogIntegrator(1, "Integrator", GridLogColours, "BLUE")
GridLogger GridLogHMC(1, "HMC", GridLogColours, "BLUE")
GridLogger GridLogMemory(1, "Memory", GridLogColours, "NORMAL")
GridLogger GridLogSolver(1, "Solver", GridLogColours, "NORMAL")
GridLogger GridLogTracing(1, "Tracing", GridLogColours, "NORMAL")
Colours GridLogColours(0)
GridLogger GridLogDslash(1, "Dslash", GridLogColours, "BLUE")
GridLogger GridLogMessage(1, "Message", GridLogColours, "NORMAL")
GridLogger GridLogWarning(1, "Warning", GridLogColours, "YELLOW")
std::string sjoin(Args &&... args) noexcept
Definition Log.h:195
void GridLogConfigure(std::vector< std::string > &logstreams)
Definition Log.cc:77
void Grid_pass(Args &&... args)
make pass messages work like python print
Definition Log.h:224
void * Grid_backtrace_buffer[_NBACKTRACE]
Definition Init.cc:644
std::string demangle(const char *name)
Definition Log.cc:40
void Grid_error(Args &&... args)
make error messages work like python print
Definition Log.h:217
void Grid_warn(Args &&... args)
make warning messages work like python print
Definition Log.h:210
#define _NBACKTRACE
Definition Log.h:229
void Grid_log(Args &&... args)
make log messages work like python print
Definition Log.h:203
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
std::chrono::microseconds GridTime
Definition Timer.h:45
Definition Log.h:46
void Active(bool activate)
Definition Log.h:56
std::map< std::string, std::string > colour
Definition Log.h:50
Colours(bool activate=false)
Definition Log.h:52
bool is_active
Definition Log.h:48
GridLogger(int on, std::string nm, Colours &col_class, std::string col_key="NORMAL")
Definition Log.h:170
int chanWidth
Definition Log.h:88
static std::ostream devnull
Definition Log.h:97
std::string colour()
Definition Log.h:101
std::string background()
Definition Log.h:99
void setChanWidth(const int w)
Definition Log.h:128
GridStopWatch * StopWatch
Definition Log.h:96
Colours & Painter
Definition Log.h:85
std::string COLOUR
Definition Log.h:91
void Reset(void)
Definition Log.h:116
friend std::ostream & operator<<(std::ostream &stream, Logger &log)
Definition Log.h:130
int timing_mode
Definition Log.h:87
void setTopWidth(const int w)
Definition Log.h:127
Logger(std::string topNm, int on, std::string nm, Colours &col_class, std::string col)
Definition Log.h:103
std::string evidence()
Definition Log.h:100
GridStopWatch LocalStopWatch
Definition Log.h:95
static GridStopWatch GlobalStopWatch
Definition Log.h:94
static void Timestamp(int on)
Definition Log.h:115
int topWidth
Definition Log.h:88
static int timestamp
Definition Log.h:89
int isActive(void)
Definition Log.h:114
void Active(int on)
Definition Log.h:113
int active
Definition Log.h:86
std::string topName
Definition Log.h:90
void TimingMode(int on)
Definition Log.h:120
std::string name
Definition Log.h:90