musicmud-2.1.6/data/
musicmud-2.1.6/data/help/
musicmud-2.1.6/data/policy/
musicmud-2.1.6/data/wild/
musicmud-2.1.6/data/world/
musicmud-2.1.6/doc/
musicmud-2.1.6/src/ident/
musicmud-2.1.6/src/lua/
musicmud-2.1.6/src/lua/include/
musicmud-2.1.6/src/lua/src/lib/
musicmud-2.1.6/src/lua/src/lua/
musicmud-2.1.6/src/lua/src/luac/
#include "musicmud.h"
#include "log.h"
#include "flags.h"
#include "pflags.h"

FILE *logfile;
extern MudObject *qui;
extern bool logstdout;

static void closelog() {
  xclose(logfile);
}

static void openlog() {
#ifndef NOLOGFILE
    if (!logfile) {
	logfile = xopen("logs", "log", "a");
	if (!logfile) {
	  fprintf(stderr, "Warning : can't open logfile.\n");
	  return;
	}
    }
    atexit(closelog);
#endif
}

#define LOGS 20

struct logmsg_t {
  int time;
  PFlag priv;
  int level;
  string sys;
  string msg;
  string who;
} log_circle[LOGS] = { { 0, PFL_NONE, 0, "", "", "" } };

int log_n = 0;

static void log(MudObject *who, const showto *whether, Flag nflag, PFlag pflag, 
		int log_level, const char *system, const char *text, PRINT_PARMS) {
  if (!logfile) 
    openlog();

  Player *o;
  int i;

  string temp = formatprint(text, GET_PARMS());
  
  string beepless;
  const char *at = temp.c_str();
  while (*at) {
    if (*at!='\a') beepless += *at;
    at++;
  }
  
  if (!who)
    who = qui;

  if (logstdout) 
    fprintf(stdout, "%li : %s : %s : %s\n", (long)now, system, who ? who->id : "", beepless.c_str());

  if (logfile) {
    fprintf(logfile, "%li : %s : %s : %s\n", (long)now, system, who ? who->id : "", beepless.c_str());
    fflush(logfile);
  }
  
  if (!whether) {
    log_circle[log_n].time = now;
    log_circle[log_n].priv = pflag;
    log_circle[log_n].who = who?who->id:"";
    log_circle[log_n].level = log_level;
    log_circle[log_n].sys = system;
    log_circle[log_n].msg = beepless;
    log_n++;
    if (log_n >= LOGS) log_n = 0;
  }
  
  if (!players)
    return;

  foreach(players, o, i) {
    if (!o->get_flag(FL_LOGGEDIN))
      continue;

    if (privs_of(o)<log_level)
      continue;

    if (nflag != FL_NONE && o->get_flag(nflag))
      continue;
    
    if (pflag != PFL_NONE && !o->get_priv(pflag))
      continue;
    
    if (whether && !(*whether)(o))
      continue;
    
    string time = "";
    if (o->get_flag(FL_LOGTIME)) {
      time = sstrftime(o, "%H:%M ", now);
    }
    const char *tim = time.c_str();
    
    FILE *f = o->of;
    o->of = 0;

    if (who && who != mud)
      if (!is_player(who) || who->get_flag(FL_LOGGEDIN))
	o->printf("%s^Y[^R%s^Y]^n ^Z(%M^n) %s\n", 
		  tim, system, who, temp.c_str());
      else
	o->printf("%s^Y[^R%s^Y]^n ^Z(%s^n) %s\n", 
		  tim, system, who->id, temp.c_str());
    else
      o->printf("%s^Y[^R%s^Y]^n ^Z%s\n", 
		tim, system, temp.c_str());
    
    o->of = f;
  }
}

void log(const showto &f, Flag nflag, PFlag pflag, int log_level, 
	 const char *system, const char *text, PRINT_PARMS) {
    log(qui, &f, nflag, pflag, log_level, system, text, PRINT_PASS_PARMS);
}

void log(Flag nflag, PFlag pflag, int log_level, const char *system, 
	 const char *text, PRINT_PARMS) {
    log(qui, NULL, nflag, pflag, log_level, system, text, PRINT_PASS_PARMS);
}

void log(PFlag pflag, int log_level, const char *system, 
	 const char *text, PRINT_PARMS) {
    log(qui, NULL, FL_NOINFO, pflag, log_level, system, text, PRINT_PASS_PARMS);
}

void log(MudObject *who, PFlag pflag, int log_level, const char *system, 
	 const char *text, PRINT_PARMS) {
    log(who, NULL, FL_NOINFO, pflag, log_level, system, text, PRINT_PASS_PARMS);
}