Duplicate logging symbols :(

This commit is contained in:
Eryn Wells 2014-08-02 00:27:50 -07:00
parent 0346e21ebd
commit 3ab7c92d36
3 changed files with 125 additions and 98 deletions

View file

@ -20,6 +20,7 @@ files = [
'basics.cc',
'camera.cc',
'light.cc',
'log.cc',
'material.cc',
'object.cc',
'objectBox.cc',

119
src/log.cc Normal file
View file

@ -0,0 +1,119 @@
/* log.cc
* vim: set tw=80:
* Eryn Wells <eryn@erynwells.me>
*/
#include "log.hh"
namespace charles {
namespace log {
/** Useful predefined levels. */
namespace level {
const unsigned int Error = 10;
const unsigned int Warning = 20;
const unsigned int Info = 30;
const unsigned int Debug = 40;
const unsigned int Trace = 50;
};
unsigned int Log::sLevel = 0;
std::ostream* Log::sOutput = nullptr;
Log::LoggerMap Log::sLoggers;
/* static */ void
Log::Init(const std::string& filename,
unsigned int level)
{
assert(sOutput == nullptr);
sOutput = new std::ofstream(filename);
sLevel = level;
Log("ROOT", 1) << "Opening log file " << filename;
Log("ROOT", 1) << "Log level set to " << sLevel;
}
/* static */ void
Log::Close()
{
assert(sOutput != nullptr);
delete sOutput;
sOutput = nullptr;
}
/**
* Construct a Log object.
*
* @param [in] name The name of the log stream. If this name hasn't been
* seen before, a new one will be created for you.
* @param [in] level The level. If this is higher than the level of the log
* stream, nothing will be output.
*/
Log::Log(const std::string& name,
unsigned int level)
: mName(name),
mLevel(level),
mOutput(Log::GetLogger(name))
{
using namespace std::chrono;
/* Write a log message leader: "<time with millis> - <name>:<level> - ". */
auto now = system_clock::now();
auto nowMillis =
duration_cast<milliseconds>(now.time_since_epoch() % seconds(1));
auto cNow = system_clock::to_time_t(now);
*this << std::put_time(std::localtime(&cNow), "%F %T")
<< "." << std::left << std::setw(3) << nowMillis.count()
<< " - " << mName
<< ":" << std::left << std::setw(2) << mLevel << " - ";
}
Log::~Log()
{
/* Add a newline at the end of this log message. */
*this << "\n";
}
/* static */ Log::Logger&
Log::GetLogger(const std::string& name)
{
/*
* TODO: For now, output is always the same: sOutput. In the future, figure
* out a way to set different outputs for different streams.
*/
auto pair = sLoggers.emplace(name, sLevel);
return pair.first->second;
}
#pragma mark Log::Logger
Log::Logger::Logger(unsigned int l)
: level(l)
{ }
#pragma mark Tracer
Tracer::Tracer(const std::string& name,
const std::string& function)
: mName(name),
mFunction(function)
{
Log(mName, level::Trace) << "--> " << mFunction;
}
Tracer::~Tracer()
{
Log(mName, level::Trace) << "<-- " << mFunction;
}
} /* namespace log */
} /* namespace charles */

View file

@ -21,11 +21,11 @@ namespace log {
/** Useful predefined levels. */
namespace level {
unsigned int Error = 10;
unsigned int Warning = 20;
unsigned int Info = 30;
unsigned int Debug = 40;
unsigned int Trace = 50;
extern const unsigned int Error;
extern const unsigned int Warning;
extern const unsigned int Info;
extern const unsigned int Debug;
extern const unsigned int Trace;
};
@ -74,67 +74,6 @@ private:
};
unsigned int Log::sLevel = 0;
std::ostream* Log::sOutput = nullptr;
Log::LoggerMap Log::sLoggers;
/* static */ void
Log::Init(const std::string& filename,
unsigned int level)
{
assert(sOutput == nullptr);
sOutput = new std::ofstream(filename);
sLevel = level;
Log("ROOT", 1) << "Opening log file: " << filename;
Log("ROOT", 1) << "Level set to " << sLevel;
}
/* static */ void
Log::Close()
{
assert(sOutput != nullptr);
delete sOutput;
sOutput = nullptr;
}
/**
* Construct a Log object.
*
* @param [in] name The name of the log stream. If this name hasn't been
* seen before, a new one will be created for you.
* @param [in] level The level. If this is higher than the level of the log
* stream, nothing will be output.
*/
Log::Log(const std::string& name,
unsigned int level)
: mName(name),
mLevel(level),
mOutput(Log::GetLogger(name))
{
using namespace std::chrono;
/* Write a log message leader: "<time with millis> - <name>:<level> - ". */
auto now = system_clock::now();
auto nowMillis =
duration_cast<milliseconds>(now.time_since_epoch() % seconds(1));
auto cNow = system_clock::to_time_t(now);
*this << std::put_time(std::localtime(&cNow), "%F %T") << "."
<< nowMillis.count()
<< " - " << mName << ":" << mLevel << " - ";
}
Log::~Log()
{
/* Add a newline at the end of this log message. */
*this << "\n";
}
template<typename T>
Log&
Log::operator<<(const T& item)
@ -147,38 +86,6 @@ Log::operator<<(const T& item)
}
/* static */ Log::Logger&
Log::GetLogger(const std::string& name)
{
/*
* TODO: For now, output is always the same: sOutput. In the future, figure
* out a way to set different outputs for different streams.
*/
auto pair = sLoggers.emplace(name, sLevel);
return pair.first->second;
}
Log::Logger::Logger(unsigned int l)
: level(l)
{ }
#pragma mark Tracer
Tracer::Tracer(const std::string& name,
const std::string& function)
: mName(name),
mFunction(function)
{
Log(mName, level::Trace) << "--> " << mFunction;
}
Tracer::~Tracer()
{
Log(mName, level::Trace) << "<-- " << mFunction;
}
} /* namespace log */
} /* namespace charles */