claw 1.9.0
 
Loading...
Searching...
No Matches
logger.cpp
Go to the documentation of this file.
1/*
2 CLAW - a C++ Library Absolutely Wonderful
3
4 CLAW is a free library without any particular aim but being useful to
5 anyone.
6
7 Copyright (C) 2005-2011 Julien Jorge
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23 contact: julien.jorge@stuff-o-matic.com
24*/
31
32#include <algorithm>
33
34namespace claw
35{
38}
39
46 : m_log_level(-1)
47 , m_message_level(0)
48{}
49
57
62{
63 std::lock_guard<std::mutex> lock(m_mutex);
64
65 stream_list_type::iterator it;
66
67 for(it = m_stream.begin(); it != m_stream.end(); ++it)
68 delete *it;
69
70 m_stream.clear();
71}
72
77void claw::log_system::merge(stream_type* s)
78{
79 std::lock_guard<std::mutex> lock(m_mutex);
80
81 m_stream.push_front(s);
82}
83
89void claw::log_system::remove(const stream_type* s)
90{
91 std::lock_guard<std::mutex> lock(m_mutex);
92
93 stream_list_type::iterator it
94 = std::find(m_stream.begin(), m_stream.end(), s);
95
96 if(it != m_stream.end())
97 m_stream.erase(it);
98}
99
104void claw::log_system::set(stream_type* s)
105{
106 clear();
107
108 std::lock_guard<std::mutex> lock(m_mutex);
109
110 m_stream.push_front(s);
111}
112
118{
119 std::lock_guard<std::mutex> lock(m_mutex);
120
121 m_log_level = lvl;
122}
123
129{
130 std::lock_guard<std::mutex> lock(m_mutex);
131
132 m_log_level = lvl.get();
133}
134
139{
140 std::lock_guard<std::mutex> lock(m_mutex);
141
142 if(m_message_level <= m_log_level)
143 {
144 stream_list_type::iterator it;
145
146 for(it = m_stream.begin(); it != m_stream.end(); ++it)
147 (*it)->flush();
148 }
149}
150
155claw::log_system& claw::log_system::operator<<(const log_level& that)
156{
157 std::lock_guard<std::mutex> lock(m_mutex);
158
159 m_message_level = that.get();
160
161 if(m_message_level <= m_log_level)
162 print(that.get_string());
163
164 return *this;
165}
166
171claw::log_system& claw::log_system::operator<<(log_system& (*pf)(log_system&))
172{
173 return pf(*this);
174}
175
181void claw::log_system::print(const std::string& s)
182{
183 stream_list_type::iterator it;
184
185 for(it = m_stream.begin(); it != m_stream.end(); ++it)
186 (*it)->write(s);
187}
188
194{
195 return log << std::endl;
196}
197
198claw::log_system& std::endl(claw::log_system& log)
199{
200 (log << "\n").flush();
201 return log;
202}
Set the level of the next message for logger_system::operator<<().
Definition log_level.hpp:58
int get() const
Get the level value.
Definition log_level.cpp:62
std::string get_string() const
Get the prefix.
Definition log_level.cpp:70
A class implementing a logging system.
Definition logger.hpp:73
CLAW_LOGGER_EXPORT void remove(const stream_type *s)
Remove a stream.
Definition logger.cpp:89
CLAW_LOGGER_EXPORT void clear()
Delete the streams.
Definition logger.cpp:61
CLAW_LOGGER_EXPORT ~log_system()
Destructor.
Definition logger.cpp:53
CLAW_LOGGER_EXPORT void set_level(int lvl)
Change the level of log.
Definition logger.cpp:117
CLAW_LOGGER_EXPORT void set(stream_type *s)
Set the output stream.
Definition logger.cpp:104
CLAW_LOGGER_EXPORT void flush()
Flush all log streams.
Definition logger.cpp:138
CLAW_LOGGER_EXPORT void merge(stream_type *s)
Add an other output stream.
Definition logger.cpp:77
CLAW_LOGGER_EXPORT log_system()
Default constructor.
Definition logger.cpp:45
Some basic classes for logging.
This is the main namespace.
CLAW_LOGGER_EXPORT log_system & lendl(log_system &log)
Add a new line caracter to a logger and flush it.
Definition logger.cpp:193
CLAW_LOGGER_EXPORT log_system logger
The default log system provided by claw.
Definition logger.cpp:37