switched Log to shared_ptrs and added extra flushes

This commit is contained in:
James Turk 2005-02-04 08:11:54 +00:00
parent 29f2d3c45c
commit 1251daf9fc
4 changed files with 74 additions and 61 deletions

View File

@ -5,10 +5,13 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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: // Revisions:
// $Log: Log.h,v $ // $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 // Revision 1.1 2005/01/27 03:35:23 cozman
// initial import (exceptions,types, and logging,oh my!) // initial import (exceptions,types, and logging,oh my!)
// //
@ -45,7 +48,7 @@ public:
// //
// Parameters: // Parameters:
// sink - Pointer to <LogSink> to add to Log. // sink - Pointer to <LogSink> to add to Log.
void addSink(LogSink *sink); void addSink(LogSinkPtr sink);
// Function: removeSink // Function: removeSink
// Remove a sink from the log by name. // Remove a sink from the log by name.
@ -59,7 +62,7 @@ public:
// //
// Parameters: // Parameters:
// sink - Pointer to sink to remove. // sink - Pointer to sink to remove.
void removeSink(LogSink *sink); void removeSink(LogSinkPtr sink);
// Function: removeSinks // Function: removeSinks
// Remove all sinks from log. // Remove all sinks from log.
@ -116,7 +119,7 @@ public:
private: private:
std::stringstream buffer_; std::stringstream buffer_;
LogLevel lastLevel_; LogLevel lastLevel_;
std::list<LogSink*> sinks_; std::list<LogSinkPtr> sinks_;
//assignment left undefined //assignment left undefined
Log(const Log &rhs); Log(const Log &rhs);

View File

