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

#ifndef FILELOGREADER_H_
#define FILELOGREADER_H_

#include "LogReader.h"

#include "io/FileInStream.h"

/**
 *  Wraps around another LogReader to read LogEntries to a file
 *
 *  Designed around assumption that it always has a log file open, though
 *  it does keep track of whether the stream is open or close, which it uses
 *  to determine if it's valid.
 *
 *  \ingroup logging
 */
class FileLogReader : public LogReader
{
public:

    /** Constructor
     *
     * \param filebase Base of name of file to read to
     * \param logReader logReader that provides input for file logging
     */
    FileLogReader( const Str& filebase, LogReader* logReader );

    /// Destructor, closes file if open.
    virtual ~FileLogReader();

    /// Is the LogReader valid?
    virtual bool valid( void )
    {
        return isReadable();
    };

    /// Read
    virtual unsigned int read();

    /// Accessors
    unsigned int getEntriesRead( void )
    {
        return entriesRead_;
    };

    /// Uses nextFilenum_ to determine the next filename
    Str nextFilename();

    /// Keep track of whether the stream is open
    bool isReadable( void )
    {
        return fileStream_.isReadable();
    };

    static void SetPriorityList( FlexArray<const char*>* priorityList )
    {
        PriorityList_ = priorityList;
    }

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

    // Using .bak extensions and PriorityList_ entries, open the indicated file,
    // returning the actual filename opened.
    Str matchAndOpen( const Str& filename );

    // Read log entries from file, if needed, step forward byte-by-byte until we find
    // valid data
    unsigned int readEntries();

    /// Name of the file being read.
    Str filebase_;

    /// The input file handle
    FILE* file_;

    /// The input file stream.
    FileInStream fileStream_;

    /// Total entries read
    unsigned int entriesRead_;

    /// The LogReader that does the actual writing
    LogReader* logReader_;

    /// Number of the next split file
    unsigned int nextFilenum_;

    static FlexArray<const char*>* PriorityList_;

};

#endif /*FILELOGREADER_H_*/
