Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

LogWriter.cc

Go to the documentation of this file.
00001 /*
00002  * LogWriter.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Logging facility with priorities and stream-like interface.
00006  *
00007  * Copyright (c) 2004 Milan Cermak
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 #include <stdio.h>
00025 #include <string.h>
00026 
00027 #include <stlib/tools/LogWriter.h>
00028 #include <stlib/EncodedStream.h>
00029 #include <stlib/SequenceableCollection.h>
00030 #include <stlib/String.h>
00031 #include <stlib/Timestamp.h>
00032 #include <stlib/os/Semaphore.h>
00033 #include <stlib/tools/Filename.h>
00034 #include <stlib/tools/FileEncodedStreamFactory.h>
00035 
00036 using namespace OS;
00037 using namespace Tools;
00038 
00039 LogWriter::LogWriter(Stream *writeStream)
00040   : StreamDecorator(writeStream)
00041 {
00042     _level = LogError;
00043     mutex = Semaphore::forMutualExclusion();
00044 }
00045 
00046 LogWriter::~LogWriter(void)
00047 {
00048         close();
00049     delete mutex;
00050 }
00051 
00052 /* Instance creation protocol */
00053 LogWriter *LogWriter::on(String *filename)
00054 {
00055     return LogWriter::on(new Filename(filename));
00056 }
00057 
00058 LogWriter *LogWriter::on(Filename *filename)
00059 {
00060     return new LogWriter(filename->withEncoding("default")->appendStream());
00061 }
00062 
00063 /* Accessing protocol */
00064 Object *LogWriter::next(void)
00065 {
00066     return underlying_stream->next();
00067 }
00068 
00069 void LogWriter::nextPut(Object *object)
00070 {
00071     underlying_stream->nextPut(object);
00072 }
00073 
00074 void LogWriter::nextPut(const char object)
00075 {
00076     underlying_stream->nextPut(object);
00077 }
00078 
00079 void LogWriter::writeLog(LogVerbosityLevel level, Collection *collection)
00080 {
00081     if (level <= _level) {
00082         mutex->wait();
00083         print(Timestamp::now());
00084         nextPutAll(": (");
00085         writeLevel(level);
00086         nextPutAll(") ");
00087         nextPutAll(collection);
00088         lf();
00089         commit();
00090     }
00091 }
00092 
00093 void LogWriter::writeLog(LogVerbosityLevel level, const char *collection)
00094 {
00095     writeLog(level, new String(collection));
00096 }
00097 
00098 void LogWriter::logAll(void)
00099 {
00100     logTraffic();
00101 }
00102 
00103 void LogWriter::logErrors(void)
00104 {
00105     verbosity(LogError);
00106 }
00107 
00108 void LogWriter::logWarnings(void)
00109 {
00110     verbosity(LogWarning);
00111 }
00112 
00113 void LogWriter::logInfos(void)
00114 {
00115     verbosity(LogInfo);
00116 }
00117 
00118 void LogWriter::logNone(void)
00119 {
00120     verbosity(LogNone);
00121 }
00122 
00123 void LogWriter::logTraffic(void)
00124 {
00125     verbosity(LogTraffic);
00126 }
00127 
00128 void LogWriter::logDebug(void)
00129 {
00130     verbosity(LogDebug);
00131 }
00132 
00133 LogVerbosityLevel LogWriter::verbosity(void)
00134 {
00135     return _level;
00136 }
00137 
00138 void LogWriter::verbosity(LogVerbosityLevel level)
00139 {
00140     _level = level;
00141 }
00142 
00143 /* Status protocol */
00144 void LogWriter::close(void)
00145 {
00146     mutex->wait();
00147     commit();
00148     underlying_stream->close();
00149 }
00150 
00151 void LogWriter::commit(void)
00152 {
00153     flush();
00154     underlying_stream->commit();
00155     mutex->signal();
00156 }
00157 
00158 /* Testing protocol */
00159 bool LogWriter::isBinary(void)
00160 {
00161     return false;
00162 }
00163 
00164 /* Private protocol */
00165 void LogWriter::writeLevel(LogVerbosityLevel level)
00166 {
00167     switch (level) {
00168     case LogNone    : nextPutAll("--"); break;
00169     case LogError   : nextPutAll("EE"); break;
00170     case LogWarning : nextPutAll("WW"); break;
00171     case LogInfo    : nextPutAll("II"); break;
00172     case LogTraffic : nextPutAll("=="); break;
00173     case LogDebug   : nextPutAll("DD"); break;
00174     }
00175 }
00176 
00177 SequenceableCollection *LogWriter::contentSpeciesFor(long items)
00178 {
00179     return new String(items);
00180 }

Generated on Mon Nov 27 09:47:55 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2