Cat
MsgSvc.cpp
Go to the documentation of this file.
1 // $Id: $
2 // Include files
3 #include <stdlib.h>
4 #include "Application.h"
5 #include "Options.h"
6 
7 // local
8 #include "MsgSvc.h"
9 
10 //-----------------------------------------------------------------------------
11 // Implementation file for class : MsgSvc
12 //
13 // 2006-10-26 :
14 //-----------------------------------------------------------------------------
15 
16 //=============================================================================
17 // Standard constructor, initializes variables
18 //=============================================================================
20  m_callSize (18),
21  m_lineLength (200){
22  colors.push_back(std::string("\033[94m"));
23  colors.push_back(std::string("\033[92m"));
24  colors.push_back(std::string("\033[93m"));
25  colors.push_back(std::string("\033[95m"));
26 // colors.push_back(std::string("\033[47m \033[7m \033[45m"));
27  colors.push_back(std::string("\033[33m"));
28  colors.push_back(std::string("\033[91m"));
29 // colors.push_back(std::string("\033[41m"));
30  colors.push_back(std::string("\033[0m"));
31 }
32 
33 //=============================================================================
34 // Destructor
35 //=============================================================================
37 }
38 
39 //=============================================================================
40 //
41 //=============================================================================
43  std::string msg,
44  std::string call ){
45  Options *opt = application() ->options() ;
46  const int OUTLEVEL = opt -> logOutputLevel();
47  if (level<OUTLEVEL) return;
48  log ( level, msg, call);
49 }
50 
51 //=============================================================================
52 //
53 //=============================================================================
54 std::string MsgSvc::logtime(){
55  time_t rawtime;
56  struct tm * timeinfo;
57 
58  time ( &rawtime );
59  timeinfo = localtime ( &rawtime );
60  char* timechar = asctime (timeinfo) ;
61  std::string timing = std::string(timechar,24) + " ";
62 
63  return timing;
64 }
65 
66 //=============================================================================
67 //
68 //=============================================================================
70  std::string msg,
71  std::string call){
72 
73  bool isFatal=false;
74 
75  //#ifndef WIN32
76  std::string output(colors[BLUE]);
77  output+=std::string(call,0,m_callSize);
78  //#else
79  // std::string output=std::string(call,0,m_callSize);
80  //#endif
81 
82 
83 
84  int clen=call.length();
85 
86  if (clen>0)
87  {
88  if (clen>m_callSize) output.replace(m_callSize+5-3,3,"...");
89  if (clen<m_callSize) output.insert (clen+5,m_callSize-clen,' ');
90  }
91 
92  std::string color;
93 
94  switch(level) {
95  case VERBOSE:
96 #ifndef WIN32
97  color=colors[BLUE];
98  output+=color;
99 #endif
100  output+=" VERBOSE ";
101  break;
102  case DEBUG:
103 #ifndef WIN32
104  color=colors[BLUE];
105  output+=color;
106 #endif
107  output+=" DEBUG ";
108  break;
109  case INFO:
110 #ifndef WIN32
111  color=colors[GREEN];
112  output+=color;
113 #endif
114  output+=" INFO ";
115  break;
116  case WARNING:
117 #ifndef WIN32
118  color=colors[YELLOW];
119  output+=color;
120 #endif
121  output+=" WARNING ";
122  break;
123  case ERR:
124 #ifndef WIN32
125  color=colors[MAGENTA];
126  output+=color;
127 #endif
128  output+=" ERROR ";
129  break;
130  case FATAL:
131 #ifndef WIN32
132  color=colors[RED];
133  output+=color;
134 #endif
135  output+=" FATAL ";
136  isFatal=true;
137  break;
138  default:
139 #ifndef WIN32
140  color = colors[CYAN];
141  output+=color;
142 #endif
143  output = call;
144  break;
145  }
146 #ifndef WIN32
147  output+=colors[WHITE];
148 #endif
149  output+=logtime();
150 #ifndef WIN32
151  output+=color;
152 #endif
153  output+=msg;
154 #ifndef WIN32
155  output+=colors[WHITE];
156 #endif
157  /*
158  int linereturn=output.length();
159 
160  while (linereturn>m_lineLength){
161  output.insert(m_lineLength,"\n");
162  output.insert(m_lineLength+1,9+m_callSize,' ');
163  linereturn-=m_lineLength;
164  }
165  */
166 
167  std::cout << output << std::endl;
168  (*application() -> options() -> stream ()) << output << std::endl; ;
169 
170  if (isFatal) {exit(0);}
171 }
172 //=============================================================================
int m_callSize
Definition: MsgSvc.h:41
MsgSvc()
Standard constructor.
Definition: MsgSvc.cpp:19
virtual ~MsgSvc()
Destructor.
Definition: MsgSvc.cpp:36
Application * application()
Definition: Tools.cpp:42
Options * options()
Definition: Application.h:86
std::vector< std::string > colors
Definition: MsgSvc.h:40
MsgLevel
Definition: MsgSvc.h:24
void log(MsgSvc::MsgLevel, std::string, std::string)
Definition: MsgSvc.cpp:69
std::string logtime()
Definition: MsgSvc.cpp:54
void msgSvc(MsgSvc::MsgLevel, std::string, std::string call=std::string(""))
Definition: MsgSvc.cpp:42