/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CSysLog.h  -  description
                             -------------------
    begin                : Tue Jan 31 2006
    copyright            : (C) 2001-2006 by Alexey Parshin
    email                : alexeyp@gmail.com

    This module creation was sponsored by Total Knowledge (http://www.total-knowledge.com).
    Author thanks the developers of CPPSERV project (http://www.total-knowledge.com/progs/cppserv)
    for defining the requirements for this class.
 ***************************************************************************/

/***************************************************************************
   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 __CFILELOG_H__
#define __CFILELOG_H__

#ifndef _WIN32
#include <syslog.h>
#else
#include <windows.h>
#endif

#include <sptk3/CBaseLog.h>
#include <string>

namespace sptk {

   /// @addtogroup log Log Classes
   /// @{

   /// @brief A log stored in the system log.
   ///
   /// On *nix , the log is sent to *nix syslog daemon.
   /// On Windows NT/2000+/XP the log is sent to Event Log (Application).
   /// On Windows 95/98/ME the system log isn't supported..
   /// The facility method allows to define - which system log is used
   /// @see CBaseLog for more information about basic log abilities.
	class CSysLog : public CBaseLog {
		static std::string m_programName;   ///< The name of the program that is used as a prefix in the log
#ifndef _WIN32
		static int         m_objectCounter; ///< The counter of the log objects
		static bool        m_logOpened;     ///< (Unix) Is the log opened?
#else
		HANDLE             m_logHandle;     ///< (Windows) The handle of the log file
		static std::string m_moduleFileName;///< (Windows) The file name of the module 
		static bool        m_registrySet;   ///< (Windows) The flag indicates that registry key for that program name was created
#endif

      /// List of facilities allows to define one or more system logs where messages would be sent
		unsigned           m_facilities;

	protected:

      /// @brief Stores or sends log message to actual destination
      ///
      /// This method should be overwritten by the actual log implementation
      /// @param date CDateTime, message timestamp
      /// @param message const char *, message text
      /// @param sz unsigned, message size
      /// @param priority CLogPriority, message priority. @see CLogPriority for more information.
		virtual void saveMessage(CDateTime date,const char *message,unsigned sz,const CLogPriority *priority) throw(CException);
	public:
      /// @brief Constructor
      ///
      /// Creates a new log object based on the syslog facility (or facilities).
      /// For Windows, parameter facilities is ignored and messages are stored
      /// into Application event log
      /// @param facilities int, log facility or a set of facilities.
		CSysLog(unsigned facilities=LOG_USER);

      /// @brief Destructor
      ///
      /// Destructs the log object, closes the log descriptor, releases all the allocated resources
		virtual ~CSysLog();

      /// @brief Sets program name
      ///
      /// The program name is optional. It is set for all the CSysLog objects at once.
      /// If set, it appears in the log as a message prefix. Every time the program 
      /// name is changed, the log is closed to be re-opened on next message.
      /// @param progName std::string, a program name
		static void programName(std::string progName);
	};	
   /// @}
}
#endif
