From 1251daf9fc9b59d0f9b8a0fc8589124f57d67b41 Mon Sep 17 00:00:00 2001 From: James Turk Date: Fri, 4 Feb 2005 08:11:54 +0000 Subject: [PATCH] switched Log to shared_ptrs and added extra flushes --- include/Log.h | 11 +++++++---- include/LogSink.h | 35 +++++++++++++++++++---------------- src/Log.cpp | 46 ++++++++++++++++++++-------------------------- src/LogSink.cpp | 43 ++++++++++++++++++++++++++++--------------- 4 files changed, 74 insertions(+), 61 deletions(-) diff --git a/include/Log.h b/include/Log.h index 600a14e..47ff0fc 100644 --- a/include/Log.h +++ b/include/Log.h @@ -5,10 +5,13 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Log.h,v 1.1 2005/01/27 03:35:23 cozman Exp $ +// $Id: Log.h,v 1.2 2005/02/04 08:11:54 cozman Exp $ // // Revisions: // $Log: Log.h,v $ +// Revision 1.2 2005/02/04 08:11:54 cozman +// switched Log to shared_ptrs and added extra flushes +// // Revision 1.1 2005/01/27 03:35:23 cozman // initial import (exceptions,types, and logging,oh my!) // @@ -45,7 +48,7 @@ public: // // Parameters: // sink - Pointer to to add to Log. - void addSink(LogSink *sink); + void addSink(LogSinkPtr sink); // Function: removeSink // Remove a sink from the log by name. @@ -59,7 +62,7 @@ public: // // Parameters: // sink - Pointer to sink to remove. - void removeSink(LogSink *sink); + void removeSink(LogSinkPtr sink); // Function: removeSinks // Remove all sinks from log. @@ -116,7 +119,7 @@ public: private: std::stringstream buffer_; LogLevel lastLevel_; - std::list sinks_; + std::list sinks_; //assignment left undefined Log(const Log &rhs); diff --git a/include/LogSink.h b/include/LogSink.h index 0a65a5b..61a5636 100644 --- a/include/LogSink.h +++ b/include/LogSink.h @@ -5,10 +5,13 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: LogSink.h,v 1.2 2005/01/27 05:24:11 cozman Exp $ +// $Id: LogSink.h,v 1.3 2005/02/04 08:11:54 cozman Exp $ // // Revisions: // $Log: LogSink.h,v $ +// Revision 1.3 2005/02/04 08:11:54 cozman +// switched Log to shared_ptrs and added extra flushes +// // Revision 1.2 2005/01/27 05:24:11 cozman // minor documentation update // @@ -23,6 +26,8 @@ #include #include +#include "types.h" + namespace photon { @@ -69,10 +74,7 @@ public: // // Parameters: // name_ - Name of LogSink, every LogSink should have a unique name. - // dynamic_ - Tells if the sink is dynamically allocated. - // This flag is false by default, and should be true if Log - // should delete the sink when it is done. - LogSink(std::string name, bool dynamic=false); + LogSink(std::string name); // Function: ~LogSink // Virtual destructor, available to make inheritance safe. @@ -99,23 +101,21 @@ public: // Returns: // Name of the LogSink. std::string getName() const; - - // Function: isDynamic - // Checks if sink is dynamic. - // - // Returns: - // True if log is dynamically allocated, false if not. - bool isDynamic() const; + + virtual std::ostream& getStream()=0; private: std::string name_; - bool dynamic_; //assignment left undefined LogSink(const LogSink&); LogSink& operator=(const LogSink&); }; +// Type: LogSinkPtr +// Pointer to a log sink, used since LogSink is abstract and will always +// be accessed via a pointer. +typedef shared_ptr LogSinkPtr; // Class: ConsoleSink // to be used with for simple console output. @@ -128,10 +128,11 @@ private: class ConsoleSink : public LogSink { public: - ConsoleSink(std::string name, bool dynamic=false); + ConsoleSink(std::string name); virtual ~ConsoleSink(); virtual void writeMessage(LogLevel level, std::string msg); + virtual std::ostream& getStream(); }; // Class: TextSink @@ -145,10 +146,11 @@ public: class TextSink : public LogSink { public: - TextSink(std::string name, bool dynamic=false); + TextSink(std::string name); virtual ~TextSink(); virtual void writeMessage(LogLevel level, std::string msg); + virtual std::ostream& getStream(); private: std::ofstream out_; }; @@ -164,10 +166,11 @@ private: class HTMLSink : public LogSink { public: - HTMLSink(std::string name, bool dynamic=false); + HTMLSink(std::string name); virtual ~HTMLSink(); virtual void writeMessage(LogLevel level, std::string msg); + virtual std::ostream& getStream(); private: std::ofstream out_; }; diff --git a/src/Log.cpp b/src/Log.cpp index 0e0d74c..603f202 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -5,10 +5,13 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Log.cpp,v 1.1 2005/01/27 03:35:24 cozman Exp $ +// $Id: Log.cpp,v 1.2 2005/02/04 08:11:54 cozman Exp $ // // Revisions: // $Log: Log.cpp,v $ +// Revision 1.2 2005/02/04 08:11:54 cozman +// switched Log to shared_ptrs and added extra flushes +// // Revision 1.1 2005/01/27 03:35:24 cozman // initial import (exceptions,types, and logging,oh my!) // @@ -32,16 +35,18 @@ Log::~Log() removeSinks(); } -void Log::addSink(LogSink *sink) +void Log::addSink(LogSinkPtr sink) { - for(std::list::iterator it = sinks_.begin(); + flush(); + + for(std::list::iterator it = sinks_.begin(); it != sinks_.end(); ++it) { if(sink == *it || sink->getName() == (*it)->getName()) { - throw Exception("Log already contains sink: " + - sink->getName()); + throw PreconditionException("Log already contains sink: " + + sink->getName()); } } @@ -50,47 +55,36 @@ void Log::addSink(LogSink *sink) void Log::removeSink(std::string sinkName) { - for(std::list::iterator it = sinks_.begin(); + flush(); + + for(std::list::iterator it = sinks_.begin(); it != sinks_.end(); ++it) { if((*it)->getName() == sinkName) { - if((*it)->isDynamic()) - { - delete *it; - } sinks_.erase(it); } } } -void Log::removeSink(LogSink *sink) +void Log::removeSink(LogSinkPtr sink) { - std::list::iterator it = + flush(); + + std::list::iterator it = std::find(sinks_.begin(),sinks_.end(),sink); if(it != sinks_.end()) { - if((*it)->isDynamic()) - { - delete *it; - } sinks_.erase(it); } } void Log::removeSinks() { - for(std::list::iterator it = sinks_.begin(); - it != sinks_.end(); - ++it) - { - if((*it)->isDynamic()) - { - delete *it; - } - } + flush(); + sinks_.clear(); } @@ -99,7 +93,7 @@ void Log::flush() std::string str = buffer_.str(); if(str.length()) { - for(std::list::iterator it = sinks_.begin(); + for(std::list::iterator it = sinks_.begin(); it != sinks_.end(); ++it) { diff --git a/src/LogSink.cpp b/src/LogSink.cpp index 1eedd4d..c05bbaa 100644 --- a/src/LogSink.cpp +++ b/src/LogSink.cpp @@ -5,10 +5,13 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: LogSink.cpp,v 1.2 2005/01/27 05:24:11 cozman Exp $ +// $Id: LogSink.cpp,v 1.3 2005/02/04 08:11:54 cozman Exp $ // // Revisions: // $Log: LogSink.cpp,v $ +// Revision 1.3 2005/02/04 08:11:54 cozman +// switched Log to shared_ptrs and added extra flushes +// // Revision 1.2 2005/01/27 05:24:11 cozman // minor documentation update // @@ -26,8 +29,8 @@ namespace photon //LogSink -LogSink::LogSink(std::string name, bool dynamic) : - name_(name),dynamic_(dynamic) +LogSink::LogSink(std::string name) : + name_(name) { } @@ -40,15 +43,10 @@ std::string LogSink::getName() const return name_; } -bool LogSink::isDynamic() const -{ - return dynamic_; -} - //ConsoleSink -ConsoleSink::ConsoleSink(std::string name, bool dynamic) : - LogSink(name,dynamic) +ConsoleSink::ConsoleSink(std::string name) : + LogSink(name) { } @@ -67,10 +65,15 @@ void ConsoleSink::writeMessage(LogLevel level, std::string msg) std::cerr << pre[static_cast(level)] << msg << std::endl; } +std::ostream& ConsoleSink::getStream() +{ + return std::cerr; +} + //TextSink -TextSink::TextSink(std::string name, bool dynamic) : - LogSink(name,dynamic), +TextSink::TextSink(std::string name) : + LogSink(name), out_(std::string(name+".txt").c_str()) { } @@ -91,17 +94,22 @@ void TextSink::writeMessage(LogLevel level, std::string msg) out_ << pre[static_cast(level)] << msg << std::endl; } +std::ostream& TextSink::getStream() +{ + return out_; +} + //HTMLSink -HTMLSink::HTMLSink(std::string name, bool dynamic) : - LogSink(name,dynamic), +HTMLSink::HTMLSink(std::string name) : + LogSink(name), out_(std::string(name+".html").c_str()) { out_ << "Error Log\n