root/logger/logger.hh

Revision 341603a0f7acf173ecace38718deb66801483644, 2.3 KB (checked in by Antti-Juhani Kaijanaho <ajk@…>, 2 years ago)

Fix #67 by using a recursive mutex

Signed-off-by: Antti-Juhani Kaijanaho <ajk@…>

  • Property mode set to 100644
Line 
1/*  This file is part of Alue, the multiprotocol Internet discussion daemon
2
3    Copyright © 2009 Antti-Juhani Kaijanaho
4
5    Alue is free software: you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    Alue is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with Alue.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20#ifndef GUARD_LOGGER_LOGGER_HH
21#define GUARD_LOGGER_LOGGER_HH
22
23#include <boost/noncopyable.hpp>
24#include <boost/thread/locks.hpp>
25#include <boost/thread/recursive_mutex.hpp>
26
27#include <fstream>
28
29namespace logger
30{
31        class logger_ : private boost::noncopyable
32        {
33                boost::recursive_mutex m;
34               
35                std::ostream *os;
36               
37                bool dirty;
38
39                bool ours;
40
41                friend class logline;
42
43        public:
44                logger_() : dirty(false), ours(false) {}
45
46                void open(std::string logfile);
47
48                void open(std::ostream &os) {
49                        m.lock();
50                        if (dirty) this->os->flush();
51                        if (ours) delete this->os;
52                        this->os = &os;
53                        dirty = false;
54                        ours = false;
55                        m.unlock();
56                }
57
58                // ensure that this is not called while the current
59                // thread has an open logline object
60                void flush() {
61                        m.lock();
62                        try {
63                                if (dirty) os->flush();
64                                dirty = false;
65                        } catch (...) {
66                                m.unlock();
67                                throw;
68                        }
69                        m.unlock();
70                }
71        };
72
73        extern logger_ logger;
74       
75};
76#endif /* GUARD_LOGGER_LOGGER_HH */
Note: See TracBrowser for help on using the browser.