// .h file
class NLog
{
private:
std::ostream m_stream;
Nbool m_bInitialized;
// Static NLog objects. One for engine logging and another for application
static NLog m_engineLog;
static NLog m_appLog;
public:
NLog();
~NLog();
// Use this initializer to redirect output to custom ostream
Nerr Initialize(const std::ostream& target);
// Use this initializer to store log in file
Nerr Initialize(Ncstr filename);
Nerr Uninitialize();
Nbool IsInitialized() const { return m_bInitialized; }
void Flush() { m_stream.flush(); }
// Redirect << operator to our stream
template<class T> inline const NLog& operator<<(const T& data)
{
if (m_bInitialized) m_stream << data;
return *this;
}
// Use this functions to access static logs
static NLog& GetEngineLog() { return m_engineLog; }
static NLog& GetAppLog() { return m_appLog; }
};
// .cpp file
#include "core/nLog.h"
#include <fstream>
NLog NLog::m_engineLog;
NLog NLog::m_appLog;
NLog::NLog() : m_stream(0)
{
m_bInitialized = false;
}
NLog::~NLog()
{
if (m_bInitialized)
Uninitialize();
}
Nerr NLog::Initialize(const std::ostream& target)
{
// Share target''s buffer with our''s m_stream
m_stream.copyfmt(target); // copy parameters
m_stream.clear(target.rdstate()); // set same flags
m_stream.rdbuf(target.rdbuf()); // set buffer pointer
m_bInitialized = true;
m_stream << "log created $$date$$\n";
return N_OK;
}
Nerr NLog::Initialize(Ncstr filename)
{
// Create new stream buffer and associate it with file
std::filebuf *fb = new std::filebuf();
fb->open(filename, std::ios::out);
// Set m_stream buffer pointer to created above buffer
m_stream.rdbuf(fb);
m_bInitialized = true;
m_stream << "log created $$date$$\n";
return N_OK;
}
Nerr NLog::Uninitialize()
{
Flush();
m_bInitialized = false;
return N_OK;
}
Have I memory leak hear? I mean in NLog::Initialize(Ncstr filename) I create file buffer using new operator and set this buffer for m_stream. Will m_stream delete this buffer on it''s destruction or I have to delete filebuffer by myself?
STL's I/O
Look at the following code
You''re leaking. rdbuf() with no parameters will return a pointer to the buffer.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
quote:Original post by Fruny
You're leaking. rdbuf() with no parameters will return a pointer to the buffer.
Thanx, but does exist a way to know in my Uninitialize function what method was used to initialize without new variable? I.e. by value of rdbuf() pointer to know if I've created this buffer by myself or I share this buffer with another stream. Maybe there is a some kind of reference counter in stream buffers?
[edited by - nail85 on October 23, 2003 3:24:43 PM]
quote:Original post by nail85
I.e. by value of rdbuf() pointer to know if I''ve created this buffer by myself or I share this buffer with another stream. Maybe there is a some kind of reference counter in stream buffers?
You can check the source code yourself, you know. I, at least, didn''t find anything like that.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement