#include "CLog.h"

CLog::CLog (CBot *bot) : mail (MAIL_SIZE), writing (0), b (bot),
  filename (PATH_MAX), old_filename (PATH_MAX)
{
}

CLog::~CLog (void)
{
  f << "Log stopped." << endl;
} 

// open <file>, return 0 on error.
// if <flush>, flush the stream on every write
// <time> can be 0 to write whole date or 1 to write only the time
bool
CLog::conf (c_char file, c_char oldfile, bool flush, int time)
{
  filename = file;
  f.open (filename, ios::app);
  if (!f)
    {
      b->write_botlog ("error opening %s: %s", (c_char)filename, strerror (errno));
      return 0;
    }
  if (oldfile != NULL)
    old_filename = oldfile;
  flushmode = flush;
  timemode = time;
  hour = get_hour ();
  return 1;
}

CLog &
operator<< (CLog &log, c_char str)
{
  if (str != EOL)
    {
      if (!log.writing)
        {
          if (log.old_filename)
            log.change_log ();
          if (log.timemode == 0)
            string_time (log.b->time_now, log.st_time);
          else
            get_asctime (log.b->time_now, log.st_time, TIME_SIZE);
          log.f << log.st_time << " ";
          log.writing = 1;
        }
      log.f << str;
    }
  else
    {
      if (log.flushmode)
        log.f << endl;
      else
        log.f << '\n';
      log.hour = log.get_hour ();
      log.writing = 0;
    }
  return log;
}

CLog &
operator<< (CLog &log, char c)
{
  if (!log.writing)
    {
      if (log.old_filename)
        log.change_log ();
      if (log.timemode == 0)
        string_time (log.b->time_now, log.st_time);
      else
        get_asctime (log.b->time_now, log.st_time, TIME_SIZE);
      log.f << log.st_time << " ";
      log.writing = 1;
    }
  log.f << c;
  return log;
}

CLog &
operator<< (CLog &log, const CString &str)
{
  if (!log.writing)
    {
      if (log.old_filename)
        log.change_log ();
      if (log.timemode == 0)
        string_time (log.b->time_now, log.st_time);
      else
        get_asctime (log.b->time_now, log.st_time, TIME_SIZE);
      log.f << log.st_time << " ";
      log.writing = 1;
    }
  log.f << str;
  return log;
}

// renew log if it's time or if the file is too big
void
CLog::change_log (void)
{
  if ((hour < LOG_HOUR && get_hour () >= LOG_HOUR)
      || f.tellp () >= LOG_SIZE)
    {
      char buf[PATH_MAX+1], buf2[PATH_MAX+1];
      f.close ();
      rename (filename, old_filename);
#ifdef USE_MAIL
      if (mail)			// send log by mail, if specified
        {
          snprintf (buf, PATH_MAX, 
                    "(%s && %s %s) | %s %s &\n", 
                    ECHO_PATH, CAT_PATH, (char *)old_filename, SENDMAIL_PATH,
                    (char *)mail);
          system (buf);
        }
#endif
      my_strncpy (buf, filename, PATH_MAX);
      if (old_filename)
        my_strncpy (buf2, old_filename, PATH_MAX);
      conf (buf, (old_filename) ? buf2 : NULL, timemode, flushmode);
      f << get_asctime (b->time_now, buf, PATH_MAX) << endl;
    }
}

// return current time
u_char
CLog::get_hour (void)
{
  tm time1;
  time_t time2 = b->time_now;
  time1 = *localtime (&time2);
  return time1.tm_hour;
}

