root/logger/logline.hh

Revision 81c6e32180ba6f1018c28ae75b7281fd38121812, 2.8 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 21 months ago)

Exception-proof destructors.

Hopefully addresses #78.

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

  • Property mode set to 100644
Line 
1/*  This file is part of Alue, the multiprotocol Internet discussion daemon
2
3    Copyright © 2009, 2010 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_LOGLINE_HH
21#define GUARD_LOGGER_LOGLINE_HH
22
23#include "logger.hh"
24
25#include <cassert>
26#include <boost/date_time/posix_time/posix_time.hpp>
27#include <boost/noncopyable.hpp>
28
29namespace logger
30{
31        class logline : private boost::noncopyable
32        {
33                bool valid;
34
35                void prefix();
36                void suffix();
37        public:
38                logline(bool doOpen = true)
39                        : valid(false)
40                        { if (doOpen) open(); }
41                ~logline() {
42                        try
43                        {
44                                if (valid) close();
45                        }
46                        catch (std::exception)
47                        {}
48                }
49
50                void open();
51                void close();
52
53                void nl() { suffix(); prefix(); }
54
55                template <typename T>
56                logline &operator<<(T t) {
57                        assert(valid);
58                        *logger.os << t;
59                        logger.dirty = true;
60                        return *this;
61                }
62        };
63
64        inline void logline::prefix()
65        {
66                using namespace boost::posix_time;
67         
68                assert(valid);
69                ptime lt = second_clock::local_time();
70                std::string s = to_iso_extended_string(lt);
71                *logger.os << s << ' ';
72                logger.dirty = true;
73        }
74
75        inline void logline::suffix()
76        {
77                assert(valid);
78                logger.os->put('\n');
79                logger.dirty = true;
80        }
81
82        inline void logline::open()
83        {
84                if (valid) return;
85                logger.m.lock();
86                valid = true;
87                prefix();
88        }
89
90        inline void logline::close()
91        {
92                if (!valid) return;
93                suffix();
94                logger.m.unlock();
95                valid = false;
96        }
97};
98#endif /* GUARD_LOGGER_LOGLINE_HH */
Note: See TracBrowser for help on using the browser.