Changeset a5893e9 in code for logger.c


Ignore:
Timestamp:
Mar 5, 2014, 4:56:11 PM (11 years ago)
Author:
Mike Belopuhov <mike@…>
Branches:
master
Children:
4e66b3a
Parents:
45bd56a
git-author:
Florian Obser <florian@…> (03/05/14 16:30:11)
git-committer:
Mike Belopuhov <mike@…> (03/05/14 16:56:11)
Message:

time accounting for log lines

File:
1 edited

Legend:

Unmodified
Added
Removed
  • logger.c

    r45bd56a ra5893e9  
    1919#include <sys/socket.h>
    2020#include <sys/time.h>
     21#include <sys/types.h>
    2122#include <sys/uio.h>
    2223#include <errno.h>
     
    2728#include <syslog.h>
    2829#include <sysexits.h>
     30#include <time.h>
    2931#include <login_cap.h>
    3032#include <event.h>
     
    3537
    3638void logger_dispatch(int, short, void *);
     39void logger_tick(int, short, void *);
     40void logger_set_ts(void);
    3741
    3842int logger_pipe;
     
    4347        size_t  length;
    4448};
     49
     50char line_ts[sizeof("[12:34 ]")];
     51
     52struct event ev_tick;
     53struct timeval tick;
    4554
    4655int
     
    100109        }
    101110
     111        /* event for the tick */
     112        tick.tv_sec = 60;
     113        tick.tv_usec = 0;
     114        evtimer_set(&ev_tick, logger_tick, NULL);
     115        if (evtimer_add(&ev_tick, &tick) < 0) {
     116                syslog(LOG_ERR, "evtimer_add: %m");
     117                exit (EX_UNAVAILABLE);
     118        }
     119        logger_set_ts();
    102120        return event_dispatch();
    103121}
     
    133151        /* TODO: check time of the day and open the next file */
    134152
    135         fprintf(stderr, "%s@%s: %s\n", e.nick, e.group, buf);
     153        fprintf(stderr, "%s%s@%s: %s\n", line_ts, e.nick, e.group, buf);
    136154}
    137155
     
    155173                syslog(LOG_ERR, "logger write: %m");
    156174}
     175
     176void
     177logger_tick(int fd __attribute__((unused)), short event __attribute__((unused)),
     178    void *arg __attribute__((unused)))
     179{
     180        logger_set_ts();
     181        if (evtimer_add(&ev_tick, &tick) < 0) {
     182                syslog(LOG_ERR, "evtimer_add: %m");
     183                exit (EX_UNAVAILABLE);
     184        }
     185}
     186void
     187logger_set_ts(void)
     188{
     189        struct tm *tm;
     190        time_t t;
     191
     192        t = time(NULL);
     193        tm = gmtime(&t);
     194        snprintf(line_ts, sizeof(line_ts), "[%02d:%02d] ", tm->tm_hour,
     195            tm->tm_min);
     196}
Note: See TracChangeset for help on using the changeset viewer.