TracerUtils.h

// TracerUtils.h
//
// Author Jesse Pepper
// (C)opyright Cedanet Pty Ltd 2007

#pragma once
#ifndef Ceda_cxUtils_TracerUtils_H
#define Ceda_cxUtils_TracerUtils_H

#include "Tracer.h"
#include "MsWindows.h"

namespace ceda
{

class TraceGroup
{
public:
    TraceGroup(const xstring& name, const ssize_t indentSpaces = 4) :
	    m_indent(indentSpaces)
	{
		Tracer() << name << "\n{\n";
		IndentTrace(m_indent);
	}

	TraceGroup(ConstStringZ name, const ssize_t indentSpaces = 4) :
	    m_indent(indentSpaces)
	{
		Tracer() << name << "\n{\n";
		IndentTrace(m_indent);
	}
	
	~TraceGroup()
	{
		IndentTrace(-m_indent);
		Tracer() << "}\n";
	}
private:	
	ssize_t m_indent;
};

#ifdef _WIN32
struct DebugStringTraceObserver : public ITraceObserver
{
    virtual void OnTrace(const xstring& str)
    {
        cxWIN32::OutputDebugStringW(as_LPCWSTR(str));
    }
};
#endif

struct StdoutTraceObserver : public ITraceObserver
{
    virtual void OnTrace(const xstring& str)
    {
        WriteToStdout( str.data(), str.size() );
    }
};

struct CompositeTraceObserver : public ITraceObserver, public xvector< ITraceObserver* >
{
    virtual void OnTrace(const xstring& str)
    {
        for( xvector< ITraceObserver* >::iterator itr( begin() ); itr != end(); ++itr )
        {
            (*itr)->OnTrace( str );
        }
    }
};

} //ns
#endif // include guard