summaryrefslogtreecommitdiff
path: root/src/generic/util/logging.cc
blob: e6dad44d85aaf653f12ab721f45df5988abb105d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/** \file logging.cc */


// Copyright (C) 2010 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

#include "logging.h"

#include <boost/enable_shared_from_this.hpp>
#include <boost/make_shared.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/weak_ptr.hpp>

#include <cwidget/generic/threads/threads.h>

#include <iostream>

using boost::enable_shared_from_this;
using boost::make_shared;
using boost::multi_index_container;
using boost::multi_index::const_mem_fun;
using boost::multi_index::hashed_non_unique;
using boost::multi_index::hashed_unique;
using boost::multi_index::indexed_by;
using boost::multi_index::tag;
using boost::optional;
using boost::weak_ptr;
using cwidget::threads::mutex;

namespace aptitude
{
  namespace util
  {
    namespace logging
    {
      const char *describe_log_level(log_level l)
      {
        switch(l)
          {
          case TRACE_LEVEL: return "TRACE";
          case DEBUG_LEVEL: return "DEBUG";
          case INFO_LEVEL: return "INFO";
          case WARN_LEVEL: return "WARN";
          case ERROR_LEVEL: return "ERROR";
          default: return "???";
          }
      }

      class Logger::Impl : public Logger,
                           public enable_shared_from_this<Logger::Impl>
      {
        // If set, the level that has been configured for this
        // logger; otherwise, indicates that no level is configured
        // (the logger will inherit its level from its parent).
        optional<log_level> configuredLevel;

        const std::string category;

        const boost::shared_ptr<Logger::Impl> parent;

        // We carry a weak pointer to the parent logging system, so
        // that we can call back into it even if it's not the global
        // instance.
        const weak_ptr<LoggingSystem::Impl> loggingSystemWeak;

        sigc::signal<void, const char *, int, log_level, LoggerPtr, std::string>
        signal_message_logged;

        static log_level getDefaultLevel() { return ERROR_LEVEL; }

        friend class LoggingSystem;

      public:
        Impl(const std::string &_category,
             const boost::shared_ptr<Logger::Impl> &_parent,
             const boost::shared_ptr<LoggingSystem::Impl> &_loggingSystem);

        void log(const char *sourceFilename,
                 int sourceLineNumber,
                 log_level logLevel,
                 const std::string &msg);

        const std::string &getCategory() const;
        const boost::shared_ptr<Logger::Impl> &getParent() const;

        void setLevel(const optional<log_level> &value);

        sigc::connection
        connect_message_logged(const sigc::slot<
                                 void,
                                 const char *,
                                 int,
                                 log_level,
                                 LoggerPtr,
                                 std::string> &slot);
      };

      Logger::Impl::Impl(const std::string &_category,
                         const boost::shared_ptr<Logger::Impl> &_parent,
                         const boost::shared_ptr<LoggingSystem::Impl> &_loggingSystem)
        : Logger(_parent.get() == NULL
                 ? getDefaultLevel()
                 : _parent->effectiveLevel),
          category(_category),
          parent(_parent),
          loggingSystemWeak(_loggingSystem)
      {
      }

      void Logger::Impl::log(const char *sourceFilename,
                             int sourceLineNumber,
                             log_level logLevel,
                             const std::string &msg)
      {
        // \note shared_from_this() could be costly; it would be more
        // efficient to receive the logger as a parameter, but that
        // would clutter the interface.


        // We emit this log message at each level of the hierarchy,
        // but the logger passed along always refers to where the
        // message started.
        const boost::shared_ptr<Impl> self = shared_from_this();
        boost::shared_ptr<Impl> logger = self;
        while(logger.get() != NULL)
          {
            logger->signal_message_logged(sourceFilename,
                                          sourceLineNumber,
                                          logLevel,
                                          self,
                                          msg);

            logger = logger->parent;
          }
      }

      const std::string &Logger::Impl::getCategory() const
      {
        return category;
      }

      const boost::shared_ptr<Logger::Impl> &Logger::Impl::getParent() const
      {
        return parent;
      }

      LoggingSystem::LoggingSystem()
      {
      }

      LoggingSystem::~LoggingSystem()
      {
      }

