GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GwLogMessage.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef Gw_LogMessage
24 #include "GwLogMessage.h"
25 #endif
26 
27 #ifndef Gw_LogCollector
28 #include "GwLogCollector.h"
29 #endif
30 
31 // anonymous namespace to protect them so that they are defined only in this file
32 namespace {
33 
34  const char ci = ' ';
35  const char gkIndent[] = {ci,ci,ci,ci,ci,ci,ci,ci,ci,ci};
36 // const char gkIndent[] = {'-','-','-','-',' ',' ',' ',' ',' ',' '};
37 
38  const char gLevel[] = {'E','w','i','o'};
39 }
40 
41 using namespace Gw;
42 
43 short LogMessage::gkMaxDepth = 5;
44 short LogMessage::gkAutoClear = 7;
45 
46 short LogMessage::gDebug = 0;
47 
48 LogMessage::LogMessage(const char *pname, unsigned int pid) :
49  fProcessName(pname),
50  fProcessMethod(""),
51  fProcessMethodTicket(""),
52  fProcessID(pid),
53  fProcessPtr(0x0),
54  fDebug(-1),
55  fTicketNumber(0u),
56  fLevel(kInfo),
57  fLowestLevel(kInfo),
58  fVerbosity(0u),
59  fIndentation(0u),
60  fRealIndentation(0u),
61  fIsANewLine(true),
62  fHeader(),
63  fCoreMsg(),
64  fFooter()
65 {
66  SetProcessPtr(this);
67 }
68 
71 {
72  ;
73 }
74 
75 void LogMessage::SetLogParameters(short global_debug_level, short max_indent, short max_before_clear)
76 {
77  if ( global_debug_level < 0 )
78  gDebug = 0;
79  else
80  gDebug = global_debug_level;
81 
82  if ( max_indent < 1 )
83  gkMaxDepth = 1;
84  else
85  gkMaxDepth = max_indent;
86 
87  if ( max_before_clear <= gkMaxDepth )
88  gkAutoClear = gkMaxDepth + 2;
89  else
90  gkAutoClear = max_before_clear;
91 }
92 
93 void LogMessage::SetDebug(short global_debug_level)
94 {
95  if ( global_debug_level < 0 )
96  gDebug = 0;
97  else
98  gDebug = global_debug_level;
99 }
100 
102 {
103  // still ok to indent
104  if ( fIndentation < gkMaxDepth )
105  fIndentation++;
106 
107  // AuoClear called, probably a SetProcessMethod()/dolog not called properly
108  if ( fRealIndentation > gkAutoClear ) {
109  std::string tmp = fProcessMethod; fProcessMethod = fProcessMethodTicket;
110  SetLevel(kWarning, 1);
111  (*this) << "Automatic clear called since max value for depth calls "
112  << gkAutoClear << " reached. Probably a missing SetProcessMethod()/dolog bracket " << nline;
113  fIndentation = 1;
114  Send();
115  fProcessMethod = tmp;
116  DoClear();
117  }
118  else fRealIndentation++;
119 }
120 
122 {
123  fCoreMsg.str("");
124  fDebug = -1;
125  fLevel = kInfo;
126  fLowestLevel = kInfo;
127  fVerbosity = 0;
128  fIndentation = fRealIndentation = 0; fIsANewLine = true;
129 }
130 
132 {
133  // a new ticket is open, force sending the message
134  if ( fIndentation ) {
135  fIndentation = 1;
136  Send();
137  }
138  else DoClear();
139 }
140 
141 void LogMessage::SetLevel(ELevel lev, unsigned short verbosity)
142 {
143  if ( lev == fLevel && verbosity == fVerbosity )
144  return;
145 
146  fLevel = lev; fVerbosity = verbosity;
147 
148  if ( fLevel < fLowestLevel )
149  fLowestLevel = fLevel;
150 
151  // modification of the internal state on a single line
152  if ( !fIsANewLine )
153  (*this) << nline;
154 
155  (*this) << "|" << gLevel[fLevel] << "," << fVerbosity << "|" << nline;
156 }
157 
159 {
160  // keep track of the method that has started the ticket.
161  fProcessMethodTicket = fProcessMethod;
162 
163  fHeader.str("");
164  fHeader << "[+[#" << fTicketNumber << "] |" << gLevel[fLevel] << "," << fVerbosity << "| "
165  << GetProcessName() << "::" << GetProcessMethod() << " : " << fProcessPtr << " ]" << std::endl ;
166 
167  // set footer
168  fFooter.str("");
169  fFooter << "[-[#" << fTicketNumber << "] "
170  << GetProcessName() << "::" << GetProcessMethod() << " : " << fProcessPtr << " ]" << std::endl ;
171 
172  // re-start indentation
173  fIndentation = fRealIndentation = 0;
174  Indent();
175 }
176 
177 void LogMessage::SetProcessMethod(const char *method)
178 {
179  if ( fIndentation == 0 ) {
180  fProcessMethod = method;
181  NewTicket();
182  }
183  else {
184  fProcessMethod = method;
185  if ( !fIsANewLine )
186  (*this) << nline;
187  (*this) << "[" << " -> ::" << fProcessMethod << " ]" << nline;
188  Indent();
189  }
190 }
191 
193 {
194  // nothing to be sent ! ... maybe an idea to have a flag to control this
195  //if ( fCoreMsg.tellp() < 0 )
196  // return;
197  fDebug = -1;
198 
199  if ( fIndentation == 0 )
200  return;
201 
202  if ( fIndentation == 1 ) {
203 
205  DoClear();
206 
207  fTicketNumber++;
208  }
209  else fIndentation--;
210 }
211 
212 void LogMessage::Indent(std::ostream &o)
213 {
214  if ( fIndentation )
215  o.write(gkIndent,fIndentation);
216 }
217 
218 // manipulator
219 
220 std::ostream &Gw::hline(std::ostream &o)
221 {
222  o << "--------------------------------------------------------------------------------\n";
223  return o;
224 }
225 
227 {
229  return log;
230 }
231 
233 {
235  return log;
236 }
237 
239 {
241  return log;
242 }
243 
245 {
246  log.fDebug = 1;
247  return log;
248 }
249 
251 {
252  log.Send();
253  return log;
254 }
255 
257 {
258  log.ClearMessage();
259  return log;
260 }
261 
263 {
264  log.fDebug = -1;
265 
266  if ( log.fIsANewLine )
267  return log;
268 
269  log.fCoreMsg << "\n"; log.fIsANewLine = true;
270  return log;
271 }
272 
273 /*
274 Gw::LogMessage &Gw::endl(Gw::LogMessage &log)
275 {
276  return Gw::nline(log);
277 }
278  */
279 
280 
281 
282 
virtual unsigned short GetVerboseLevel() const
To get the current level of the message.
Definition: GwLogMessage.h:275
LogMessage & error(LogMessage &)
LogMessage & warning(LogMessage &)
std::ostream & hline(std::ostream &)
LogMessage & nline(LogMessage &)
Base class for a Log message.
Definition: GwLogMessage.h:94
static void SetLogParameters(short global_debug_level=0, short max_indent=5, short max_before_clear=7)
to modify the global parameters
static void SetDebug(short global_debug_level=0)
virtual ~LogMessage()
should purge the message
header file for GwLogMessage.cpp
virtual void ClearMessage()
To clear the current message.
LogMessage(const char *pname, unsigned int pid=0u)
LogMessage & info(LogMessage &)
manipulator to modify the LogMessage
virtual void SetProcessPtr(void *address)
To set the Process address.
Definition: GwLogMessage.h:220
virtual void Send()
To send the message to the log collector.
LogMessage & dolog(LogMessage &)
LogMessage & debug(LogMessage &)
virtual void Indent()
to format the output string ... increase indent level
LogMessage & clear(LogMessage &)
others
virtual std::string & GetProcessName()
To get the Process name.
Definition: GwLogMessage.h:224
static LogCollector * theCollector()
return the current logcollector
ADF::LogMessage & endl(ADF::LogMessage &log)
ELevel
message level
Definition: GwLogMessage.h:109
virtual void DoCollect(LogMessage &)
Default, collect nothing !
virtual void SetProcessMethod(const char *)
To set the current method.
virtual std::string & GetProcessMethod()
To get the current method.
Definition: GwLogMessage.h:227
virtual void DoClear()
really clear the message
virtual void SetLevel(ELevel lev, unsigned short verbosity=0u)
To get the current level of the message.