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

#include "DirectoryLogWriter.h"
#include "data/Slate.h"
#include "io/OutStream.h"
#include "logger/DirectoryEntry.h"

/// Static initialization
bool DirectoryLogWriter::Initialized_( false );
bool DirectoryLogWriter::Restarted_( false );

/// Default Constructor
DirectoryLogWriter::DirectoryLogWriter( bool forceInitialized )
    : BinaryLogWriter( true ),
      initialized_( false )
{
    if( forceInitialized )
    {
        Initialized_ = initialized_ = true;
    }
}

/// Destructor
DirectoryLogWriter::~DirectoryLogWriter()
{}

unsigned int DirectoryLogWriter::writeHeader()
{
    unsigned int bytesWritten( 0 );
    bytesWritten += BinaryLogWriter::writeHeader();

    if( !Initialized_ )
    {
        Slate::WriteDirectory( NULL, Restarted_ );
        Initialized_ = true;
    }
    else if( initialized_ )
    {
        // If we get here, this is the 2nd, 3rd, 4th... call to writeHeader
        Slate::WriteDirectory( this, Restarted_ );
        Restarted_ = false;
    }

    initialized_ = true;
    return bytesWritten;
}

void DirectoryLogWriter::Uninitialize()
{
    Initialized_ = false;
    Restarted_ = true;
}
