| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HistorySvc.cpp
Go to the documentation of this file.
1 #include "HistorySvc.h"
2
3 #include "GaudiKernel/HistoryObj.h"
4 #include "GaudiKernel/IVersHistoryObj.h"
5
6 #include "GaudiKernel/JobHistory.h"
7 #include "GaudiKernel/AlgorithmHistory.h"
8 #include "GaudiKernel/AlgToolHistory.h"
9 #include "GaudiKernel/DataHistory.h"
10 #include "GaudiKernel/ServiceHistory.h"
11
12 #include "GaudiKernel/ISvcLocator.h"
13
14 #include "GaudiKernel/System.h"
15 #include "GaudiKernel/Bootstrap.h"
16 #include "GaudiKernel/IAlgManager.h"
17 #include "GaudiKernel/IAlgorithm.h"
18 #include "GaudiKernel/Algorithm.h"
19 #include "GaudiKernel/IAlgTool.h"
20 #include "GaudiKernel/AlgTool.h"
21 #include "GaudiKernel/IService.h"
22 #include "GaudiKernel/IJobOptionsSvc.h"
23 #include "GaudiKernel/IAppMgrUI.h"
24 #include "GaudiKernel/IIncidentSvc.h"
25 #include "GaudiKernel/IToolSvc.h"
26 #include "GaudiKernel/ServiceHandle.h"
27
28 #include "GaudiKernel/IAlgContextSvc.h"
29
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <limits>
34
35 #define ON_DEBUG if (UNLIKELY(outputLevel() <= MSG::DEBUG))
36 #define ON_VERBOSE if (UNLIKELY(outputLevel() <= MSG::VERBOSE))
37
38 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
39 DECLARE_COMPONENT(HistorySvc)
40
41 using namespace std;
42
43 //
45 //
46
47 struct DHH {
48 CLID id;
49 std::string key;
50
51 DHH(const CLID& i, const std::string& k):id(i), key(k) {}
52
53 bool operator < ( DHH const &rhs ) const {
54 if (id != rhs.id) {
55 return (id < rhs.id);
56 } else {
57 return (key < rhs.key);
58 }
59 }
60 };
61
62 void fenv_dummy(char**) {}
63
64 HistorySvc::HistorySvc( const std::string& name, ISvcLocator* svc )
65 : base_class( name, svc ),
66 m_isInitialized(false),
67 m_dump(false),
68 p_algCtxSvc(0),
69 m_jobHistory(0),
70 m_outputFile(""),
71 m_incidentSvc(0),
72 m_log(msgSvc(), name ),
73 m_outputFileTypeXML(false)
74
75 {
76 declareProperty("Dump",m_dump);
77 declareProperty("Activate", m_activate=true);
78 declareProperty("OutputFile",m_outputFile);
79
80 // hack to bring in environ
81 vector<string> envtmp = System::getEnv();
82 envtmp.size(); // prevent icc remark #177: X declared but never referenced
83 }
84
85 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
86
87 HistorySvc::~HistorySvc() {
88 delete m_jobHistory;
89 }
90
91 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
92
93 StatusCode HistorySvc::reinitialize() {
94
95 clearState();
96
97 m_state = Gaudi::StateMachine::OFFLINE;
98
99 return initialize();
100
101 }
102
103 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
104
105 void HistorySvc::clearState() {
106 m_algs.clear();
107 std::map<const Algorithm*, AlgorithmHistory*>::iterator algitr;
108 for (algitr = m_algmap.begin(); algitr != m_algmap.end(); ++algitr) {
109 AlgorithmHistory* h = algitr->second;
110 (const_cast<Algorithm*> (algitr->first))->release();
111 delete h;
112 }
113 m_algmap.clear();
114
115 m_ialgtools.clear();
116 m_algtools.clear();
117 std::map<const AlgTool*, AlgToolHistory*>::iterator atitr;
118 for (atitr=m_algtoolmap.begin(); atitr != m_algtoolmap.end(); ++atitr) {
119 (const_cast<AlgTool*> (atitr->first))->release();
120 delete atitr->second;
121 }
122 m_algtoolmap.clear();
123
124 m_svcs.clear();
125 std::map<const IService*, ServiceHistory*>::iterator svitr;
126 for (svitr = m_svcmap.begin(); svitr != m_svcmap.end(); ++svitr) {
127 (const_cast<IService*> (svitr->first))->release();
128 delete svitr->second;
129 }
130 m_svcmap.clear();
131 }
132
133
134 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
135
136 StatusCode HistorySvc::initialize() {
137
138 StatusCode status = Service::initialize();
139 m_log.setLevel( m_outputLevel.value() );
140
141 if (status.isFailure()) {
142
143 ON_DEBUG
144 m_log << MSG::DEBUG << "Failed to initialize the base class (Service)"
145 << endmsg;
146 return status;
147 }
148
149 ON_DEBUG
150 m_log << MSG::DEBUG << "Initializing HistorySvc" << endmsg;
151
152 if (!m_activate) return StatusCode::SUCCESS;
153
154 static const bool CREATEIF(true);
155
156 if ( service("AlgContextSvc",p_algCtxSvc,CREATEIF).isFailure() ) {
157 m_log << MSG::ERROR << "unable to get the AlgContextSvc" << endmsg;
158 return StatusCode::FAILURE;
159 }
160
161 if (service("IncidentSvc", m_incidentSvc, CREATEIF).isFailure()) {
162 m_log << MSG::ERROR << "unable to get the IncidentSvc" << endmsg;
163 return StatusCode::FAILURE;
164 }
165
166 // create a weak dependency on the ToolSvc, so that it doesn't get deleted
167 // before we're done with it in finalize
168 m_toolSvc = serviceLocator()->service("ToolSvc");
169 if (! m_toolSvc) {
170 m_log << MSG::ERROR << "could not retrieve the ToolSvc handle !"
171 << endmsg;
172 return StatusCode::FAILURE;
173 }
174
175 // add listener to be triggered by first BeginEvent with low priority
176 // so it gets called first
177 const bool rethrow = false;
178 const bool oneShot = true; // make the listener called only once
179 m_incidentSvc->addListener(this,IncidentType::BeginEvent,
180 std::numeric_limits<long>::min(),rethrow,oneShot);
181
182 if (m_outputFile.find(".XML") != string::npos || m_outputFile.find(".xml") != string::npos) {
183 ON_DEBUG
184 m_log << MSG::DEBUG << "output format is XML" << endmsg;
185 m_outputFileTypeXML = true;
186 }
187
188 m_isInitialized = true;
189
190 return StatusCode::SUCCESS;
191
192 }
193 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
194
195 StatusCode HistorySvc::captureState() {
196
197 if (m_jobHistory == 0) {
198 m_jobHistory = new JobHistory;
199 IJobOptionsSvc *jo;
200 if (service("JobOptionsSvc",jo).isFailure()) {
201 m_log << MSG::ERROR
202 << "Could not get jobOptionsSvc - "
203 << "not adding properties to JobHistory" << endmsg;
204 } else {
205
206 bool foundAppMgr(false);
207
208 std::vector<std::string> clients = jo->getClients();
209 std::vector<std::string>::const_iterator it;
210 std::vector<const Property*>::const_iterator itr;
211 for (it=clients.begin(); it!=clients.end(); ++it) {
212 if (*it == "ApplicationMgr") {
213 foundAppMgr = true;
214 }
215 const std::vector<const Property*> *props = jo->getProperties(*it);
216 for (itr=props->begin(); itr != props->end(); ++itr) {
217 m_jobHistory->addProperty( *it, *itr );
218 }
219 }
220
221 if (!foundAppMgr) {
222 IProperty *ap;
223 if (service("ApplicationMgr",ap).isFailure()) {
224 m_log << MSG::ERROR << "could not get the ApplicationMgr" << endmsg;
225 } else {
226 std::vector<Property*>::const_iterator itr2;
227 const std::vector<Property*> props = ap->getProperties();
228 for (itr2=props.begin(); itr2 != props.end(); ++itr2) {
229 m_jobHistory->addProperty( "ApplicationMgr", *itr2 );
230 }
231 }
232 }
233
234 }
235 }
236
238
239 StatusCode sc;
240 IAlgManager* algMgr = 0;
241 sc = Gaudi::svcLocator()->queryInterface( IAlgManager::interfaceID(),
242 pp_cast<void>(&algMgr) );
243 if (sc.isFailure()) {
244 m_log << MSG::ERROR << "Could not get AlgManager" << endmsg;
245 return StatusCode::FAILURE;
246 } else {
247
248 std::list<IAlgorithm*> algs;
249 algs = algMgr->getAlgorithms();
250 std::list<IAlgorithm*>::const_iterator itr;
251 for (itr=algs.begin(); itr!=algs.end(); ++itr) {
252 Algorithm* alg = dynamic_cast<Algorithm*> (*itr);
253 if (alg == 0) {
254 m_log << MSG::WARNING << "Algorithm " << (*itr)->name()
255 << " does not inherit from Algorithm. Not registering it."
256 << endmsg;
257 } else {
258 registerAlg( *alg ).ignore();
259 }
260 }
261
262 m_log << MSG::INFO;
263 m_log << "Registered " << algs.size() << " Algorithms" << endmsg;
264
265 }
266
268
269 m_isInitialized = true;
270 std::set<const IAlgTool*>::const_iterator itra;
271 for (itra = m_ialgtools.begin(); itra != m_ialgtools.end(); ++itra) {
272 (const_cast<IAlgTool*>(*itra))->addRef();
273 registerAlgTool(**itra).ignore();
274 }
275
276 m_log << MSG::INFO << "Registered " << m_algtools.size() << " AlgTools"
277 << endmsg;
278
280
281 std::list<IService*> svcs = Gaudi::svcLocator()->getServices();
282
283 std::list<IService*>::const_iterator itrs;
284 for (itrs=svcs.begin(); itrs!=svcs.end(); ++itrs) {
285 (*itrs)->addRef();
286 registerSvc(**itrs).ignore();
287 }
288
289 m_log << MSG::INFO;
290 m_log << "Registered " << svcs.size() << " Services" << endmsg;
291
292 return StatusCode::SUCCESS;
293
294
295 }
296
297 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
298
299 StatusCode HistorySvc::stop() {
300
301 if (!m_activate) return StatusCode::SUCCESS;
302
303 if (m_dump) {
304 listProperties().ignore();
305 }
306
307 if (m_outputFile != "") {
308 std::ofstream ofs;
309 ofs.open(m_outputFile.c_str());
310 if (!ofs) {
311 m_log << MSG::ERROR << "Unable to open output file \"m_outputFile\""
312 << endmsg;
313 } else {
314
315 // dumpProperties(ofs);
316 dumpState(ofs);
317
318 ofs.close();
319 }
320 }
321
322 clearState();
323
324 return StatusCode::SUCCESS;
325
326 }
327
328 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
329
330 StatusCode HistorySvc::finalize() {
331
332 ON_VERBOSE
333 m_log << MSG::VERBOSE << "HistorySvc::finalize()" << endmsg;
334
335
336 clearState();
337
338 StatusCode status = Service::finalize();
339
340 if ( status.isSuccess() )
341 m_log << MSG::INFO << "Service finalised successfully" << endmsg;
342
343 return status;
344
345 }
346
347 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
348
349 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
350 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
351
352 StatusCode
353 HistorySvc::registerAlg(const Algorithm &alg) {
354
355 JobHistory *job = getJobHistory();
356 if (m_algmap.find(&alg) != m_algmap.end()) {
357 m_log << MSG::WARNING << "Algorithm " << alg.name()
358 << " already registered with HistorySvc" << endmsg;
359 return StatusCode::SUCCESS;
360 }
361
362 (const_cast<Algorithm*>(&alg))->addRef();
363
364 m_algs.insert(&alg);
365
366 AlgorithmHistory *algHist = new AlgorithmHistory(alg, job);
367 m_algmap[&alg] = algHist;
368
369 ON_DEBUG {
370 m_log << MSG::DEBUG << "Registering algorithm: ";
371 m_log.setColor(MSG::CYAN);
372 m_log << alg.name() << endmsg;
373 m_log.resetColor();
374 }
375
376 return StatusCode(StatusCode::SUCCESS,true);
377
378 }
379 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
380
381 StatusCode
382 HistorySvc::listProperties(const Algorithm &alg) const {
383
384 m_log << MSG::INFO << "Dumping properties for " << alg.name() << endl;
385
386 AlgorithmHistory *hist = getAlgHistory( alg );
387
388 if (hist == 0) {
389 return StatusCode::FAILURE;
390 }
391
392 ostringstream ost;
393 ost << *hist;
394
395 std::string str = ost.str();
396
397 m_log << MSG::INFO << alg.name() << " --> " << endl << str << endmsg;
398
399 return StatusCode(StatusCode::SUCCESS,true);
400
401 }
402 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
403
404 void
405 HistorySvc::dumpProperties(const Algorithm &alg, std::ofstream& ofs) const {
406
407 AlgorithmHistory *hist = getAlgHistory( alg );
408
409 if (hist == 0) {
410 return;
411 }
412
413 PropertyList::const_iterator itr;
414 for (itr=hist->properties().begin(); itr!=hist->properties().end(); ++itr) {
415 ofs << alg.name() << " " << dumpProp(*itr) << std::endl;
416 }
417
418 }
419 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
420
421 AlgorithmHistory*
422 HistorySvc::getAlgHistory(const Algorithm &alg) const {
423
424 const Algorithm *palg = &alg;
425 set<const Algorithm*>::const_iterator itr = m_algs.find(palg);
426 if ( itr == m_algs.end() ) {
427 m_log << MSG::WARNING << "Algorithm " << alg.name() << " not registered"
428 << endmsg;
429 return 0;
430 }
431
432 map<const Algorithm*, AlgorithmHistory*>::const_iterator itr2;
433 itr2 = m_algmap.find( *itr );
434
435 return ( itr2->second );
436
437 }
438 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
439
440 void
441 HistorySvc::getAlgHistory(std::set<AlgorithmHistory*>& algs) const {
442
443 set<const Algorithm*>::const_iterator itr;
444 for (itr=m_algs.begin(); itr!=m_algs.end(); ++itr) {
445 AlgorithmHistory *ah = m_algmap.find(*itr)->second;
446
447 algs.insert(ah);
448 }
449
450 }
451
452
453 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
454 StatusCode
455 HistorySvc::registerJob() {
456
457
458 return StatusCode(StatusCode::SUCCESS,true);
459
460
461 }
462 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
463
464
465 StatusCode
466 HistorySvc::listProperties() const {
467
468 m_log << MSG::INFO;
469 m_log.setColor(MSG::CYAN);
470 m_log << "Dumping properties for all Algorithms (" << m_algmap.size()
471 << ")" << endmsg;
472
473 std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
474 for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
475 const Algorithm* alg = itr->first;
476
477 listProperties( *alg ).ignore();
478
479 }
480
481 m_log << MSG::INFO;
482 m_log.setColor(MSG::CYAN);
483 m_log << "Dumping properties for all AlgTools (" << m_algtoolmap.size()
484 << ")" << endmsg;
485
486 std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
487 for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
488 ON_DEBUG
489 m_log << MSG::DEBUG << " --> " << itr_a->second->algtool_name() << endmsg;
490 const AlgTool* alg = itr_a->first;
491
492 listProperties( *alg ).ignore();
493
494 }
495
496 m_log << MSG::INFO;
497 m_log.setColor(MSG::CYAN);
498 m_log << "Dumping properties for all Services (" << m_svcmap.size()
499 << ")" << endmsg;
500
501 std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
502 for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
503 const IService* svc = itr_s->first;
504
505 listProperties( *svc ).ignore();
506
507 }
508
509 m_log << MSG::INFO;
510 m_log.setColor(MSG::CYAN);
511 m_log << "Dumping properties for Job";
512 m_log.resetColor();
513
514 ostringstream ost;
515 ost << *m_jobHistory;
516 std::string str = ost.str();
517
518 m_log << std::endl << str << endmsg;
519
520 return StatusCode(StatusCode::SUCCESS,true);
521
522 }
523 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
524
525 void
526 HistorySvc::dumpProperties(std::ofstream& ofs) const {
527
528
529 ofs << "GLOBAL" << std::endl;
530 const JobHistory::PropertyPairList props = m_jobHistory->propertyPairs();
531 JobHistory::PropertyPairList::const_iterator itrj;
532 for (itrj=props.begin(); itrj != props.end(); ++itrj) {
533 std::string client = itrj->first;
534 const Property* prp = itrj->second;
535 ofs << client << " " << dumpProp(prp) << std::endl;
536 }
537
538 ofs << std::endl << "SERVICES" << std::endl;
539 std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
540 for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
541 const IService* svc = itr_s->first;
542
543 dumpProperties( *svc, ofs );
544
545 }
546
547 ofs << std::endl << "ALGORITHMS" << std::endl;
548 std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
549 for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
550 const Algorithm* alg = itr->first;
551
552 dumpProperties( *alg, ofs );
553
554 }
555
556 ofs << std::endl << "ALGTOOLS" << std::endl;
557 std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
558 for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
559 const AlgTool* alg = itr_a->first;
560
561 dumpProperties( *alg, ofs );
562
563 }
564
565
566 }
567 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
568
569 JobHistory*
570 HistorySvc::getJobHistory() const {
571
572 return m_jobHistory;
573
574 }
575
576 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
577 IAlgorithm*
578 HistorySvc::getCurrentIAlg() const {
579 if (p_algCtxSvc == 0) {
580 m_log << MSG::WARNING << "trying to create DataHistoryObj before "
581 << "HistorySvc has been initialized" << endmsg;
582 return 0;
583
584 } else {
585 return p_algCtxSvc->currentAlg();
586 }
587 }
588
589
590 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
591
592 DataHistory*
593 HistorySvc::createDataHistoryObj(const CLID& id, const std::string& key,
594 const std::string& /* storeName */) {
595
596 if (!m_activate) return 0;
597
598
599 AlgorithmHistory *algHist;
600
601 IAlgorithm* ialg = getCurrentIAlg();
602 if (ialg == 0) {
603 ON_DEBUG
604 m_log << MSG::DEBUG
605 << "Could not discover current Algorithm:" << endl
606 << " object CLID: " << id << " key: \"" << key
607 << "\"" << endmsg;
608 algHist = 0;
609 } else {
610 Algorithm* alg = dynamic_cast<Algorithm*>(ialg);
611 if (alg != 0) {
612 algHist = getAlgHistory( *alg );
613 } else {
614 m_log << MSG::WARNING
615 << "Could not extract concrete Algorithm:"
616 << endl
617 << " object CLID: " << id << " key: \"" << key
618 << "\"" << endmsg;
619 algHist = 0;
620 }
621 }
622
623 DataHistory *hist = new DataHistory(id, key, algHist);
624
625 return hist;
626
627 }
628
629 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
630 StatusCode
631 HistorySvc::registerDataHistory(const CLID& id, const std::string& key,
632 const std::string& storeName) {
633
634 DHH dhh(id,key);
635
636 pair<DHMitr,DHMitr> mitr = m_datMap.equal_range(dhh);
637
638 if (mitr.first == mitr.second) {
639 // not in the map
640 DataHistory *dh = createDataHistoryObj(id,key,storeName);
641 m_datMap.insert(pair<DHH,DataHistory*>(dhh,dh));
642 } else {
643 // found at least one
644 bool match(false);
645
646 std::string algName;
647 IAlgorithm *ialg = getCurrentIAlg();
648 if (ialg != 0) {
649 algName = ialg->name();
650 } else {
651 algName = "UNKNOWN";
652 }
653
654 for (DHMitr itr = mitr.first; itr != mitr.second; ++itr) {
655 DataHistory *dh = itr->second;
656 if (dh->algorithmHistory()->algorithm_name() == algName) {
657 match = true;
658 break;
659 }
660 }
661
662 if (! match) {
663 DataHistory *dh = createDataHistoryObj(id,key,storeName);
664 m_datMap.insert(pair<DHH,DataHistory*>(dhh,dh));
665 }
666 }
667
668 return StatusCode::SUCCESS;
669
670 }
671
672
673 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
674 DataHistory*
675 HistorySvc::getDataHistory(const CLID& id, const std::string& key,
676 const std::string& /*storeName*/) const {
677
678 DHH dhh(id,key);
679
680 pair<DHMCitr,DHMCitr> mitr = m_datMap.equal_range(dhh);
681
682 if(mitr.first == mitr.second) {
683 return 0;
684 }
685
686 return mitr.first->second;
687
688 }
689
690 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
691
692 int
693 HistorySvc::getDataHistory(const CLID& id, const std::string& key,
694 const std::string& /*storeName*/,
695 std::list<DataHistory*>& dhlist) const {
696
697 DHH dhh(id,key);
698
699 int n(0);
700
701 pair<DHMCitr,DHMCitr> mitr = m_datMap.equal_range(dhh);
702
703 for (DHMCitr itr=mitr.first; itr != mitr.second; ++itr) {
704 dhlist.push_back(itr->second);
705 n++;
706 }
707
708 return n;
709
710 }
711
712 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
713
714 StatusCode
715 HistorySvc::registerSvc(const IService &svc) {
716
717 if ( svc.name() == "HistoryStore" ) {
718 // m_log << MSG::WARNING << "not registering store" << endmsg;
719 return StatusCode(StatusCode::SUCCESS,true);
720 }
721
722 JobHistory *job = getJobHistory();
723 const IService* psvc = &svc;
724 map<const IService*, ServiceHistory*>::const_iterator itr =
725 m_svcmap.find(psvc);
726 if (itr == m_svcmap.end()) {
727
728 if (m_log.level() <= MSG::DEBUG) {
729 m_log << MSG::DEBUG << "Registering Service: ";
730 m_log.setColor(MSG::CYAN);
731 m_log << svc.name() << endmsg;
732 }
733
734 m_svcs.insert(psvc);
735
736 ServiceHistory *svcHist = new ServiceHistory(&svc, job);
737 m_svcmap[psvc] = svcHist;
738
739 (const_cast<IService*>(psvc))->addRef();
740
741 }
742
743 m_log.resetColor();
744 return StatusCode(StatusCode::SUCCESS,true);
745
746 }
747
748 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
749
750 ServiceHistory*
751 HistorySvc::getServiceHistory(const IService &svc) const {
752
753 const IService *psvc = &svc;
754 map<const IService*, ServiceHistory*>::const_iterator itr =
755 m_svcmap.find(psvc);
756 if ( itr == m_svcmap.end() ) {
757 m_log << MSG::WARNING << "Service " << svc.name() << " not registered"
758 << endmsg;
759 return 0;
760 }
761
762 return ( itr->second );
763
764 }
765 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
766
767 void
768 HistorySvc::getServiceHistory(std::set<ServiceHistory*>& svcs) const {
769
770 set<const IService*>::const_iterator itr;
771 for (itr=m_svcs.begin(); itr!=m_svcs.end(); ++itr) {
772 ServiceHistory *sh = m_svcmap.find( *itr )->second;
773
774 svcs.insert(sh);
775 }
776
777 }
778
779 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
780
781 StatusCode
782 HistorySvc::listProperties(const IService &svc) const {
783
784 m_log << MSG::INFO << "Dumping properties for " << svc.name() << endl;
785
786 ServiceHistory *hist = getServiceHistory( svc );
787
788 if (hist == 0) {
789 return StatusCode::FAILURE;
790 }
791
792 ostringstream ost;
793 ost << *hist;
794
795 std::string str = ost.str();
796
797 m_log << MSG::INFO << svc.name() << " --> " << endl << str << endmsg;
798
799
800
801 return StatusCode(StatusCode::SUCCESS,true);
802
803 }
804
805 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
806
807 void
808 HistorySvc::dumpProperties(const IService &svc, std::ofstream &ofs) const {
809
810 ServiceHistory *hist = getServiceHistory( svc );
811
812 if (hist == 0) {
813 return;
814 }
815
816 PropertyList::const_iterator itr;
817 for (itr=hist->properties().begin(); itr != hist->properties().end();++itr) {
818 ofs << svc.name() << " " << dumpProp(*itr) << std::endl;
819 }
820
821 }
822
823 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
824
825 StatusCode
826 HistorySvc::registerAlgTool(const IAlgTool& ialg) {
827
828 if (! m_isInitialized) {
829 if (p_algCtxSvc == 0) {
830 if ( service("AlgContextSvc",p_algCtxSvc,true).isFailure() ) {
831 m_log << MSG::ERROR << "unable to get the AlgContextSvc" << endmsg;
832 return StatusCode::FAILURE;
833 }
834 }
835
836 m_ialgtools.insert(&ialg);
837 return StatusCode::SUCCESS;
838 }
839
840 const AlgTool *alg = dynamic_cast<const AlgTool*>( &ialg );
841 if ( alg == 0 ) {
842 m_log << MSG::ERROR << "Could not dcast IAlgTool \"" << ialg.name()
843 << "\" to an AlgTool" << endmsg;
844 return StatusCode::FAILURE;
845 }
846
847 if (m_algtools.find(alg) != m_algtools.end()) {
848 m_log << MSG::WARNING << "AlgTool " << ialg.name()
849 << " already registered in HistorySvc" << endmsg;
850 return StatusCode::SUCCESS;
851 }
852
853 m_algtools.insert(alg);
854 (const_cast<AlgTool*>(alg))->addRef();
855
856 const JobHistory *job = getJobHistory();
857 AlgToolHistory *algHist = new AlgToolHistory(*alg, job);
858 m_algtoolmap[alg] = algHist;
859
860 if (m_log.level() <= MSG::DEBUG) {
861 m_log << MSG::DEBUG << "Registering algtool: ";
862 m_log.setColor(MSG::CYAN);
863 m_log << alg->name() << endmsg;
864 m_log.resetColor();
865 }
866
867 return StatusCode::SUCCESS;
868
869 }
870
871 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
872
873 StatusCode
874 HistorySvc::listProperties(const IAlgTool& alg) const {
875
876 m_log << MSG::INFO << "Dumping properties for " << alg.name() << endl;
877
878 AlgToolHistory *hist = getAlgToolHistory( alg );
879
880 if (hist == 0) {
881 return StatusCode::FAILURE;
882 }
883
884 ostringstream ost;
885 ost << *hist;
886
887 std::string str = ost.str();
888
889 m_log << MSG::INFO << alg.name() << " --> " << endl << str << endmsg;
890
891 return StatusCode::SUCCESS;
892
893 }
894 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
895
896 void
897 HistorySvc::dumpProperties(const IAlgTool& alg, std::ofstream &ofs) const {
898
899 AlgToolHistory *hist = getAlgToolHistory( alg );
900
901 if (hist == 0) {
902 return;
903 }
904
905 PropertyList::const_iterator itr;
906 for (itr=hist->properties().begin(); itr!=hist->properties().end(); ++itr) {
907 ofs << alg.name() << " " << dumpProp(*itr) << std::endl;
908 }
909
910 }
911
912 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
913
914 AlgToolHistory*
915 HistorySvc::getAlgToolHistory(const IAlgTool& alg) const {
916
917 const AlgTool *palg = dynamic_cast<const AlgTool*>(&alg);
918 set<const AlgTool*>::const_iterator itr = m_algtools.find(palg);
919 if ( itr == m_algtools.end() ) {
920 m_log << MSG::WARNING << "AlgTool " << alg.name() << " not registered"
921 << endmsg;
922 return 0;
923 }
924
925 map<const AlgTool*, AlgToolHistory*>::const_iterator itr2;
926 itr2 = m_algtoolmap.find( *itr );
927
928 return ( itr2->second );
929
930 }
931
932 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
933
934 void
935 HistorySvc::getAlgToolHistory(std::set<AlgToolHistory*>& algs) const {
936
937 set<const AlgTool*>::const_iterator itr;
938 for (itr=m_algtools.begin(); itr!=m_algtools.end(); ++itr) {
939 AlgToolHistory *ah = m_algtoolmap.find(*itr)->second;
940
941 algs.insert(ah);
942 }
943
944 }
945
946 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
947
948 void
949 HistorySvc::handle(const Incident& incident) {
950
951 if (incident.type() == IncidentType::BeginEvent) {
952 if (captureState().isFailure()) {
953 m_log << MSG::WARNING << "Error capturing state." << endl
954 << "Will try again at next BeginEvent incident" << endmsg;
955 }
956 }
957
958 }
959
960 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
961
962
963 std::string
964 HistorySvc::dumpProp(const Property* prop, bool isXML, int ind) const {
965 std::ostringstream ost;
966 if (isXML) {
967 while (ind > 0) {
968 ost << " ";
969 ind --;
970 }
971 ost << "<PROPERTY name=\"" << prop->name()
972 << "\" value=\"" << HistoryObj::convert_string(prop->toString())
973 << "\" documentation=\"" << HistoryObj::convert_string(prop->documentation())
974 << "\">";
975 } else {
976 prop->fillStream(ost);
977 }
978 return ost.str();
979 }
980
981 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
982
983 void
984 HistorySvc::dumpState(std::ofstream& ofs) const {
985
986
987 if(m_outputFileTypeXML) {
988 //xml header
989 ofs << "<?xml version=\"1.0\" ?> " << std::endl;
990 ofs << "<!--Test-xml-->" << std::endl;
991 ofs << "<SETUP>" << std::endl;
992 ofs << " <GLOBAL>" << std::endl;
993 } else {
994 ofs << "GLOBAL" << std::endl;
995 }
996
997 const JobHistory::PropertyPairList props = m_jobHistory->propertyPairs();
998 JobHistory::PropertyPairList::const_iterator itrj;
999 std::string client_currently_open = "start";
1000 for (itrj=props.begin(); itrj != props.end(); ++itrj) {
1001 // client is the name of the component of the current property
1002 std::string client = itrj->first;
1003 const Property* prp = itrj->second;
1004
1005 if (m_outputFileTypeXML) {
1006
1007 if (client != client_currently_open) {
1008 if(client_currently_open!="start") ofs << " </COMPONENT>" << endl;
1009 ofs << " <COMPONENT name=\""
1010 << client << "\" class=\"undefined\">" << std::endl;
1011 }
1012 } else {
1013 ofs << client << " ";
1014 }
1015
1016 ofs << dumpProp(prp,m_outputFileTypeXML,6) << endl;
1017
1018 client_currently_open = client;
1019
1020 if (m_outputFileTypeXML)
1021 ofs << " </COMPONENT>" << endl;
1022 }
1023
1024
1025 if(m_outputFileTypeXML) {
1026 ofs << "</GLOBAL>" << endl << "<SERVICES>" << endl;
1027 } else {
1028 ofs << "SERVICES" << std::endl;
1029 }
1030
1031 std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
1032 for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
1033 const IService* svc = itr_s->first;
1034
1035 dumpState(svc,ofs);
1036
1037 }
1038
1039 if(m_outputFileTypeXML) {
1040 ofs << "</SERVICES>" << endl << "<ALGORITHMS> " << endl;
1041 } else {
1042 ofs << "ALGORITHMS" << std::endl;
1043 }
1044
1045 std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
1046 for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
1047 const Algorithm* alg = itr->first;
1048
1049 dumpState(alg,ofs);
1050
1051 }
1052
1053
1054 if(m_outputFileTypeXML) {
1055 ofs << "</ALGORITHMS>" << endl << "<ALGTOOLS> " << endl;
1056 } else {
1057 ofs << "ALGTOOLS" << std::endl;
1058 }
1059
1060 std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
1061 for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
1062 const AlgTool* alg = itr_a->first;
1063
1064 dumpState( alg, ofs);
1065
1066 }
1067
1068 if(m_outputFileTypeXML) {
1069 ofs << "</ALGTOOLS>" << endl << "</SETUP>" << endl;
1070 }
1071
1072 }
1073
1074 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1075
1076 void
1077 HistorySvc::dumpState(const INamedInterface *in, std::ofstream& ofs) const {
1078
1079 HistoryObj *hist(0);
1080 IVersHistoryObj *vhist(0);
1081
1082 const IService* is(0);
1083 const Algorithm* ia(0);
1084 const IAlgTool* it(0);
1085 if ( (is=dynamic_cast<const IService*>(in)) != 0) {
1086 ON_VERBOSE
1087 m_log << MSG::VERBOSE << in->name() << " is Service" << endmsg;
1088 ServiceHistory* o = getServiceHistory( *is );
1089 hist = dynamic_cast<HistoryObj*>( o );
1090 vhist = dynamic_cast<IVersHistoryObj*>( o );
1091 } else if ( (ia = dynamic_cast<const Algorithm*>(in)) != 0 ) {
1092 ON_VERBOSE
1093 m_log << MSG::VERBOSE << in->name() << " is Alg" << endmsg;
1094 AlgorithmHistory *o = getAlgHistory( *ia );
1095 hist = dynamic_cast<HistoryObj*>( o );
1096 vhist = dynamic_cast<IVersHistoryObj*>( o );
1097 } else if ( (it = dynamic_cast<const IAlgTool*>(in)) != 0 ) {
1098 ON_VERBOSE
1099 m_log << MSG::VERBOSE << in->name() << " is AlgTool" << endmsg;
1100 AlgToolHistory *o = getAlgToolHistory( *it );
1101 hist = dynamic_cast<HistoryObj*>( o );
1102 vhist = dynamic_cast<IVersHistoryObj*>( o );
1103 } else {
1104 m_log << MSG::ERROR
1105 << "Could not dcast interface to accepted History Obj type for "
1106 << in->name() << endreq;
1107 return;
1108 }
1109
1110 if (hist == 0 || vhist == 0) {
1111 m_log << MSG::ERROR << "Could not dcast recognized object to HistoryObj or IVersHistoryObj. This should never happen."
1112 << endmsg;
1113 return;
1114 }
1115
1116 if (m_outputFileTypeXML) {
1117 hist->dump(ofs,true);
1118 } else {
1119 ofs << ">> " << vhist->name() << endl << *hist << endl;
1120 }
1121
1122 }