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 <logging/logger_employer.h>
27 #include <core/threading/thread_initializer.h>
28 #include <core/threading/thread_finalizer.h>
29 
30 namespace fawkes {
31 #if 0 /* just to make Emacs auto-indent happy */
32 }
33 #endif
34 
35 /** @class LoggerAspectIniFin <aspect/inifins/logger.h>
36  * Initializer/finalizer for the LoggerAspect.
37  * @author Tim Niemueller
38  */
39 
40 /** Constructor.
41  * @param employer logger employer to register loggers to
42  */
44  : AspectIniFin("LoggerAspect")
45 {
46  __employer = employer;
47 }
48 
49 
50 void
52 {
53  LoggerAspect *logger_thread;
54  logger_thread = dynamic_cast<LoggerAspect *>(thread);
55  if (logger_thread == 0) {
56  throw CannotInitializeThreadException("Thread '%s' claims to have the "
57  "LoggerAspect, but RTTI says it "
58  "has not. ", thread->name());
59  }
60 
61  try {
62  __employer->add_logger(logger_thread->get_logger());
63  } catch (Exception &e) {
64  CannotInitializeThreadException ce("Thread has LoggerAspect but Logger "
65  "could not be added.");
66  ce.append(e);
67  throw ce;
68  } catch (...) {
69  throw CannotInitializeThreadException("Thread has LoggerAspect but Logger "
70  "could not be added.");
71  }
72 }
73 
74 
75 void
77 {
78  LoggerAspect *logger_thread;
79  logger_thread = dynamic_cast<LoggerAspect *>(thread);
80  if (logger_thread == 0) {
81  throw CannotFinalizeThreadException("Thread '%s' claims to have the "
82  "LoggerAspect, but RTTI says it "
83  "has not. ", thread->name());
84  }
85 
86  try {
87  __employer->remove_logger(logger_thread->get_logger());
88  } catch (Exception &e) {
89  CannotFinalizeThreadException ce("Failed to remove logger");
90  ce.append(e);
91  throw;
92  }
93 }
94 
95 } // end namespace fawkes
LoggerAspectIniFin(LoggerEmployer *employer)
Constructor.
Definition: logger.cpp:43
virtual void init(Thread *thread)
Initialize thread.
Definition: logger.cpp:51
Thread aspect that allows to provide a logger to Fawkes.
Definition: logger.h:36
virtual void finalize(Thread *thread)
Finalize thread.
Definition: logger.cpp:76
virtual void add_logger(Logger *logger)=0
Add a new logger.
Fawkes library namespace.
Logger employer The LoggerEmployer shall pipe all log messages of the system to added loggers...
Thread class encapsulation of pthreads.
Definition: thread.h:42
Thread cannot be initialized.
Base class for exceptions in Fawkes.
Definition: exception.h:36
const char * name() const
Get name of thread.
Definition: thread.h:95
virtual void remove_logger(Logger *logger)=0
Remove a logger.
Thread cannot be finalized.
Logger * get_logger() const
Get time source.
Definition: logger.cpp:64
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:341
Aspect initializer/finalizer base class.
Definition: inifin.h:36