@ -5,10 +5,13 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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: // Revisions:
// $Log: LogSink.h,v $ // $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 // Revision 1.2 2005/01/27 05:24:11 cozman
// minor documentation update // minor documentation update
// //
@ -23,6 +26,8 @@
#include <string> #include <string>
#include <fstream> #include <fstream>
#include "types.h"
namespace photon namespace photon
{ {
@ -69,10 +74,7 @@ public:
// //
// Parameters: // Parameters:
// name_ - Name of LogSink, every LogSink should have a unique name. // name_ - Name of LogSink, every LogSink should have a unique name.
// dynamic_ - Tells if the sink is dynamically allocated. LogSink(std::string name);
// 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);
// Function: ~LogSink // Function: ~LogSink
// Virtual destructor, available to make inheritance safe. // Virtual destructor, available to make inheritance safe.
@ -100,22 +102,20 @@ public:
// Name of the LogSink. // Name of the LogSink.
std::string getName() const; std::string getName() const;
// Function: isDynamic virtual std::ostream& getStream()=0;
// Checks if sink is dynamic.
//
// Returns:
// True if log is dynamically allocated, false if not.
bool isDynamic() const;
private: private:
std::string name_; std::string name_;
bool dynamic_;
//assignment left undefined //assignment left undefined
LogSink(const LogSink&); LogSink(const LogSink&);
LogSink& operator=(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<LogSink> LogSinkPtr;
// Class: ConsoleSink // Class: ConsoleSink
// <LogSink> to be used with <Log> for simple console output. // <LogSink> to be used with <Log> for simple console output.
@ -128,10 +128,11 @@ private:
class ConsoleSink : public LogSink class ConsoleSink : public LogSink
{ {
public: public:
ConsoleSink(std::string name, bool dynamic=false); ConsoleSink(std::string name);
virtual ~ConsoleSink(); virtual ~ConsoleSink();
virtual void writeMessage(LogLevel level, std::string msg); virtual void writeMessage(LogLevel level, std::string msg);
virtual std::ostream& getStream();
}; };
// Class: TextSink // Class: TextSink
@ -145,10 +146,11 @@ public:
class TextSink : public LogSink class TextSink : public LogSink
{ {
public: public:
TextSink(std::string name, bool dynamic=false); TextSink(std::string name);
virtual ~TextSink(); virtual ~TextSink();
virtual void writeMessage(LogLevel level, std::string msg); virtual void writeMessage(LogLevel level, std::string msg);
virtual std::ostream& getStream();
private: private:
std::ofstream out_; std::ofstream out_;
}; };
@ -164,10 +166,11 @@ private:
class HTMLSink : public LogSink class HTMLSink : public LogSink
{ {
public: public:
HTMLSink(std::string name, bool dynamic=false); HTMLSink(std::string name);
virtual ~HTMLSink(); virtual ~HTMLSink();
virtual void writeMessage(LogLevel level, std::string msg); virtual void writeMessage(LogLevel level, std::string msg);
virtual std::ostream& getStream();
private: private:
std::ofstream out_; std::ofstream out_;
}; };

View File

@ -5,10 +5,13 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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: // Revisions:
// $Log: Log.cpp,v $ // $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 // Revision 1.1 2005/01/27 03:35:24 cozman
// initial import (exceptions,types, and logging,oh my!) // initial import (exceptions,types, and logging,oh my!)
// //
@ -32,16 +35,18 @@ Log::~Log()
removeSinks(); removeSinks();
} }
void Log::addSink(LogSink *sink) void Log::addSink(LogSinkPtr sink)
{ {
for(std::list<LogSink*>::iterator it = sinks_.begin(); flush();
for(std::list<LogSinkPtr>::iterator it = sinks_.begin();
it != sinks_.end(); it != sinks_.end();
++it) ++it)
{ {
if(sink == *it || sink->getName() == (*it)->getName()) if(sink == *it || sink->getName() == (*it)->getName())
{ {
throw Exception("Log already contains sink: " + throw PreconditionException("Log already contains sink: " +
sink->getName()); sink->getName());
} }
} }
@ -50,47 +55,36 @@ void Log::addSink(LogSink *sink)
void Log::removeSink(std::string sinkName) void Log::removeSink(std::string sinkName)
{ {
for(std::list<LogSink*>::iterator it = sinks_.begin(); flush();
for(std::list<LogSinkPtr>::iterator it = sinks_.begin();
it != sinks_.end(); it != sinks_.end();
++it) ++it)
{ {
if((*it)->getName() == sinkName) if((*it)->getName() == sinkName)
{ {
if((*it)->isDynamic())
{
delete *it;
}
sinks_.erase(it); sinks_.erase(it);
} }
} }
} }
void Log::removeSink(LogSink *sink) void Log::removeSink(LogSinkPtr sink)
{ {
std::list<LogSink*>::iterator it = flush();
std::list<LogSinkPtr>::iterator it =
std::find(sinks_.begin(),sinks_.end(),sink); std::find(sinks_.begin(),sinks_.end(),sink);
if(it != sinks_.end()) if(it != sinks_.end())
{ {
if((*it)->isDynamic())
{
delete *it;
}
sinks_.erase(it); sinks_.erase(it);
} }
} }
void Log::removeSinks() void Log::removeSinks()
{ {
for(std::list<LogSink*>::iterator it = sinks_.begin(); flush();
it != sinks_.end();
++it)
{
if((*it)->isDynamic())
{
delete *it;
}
}
sinks_.clear(); sinks_.clear();
} }
@ -99,7 +93,7 @@ void Log::flush()
std::string str = buffer_.str(); std::string str = buffer_.str();
if(str.length()) if(str.length())
{ {
for(std::list<LogSink*>::iterator it = sinks_.begin(); for(std::list<LogSinkPtr>::iterator it = sinks_.begin();
it != sinks_.end(); it != sinks_.end();
++it) ++it)
{ {

View File

@ -5,10 +5,13 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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: // Revisions:
// $Log: LogSink.cpp,v $ // $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 // Revision 1.2 2005/01/27 05:24:11 cozman
// minor documentation update // minor documentation update
// //
@ -26,8 +29,8 @@ namespace photon
//LogSink //LogSink
LogSink::LogSink(std::string name, bool dynamic) : LogSink::LogSink(std::string name) :
name_(name),dynamic_(dynamic) name_(name)
{ {
} }
@ -40,15 +43,10 @@ std::string LogSink::getName() const
return name_; return name_;
} }
bool LogSink::isDynamic() const
{
return dynamic_;
}
//ConsoleSink //ConsoleSink
ConsoleSink::ConsoleSink(std::string name, bool dynamic) : ConsoleSink::ConsoleSink(std::string name) :
LogSink(name,dynamic) LogSink(name)
{ {
} }
@ -67,10 +65,15 @@ void ConsoleSink::writeMessage(LogLevel level, std::string msg)
std::cerr << pre[static_cast<int>(level)] << msg << std::endl; std::cerr << pre[static_cast<int>(level)] << msg << std::endl;
} }
std::ostream& ConsoleSink::getStream()
{
return std::cerr;
}
//TextSink //TextSink
TextSink::TextSink(std::string name, bool dynamic) : TextSink::TextSink(std::string name) :
LogSink(name,dynamic), LogSink(name),
out_(std::string(name+".txt").c_str()) out_(std::string(name+".txt").c_str())
{ {
} }
@ -91,17 +94,22 @@ void TextSink::writeMessage(LogLevel level, std::string msg)
out_ << pre[static_cast<int>(level)] << msg << std::endl; out_ << pre[static_cast<int>(level)] << msg << std::endl;
} }
std::ostream& TextSink::getStream()
{
return out_;
}
//HTMLSink //HTMLSink
HTMLSink::HTMLSink(std::string name, bool dynamic) : HTMLSink::HTMLSink(std::string name) :
LogSink(name,dynamic), LogSink(name),
out_(std::string(name+".html").c_str()) out_(std::string(name+".html").c_str())
{ {
out_ << "<html><head><title>Error Log</title>\n<style type=\"text/css\">" out_ << "<html><head><title>Error Log</title>\n<style type=\"text/css\">"
<< std::endl << "<!--" << std::endl << std::endl << "<!--" << std::endl
<< "p { margin: 0 }" << std::endl << "p { margin: 0 }" << std::endl
<< ".note { font-style:italic color:gray }" << std::endl << ".note { font-style:italic color:gray }" << std::endl
<< ".verbose { font-style:italic; background:yellow; font-size:small }" << ".verbose { font-style:italic; font-size:small }"
<< std::endl << std::endl
<< ".warning { font-weight:bold; background:yellow }" << std::endl << ".warning { font-weight:bold; background:yellow }" << std::endl
<< ".error { font-weight:bold; background:orange }" << std::endl << ".error { font-weight:bold; background:orange }" << std::endl
@ -130,4 +138,9 @@ void HTMLSink::writeMessage(LogLevel level, std::string msg)
<< pre[static_cast<int>(level)] << msg << "</p>" << std::endl; << pre[static_cast<int>(level)] << msg << "</p>" << std::endl;
} }
std::ostream& HTMLSink::getStream()
{
return out_;
}
} }