Hello,
I'm trying to make a wrapper for Ogre (an open source 3D engine) 's standard logging class. I want it to have the same syntax as std::cerr
, and also output to cerr when running on Linux. Here's what I have:
#ifndef _LOGGER_H_
#define _LOGGER_H_
#ifndef _XSTRING_
#include <xstring>
#endif
#ifndef __LogManager_H__
#include "OgreLogManager.h"
#endif
class Logger
{
public:
static Logger* m_Instance;
static Logger* getInstance() { return m_Instance; }
static const Logger& getInstanceConst() { return *m_Instance; }
Logger& operator << (const std::string& a_Message)
{
m_Log.append(a_Message);
_CheckNewLine();
return *m_Instance;
}
Logger& operator << (const char* a_Message)
{
m_Log += a_Message;
_CheckNewLine();
return *m_Instance;
}
private:
std::string m_Log;
Logger()
{
m_Log = "";
}
void _CheckNewLine()
{
if (m_Log.at(m_Log.size() - 1) == '\n')
{
Ogre::LogManager::getSingleton().logMessage(m_Log);
#if OGRE_PLATFORM != PLATFORM_WIN32 && OGRE_PLATFORM != OGRE_PLATFORM_WIN32
std::cerr << m_Log;
#endif
m_Log.clear();
}
}
};
#endif
Now, this works fine, and this singleton is instanced in a .cpp:
#include "logger.h"
Logger* Logger::m_Instance = new Logger();
The problem comes when I want to use the singleton in multiple headers. I instantiate it in game3d.h
, which is included by pretty much all the headers like this:
Logger awesomelogger = Logger::getInstance();
Unfortunately, this gives multiple errors about headers trying to redeclare awesomelogger
.
I want to make it a const, which would make that go away, but that introduces new errors. This is what I tried:
friend Logger& operator << (const Logger& a_Logger, const std::string& a_Message)
{
a_Logger.m_Log.append(a_Message);
a_Logger._CheckNewLine();
return *m_Instance;
}
My question is: how can I make an instance of this class constant OR how can I rewrite this class but still be able to do awesomelogger << "output" << s_Stuff << "\n";