/*
 * hattrick.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2007
 *
 * Copyright: See COPYING file that comes with this distribution
 */

/*
 * The following ___Logger class has been found on: 
 * http://www.velocityreviews.com/forums/t283570-p-logging-function.html
 */

#ifndef ___HATTRICK_DEBUG_H
#define ___HATTRICK_DEBUG_H

#include "settings.h"
#include <iostream>
#include <sstream>
#include <pthread.h>
#include <sys/time.h>

static
pthread_mutex_t ___logger_mutex = PTHREAD_MUTEX_INITIALIZER;

class ___Logger
{
public:

    std::stringstream* buffer;
    struct timeval now;
    struct tm *logtime;

    ___Logger ( void ) :
            buffer( new std::stringstream )
    {}

    ~___Logger ( void )
    {

        if(cSettings::GetInstance( ).IsDebugmode( ) )
        {
            gettimeofday(&now, NULL);
            logtime = localtime(&now.tv_sec);

            pthread_mutex_lock( &___logger_mutex);

            std::cerr << "HATTRICK_DEBUG (" <<
            logtime->tm_hour << ":" << logtime->tm_min << ":" << logtime->tm_sec <<
            buffer->str() << std::endl;

            pthread_mutex_unlock( &___logger_mutex );
            delete buffer;
        }
    }

}
; // ___Logger

template< typename T>
const ___Logger& operator<< ( const ___Logger& io, const T& t )
{
    *io.buffer << t;
    return( io );
}

#define rlog ( ___Logger() << ": vdr-hattrick, " << __FILE__ << ", " << __LINE__ << "): " )


#endif // ___HATTRICK_DEBUG_H
