GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ADFCentralLog.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 <iostream>
24 
25 #include "ADFLogCollector.h"
26 #include "ADFCentralLog.h"
27 
28 using namespace ADF;
29 
30 CentralLogDestroyer CentralLog::gTheCentralLogDestroyer;
31 CentralLog *CentralLog::gTheCentralLog = 0x0;
32 
34 {
35  fCentralLog = s;
36 }
37 
39 {
40  if (fCentralLog)
41  delete fCentralLog;
42 
43  fCentralLog = 0x0;
44 }
45 
47 {
48  fCentralLog = log;
49 }
50 
51 CentralLog::CentralLog() :
52  fCurrentCollector(0x0),
53  fListOfCollectors()
54 {
55 #ifdef DEBUGSTATIC
56  std::clog << "[[static]] CentralLog::CentralLog() ]\n[...\n" ;
57  std::clog << " Default collectors are built " << std::endl;
58 #endif
59 
60  Add ( new LogCollector("Null",true) );
61  Add ( new OStreamCollector("cout",true) );
62 
63  // default is clog
64  ChangeCollector("cout");
65 
66 #ifdef DEBUGSTATIC
67  std::clog << "...]" << std::endl;
68 #endif
69 }
70 
71 CentralLog::~CentralLog()
72 {
73 #ifdef DEBUGSTATIC
74  std::clog << "[[static]] CentralLog::~CentralLog() ]\n[...\n" ;
75  std::clog << " Collectors owned by Central Log are deleted " << std::endl;
76 #endif
77 
78  fCurrentCollector = 0x0;
79 
80  // loop to check the name does not yet exist
81  std::list< LogCollector * >::iterator
82  listed_item;
83  for ( listed_item = fListOfCollectors.begin() ; listed_item != fListOfCollectors.end() ; listed_item++ ) {
84  if ( (*listed_item)->IsDeleteByCentralLog() ) {
85 #ifdef DEBUGSTATIC
86  std::clog << " --> " << (*listed_item)->GetName()
87  << " deleted from the central log ... ";
88 #endif
89  delete (*listed_item);
90 #ifdef DEBUGSTATIC
91  std::clog << " ... done " << std::endl;
92 #endif
93  }
94  }
95 #ifdef DEBUGSTATIC
96  std::clog << "...]" << std::endl;
97 #endif
98 
99 }
100 
102 {
103  if ( gTheCentralLog == 0x0 ) {
104  gTheCentralLog = new CentralLog();
105  gTheCentralLogDestroyer.SetCentralLog (gTheCentralLog) ;
106  }
107 
108  return gTheCentralLog;
109 }
110 
111 bool CentralLog::ChangeCollector(std::string whichlog)
112 {
113  bool ok = false;
114 
115  // loop to check the name does not yet exist
116  std::list< LogCollector * >::iterator
117  listed_item;
118  for ( listed_item = fListOfCollectors.begin() ; listed_item != fListOfCollectors.end() ; listed_item++ ) {
119  if ( (*listed_item)->GetName() == whichlog ) {
120  fCurrentCollector = (*listed_item);
121  ok = true;
122 
123  break;
124  }
125  }
126  if ( ok )
127  std::clog << "[[info]] CentralLog::ChangeCollector() ] [ "
128  << fCurrentCollector->GetName() << " is now the current collector ]" << std::endl;
129 
130  return ok;
131 }
132 
133 bool CentralLog::IsCollector(std::string whichlog)
134 {
135  bool ok = false;
136 
137  // loop to check the name does not yet exist
138  std::list< LogCollector * >::iterator
139  listed_item;
140  for ( listed_item = fListOfCollectors.begin() ; listed_item != fListOfCollectors.end() ; listed_item++ ) {
141  if ( (*listed_item)->GetName() == whichlog ) {
142  fCurrentCollector = (*listed_item);
143  ok = true;
144 
145  break;
146  }
147  }
148 
149  return ok;
150 }
151 
153 {
154  return fCurrentCollector;
155 }
156 
158 {
159  LogCollector *toremove = 0x0;
160 
161 #ifdef DEBUGSTATIC
162  std::clog << "[[info]] CentralLog::Add() ] [ ";
163 #endif
164  if ( acollector == 0x0 ) {
165 #ifdef DEBUGSTATIC
166  std::clog << "cannot add null collectors to the central log ]" << std::endl;
167 #endif
168  return toremove;
169  }
170 
171  // loop to check the name does not yet exist
172  std::list< LogCollector * >::iterator
173  listed_item;
174  for ( listed_item = fListOfCollectors.begin() ; listed_item != fListOfCollectors.end() ; listed_item++ ) {
175  if ( (*listed_item)->GetName() == acollector->GetName() ) {
176  toremove = (*listed_item);
177  break;
178  }
179  }
180  // already a collector with the same name, remove it from the list
181  if ( toremove ) {
182  fListOfCollectors.erase(listed_item);
183 #ifdef DEBUGSTATIC
184  std::clog << toremove->GetName()
185  << " removed from the central log ]" << std::endl;
186 #endif
187 
188  }
189  fListOfCollectors.push_back( acollector );
190 #ifdef DEBUGSTATIC
191  std::clog << acollector->GetName()
192  << " added to the central log ]" << std::endl;
193 #endif
194 
195  // return removed collector if any
196  return toremove;
197 }
198 
199 
200 
201 
CentralLog is a singleton that should be destroyed at exit time.
Definition: ADFCentralLog.h:59
virtual std::string & GetName()
Base class for a Log collector.
Base class for a Log collector.
void SetCentralLog(CentralLog *)
LogCollector * GetCurrentLogCollector() const
Return the current logcollector.
bool IsCollector(std::string whichlog)
Check if the given collector already exists.
ADF::LogMessage & endl(ADF::LogMessage &log)
To properly destroy the singleton.
Definition: ADFCentralLog.h:40
LogCollector * Add(LogCollector *)
Add a new LogCollector to the list of known collectors.
CentralLogDestroyer(CentralLog *s=0x0)
bool ChangeCollector(std::string whichlog=std::string("Null"))
to change the current collector
static CentralLog * theCentralLog()
CentralLog is a singleton.