Changeset 4e66b3a in code


Ignore:
Timestamp:
Mar 5, 2014, 5:28:43 PM (11 years ago)
Author:
Mike Belopuhov <mike@…>
Branches:
master
Children:
1dd3554
Parents:
a5893e9
Message:

Working logging

File:
1 edited

Legend:

Unmodified
Added
Removed
  • logger.c

    ra5893e9 r4e66b3a  
    1818#include <sys/param.h>
    1919#include <sys/socket.h>
     20#include <sys/stat.h>
    2021#include <sys/time.h>
    2122#include <sys/types.h>
     
    3738
    3839void logger_dispatch(int, short, void *);
     40FILE *logger_open(char *);
    3941void logger_tick(int, short, void *);
    40 void logger_set_ts(void);
    41 
    42 int logger_pipe;
     42void logger_setts(void);
    4343
    4444struct icbd_logentry {
     
    4848};
    4949
    50 char line_ts[sizeof("[12:34 ]")];
    51 
     50struct {
     51        char    group[ICB_MAXGRPLEN];
     52        FILE    *fp;
     53} logfiles[10];
     54int nlogfiles;
     55
     56int logger_pipe;
     57
     58char file_ts[sizeof "0000-00"];
     59char line_ts[sizeof "[00:00] "];
    5260struct event ev_tick;
    53 struct timeval tick;
    5461
    5562int
     
    5865        static struct event ev;
    5966        struct passwd *pw;
     67        struct timeval tv = { 60, 0 };
    6068        int pipes[2];
    6169
     
    110118
    111119        /* event for the tick */
    112         tick.tv_sec = 60;
    113         tick.tv_usec = 0;
    114120        evtimer_set(&ev_tick, logger_tick, NULL);
    115         if (evtimer_add(&ev_tick, &tick) < 0) {
     121        if (evtimer_add(&ev_tick, &tv) < 0) {
    116122                syslog(LOG_ERR, "evtimer_add: %m");
    117123                exit (EX_UNAVAILABLE);
    118124        }
    119         logger_set_ts();
     125        logger_setts();
    120126        return event_dispatch();
    121127}
     
    124130logger_dispatch(int fd, short event, void *arg __attribute__((unused)))
    125131{
    126         char buf[512];
     132        char buf[ICB_MSGSIZE];
    127133        struct icbd_logentry e;
    128134        struct iovec iov[2];
     135        FILE *fp = NULL;
     136        int i;
    129137
    130138        if (event != EV_READ)
     
    149157        }
    150158
    151         /* TODO: check time of the day and open the next file */
    152 
    153         fprintf(stderr, "%s%s@%s: %s\n", line_ts, e.nick, e.group, buf);
     159        for (i = 0; i < nlogfiles; i++)
     160                if (strcmp(logfiles[i].group, e.group) == 0)
     161                        fp = logfiles[i].fp;
     162        if (!fp && (fp = logger_open(e.group)) == NULL)
     163                return;
     164
     165        fprintf(fp, "%s<%s> %s\n", line_ts, e.nick, buf);
     166}
     167
     168FILE *
     169logger_open(char *group)
     170{
     171        char path[MAXPATHLEN];
     172        FILE *fp = NULL;
     173
     174        if (mkdir(group, 0755) < 0 && errno != EEXIST) {
     175                syslog(LOG_ERR, "%s: %m", group);
     176                return (NULL);
     177        }
     178        snprintf(path, sizeof path, "%s/%s", group, file_ts);
     179        if ((fp = fopen(path, "a")) == NULL) {
     180                syslog(LOG_ERR, "%s: %m", path);
     181                return (NULL);
     182        }
     183        setvbuf(fp, NULL, _IOLBF, 0);
     184        if (verbose)
     185                syslog(LOG_DEBUG, "logger_open: %s", path);
     186        strlcpy(logfiles[nlogfiles].group, group, ICB_MAXGRPLEN);
     187        logfiles[nlogfiles++].fp = fp;
     188        return (fp);
    154189}
    155190
     
    178213    void *arg __attribute__((unused)))
    179214{
    180         logger_set_ts();
    181         if (evtimer_add(&ev_tick, &tick) < 0) {
     215        struct timeval tv = { 60, 0 };
     216
     217        logger_setts();
     218        if (evtimer_add(&ev_tick, &tv) < 0) {
    182219                syslog(LOG_ERR, "evtimer_add: %m");
    183220                exit (EX_UNAVAILABLE);
    184221        }
    185222}
    186 void
    187 logger_set_ts(void)
     223
     224void
     225logger_setts(void)
    188226{
    189227        struct tm *tm;
    190228        time_t t;
    191229
    192         t = time(NULL);
     230        time(&t);
    193231        tm = gmtime(&t);
    194         snprintf(line_ts, sizeof(line_ts), "[%02d:%02d] ", tm->tm_hour,
     232        snprintf(file_ts, sizeof file_ts, "%04d-%02d", tm->tm_year + 1900,
     233            tm->tm_mon + 1);
     234        snprintf(line_ts, sizeof line_ts, "[%02d:%02d] ", tm->tm_hour,
    195235            tm->tm_min);
    196236}
Note: See TracChangeset for help on using the changeset viewer.