Fawkes API  Fawkes Development Version
logger.cpp
00001 
00002 /***************************************************************************
00003  *  logger.cpp - Fawkes LoggerAspect initializer/finalizer
00004  *
00005  *  Created: Wed Nov 24 01:17:09 2010
00006  *  Copyright  2006-2010  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <aspect/inifins/logger.h>
00025 #include <aspect/logger.h>
00026 #include <logging/logger_employer.h>
00027 #include <core/threading/thread_initializer.h>
00028 #include <core/threading/thread_finalizer.h>
00029 
00030 namespace fawkes {
00031 #if 0 /* just to make Emacs auto-indent happy */
00032 }
00033 #endif
00034 
00035 /** @class LoggerAspectIniFin <aspect/inifins/logger.h>
00036  * Initializer/finalizer for the LoggerAspect.
00037  * @author Tim Niemueller
00038  */
00039 
00040 /** Constructor.
00041  * @param employer logger employer to register loggers to
00042  */
00043 LoggerAspectIniFin::LoggerAspectIniFin(LoggerEmployer *employer)
00044   : AspectIniFin("LoggerAspect")
00045 {
00046   __employer = employer;
00047 }
00048 
00049 
00050 void
00051 LoggerAspectIniFin::init(Thread *thread)
00052 {
00053   LoggerAspect *logger_thread;
00054   logger_thread = dynamic_cast<LoggerAspect *>(thread);
00055   if (logger_thread == 0) {
00056     throw CannotInitializeThreadException("Thread '%s' claims to have the "
00057                                           "LoggerAspect, but RTTI says it "
00058                                           "has not. ", thread->name());
00059   }
00060 
00061   try {
00062     __employer->add_logger(logger_thread->get_logger());
00063   } catch (Exception &e) {
00064     CannotInitializeThreadException ce("Thread has LoggerAspect but Logger "
00065                                        "could not be added.");
00066     ce.append(e);
00067     throw ce;
00068   } catch (...) {
00069     throw CannotInitializeThreadException("Thread has LoggerAspect but Logger "
00070                                           "could not be added.");
00071   }
00072 }
00073 
00074 
00075 void
00076 LoggerAspectIniFin::finalize(Thread *thread)
00077 {
00078   LoggerAspect *logger_thread;
00079   logger_thread = dynamic_cast<LoggerAspect *>(thread);
00080   if (logger_thread == 0) {
00081     throw CannotFinalizeThreadException("Thread '%s' claims to have the "
00082                                         "LoggerAspect, but RTTI says it "
00083                                         "has not. ", thread->name());
00084   }
00085 
00086   try {
00087     __employer->remove_logger(logger_thread->get_logger());
00088   } catch (Exception &e) {
00089     CannotFinalizeThreadException ce("Failed to remove logger");
00090     ce.append(e);
00091     throw;
00092   }
00093 }
00094 
00095 } // end namespace fawkes