Library of Assembled Shared Sources
logger.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
44#include "logger.h"
45
46namespace lass
47{
48namespace io
49{
50
51/** Default constructor for the sake of static library. We don't recommend of using it.
52 *//*
53Logger::Logger()
54{
55 openLog("lass.log", lmClear);
56}
57*/
58
59
60/** Create a logger to a log file and specify if it must append to the file or clear it.
61 */
62Logger::Logger(const std::string &iLogFilename, LogMode iLogMode)
63{
64 openLog(iLogFilename, iLogMode);
65}
66
67
68
69/** Unsubscribe to all proxys
70 */
72{
73 for (TProxyList::iterator pit = proxyList_.begin(); pit != proxyList_.end(); ++pit)
74 {
75 ProxyOStream* proxy = *pit;
76 proxy->remove(&logFile_);
77 }
78}
79
80
81
82/** Subscribe this logger to a proxy stream and set the filter on it.
83 * @param iProxy if null pointer, then an exception is thrown.
84 * @param iFilter filter between proxy and logger
85 */
86void Logger::subscribeTo(ProxyOStream* iProxy, TMask iFilter)
87{
88 proxyList_.push_back(LASS_ENFORCE_POINTER(iProxy));
89 iProxy->add(&logFile_, iFilter);
90}
91
92
93
94/** Unsubscribe this logger to a proxy stream
95 * @param iProxy if null pointer, then an exception is thrown.
96 */
98{
99 proxyList_.remove(LASS_ENFORCE_POINTER(iProxy));
100 iProxy->remove(&logFile_);
101}
102
103
104
105/** return the filter between proxy and logger
106 * @param iProxy logger must be subscribed to this proxy (and proxy should not be a NULL pointer),
107 * otherwise and exception is thrown.
108 */
109Logger::TMask Logger::filter(ProxyOStream* iProxy)
110{
111 return (LASS_ENFORCE_POINTER(iProxy))->filter(&logFile_);
112}
113
114
115
116/** Set filter between proxy and logger
117 * @param iProxy logger must be subscribed to this proxy (and proxy should not be a NULL pointer),
118 * otherwise and exception is thrown.
119 * @param iFilter the filter between proxy and logger.
120 */
121void Logger::setFilter(ProxyOStream* iProxy, TMask iFilter)
122{
123 (LASS_ENFORCE_POINTER(iProxy))->setFilter(&logFile_, iFilter);
124}
125
126
127
128/** open file to write logs to.
129 * You should do this in (and only in) the constructor. The Logger doesn't like to have no
130 * filestreams open, or to change the file stream since it relies on it to subscribe to
131 * the different proxy streams. So make sure that you have a file stream op after construction,
132 * and that you never change it!
133 *
134 * QUESTION: is this true? afterall, if you open a different file, the pointer to lofFile_
135 * will not change, but maybe the proxy streams don't like closed filestreams?
136 */
137void Logger::openLog(const std::string &iLogFilename, LogMode iLogMode)
138{
139 if (logFile_.is_open())
140 {
141 LASS_THROW("There's already an open log file! Can't open new one ...");
142 }
143
144 std::ios::openmode mode = std::ios::out;
145 mode |= (iLogMode == lmAppend) ? std::ios::app : std::ios::trunc;
146
147 logFile_.open(iLogFilename.c_str(), mode);
148
149 if (!logFile_.is_open())
150 {
151 LASS_THROW("Could not open log file '" << iLogFilename << "'");
152 }
153}
154
155}
156
157}
158
159// EOF
void setFilter(ProxyOStream *iProxy, TMask iFilter)
Set filter between proxy and logger.
Definition logger.cpp:121
void unsubscribeTo(ProxyOStream *iProxy)
Unsubscribe this logger to a proxy stream.
Definition logger.cpp:97
TMask filter(ProxyOStream *iProxy)
return the filter between proxy and logger
Definition logger.cpp:109
~Logger()
Unsubscribe to all proxys.
Definition logger.cpp:71
void subscribeTo(ProxyOStream *iProxy, TMask iFilter=ProxyOStream::acceptAll)
Subscribe this logger to a proxy stream and set the filter on it.
Definition logger.cpp:86
Logger(const std::string &iLogFile, LogMode iLogMode=lmClear)
Default constructor for the sake of static library.
Definition logger.cpp:62
A proxy output stream can distribute output to multiple destination streams.
void add(std::ostream *destination, TMask filterMask=acceptAll)
Add a std::ostream to the list of destination streams.
void remove(std::ostream *destination)
Remove a std::ostream from the list of destination streams.
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53