#ifndef _KLOG_H_
#define _KLOG_H_

#include <log4cpp/Category.hh>
#include <ostream>

class cLogOStream;

class cLogBuffer : public std::basic_streambuf<char, std::char_traits<char> >
{
private:
   cLogOStream & mStream;
   char mLogBuffer[1024];

public:
   cLogBuffer(cLogOStream &p) : mStream(p)
   {
      setp(&mLogBuffer[0],&mLogBuffer[1023]);
   }
protected:
   virtual int sync();
};


class cLogOStream : public std::basic_ostream< char, std::char_traits< char > >
{
private:
   cLogBuffer mBuffer;
public:
   cLogOStream() : mBuffer(*this)
    {
       init(&mBuffer);
    }
   virtual void logOutput(char *str) = 0;
};


class cLog : public cLogOStream
{
public:
   cLog(const char * log_name) : mLogName(log_name)
   {

   }
   virtual void logOutput(char *str)
   {
      log4cpp::Category & cat = log4cpp::Category::getInstance(mLogName);
      cat.info(str);
   }
private:
   const char * mLogName;
};

class cDebug : public cLogOStream
{
public:
   cDebug(const char * log_name) : mLogName(log_name)
   {

   }
   virtual void logOutput(char *str)
   {
      log4cpp::Category & cat = log4cpp::Category::getInstance(mLogName);
      cat.debug(str);
   }
private:
   const char * mLogName;
};

class cError : public cLogOStream
{
public:
   cError(const char * log_name) : mLogName(log_name)
   {

   }
   virtual void logOutput(char *str)
   {
      log4cpp::Category & cat = log4cpp::Category::getInstance(mLogName);
      cat.error(str);
   }
private:
   const char * mLogName;
};


class cKlog
{
private:

   static bool mInitialised;
   const char * mCatName;
   class cLogOStream;
   char mLogBuffer[1024];

public:
   enum KlogPiority 
      {
      EMERG  = 0, 
      FATAL  = 0,
      ALERT  = 100,
      CRIT   = 200,
      ERROR  = 300, 
      WARN   = 400,
      NOTICE = 500,
      INFO   = 600,
      DEBUG  = 700,
      NOTSET = 800
      };
   static void init(KlogPiority p = INFO);
   static void init(const char * cfg_file_name);
   static log4cpp::Category & getLog(const char * cat_name);
   cKlog(const char * cat_name);
   void setLogName(const char * cat_name);
   void fatal(const char * fmt,...);
   void error(const char * fmt,...);
   void warn(const char * fmt,...);
   void info(const char * fmt,...);
   void debug(const char * fmt,...);
   bool debugEnabled();
   cLog log;
   cDebug dbg;
   cError err;

};
#endif


