Skip to content

Commit

Permalink
add support for OutputDebugString on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
fakechris committed Sep 28, 2011
1 parent 2167514 commit 7e4552b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cpplog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include <ctime>
#include <vector>

#ifdef _WIN32
#include "outputdebugstram.hpp"
#endif

// The following #define's will change the behaviour of this library.
// #define CPPLOG_FILTER_LEVEL <level>
// Prevents all log messages with level less than <level> from being emitted.
Expand Down Expand Up @@ -382,6 +386,17 @@ namespace cpplog
}
};

#ifdef _WIN32
class OutputDebugStringLogger : public OstreamLogger
{
private:
dbgwin_stream m_stream;
public:
OutputDebugStringLogger() : OstreamLogger(m_stream)
{ }
};
#endif

// Log to file.
class FileLogger : public OstreamLogger
{
Expand Down
45 changes: 45 additions & 0 deletions outputdebugstram.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once
#ifndef _OUTPUT_DEBUG_STREAM_H
#define _OUTPUT_DEBUG_STREAM_H

#include <windows.h>

template <class Elem>
class outputdebug_buf: public std::basic_stringbuf<Elem>
{
public:
virtual int sync ( )
{
output_debug_string(str().c_str());
return 0;
}
private:
void output_debug_string(const Elem* e);
};

template<>
void outputdebug_buf<char>::output_debug_string(const char* e)
{
::OutputDebugStringA(e);
}

template<>
void outputdebug_buf<wchar_t>::output_debug_string(const wchar_t* e)
{
::OutputDebugStringW(e);
}

template <class _Elem>
class outputdebug_stream: public std::basic_ostream<_Elem>
{
public:
outputdebug_stream() : std::basic_ostream<_Elem>(new outputdebug_buf<_Elem>())
{}

~outputdebug_stream(){ delete rdbuf(); }
};

typedef outputdebug_stream<char> dbgwin_stream;
typedef outputdebug_stream<wchar_t> wdbgwin_stream;

#endif

0 comments on commit 7e4552b

Please sign in to comment.