switched Log to shared_ptrs and added extra flushes
This commit is contained in:
parent
29f2d3c45c
commit
1251daf9fc
@ -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 <LogSink> 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<LogSink*> sinks_;
|
||||
std::list<LogSinkPtr> sinks_;
|
||||
|
||||
//assignment left undefined
|
||||
Log(const Log &rhs);
|
||||
|
@ -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 <string>
|
||||
#include <fstream>
|
||||
|
||||
#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.
|
||||
@ -100,22 +102,20 @@ public:
|
||||
// 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<LogSink> LogSinkPtr;
|
||||
|
||||
// Class: ConsoleSink
|
||||
// <LogSink> to be used with <Log> 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_;
|
||||
};
|
||||
|
46
src/Log.cpp
46
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<LogSink*>::iterator it = sinks_.begin();
|
||||
flush();
|
||||
|
||||
for(std::list<LogSinkPtr>::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<LogSink*>::iterator it = sinks_.begin();
|
||||
flush();
|
||||
|
||||
for(std::list<LogSinkPtr>::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<LogSink*>::iterator it =
|
||||
flush();
|
||||
|
||||
std::list<LogSinkPtr>::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<LogSink*>::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<LogSink*>::iterator it = sinks_.begin();
|
||||
for(std::list<LogSinkPtr>::iterator it = sinks_.begin();
|
||||
it != sinks_.end();
|
||||
++it)
|
||||
{
|
||||
|
@ -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<int>(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<int>(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_ << "<html><head><title>Error Log</title>\n<style type=\"text/css\">"
|
||||
<< std::endl << "<!--" << std::endl
|
||||
<< "p { margin: 0 }" << 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
|
||||
<< ".warning { font-weight:bold; background:yellow }" << 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;
|
||||
}
|
||||
|
||||
std::ostream& HTMLSink::getStream()
|
||||
{
|
||||
return out_;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user