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