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
30namespace 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 */
41{
42 employer_ = employer;
43}
44
45void
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
70void
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
Aspect initializer/finalizer base class.
Definition: inifin.h:34
Thread cannot be finalized.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void append(const char *format,...) noexcept
Append messages to the message list.
Definition: exception.cpp:333
LoggerAspectIniFin(LoggerEmployer *employer)
Constructor.
Definition: logger.cpp:40
virtual void init(Thread *thread)
Initialize thread.
Definition: logger.cpp:46
virtual void finalize(Thread *thread)
Finalize thread.
Definition: logger.cpp:71
Thread aspect that allows to provide a logger to Fawkes.
Definition: logger.h:34
Logger * get_logger() const
Get time source.
Definition: logger.cpp:59
Logger employer The LoggerEmployer shall pipe all log messages of the system to added loggers.
virtual void add_logger(Logger *logger)=0
Add a new logger.
virtual void remove_logger(Logger *logger)=0
Remove a logger.
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
Fawkes library namespace.