Skip to content

Commit

Permalink
Add verbose console logger for troubleshooting purposes, activated by…
Browse files Browse the repository at this point in the history
… setting an environment variable
  • Loading branch information
codereader committed Apr 23, 2021
1 parent 50611b0 commit b5038de
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
80 changes: 80 additions & 0 deletions test/ConsoleLogger.h
@@ -0,0 +1,80 @@
#pragma once

#include <sstream>
#include <iostream>
#include <vector>
#include <memory>
#include <mutex>
#include "string/replace.h"
#include "ilogwriter.h"

namespace test
{

/**
* The ConsoleLogger writes all messages to the std output streams
*/
class ConsoleLogger :
public applog::ILogDevice
{
private:
applog::LogLevel _currentLevel;
std::string _buffer;

public:
/**
* greebo: This method gets called by the Writer with
* a logging string as argument.
*/
void writeLog(const std::string& outputStr, applog::LogLevel level) override
{
// The text usually arrives in single characters at a time
// In case the level changes, we need to flush the line
if (_currentLevel != level)
{
flushLine();
}

// Write to the buffer first
_currentLevel = level;
_buffer.append(outputStr);

// Once we hit a single newline, flush the line
if (outputStr == "\n")
{
flushLine();
}
}

private:
void flushLine()
{
if (!_buffer.empty())
{
// Replace NULL characters
string::replace_all(_buffer, std::string(1, '\0'), "NULL");

switch (_currentLevel)
{
case applog::LogLevel::Verbose:
std::cout << "[VRB]: " << _buffer;
break;
case applog::LogLevel::Standard:
std::cout << "[STD]: " << _buffer;
break;
case applog::LogLevel::Warning:
std::cout << "[WRN]: " << _buffer;
break;
case applog::LogLevel::Error:
std::cerr << "[ERR]: " << _buffer;
break;
default:
std::cout << "[???]: " << _buffer;
};

_buffer.clear();
}
}
};

} // namespace applog
5 changes: 5 additions & 0 deletions test/RadiantTest.h
Expand Up @@ -11,6 +11,7 @@

#include "TestContext.h"
#include "TestLogFile.h"
#include "ConsoleLogger.h"
#include "HeadlessOpenGLContext.h"
#include "module/CoreModule.h"
#include "messages/GameConfigNeededMessage.h"
Expand Down Expand Up @@ -39,6 +40,7 @@ class RadiantTest :
std::shared_ptr<gl::HeadlessOpenGLContextModule> _glContextModule;

std::unique_ptr<TestLogFile> _testLogFile;
std::unique_ptr<ConsoleLogger> _consoleLogger;

protected:
RadiantTest()
Expand Down Expand Up @@ -144,6 +146,9 @@ class RadiantTest :
auto fullPath = _context.getCacheDataPath() + "test.log";
_testLogFile.reset(new TestLogFile(fullPath));
_coreModule->get()->getLogWriter().attach(_testLogFile.get());

_consoleLogger.reset(new ConsoleLogger);
_coreModule->get()->getLogWriter().attach(_consoleLogger.get());
}

virtual void setupGameFolder()
Expand Down

0 comments on commit b5038de

Please sign in to comment.