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