2005-01-27 03:35:23 +00:00
|
|
|
//This file is part of Photon (http://photon.sourceforge.net)
|
|
|
|
//Copyright (C) 2004-2005 James Turk
|
|
|
|
//
|
|
|
|
// Author:
|
|
|
|
// James Turk (jpt2433@rit.edu)
|
|
|
|
//
|
|
|
|
// Version:
|
2005-02-04 08:11:54 +00:00
|
|
|
// $Id: Log.cpp,v 1.2 2005/02/04 08:11:54 cozman Exp $
|
2005-01-27 03:35:23 +00:00
|
|
|
//
|
|
|
|
// Revisions:
|
|
|
|
// $Log: Log.cpp,v $
|
2005-02-04 08:11:54 +00:00
|
|
|
// Revision 1.2 2005/02/04 08:11:54 cozman
|
|
|
|
// switched Log to shared_ptrs and added extra flushes
|
|
|
|
//
|
2005-01-27 03:35:23 +00:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2005-02-04 08:11:54 +00:00
|
|
|
void Log::addSink(LogSinkPtr sink)
|
2005-01-27 03:35:23 +00:00
|
|
|
{
|
2005-02-04 08:11:54 +00:00
|
|
|
flush();
|
|
|
|
|
|
|
|
for(std::list<LogSinkPtr>::iterator it = sinks_.begin();
|
2005-01-27 03:35:23 +00:00
|
|
|
it != sinks_.end();
|
|
|
|
++it)
|
|
|
|
{
|
|
|
|
if(sink == *it || sink->getName() == (*it)->getName())
|
|
|
|
{
|
2005-02-04 08:11:54 +00:00
|
|
|
throw PreconditionException("Log already contains sink: " +
|
|
|
|
sink->getName());
|
2005-01-27 03:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sinks_.push_back(sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::removeSink(std::string sinkName)
|
|
|
|
{
|
2005-02-04 08:11:54 +00:00
|
|
|
flush();
|
|
|
|
|
|
|
|
for(std::list<LogSinkPtr>::iterator it = sinks_.begin();
|
2005-01-27 03:35:23 +00:00
|
|
|
it != sinks_.end();
|
|
|
|
++it)
|
|
|
|
{
|
|
|
|
if((*it)->getName() == sinkName)
|
|
|
|
{
|
|
|
|
sinks_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-04 08:11:54 +00:00
|
|
|
void Log::removeSink(LogSinkPtr sink)
|
2005-01-27 03:35:23 +00:00
|
|
|
{
|
2005-02-04 08:11:54 +00:00
|
|
|
flush();
|
|
|
|
|
|
|
|
std::list<LogSinkPtr>::iterator it =
|
2005-01-27 03:35:23 +00:00
|
|
|
std::find(sinks_.begin(),sinks_.end(),sink);
|
|
|
|
|
|
|
|
if(it != sinks_.end())
|
|
|
|
{
|
|
|
|
sinks_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::removeSinks()
|
|
|
|
{
|
2005-02-04 08:11:54 +00:00
|
|
|
flush();
|
|
|
|
|
2005-01-27 03:35:23 +00:00
|
|
|
sinks_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::flush()
|
|
|
|
{
|
|
|
|
std::string str = buffer_.str();
|
|
|
|
if(str.length())
|
|
|
|
{
|
2005-02-04 08:11:54 +00:00
|
|
|
for(std::list<LogSinkPtr>::iterator it = sinks_.begin();
|
2005-01-27 03:35:23 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
}
|