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


#ifndef JSONLOGWRITER_H_
#define JSONLOGWRITER_H_

#include "data/BinaryDataType.h"
#include "logger/FilterLogWriter.h"
#include "logger/HasFilename.h"
#include "utils/Str.h"
#include "../Lib/cJSON/cJSON.h"

class DataValue;

/**
 *  Defines a LogWriter that generates a JSON table of values.  A list of
 *  variable names is supplied to the constructor.
 *
 *  \ingroup logging
 */
class JsonLogWriter : public FilterLogWriter
{
public:

    /// Constructor.
    JsonLogWriter( bool formatted, int varCount, char** varList, char** unitList,
                   int attCount = 0, char** attList = NULL );

    /// Destructor.
    virtual ~JsonLogWriter();

    /// Start writing the Json file
    unsigned int writeHeader();

    /// Write an entry to the buffer.
    unsigned int write( const LogEntry *entry, const Unit* unit = NULL );

    /// Finish writing the Json file
    unsigned int writeFooter();


protected:

    // Override this method to do something special with data entries
    virtual unsigned int writeData( const DataEntry *dataEntry, int index, const Unit* unit );

    // Override this method to do something special with attribute entries
    virtual unsigned int writeAtt( const DataEntry *dataEntry, int index, const Unit* unit );

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

    /// Initialize variables, etc
    void initializeJsonLogWriter();

    /// Add a new variable
    void addVar( const int index, const ElementURI* uri,
                 BinaryDataType binaryType, const Unit& unit,
                 short universalCode );

    /// Set an attribute value
    void setAttValue( const DataEntry *dataEntry, const int attIndex );

    cJSON* json_;
    bool jsonLogWriterInitialized_;
    bool finalized_;
    bool formatted_;
    cJSON** vars_;
    cJSON** timeVars_;
    const Unit** units_;
    const ElementURI** uris_;
};

#endif /*JSONLOGWRITER_H_*/
