Show
Ignore:
Timestamp:
02/17/08 04:48:33 (6 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1203223713 +0000
git-parent:

[9a02263ee28afbf8381cc8649c3b56a3f118ecbe]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1203223713 +0000
Message:

abstract logging a bit

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/utils/noit_log.c

    r1afde4e r44d8413  
    1616noit_log_stream_t noit_error = NULL; 
    1717noit_log_stream_t noit_debug = NULL; 
     18 
     19static int 
     20posix_logio_open(noit_log_stream_t ls) { 
     21  int fd; 
     22  fd = open(ls->path, O_CREAT|O_WRONLY|O_APPEND); 
     23  if(fd < 0) { 
     24    ls->op_ctx = NULL; 
     25    return -1; 
     26  } 
     27  ls->op_ctx = (void *)fd; 
     28  return 0; 
     29} 
     30static int 
     31posix_logio_reopen(noit_log_stream_t ls) { 
     32  if(ls->path) { 
     33    int newfd, oldfd; 
     34    oldfd = (int)ls->op_ctx; 
     35    newfd = open(ls->path, O_CREAT|O_WRONLY|O_APPEND); 
     36    if(newfd >= 0) { 
     37      ls->op_ctx = (void *)newfd; 
     38      if(oldfd >= 0) close(oldfd); 
     39      return 0; 
     40    } 
     41  } 
     42  return -1; 
     43} 
     44static int 
     45posix_logio_write(noit_log_stream_t ls, const void *buf, size_t len) { 
     46  int fd; 
     47  fd = (int)ls->op_ctx; 
     48  return write(fd, buf, len); 
     49} 
     50static int 
     51posix_logio_close(noit_log_stream_t ls) { 
     52  int fd; 
     53  fd = (int)ls->op_ctx; 
     54  return close(fd); 
     55} 
     56static logops_t posix_logio_ops = { 
     57  posix_logio_open, 
     58  posix_logio_reopen, 
     59  posix_logio_write, 
     60  posix_logio_close, 
     61}; 
    1862 
    1963void 
     
    3074  ls = calloc(1, sizeof(*ls)); 
    3175  ls->name = strdup(name); 
    32   ls->fd = fd; 
     76  ls->ops = &posix_logio_ops; 
     77  ls->op_ctx = (void *)fd; 
    3378  ls->enabled = 1; 
    3479  if(noit_hash_store(&noit_loggers, ls->name, strlen(ls->name), ls) == 0) { 
     
    4590  ls = calloc(1, sizeof(*ls)); 
    4691  ls->path = strdup(path); 
    47   ls->fd = open(ls->path, O_CREAT|O_WRONLY|O_APPEND)
    48   if(ls->fd < 0) { 
     92  ls->ops = &posix_logio_ops
     93  if(ls->ops->openop(ls)) { 
    4994    free(ls->path); 
    5095    free(ls); 
     
    63108 
    64109noit_log_stream_t 
    65 noit_log_stream_new(const char *name) { 
     110noit_log_stream_new(const char *name, logops_t *ops) { 
    66111  noit_log_stream_t ls; 
    67112  ls = calloc(1, sizeof(*ls)); 
    68113  ls->name = strdup(name); 
    69   ls->fd = -1; 
    70114  ls->enabled = 1; 
     115  ls->ops = ops ? ops : &posix_logio_ops; 
    71116  if(noit_hash_store(&noit_loggers, ls->name, strlen(ls->name), ls) == 0) { 
    72117    free(ls->name); 
     
    125170void noit_log_stream_reopen(noit_log_stream_t ls) { 
    126171  struct _noit_log_stream_outlet_list *node; 
    127   if(ls->path) { 
    128     int newfd, oldfd; 
    129     oldfd = ls->fd; 
    130     newfd = open(ls->path, O_CREAT|O_WRONLY|O_APPEND); 
    131     if(newfd >= 0) { 
    132       ls->fd = newfd; 
    133       if(oldfd >= 0) close(oldfd); 
    134     } 
    135   } 
     172  ls->ops->reopenop(ls); 
    136173  for(node = ls->outlets; node; node = node->next) { 
    137174    noit_log_stream_reopen(node->outlet); 
     
    142179noit_log_stream_close(noit_log_stream_t ls) { 
    143180  struct _noit_log_stream_outlet_list *node; 
    144   if(ls->fd >= 0) { 
    145     int oldfd; 
    146     oldfd = ls->fd; 
    147     ls->fd = -1; 
    148     close(oldfd); 
    149   } 
     181  ls->ops->closeop(ls); 
    150182  for(node = ls->outlets; node; node = node->next) { 
    151183    noit_log_stream_close(node->outlet); 
     
    178210#endif 
    179211 
    180   if(ls->fd >= 0) { 
     212  if(ls->enabled) { 
    181213    int len; 
    182214#ifdef va_copy 
     
    187219    len = vsnprintf(buffer, sizeof(buffer), format, arg); 
    188220#endif 
    189     write(ls->fd, buffer, len); /* Not much one can do about errors */ 
     221    ls->ops->writeop(ls, buffer, len); /* Not much one can do about errors */ 
    190222  } 
    191223 
  • src/utils/noit_log.h

    r1afde4e r44d8413  
    1515  struct _noit_log_stream_outlet_list *next; 
    1616}; 
     17 
     18typedef struct { 
     19  int (*openop)(struct _noit_log_stream *); 
     20  int (*reopenop)(struct _noit_log_stream *); 
     21  int (*writeop)(struct _noit_log_stream *, const void *, size_t); 
     22  int (*closeop)(struct _noit_log_stream *); 
     23} logops_t; 
     24 
    1725typedef struct _noit_log_stream { 
    1826  char *name; 
    1927  int enabled; 
    20   int fd; 
    2128  char *path; 
     29  logops_t *ops; 
     30  void *op_ctx; 
    2231  struct _noit_log_stream_outlet_list *outlets; 
    2332} * noit_log_stream_t;