src/logcache.c File Reference

#include "copyright.h"
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include "mudconf.h"
#include "externs.h"
#include "interface.h"
#include "flags.h"
#include "command.h"
#include "attrs.h"
#include "rbtree.h"
#include "debug.h"

Include dependency graph for logcache.c:

Go to the source code of this file.

Data Structures

struct  logfile_t

Defines

#define LOGFILE_TIMEOUT   300

Functions

static int logcache_compare (void *vleft, void *vright, void *arg)
static int logcache_close (struct logfile_t *log)
static void logcache_expire (int fd, short event, void *arg)
static int _logcache_list (void *key, void *data, int depth, void *arg)
void logcache_list (dbref player)
static int logcache_open (char *filename)
void logcache_init ()
static int _logcache_destruct (void *key, void *data, int depth, void *arg)
void logcache_destruct ()
int logcache_writelog (char *fname, char *fdata)

Variables

rbtree logfiles = NULL


Define Documentation

#define LOGFILE_TIMEOUT   300

Definition at line 38 of file logcache.c.

Referenced by logcache_open(), and logcache_writelog().


Function Documentation

static int _logcache_destruct ( void *  key,
void *  data,
int  depth,
void *  arg 
) [static]

Definition at line 140 of file logcache.c.

References logcache_close().

Referenced by logcache_destruct().

00141 {
00142         struct logfile_t *log = (struct logfile_t *) data;
00143         logcache_close(log);
00144         return 1;
00145 }

static int _logcache_list ( void *  key,
void *  data,
int  depth,
void *  arg 
) [static]

Definition at line 75 of file logcache.c.

References logfile_t::ev, logfile_t::filename, mudstate, notify_printf(), and statedata::now.

Referenced by logcache_list().

00076 {
00077         struct timeval tv;
00078         struct logfile_t *log = (struct logfile_t *) data;
00079         dbref player = *(dbref *) arg;
00080         evtimer_pending(&log->ev, &tv);
00081         notify_printf(player, "%-40s%d", log->filename, tv.tv_sec - mudstate.now);
00082         return 1;
00083 }

static int logcache_close ( struct logfile_t log  )  [static]

Definition at line 53 of file logcache.c.

References dprintk, logfile_t::ev, logfile_t::fd, logfile_t::filename, logfiles, and rb_delete().

Referenced by _logcache_destruct(), logcache_expire(), and logcache_writelog().

00054 {
00055         dprintk("closing logfile '%s'.", log->filename);
00056         if(evtimer_pending(&log->ev, NULL)) {
00057                 evtimer_del(&log->ev);
00058         }
00059         close(log->fd);
00060         rb_delete(logfiles, log->filename);
00061         if(log->filename)
00062                 free(log->filename);
00063         log->filename = NULL;
00064         log->fd = -1;
00065         free(log);
00066         return 1;
00067 }

static int logcache_compare ( void *  vleft,
void *  vright,
void *  arg 
) [static]

Definition at line 48 of file logcache.c.

Referenced by logcache_init().

00049 {
00050         return strcmp((char *) vleft, (char *) vright);
00051 }

void logcache_destruct (  ) 

Definition at line 147 of file logcache.c.

References _logcache_destruct(), dprintk, logfiles, rb_destroy(), rb_walk(), and WALK_INORDER.

Referenced by main(), and shutdown_services().

00148 {
00149         dprintk("logcache destructing.");
00150         if(!logfiles) {
00151                 dprintk("logcache_destruct() CALLED WHILE UNITIALIZED!");
00152                 return;
00153         }
00154         rb_walk(logfiles, WALK_INORDER, _logcache_destruct, NULL);
00155         rb_destroy(logfiles);
00156         logfiles = NULL;
00157 }

static void logcache_expire ( int  fd,
short  event,
void *  arg 
) [static]

Definition at line 69 of file logcache.c.

References dprintk, and logcache_close().

Referenced by logcache_open().

00070 {
00071         dprintk("Expiring '%s'.", ((struct logfile_t *) arg)->filename);
00072         logcache_close((struct logfile_t *) arg);
00073 }

void logcache_init (  ) 

Definition at line 130 of file logcache.c.

References dprintk, logcache_compare(), logfiles, and rb_init().

Referenced by logcache_writelog(), and main().

