log_file.c

MudOSa4DGD/mudlib/dgd/sfun/log_file.c · 2.1 KB

From the archive MudOSa4DGD-30.06.98.tar.gz

Download the archive   « Back to the archive contents

/*
 * log_file.c
 *
 * SFUN: Log a message to file
 *
 * (C) Frank Schmidt, Jesus@NorseMUD
 *
 */


#ifdef __AUTO   /* in AUTO object */


#include <std.h>

/* max filesizes for logfiles */
#define MAX_LOG_SIZE      200000
#define MAX_ENTER_LOG    1000000


/* path to object holding log_file variables and functions */
#define LOG_FILE_OB     SIMUL_EFUN



static int log_file(string file, string what) {
  int maxlog, nr;
  string pre;

  /* wrong file? */
  if (!file || !strlen(file))
    return 0;

  /* absolute path, or not? */
  if (file[0] != '/') {
    /* parse the path into domains and playerdirs */
    string pfile, domain;
    pfile = file_name();
    if (sscanf(pfile, DOMAIN_DIR+"%s/%*s", domain) == 2)
      file = DOMAIN_DIR+domain+LOCAL_LOG_DIR+file;
    else if (sscanf(pfile, PLAYER_DIR+"%s/%*s", domain) == 2)
      file = PLAYER_DIR+domain+LOCAL_LOG_DIR+file;
    else
      /* set default path for log_file */
      file = LOG_DIR + file;
  }

  /*D_LOGWATCH->logwatch(file, what); */

  if (file == LOG_DIR+"ENTER")
    maxlog = MAX_ENTER_LOG;
  else
    maxlog = MAX_LOG_SIZE;

  /* save exceeded log file, and start fresh? */
  if (file_size(file) > maxlog) {
    rename_file(file, file+".old");
  }

  pre = short_ctime(time()) + " ";
  if (nr=LOG_FILE_OB->log_file(file, what))
    pre = "Last message repeated "+(string)nr+" times.\n" + pre;
  if (nr != -1)
    /* append <what> message to logfile */
    return write_file(file, pre + what);
  return 1;
}



#undef LOG_FILE_OB
#else /* in SIMUL_EFUN object */

/*
 *  Number buffering for log_file() counting occurences
 */

#define INIT_LOG_FILE() init_log_file()

#define LF_WHAT 0
#define LF_NR   1

private mapping log_file_m;


static void init_log_file() {
  /* restart log_file buffer */
  log_file_m = ([ ]);
}


int log_file(string file, string what) {
  int nr;
  mixed *tmp;

  if (!AUTO_PRIV()) {
    illegal();
    return 0;
  }

  tmp = log_file_m[file];
  if (arrayp(tmp)) {
    if (what == tmp[LF_WHAT]) {
      ++tmp[LF_NR];
      return -1;
    }
    else
      nr = tmp[LF_NR];
  }
  log_file_m[file] = ({ what, 0 });
  return nr;
}


#endif

Site Stats

Snippets: 1791
Codebases: 571