Refactored logging system using iostream

This commit is contained in:
Michaël Lemaire 2015-08-13 00:33:16 +02:00
parent a8ae50df94
commit 46e9e73793
9 changed files with 87 additions and 15 deletions

View file

@ -61,7 +61,7 @@ DefinitionNode* Layers::getLayer(int position) const
}
else
{
logWarning("Asked for a undefined layer %d on a total of %d", position, (int)layers.size());
Logs::warning() << "Asked for a undefined layer " << position << " on a total of " << (int)layers.size() << std::endl;
return null_layer;
}
}
@ -77,7 +77,7 @@ int Layers::findLayer(DefinitionNode* layer) const
}
i++;
}
logWarning("Layer %p (%s) not found, on a total of %d", layer, layer->getName().c_str(), (int)layers.size());
Logs::warning() << "Layer " << layer << " (" << layer->getName() << " not found, on a total of " << (int)layers.size() << std::endl;
return -1;
}
@ -91,7 +91,7 @@ int Layers::addLayer(DefinitionNode* layer)
}
else
{
logWarning("Add layer ignored because limit of %d reached", max_layer_count);
Logs::warning() << "Add layer ignored because limit of " << max_layer_count << " reached" << std::endl;
delete layer;
return -1;
}
@ -113,7 +113,7 @@ void Layers::removeLayer(int position)
}
else
{
logWarning("Removing unknown layer %d on %d from '%s'", position, (int)layers.size(), getName().c_str());
Logs::warning() << "Removing unknown layer " << position << " on " << (int)layers.size() << " from '" << getName() << "'" << std::endl;
}
}

View file

@ -19,7 +19,7 @@ MainModelerWindow::MainModelerWindow()
scenery = new Scenery();
scenery->autoPreset();
logDebug("Initialized scenery:\n%s", QString::fromStdString(scenery->toString()).toLatin1().data());
Logs::debug() << "Initialized scenery:\n" << scenery->toString() << std::endl;
renderer = new OpenGLRenderer(scenery);

View file

@ -72,7 +72,7 @@ void OpenGLRenderer::initialize()
}
else
{
logError("Failed to initialize OpenGL bindings");
Logs::error() << "Failed to initialize OpenGL bindings" << std::endl;
}
}
@ -140,7 +140,7 @@ void OpenGLRenderer::paint()
int error_code;
while ((error_code = glGetError()) != GL_NO_ERROR)
{
logWarning("[OpenGL] ERROR : %s", (const char*)gluErrorString(error_code));
Logs::warning() << "[OpenGL] ERROR : " << (const char*)gluErrorString(error_code) << std::endl;
}
}
}

View file

@ -33,7 +33,7 @@ void OpenGLShaderProgram::addVertexSource(QString path)
}
else
{
logError() << "Can't open vertex file " << file.fileName();
Logs::error() << "Can't open vertex file " << file.fileName().toStdString() << std::endl;
}
}
@ -46,7 +46,7 @@ void OpenGLShaderProgram::addFragmentSource(QString path)
}
else
{
logError() << "Can't open fragment file " << file.fileName();
Logs::error() << "Can't open fragment file " << file.fileName().toStdString() << std::endl;
}
}

View file

@ -23,7 +23,7 @@ std::string DataFile::findDir(const std::string &relpath)
bool DataFile::tryDataDir(const QDir &dir)
{
logDebug("[System] Try data dir %s", dir.absolutePath().toLocal8Bit().data());
Logs::debug() << "[System] Try data dir " << dir.absolutePath().toStdString() << std::endl;
return dir.exists("data/.paysages_data");
}
@ -58,13 +58,13 @@ std::string DataFile::initDataDir()
std::string parent = locateDataDir();
if (parent.empty())
{
logWarning("[System] Data files not found");
Logs::warning() << "[System] Data files not found" << std::endl;
return parent;
}
else
{
std::string result = QDir(QString::fromStdString(parent)).absoluteFilePath("data").toStdString();
logDebug("[System] Data files found : %s", result.c_str());
Logs::debug() << "[System] Data files found : " << result << std::endl;
return result;
}
}

View file

@ -1 +1,52 @@
#include "Logs.h"
static std::ostream NULL_STREAM(0);
static bool enabled = true;
std::ostream &Logs::debug()
{
#ifdef NDEBUG
return NULL_STREAM;
#else
if (enabled)
{
std::cout << "DEBUG - ";
return std::cout;
}
else
{
return NULL_STREAM;
}
#endif
}
std::ostream &Logs::warning()
{
if (enabled)
{
std::cerr << "WARN - ";
return std::cerr;
}
else
{
return NULL_STREAM;
}
}
std::ostream &Logs::error()
{
if (enabled)
{
std::cerr << "ERROR - ";
return std::cerr;
}
else
{
return NULL_STREAM;
}
}
void Logs::disable()
{
enabled = false;
}

View file

@ -3,8 +3,26 @@
#include "system_global.h"
#define logDebug qDebug
#define logWarning qWarning
#define logError qCritical
#include <iostream>
namespace paysages {
namespace system {
/**
* Logger streams
*/
class Logs
{
public:
static std::ostream &debug();
static std::ostream &warning();
static std::ostream &error();
// Disable all logs from now on
static void disable();
};
}
}
#endif // LOGS_H

View file

@ -14,6 +14,7 @@
namespace paysages {
namespace system {
class Logs;
class PackStream;
class ParallelWork;
class ParallelPool;

View file

@ -1,6 +1,7 @@
#include "BaseTestCase.h"
#include <QtCore/QDebug>
#include "Logs.h"
void noMessageOutput(QtMsgType, const QMessageLogContext&, const QString&)
{
@ -11,6 +12,7 @@ int main(int argc, char **argv)
int result;
qInstallMessageHandler(noMessageOutput);
Logs::disable();
testing::InitGoogleTest(&argc, argv);
result = RUN_ALL_TESTS();