MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
print_function.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: print_function.h
5  *
6  * Description: Some of basic print routines for c++
7  *
8  * Version: 1.0
9  * Created: 07/18/2013 07:34:06 PM
10  * Revision: none
11  * Compiler: gcc/g++
12  *
13  * Author: Dilawar Singh (), dilawars@ncbs.res.in
14  * Organization: NCBS Bangalore.
15  *
16  * =====================================================================================
17  */
18 
19 #ifndef print_function_INC
20 #define print_function_INC
21 
22 #ifndef DEBUG_LEVEL
23 #define DEBUG_LEVEL 0
24 #endif
25 
26 #include <iostream>
27 #include <sstream>
28 #include <fstream>
29 #include <vector>
30 #include <string>
31 #include <map>
32 #include <iomanip>
33 #include <ctime>
34 #include <algorithm>
35 
36 #define T_RESET "\033[0m"
37 #define T_BLACK "\033[30m" /* Black */
38 #define T_RED "\033[31m" /* Red */
39 #define T_GREEN "\033[32m" /* Green */
40 #define T_YELLOW "\033[33m" /* Yellow */
41 #define T_BLUE "\033[34m" /* Blue */
42 #define T_MAGENTA "\033[35m" /* Magenta */
43 #define T_CYAN "\033[36m" /* Cyan */
44 #define T_WHITE "\033[37m" /* White */
45 #define T_BOLDBLACK "\033[1m\033[30m" /* Bold Black */
46 #define T_BOLDRED "\033[1m\033[31m" /* Bold Red */
47 #define T_BOLDGREEN "\033[1m\033[32m" /* Bold Green */
48 #define T_BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
49 #define T_BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
50 #define T_BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
51 #define T_BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
52 #define T_BOLDWHITE "\033[1m\033[37m" /* Bold White */
53 
54 using namespace std;
55 
56 /* --------------------------------------------------------------------------*/
60 /* ----------------------------------------------------------------------------*/
61 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
62 #ifdef NDEBUG
63 #define MOOSE_DEBUG( a ) { \
64  stringstream ss; ss << a; \
65  cout << "DEBUG: " << __FILENAME__ << ":" << __LINE__ << "| " << ss.str(); \
66  }
67 #else
68 #define MOOSE_DEBUG( a ) {}
69 #endif
70 
71 namespace moose {
72 
78  };
79 
80  static string levels_[9] = {
81  "TRACE", "DEBUG", "INFO", "WARNING", "FIXME" , "ERROR", "FATAL", "FAILED"
82  };
83 
84  /*
85  * === FUNCTION ==============================================================
86  * Name: mapToString
87  * Description: GIven a map, return a string representation of it.
88  *
89  * If the second argument is true then print the value with key. But default it
90  * is true.
91  * ==============================================================================
92  */
93 
94  template<typename A, typename B>
95  string mapToString(const map<A, B>& m, bool value=true)
96  {
97  unsigned int width = 81;
98  unsigned int mapSize = m.size();
99  unsigned int size = 0;
100 
101  vector<string> row;
102 
103  /* Get the maximum size of any entry in map */
104  stringstream ss;
105  typename map<A, B>::const_iterator it;
106  for(it = m.begin(); it != m.end(); it++)
107  {
108  ss.str("");
109  ss << it->first;
110  if(value)
111  ss << ": " << it->second;
112  row.push_back(ss.str());
113  if(ss.str().size() > size)
114  size = ss.str().size()+1;
115  }
116 
117  unsigned int colums = width / size;
118  ss.str("");
119 
120  size_t i = 0;
121  for(unsigned int ii = 0; ii < row.size(); ii++)
122  {
123  if(i < colums)
124  {
125  ss << setw(size+1) << row[ii];
126  i++;
127  }
128  else
129  {
130  ss << endl;
131  i = 0;
132  }
133  }
134  return ss.str();
135  }
136 
137  inline string colored(string msg)
138  {
139  stringstream ss;
140  ss << T_RED << msg << T_RESET;
141  return ss.str();
142  }
143 
144  inline string colored(string msg, string colorName)
145  {
146  stringstream ss;
147  ss << colorName << msg << T_RESET;
148  return ss.str();
149  }
150 
151  // Not print it when built for release.
152  inline string debugPrint(string msg, string prefix = "DEBUG"
153  , string color=T_RESET, unsigned debugLevel = 0
154  )
155  {
156  stringstream ss; ss.str("");
157  if(debugLevel <= DEBUG_LEVEL)
158  {
159  ss << setw(debugLevel/2) << "[" << prefix << "] "
160  << color << msg << T_RESET;
161  }
162  return ss.str();
163  }
164 
165  /*-----------------------------------------------------------------------------
166  * This function __dump__ a message onto console. Fills appropriate colors as
167  * needed.
168  *-----------------------------------------------------------------------------*/
169 
170  inline void __dump__(string msg, serverity_level_ type = debug, bool autoFormat = true)
171  {
172  stringstream ss;
173  ss << "[" << levels_[type] << "] ";
174  bool set = false;
175  bool reset = true;
176  string color = T_GREEN;
177  if(type == warning || type == fixme )
178  color = T_YELLOW;
179  else if(type == debug )
180  color = T_CYAN;
181  else if(type == error || type == failed )
182  color = T_RED;
183  else if(type == info )
184  color = T_MAGENTA;
185 
186  for(unsigned int i = 0; i < msg.size(); ++i)
187  {
188  if('`' == msg[i])
189  {
190  if(!set and reset)
191  {
192  set = true;
193  reset = false;
194  ss << color;
195  }
196  else if(set && !reset)
197  {
198  reset = true;
199  set = false;
200  ss << T_RESET;
201  }
202  }
203  else if('\n' == msg[i])
204  ss << "\n | ";
205  else
206  ss << msg[i];
207  }
208 
209  /* Be safe than sorry */
210  if(!reset)
211  ss << T_RESET;
212  cout << ss.str() << endl;
213  cout.flush( );
214  }
215 
216  /*
217  * Wrapper function around __dump__
218  */
219  inline void showInfo( string msg )
220  {
222  }
223 
224  inline void showWarn( string msg )
225  {
227  }
228 
229  inline void showDebug( const string msg )
230  {
231 #ifdef DISABLE_DEBUG
232 
233 #else
235 #endif
236  }
237 
238  inline void showError( string msg )
239  {
241  }
242 
251 #ifdef NDEBUG
252 #define LOG(t, a ) ((void)0);
253 #else /* ----- not NDEBUG ----- */
254 #define LOG(t, a) { stringstream __ss__; __ss__ << a; moose::__dump__(__ss__.str(), t ); }
255 #endif /* ----- not NDEBUG ----- */
256 
257  /*-----------------------------------------------------------------------------
258  * Log to a file, and also to console.
259  *-----------------------------------------------------------------------------*/
260  inline bool isBackTick(char a)
261  {
262  if('`' == a)
263  return true;
264  return false;
265  }
266 
267  inline string formattedMsg(string& msg)
268  {
269  remove_if(msg.begin(), msg.end(), isBackTick);
270  return msg;
271  }
272 
281  inline void log(string msg, serverity_level_ type = debug
282  , bool redirectToConsole = true
283  , bool removeTicks = true
284  )
285  {
286 
287  if(redirectToConsole)
288  __dump__(msg, type, true);
289 
290  /* remove any backtick from the string. */
291  formattedMsg( msg );
292 
293  fstream logF;
294  logF.open( "__moose__.log", ios::app);
295  time_t rawtime; time(&rawtime);
296  struct tm* timeinfo;
297  timeinfo = localtime(&rawtime);
298 
299  logF << asctime(timeinfo) << ": " << msg;
300 
301  logF.close();
302  }
303 
304 }
305 #endif /* ----- #ifndef print_function_INC ----- */
uint32_t value
Definition: moosemodule.h:42
serverity_level_
Enumerate type for debug and log.
void log(string msg, serverity_level_ type=debug, bool redirectToConsole=true, bool removeTicks=true)
Log to console (and to a log-file)
static string levels_[9]
string debugPrint(string msg, string prefix="DEBUG", string color=T_RESET, unsigned debugLevel=0)
void showDebug(const string msg)
string mapToString(const map< A, B > &m, bool value=true)
void showError(mu::Parser::exception_type &e)
Definition: FuncTerm.cpp:73
string formattedMsg(string &msg)
string colored(string msg, string colorName)
void __dump__(string msg, serverity_level_ type=debug, bool autoFormat=true)
void showInfo(string msg)
void showWarn(string msg)
bool isBackTick(char a)