      /** \brief Wrapper for the state of the logging system.
       *
       *  A singleton class is used to better encapsulate the state
       *  and to handle lifetime issues properly.
       */
      class LoggingSystem::Impl
        : public LoggingSystem,
          public enable_shared_from_this<LoggingSystem::Impl>
      {
        class by_category_tag;
        class by_parent_tag;

        typedef multi_index_container<
          boost::shared_ptr<Logger::Impl>,
          indexed_by<
            hashed_unique<tag<by_category_tag>,
                          const_mem_fun<Logger::Impl,
                                        const std::string &,
                                        &Logger::Impl::getCategory> >,
            hashed_non_unique<tag<by_parent_tag>,
                              const_mem_fun<Logger::Impl,
                                            const boost::shared_ptr<Logger::Impl> &,
                                            &Logger::Impl::getParent> > > >
        logger_table;

        typedef logger_table::index<by_category_tag>::type by_category_index;
        typedef logger_table::index<by_parent_tag>::type by_parent_index;

        logger_table loggers;

        // Protects access to the table of loggers.  Although
        // setLevel() is not thread-safe, the other public routines
        // are, and some of them need to access the table.
        mutex loggers_mutex;

        typedef by_parent_index::const_iterator child_iterator;

        /** \brief Find the immediate children of the given logger. */
        std::pair<child_iterator, child_iterator>
        find_children(const boost::shared_ptr<Logger::Impl> &parent)
        {
          return loggers.get<by_parent_tag>().equal_range(parent);
        }

        /** \brief Set the effective level of the given logger and all
         *  its descendants, stopping at proper descendants that don't
         *  have a configured level.
         *
         *  \param logger  The starting logger.  This logger's effective
         *                 level is always modified, even if it has a
         *                 configured level.
         *
         *  \param effectiveLevel   The new effective level of logger
         *                          and of its descendants.
         */
        void recursiveSetEffectiveLevel(const boost::shared_ptr<Logger::Impl> &logger,
                                        log_level effectiveLevel)
        {
          logger->effectiveLevel = effectiveLevel;
          std::pair<child_iterator, child_iterator> children =
            find_children(logger);

          for(child_iterator it = children.first; it != children.second; ++it)
            {
              const boost::shared_ptr<Logger::Impl> child = *it;

              if(!child->configuredLevel)
                recursiveSetEffectiveLevel(child, effectiveLevel);
            }
        }

        Impl(const Impl &);

        // Like getLogger, but
        //  1. Returns a Logger::Impl instead of a Logger;
        //  2. Doesn't lock the mutex (so it can be used recursively
        //     from within other routines).
        boost::shared_ptr<Logger::Impl> doGetLogger(const std::string &category);

      public:
        Impl()
        {
        }

        void setLevel(const boost::shared_ptr<Logger::Impl> &category,
                      const optional<log_level> &level);

        LoggerPtr getLogger(const std::string &category);

        void log(const char *sourceFilename,
                 int sourceLineNumber,
                 log_level logLevel,
                 const std::string &msg);

        /** \brief Get the implicit global logging system. */
        static Impl &get()
        {
          // We deliberately leak this to avoid unpleasant surprises.
          // Otherwise, the system would be destroyed when global
          // destructors ran, which has all sorts of nasty
          // possibilities.
          //
          // Needs to be a shared_ptr so enable_shared_from_this()
          // works (which allows loggers to take weak references to
          // the logging system).
          static boost::shared_ptr<Impl> *system =
            new boost::shared_ptr<Impl>(make_shared<Impl>());

          return **system;
        }

        /** \brief Create a new logging system. */
        static boost::shared_ptr<Impl> create()
        {
          return make_shared<Impl>();
        }
      };

      void LoggingSystem::Impl::setLevel(const boost::shared_ptr<Logger::Impl> &category,
                                         const optional<log_level> &level)
      {
        mutex::lock l(loggers_mutex);

        // Update the category itself.
        category->configuredLevel = level;

        // If the configured level is now blank, we need to propagate
        // the effective level from our parent (which is assumed to
        // already be correct).  If we have no parent, we use Logger's
        // default level.
        log_level newEffectiveLevel;

        if(!level)
          {
            const boost::shared_ptr<Logger::Impl> parent = category->getParent();
            if(parent.get() == NULL)
              newEffectiveLevel = Logger::Impl::getDefaultLevel();
            else
              newEffectiveLevel = parent->effectiveLevel;
          }
        else
          newEffectiveLevel = *level;

        recursiveSetEffectiveLevel(category, newEffectiveLevel);
      }

      LoggerPtr LoggingSystem::Impl::getLogger(const std::string &category)
      {
        mutex::lock l(loggers_mutex);

        return doGetLogger(category);
      }

      boost::shared_ptr<Logger::Impl>
      LoggingSystem::Impl::doGetLogger(const std::string &category)
      {
        by_category_index &by_category = loggers.get<by_category_tag>();

        // First, try to find it directly.
        {
          by_category_index::const_iterator found =
            by_category.find(category);

          if(found != by_category.end())
            return *found;
        }

        boost::shared_ptr<Logger::Impl> parent;
        // If they aren't asking for the root logger, find or
        // instantiate the parent.
        if(!category.empty())
          {
            // TODO: make "find the parent category name" a separate
            // static method.
            const std::string::size_type last_dot = category.rfind('.');
            std::string parent_category_name;
            if(last_dot != std::string::npos)
              parent_category_name.assign(category, 0, last_dot);

            parent = doGetLogger(parent_category_name);
          }

        boost::shared_ptr<Logger::Impl> rval =
          make_shared<Logger::Impl>(category, parent, shared_from_this());

        loggers.insert(rval);

        return rval;
      }

      void Logger::Impl::setLevel(const optional<log_level> &value)
      {
        boost::shared_ptr<LoggingSystem::Impl> loggingSystem =
          loggingSystemWeak.lock();

        if(loggingSystem.get() != NULL)
          loggingSystem->setLevel(shared_from_this(), value);
      }

      sigc::connection
      Logger::Impl::connect_message_logged(const sigc::slot<
                                             void,
                                             const char *,
                                             int,
                                             log_level,
                                             LoggerPtr,
                                             std::string> &slot)
      {
        return signal_message_logged.connect(slot);
      }


      Logger::Logger(log_level _effectiveLevel)
        : effectiveLevel(_effectiveLevel)
      {
      }

      Logger::~Logger()
      {
      }

      LoggerPtr Logger::getLogger(const std::string &category)
      {
        return LoggingSystem::Impl::get().getLogger(category);
      }

      boost::shared_ptr<LoggingSystem> createLoggingSystem()
      {
        return LoggingSystem::Impl::create();
      }
    }
  }
}