/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          clogfile.h  -  description
                             -------------------
    begin                : Fri Jul 20 2001
    copyright            : (C) 2001-2005 by Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at 
   your option) any later version.

   This library is distributed in the hope that it will be useful, but 
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#ifndef __CLOGFILE_H__
#define __CLOGFILE_H__

#include <stdio.h>
#include <time.h>

#include <sptk3/sptk.h>
#include <sptk3/CStrings.h>
#include <sptk3/CWaiter.h>

/// @addtogroup utility Utility Classes
/// @{

/// @brief Simple log file. 
///
/// Prints log information into the file.
/// Every row of information has a time mark.
/// Can print the strings and binary buffers.
class SP_EXPORT CLogFile : protected CWaiter {
   bool     	 m_autoflush;	///< Auto flush buffers file
   bool     	 m_active;	   ///< Is the log active?
   std::string  m_filename;	///< Output file name
   FILE        *m_file;		   ///< Output file descriptor

   /// Checks if the output file is open
   bool     checkOpen();
   /// Prints time to the output file
   void     printTime();

public:

   /// Constructor
   /// @param logfile CString, log file name
   /// @param autoFlush bool, auto flush buffers on disk
   /// @param act bool, true to activate the log
   CLogFile(std::string logfile,bool autoFlush=true,bool act=true);

   /// Destructor
   ~CLogFile();

   /// Activates or deactivates log. Inactive log ignores print commands.
   /// @param act bool, true to activate the log
   void active(bool act);

   /// Returns true if the log is active
   bool active() { return m_active; }

   /// Truncates the log to zero length
   void reset();

   /// Flushes the log into the file
   void flush();

   /// Prints a string to the log
   /// @param str std::string, string to print
   void writeString(std::string str);

   /// Prints a buffer to the log
   /// @param label const char *, buffer information label
   /// @param data const char *, buffer data
   /// @param len int, buffer length
   /// @param printCheckSum bool, print a checksum
   void writeBinaryBuffer(const char *label,const void *data,int len,bool printCheckSum);

   /// @brief Returns output file name
   std::string fileName() const { return m_filename; }
};

/// @}

#endif
