cpp_photon/src/Log.cpp

150 lines
2.5 KiB
C++
Raw Normal View History

//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Log.cpp,v 1.1 2005/01/27 03:35:24 cozman Exp $
//
// Revisions:
// $Log: Log.cpp,v $
// Revision 1.1 2005/01/27 03:35:24 cozman
// initial import (exceptions,types, and logging,oh my!)
//
//
#include "Log.h"
#include <algorithm>
#include "exceptions.h"
namespace photon
{
Log::Log()
{
}
Log::~Log()
{
flush();
removeSinks();
}
void Log::addSink(LogSink *sink)
{
for(std::list<LogSink*>::iterator it = sinks_.begin();
it != sinks_.end();
++it)
{
if(sink == *it || sink->getName() == (*it)->getName())
{
throw Exception("Log already contains sink: " +
sink->getName());
}
}
sinks_.push_back(sink);
}
void Log::removeSink(std::string sinkName)
{
for(std::list<LogSink*>::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)
{
std::list<LogSink*>::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;
}
}
sinks_.clear();
}
void Log::flush()
{
std::string str = buffer_.str();
if(str.length())
{
for(std::list<LogSink*>::iterator it = sinks_.begin();
it != sinks_.end();
++it)
{
(*it)->writeMessage(lastLevel_,str);
}
buffer_.str("");
}
}
std::ostream& Log::note()
{
flush();
lastLevel_ = LOG_NOTE;
return buffer_;
}
std::ostream& Log::verbose()
{
flush();
lastLevel_ = LOG_VERBOSE;
return buffer_;
}
std::ostream& Log::warning()
{
flush();
lastLevel_ = LOG_WARNING;
return buffer_;
}
std::ostream& Log::error()
{
flush();
lastLevel_ = LOG_ERROR;
return buffer_;
}
std::ostream& Log::critical()
{
flush();
lastLevel_ = LOG_CRITICAL;
return buffer_;
}
Log log;
}