Fawkes API  Fawkes Development Version
logger.cpp
1 
2 /***************************************************************************
3  * logger.cpp - Fawkes LoggerAspect initializer/finalizer
4  *
5  * Created: Wed Nov 24 01:17:09 2010
6  * Copyright 2006-2010 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <aspect/inifins/logger.h>
25 #include <aspect/logger.h>
26 #include <core/threading/thread_finalizer.h>
27 #include <core/threading/thread_initializer.h>
28 #include <logging/logger_employer.h>
29 
30 namespace fawkes {
31 
32 /** @class LoggerAspectIniFin <aspect/inifins/logger.h>
33  * Initializer/finalizer for the LoggerAspect.
34  * @author Tim Niemueller
35  */
36 
37 /** Constructor.
38  * @param employer logger employer to register loggers to
39  */
40 LoggerAspectIniFin::LoggerAspectIniFin(LoggerEmployer *employer) : AspectIniFin("LoggerAspect")
41 {
42  employer_ = employer;
43 }
44 
45 void
47 {
48  LoggerAspect *logger_thread;
49  logger_thread = dynamic_cast<LoggerAspect *>(thread);
50  if (logger_thread == 0) {
51  throw CannotInitializeThreadException("Thread '%s' claims to have the "
52  "LoggerAspect, but RTTI says it "
53  "has not. ",
54  thread->name());
55  }
56 
57  try {
58  employer_->add_logger(logger_thread->get_logger());
59  } catch (Exception &e) {
60  CannotInitializeThreadException ce("Thread has LoggerAspect but Logger "
61  "could not be added.");
62  ce.append(e);
63  throw ce;
64  } catch (...) {
65  throw CannotInitializeThreadException("Thread has LoggerAspect but Logger "
66  "could not be added.");
67  }
68 }
69 
70 void
71 LoggerAspectIniFin::finalize(Thread *thread)
72 {
73  LoggerAspect *logger_thread;
74  logger_thread = dynamic_cast<LoggerAspect *>(thread);
75  if (logger_thread == 0) {
76  throw CannotFinalizeThreadException("Thread '%s' claims to have the "
77  "LoggerAspect, but RTTI says it "
78  "has not. ",
79  thread->name());
80  }
81 
82  try {
83  employer_->remove_logger(logger_thread->get_logger());
84  } catch (Exception &e) {
85  CannotFinalizeThreadException ce("Failed to remove logger");
86  ce.append(e);
87  throw;
88  }
89 }
90 
91 } // end namespace fawkes
fawkes::LoggerEmployer::remove_logger
virtual void remove_logger(Logger *logger)=0
Remove a logger.
fawkes::LoggerEmployer::add_logger
virtual void add_logger(Logger *logger)=0
Add a new logger.
fawkes::CannotInitializeThreadException
Definition: thread_initializer.h:39
fawkes::Thread::name
const char * name() const
Definition: thread.h:100
fawkes::LoggerAspect
Definition: logger.h:39
fawkes
fawkes::LoggerAspectIniFin::LoggerAspectIniFin
LoggerAspectIniFin(LoggerEmployer *employer)
Constructor.
Definition: logger.cpp:46
fawkes::LoggerAspectIniFin::init
virtual void init(Thread *thread)
Definition: logger.cpp:52
fawkes::LoggerAspectIniFin::finalize
virtual void finalize(Thread *thread)
Definition: logger.cpp:77
fawkes::Thread
Definition: thread.h:45