library of assembled shared sources

http://lass.cocamware.com

logger.cpp

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #include "io_common.h"
00044 #include "logger.h"
00045 
00046 namespace lass
00047 {
00048 namespace io
00049 {
00050 
00051 /** Default constructor for the sake of static library.  We don't recommend of using it.
00052  *//*
00053 Logger::Logger()
00054 {
00055     openLog("lass.log", lmClear);
00056 }
00057 */
00058 
00059 
00060 /** Create a logger to a log file and specify if it must append to the file or clear it.
00061  */
00062 Logger::Logger(const std::string &iLogFilename, LogMode iLogMode)
00063 {
00064     openLog(iLogFilename, iLogMode);
00065 }
00066 
00067 
00068 
00069 /** Unsubscribe to all proxys
00070  */
00071 Logger::~Logger()
00072 {
00073     for (TProxyList::iterator pit = proxyList_.begin(); pit != proxyList_.end(); ++pit)
00074     {
00075         ProxyOStream* proxy = *pit;
00076         proxy->remove(&logFile_);
00077     }
00078 }
00079 
00080 
00081 
00082 /** Subscribe this logger to a proxy stream and set the filter on it.
00083  *  @param iProxy if null pointer, then an exception is thrown.
00084  *  @param iFilter filter between proxy and logger
00085  */
00086 void Logger::subscribeTo(ProxyOStream* iProxy, TMask iFilter)
00087 {
00088     proxyList_.push_back(LASS_ENFORCE_POINTER(iProxy));
00089     iProxy->add(&logFile_, iFilter);
00090 }
00091 
00092 
00093 
00094 /** Unsubscribe this logger to a proxy stream
00095  *  @param iProxy if null pointer, then an exception is thrown.
00096  */
00097 void Logger::unsubscribeTo(ProxyOStream* iProxy)
00098 {
00099     proxyList_.remove(LASS_ENFORCE_POINTER(iProxy));
00100     iProxy->remove(&logFile_);
00101 }
00102 
00103 
00104 
00105 /** return the filter between proxy and logger
00106  *  @param iProxy logger must be subscribed to this proxy (and proxy should not be a NULL pointer),
00107  *                otherwise and exception is thrown.
00108  */
00109 Logger::TMask Logger::filter(ProxyOStream* iProxy)
00110 {
00111     return (LASS_ENFORCE_POINTER(iProxy))->filter(&logFile_);
00112 }
00113 
00114 
00115 
00116 /** Set filter between proxy and logger
00117  *  @param iProxy logger must be subscribed to this proxy (and proxy should not be a NULL pointer),
00118  *                otherwise and exception is thrown.
00119  *  @param iFilter the filter between proxy and logger.
00120  */
00121 void Logger::setFilter(ProxyOStream* iProxy, TMask iFilter)
00122 {
00123     (LASS_ENFORCE_POINTER(iProxy))->setFilter(&logFile_, iFilter);
00124 }
00125 
00126 
00127 
00128 /** open file to write logs to.
00129  *  You should do this in (and only in) the constructor.  The Logger doesn't like to have no
00130  *  filestreams open, or to change the file stream since it relies on it to subscribe to
00131  *  the different proxy streams.  So make sure that you have a file stream op after construction,
00132  *  and that you never change it!
00133  *
00134  *  QUESTION: is this true?  afterall, if you open a different file, the pointer to lofFile_
00135  *  will not change, but maybe the proxy streams don't like closed filestreams?
00136  */
00137 void Logger::openLog(const std::string &iLogFilename, LogMode iLogMode)
00138 {
00139     if (logFile_.is_open())
00140     {
00141         LASS_THROW("There's already an open log file!  Can't open new one ...");
00142     }
00143 
00144     std::ios::openmode mode = std::ios::out;
00145     mode |= (iLogMode == lmAppend) ? std::ios::app : std::ios::trunc;
00146 
00147     logFile_.open(iLogFilename.c_str(), mode);
00148 
00149     if (!logFile_.is_open())
00150     {
00151         LASS_THROW("Could not open log file '" << iLogFilename << "'");
00152     }
00153 }
00154 
00155 }
00156 
00157 }
00158 
00159 // EOF

Generated on Mon Nov 10 14:20:30 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo