/** \file
 *
 *  Contains the LogWriter class definition.
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 *
 */

#ifndef LOGWRITER_H_
#define LOGWRITER_H_

#include "LogEntry.h"
#include "utils/AuvMath.h"
#include "utils/FlexArray.h"
#include "utils/Mutex.h"

class OutStream;
class Unit;

/// Base class for a generalized set of "log data destinations," including
/// to files (text or binary), over communications links, etc.
///
/// \ingroup logging
class LogWriter
{
public:

    /// Destructor
    virtual ~LogWriter();

    /// Write the log header, if any
    virtual unsigned int writeHeader()
    {
        return 0;
    }

    /// Sets the OutStream
    virtual void setOutStream( OutStream& outStream );

    /// Return true if logStream_ is available.
    virtual bool isValid();

    /// Write the given LogEntry - should lock the writeMutex_
    virtual unsigned int write( const LogEntry* entry, const Unit* unit = NULL ) = 0;

    /// Do what needs to be done at the end of the cycle.
    virtual void endCycle() {}

    /// Write the log footer, if any
    virtual unsigned int writeFooter()
    {
        return 0;
    }

    /// Configure from a config file
    virtual void config( Logger& logger ) {}

    /// Reset cached input settings
    virtual void resetInputs() {}

    void addParent()
    {
        ++parentCount_;
    }

    void removeParent()
    {
        --parentCount_;
    }

    int getParentCount()
    {
        return parentCount_;
    }

    virtual void activate()
    {
        active_ = true;
    }

    virtual void deactivate()
    {
        active_ = false;
    }

    virtual bool isActive()
    {
        return active_;
    }

    virtual void lockWrites()
    {
        writeMutex_.lock();
    }

    virtual void unlockWrites()
    {
        writeMutex_.unlock();
    }

protected:

    /// Protected constructor for pure virtual class
    LogWriter();

    /// Protected constructor for pure virtual class
    LogWriter( OutStream& outStream );

    /// Stores an OutStream.
    OutStream* logStream_;

    bool wroteHeader_;

    bool wroteFooter_;

    Mutex writeMutex_;

    int parentCount_;

    bool active_;

private:
    // Note that the copy constructor below is private and not given a body.
    // Any attempt to call it will return a compiler error.
    LogWriter( const LogWriter& old ); // disallow copy constructor

};

#endif /*LOGWRITER_H_*/
