1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2010 Licq developers
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "logdistributor.h"
21 #include <licq/thread/mutexlocker.h>
22 
23 #include <boost/foreach.hpp>
24 
25 using Licq::LogSink;
26 using Licq::MutexLocker;
27 using namespace LicqDaemon;
28 
registerSink(LogSink::Ptr sink)29 void LogDistributor::registerSink(LogSink::Ptr sink)
30 {
31   MutexLocker locker(myMutex);
32   if (std::find(mySinks.begin(), mySinks.end(), sink) != mySinks.end())
33     return;
34 
35   mySinks.push_back(sink);
36 }
37 
unregisterSink(LogSink::Ptr sink)38 void LogDistributor::unregisterSink(LogSink::Ptr sink)
39 {
40   MutexLocker locker(myMutex);
41   mySinks.remove(sink);
42 }
43 
isLogging(Licq::Log::Level level) const44 bool LogDistributor::isLogging(Licq::Log::Level level) const
45 {
46   MutexLocker locker(myMutex);
47   BOOST_FOREACH(LogSink::Ptr sink, mySinks)
48   {
49     if (sink->isLogging(level))
50       return true;
51   }
52   return false;
53 }
54 
isLoggingPackets() const55 bool LogDistributor::isLoggingPackets() const
56 {
57   MutexLocker locker(myMutex);
58   BOOST_FOREACH(LogSink::Ptr sink, mySinks)
59   {
60     if (sink->isLoggingPackets())
61       return true;
62   }
63   return false;
64 }
65 
log(Message::Ptr message)66 void LogDistributor::log(Message::Ptr message)
67 {
68   MutexLocker locker(myMutex);
69   BOOST_FOREACH(LogSink::Ptr sink, mySinks)
70   {
71     if (sink->isLogging(message->level))
72       sink->log(message);
73   }
74 }
75