00131 {
00132         if(!logfiles) {
00133                 dprintk("logcache initialized.");
00134                 logfiles = rb_init(logcache_compare, NULL);
00135         } else {
00136                 dprintk("REDUNDANT CALL TO logcache_init()!");
00137         }
00138 }

void logcache_list ( dbref  player  ) 

Definition at line 85 of file logcache.c.

References _logcache_list(), logfiles, notify, rb_size(), rb_walk(), and WALK_INORDER.

Referenced by do_list().

00086 {
00087         notify(player, "/--------------------------- Open Logfiles");
00088         if(rb_size(logfiles) == 0) {
00089                 notify(player, "- There are no open logfile handles.");
00090                 return;
00091         }
00092         notify(player, "Filename                               Timeout");
00093         rb_walk(logfiles, WALK_INORDER, _logcache_list, &player);
00094 }

static int logcache_open ( char *  filename  )  [static]

Definition at line 96 of file logcache.c.

References dprintk, logfile_t::ev, logfile_t::fd, logfile_t::filename, log_perror(), logcache_expire(), LOGFILE_TIMEOUT, logfiles, rb_exists(), and rb_insert().

Referenced by logcache_writelog().

00097 {
00098         int fd;
00099         struct logfile_t *newlog;
00100         struct timeval tv = { LOGFILE_TIMEOUT, 0 };
00101 
00102         if(rb_exists(logfiles, filename)) {
00103                 fprintf(stderr,
00104                                 "Serious braindamage, logcache_open() called for already open logfile.\n");
00105                 return 0;
00106         }
00107 
00108         fd = open(filename, O_RDWR | O_APPEND | O_CREAT, 0644);
00109         if(fd < 0) {
00110                 fprintf(stderr,
00111                                 "Failed to open logfile %s because open() failed with code: %d -  %s\n",
00112                                 filename, errno, strerror(errno));
00113                 return 0;
00114         }
00115         if(fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
00116                 log_perror("LOGCACHE", "FAIL", NULL,
00117                                    "fcntl(fd, F_SETFD, FD_CLOEXEC)");
00118         }
00119 
00120         newlog = malloc(sizeof(struct logfile_t));
00121         newlog->fd = fd;
00122         newlog->filename = strdup(filename);
00123         evtimer_set(&newlog->ev, logcache_expire, newlog);
00124         evtimer_add(&newlog->ev, &tv);
00125         rb_insert(logfiles, newlog->filename, newlog);
00126         dprintk("opened logfile '%s' fd = %d.", filename, fd);
00127         return 1;
00128 }

int logcache_writelog ( char *  fname,
char *  fdata 
)

Definition at line 159 of file logcache.c.

References logfile_t::ev, logfile_t::fd, logfile_t::filename, logcache_close(), logcache_init(), logcache_open(), LOGFILE_TIMEOUT, logfiles, and rb_find().

00160 {
00161         struct logfile_t *log;
00162         struct timeval tv = { LOGFILE_TIMEOUT, 0 };
00163         int len;
00164 
00165         if(!logfiles)
00166                 logcache_init();
00167 
00168         len = strlen(fdata);
00169 
00170         log = rb_find(logfiles, fname);
00171 
00172         if(!log) {
00173                 if(logcache_open(fname) < 0) {
00174                         return 0;
00175                 }
00176                 log = rb_find(logfiles, fname);
00177                 if(!log) {
00178                         return 0;
00179                 }
00180         }
00181 
00182         if(evtimer_pending(&log->ev, NULL)) {
00183                 event_del(&log->ev);
00184                 event_add(&log->ev, &tv);
00185         }
00186 
00187         if(write(log->fd, fdata, len) < 0) {
00188                 fprintf(stderr,
00189                                 "System failed to write data to file with error '%s' on logfile '%s'. Closing.\n",
00190                                 strerror(errno), log->filename);
00191                 logcache_close(log);
00192         }
00193         return 1;
00194 }


Variable Documentation

rbtree logfiles = NULL

Definition at line 46 of file logcache.c.

Referenced by logcache_close(), logcache_destruct(), logcache_init(), logcache_list(), logcache_open(), and logcache_writelog().


Generated on Mon May 28 04:25:45 2007 for BattletechMUX by  doxygen 1.4.7