GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ADFLogMessage.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 #include "ADFLogMessage.h"
24 #include "ADFLogCollector.h"
25 
26 // anonymous namespace to protect them so that they are defined only in this file
27 namespace {
28  const char ci = ' ';
29  const char gkIndent[] = {ci,ci,ci,ci,ci,ci,ci,ci,ci,ci};
30 // const char gkIndent[] = {'-','-','-','-',' ',' ',' ',' ',' ',' '};
31 
32  const char gLevel[] = {'E','w','i','o'};
33 }
34 
35 using namespace ADF;
36 
37 short LogMessage::gkMaxDepth = 5;
38 short LogMessage::gkAutoClear = 7;
39 
40 short LogMessage::gDebug = 0;
41 
42 LogMessage::LogMessage(const char *pname, unsigned int pid) :
43  fProcessName(pname),
44  fProcessMethod(""),
45  fProcessMethodTicket(""),
46  fProcessID(pid),
47  fProcessPtr(0x0),
48  fDebug(-1),
49  fTicketNumber(0u),
50  fLevel(kInfo),
51  fLowestLevel(kInfo),
52  fVerbosity(0u),
53  fIndentation(0u),
54  fRealIndentation(0u),
55  fIsANewLine(true),
56  fHeader(),
57  fCoreMsg(),
58  fFooter()
59 {
60  SetProcessPtr(this);
61 }
62 
65 {
66  ;
67 }
68 
69 void LogMessage::SetLogParameters(short global_debug_level, short max_indent, short max_before_clear)
70 {
71  if ( global_debug_level < 0 )
72  gDebug = 0;
73  else
74  gDebug = global_debug_level;
75 
76  if ( max_indent < 1 )
77  gkMaxDepth = 1;
78  else
79  gkMaxDepth = max_indent;
80 
81  if ( max_before_clear <= gkMaxDepth )
82  gkAutoClear = gkMaxDepth + 2;
83  else
84  gkAutoClear = max_before_clear;
85 }
86 
87 void LogMessage::SetDebug(short global_debug_level)
88 {
89  if ( global_debug_level < 0 )
90  gDebug = 0;
91  else
92  gDebug = global_debug_level;
93 }
94 
96 {
97  // still ok to indent
98  if ( fIndentation < gkMaxDepth )
99  fIndentation++;
100 
101  // AuoClear called, probably a SetProcessMethod()/dolog not called properly
102  if ( fRealIndentation > gkAutoClear ) {
103  std::string tmp = fProcessMethod; fProcessMethod = fProcessMethodTicket;
104  SetLevel(kWarning, 1);
105  (*this) << "Automatic clear called since max value for depth calls "
106  << gkAutoClear << " reached. Probably a missing SetProcessMethod()/dolog bracket " << nline;
107  fIndentation = 1;
108  Send();
109  fProcessMethod = tmp;
110  DoClear();
111  }
112  else fRealIndentation++;
113 }
114 
116 {
117  fCoreMsg.str("");
118  fDebug = -1;
119  fLevel = kInfo;
120  fLowestLevel = kInfo;
121  fVerbosity = 0;
122  fIndentation = fRealIndentation = 0; fIsANewLine = true;
123 }
124 
126 {
127  // a new ticket is open, force sending the message
128  if ( fIndentation ) {
129  fIndentation = 1;
130  Send();
131  }
132  else DoClear();
133 }
134 
135 void LogMessage::SetLevel(ELevel lev, unsigned short verbosity)
136 {
137  if ( lev == fLevel && verbosity == fVerbosity )
138  return;
139 
140  fLevel = lev; fVerbosity = verbosity;
141 
142  if ( fLevel < fLowestLevel )
143  fLowestLevel = fLevel;
144 
145  // modification of the internal state on a single line
146  if ( !fIsANewLine )
147  (*this) << nline;
148 
149  (*this) << "|" << gLevel[fLevel] << "," << fVerbosity << "|" << nline;
150 }
151 
153 {
154  // keep track of the method that has started the ticket.
155  fProcessMethodTicket = fProcessMethod;
156 
157  fHeader.str("");
158  fHeader << "[+[#" << fTicketNumber << "] |" << gLevel[fLevel] << "," << fVerbosity << "| "
159  << GetProcessName() << "::" << GetProcessMethod() << " : " << fProcessPtr << " ]" << std::endl ;
160 
161  // set footer
162  fFooter.str("");
163  fFooter << "[-[#" << fTicketNumber << "] "
164  << GetProcessName() << "::" << GetProcessMethod() << " : " << fProcessPtr << " ]" << std::endl ;
165 
166  // re-start indentation
167  fIndentation = fRealIndentation = 0;
168  Indent();
169 }
170 
171 void LogMessage::SetProcessMethod(const char *method)
172 {
173  if ( fIndentation == 0 ) {
174  fProcessMethod = method;
175  NewTicket();
176  }
177  else {
178  fProcessMethod = method;
179  if ( !fIsANewLine )
180  (*this) << nline;
181  (*this) << "[" << " -> ::" << fProcessMethod << " ]" << nline;
182  Indent();
183  }
184 }
185 
187 {
188  // nothing to be sent ! ... maybe an idea to have a flag to control this
189  //if ( fCoreMsg.tellp() < 0 )
190  // return;
191  fDebug = -1;
192 
193  if ( fIndentation == 0 )
194  return;
195 
196  if ( fIndentation == 1 ) {
197 
199  DoClear();
200 
201  fTicketNumber++;
202  }
203  else fIndentation--;
204 }
205 
206 void LogMessage::Indent(std::ostream &o)
207 {
208  if ( fIndentation )
209  o.write(gkIndent,fIndentation);
210 }
211 
212 std::ostream &ADF::hline(std::ostream &o)
213 {
214  o << "--------------------------------------------------------------------------------\n";
215  return o;
216 }
217 
219 {
221  return log;
222 }
223 
225 {
227  return log;
228 }
229 
231 {
233  return log;
234 }
235 
237 {
238  log.fDebug = 1;
239  return log;
240 }
241 
243 {
244  log.Send();
245  return log;
246 }
247 
249 {
250  log.ClearMessage();
251  return log;
252 }
253 
255 {
256  log.fDebug = -1;
257 
258  if ( log.fIsANewLine )
259  return log;
260 
261  log.fCoreMsg << "\n"; log.fIsANewLine = true;
262  return log;
263 }
264 
266 {
267  return nline(log);
268 }
269 
270 
271 
272 
virtual void SetLevel(ELevel lev, unsigned short verbosity=0u)
To get the current level of the message.
static LogCollector * theCollector()
return the current logcollector
LogMessage & info(LogMessage &)
manipulator to modify the LogMessage
static void SetDebug(short global_debug_level=0)
virtual ~LogMessage()
should purge the message
LogMessage & clear(LogMessage &)
others
LogMessage & error(LogMessage &)
std::ostream & hline(std::ostream &)
LogMessage(const char *pname, unsigned int pid=0u)
virtual std::string & GetProcessMethod()
To get the current method.
LogMessage & debug(LogMessage &)
virtual void SetProcessMethod(const char *)
To set the current method.
virtual void DoCollect(LogMessage &)
Default, collect nothing !
LogMessage & nline(LogMessage &)
ELevel
message level
Definition: ADFLogMessage.h:95
virtual void Indent()
to format the output string ... increase indent level
virtual void ClearMessage()
To clear the current message.
Base class for a Log message.
Definition: ADFLogMessage.h:80
static void SetLogParameters(short global_debug_level=0, short max_indent=5, short max_before_clear=7)
to modify the global parameters
header file for ADFLogMessage.cpp
ADF::LogMessage & endl(ADF::LogMessage &log)
virtual unsigned short GetVerboseLevel() const
To get the current level of the message.
LogMessage & dolog(LogMessage &)
virtual std::string & GetProcessName()
To get the Process name.
virtual void Send()
To send the message to the log collector.
virtual void SetProcessPtr(void *address)
To set the Process address.
LogMessage & endl(LogMessage &)
friend LogMessage & nline(LogMessage &)
LogMessage & warning(LogMessage &)
virtual void DoClear()
really clear the message