GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GwLogCollector.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_CentralLog
28 #include "GwCentralLog.h"
29 #endif
30 
31 #ifndef Gw_LogCollector
32 #include "GwLogCollector.h"
33 #endif
34 
35 #include <ctime>
36 
37 using namespace Gw;
38 
39 LogCollector::LogCollector(const char *name, bool isdelbycental) :
40  fName(name),
41  fMinLevel(LogMessage::kError),
42  fMaxLevel(LogMessage::kInfo),
43  fIsDeleteByCentralLog(isdelbycental)
44 {
45 
46 }
47 
49 {
50 
51 }
52 
54 {
56 }
57 
58 #include <fstream>
59 #include <ios>
60 OStreamCollector::OStreamCollector(const char *name, bool isdelbycental) :
61  LogCollector(name,isdelbycental),
62  fOutputStream(0x0)
63 {
64  std::string tmp = name;
65 
66  if ( tmp == "cout" ) {
67  fOutputStream = &std::cout;
68  }
69  if ( tmp == "cerr" ) {
70  fOutputStream = &std::cerr;
71  }
72  if ( tmp == "clog" ) {
73  fOutputStream = &std::clog;
74  }
75  if ( fOutputStream == 0x0 )
76  { fOutputStream = new std::ofstream(GetName().data(),std::ios_base::app) ; }
77 }
79 {
80  bool donot = fOutputStream == &std::cout || fOutputStream == &std::cerr || fOutputStream == &std::clog ;
81  if ( !donot )
82  if ( fOutputStream ) delete fOutputStream;
83 }
84 
86 {
87  if ( alogmsg.GetLevel() < fMinLevel || alogmsg.GetLevel() > fMaxLevel )
88  return;
89 
90  std::string message = alogmsg.Message();
91  if ( message.size() < 1 )
92  return;
93 
94  if ( fOutputStream ) {
95  // header of the message
96  (*fOutputStream) << alogmsg.Header();
97  // the message
98  (*fOutputStream) << message;
99  // end of the message
100  if ( message[message.size()-1] != '\n' )
101  (*fOutputStream) << std::endl;
102  (*fOutputStream) << alogmsg.Footer();
103 
104  // flush to be synchronized
105  fOutputStream->flush();
106  }
107 }
108 
109 
110 
111 
unsigned short fMaxLevel
virtual void DoCollect(LogMessage &)
write the message in a file
Base class for a Log message.
Definition: GwLogMessage.h:94
virtual std::string Message() const
To get the current message.
Definition: GwLogMessage.h:281
header file for GwLogMessage.cpp
virtual std::string & GetName()
OStreamCollector(const char *, bool isdelbycental=false)
unsigned short fMinLevel
to send to the log server only message having their level between min and max
Base class for a Log collector.
virtual std::string Footer() const
To get the Footer of the current message.
Definition: GwLogMessage.h:290
LogCollector * GetCurrentLogCollector() const
Return the current logcollector.
LogCollector(const char *, bool isdelbycental=false)
virtual ~LogCollector()
virtual ELevel GetLevel() const
To get the current level of the message.
Definition: GwLogMessage.h:266
static LogCollector * theCollector()
return the current logcollector
ADF::LogMessage & endl(ADF::LogMessage &log)
virtual std::string Header() const
To get the header of the current message.
Definition: GwLogMessage.h:287
Central log for all the Gammaware modules.
static CentralLog * theCentralLog()
CentralLog is a singleton.