Changeset 4e66b3a in code
- Timestamp:
- Mar 5, 2014, 5:28:43 PM (11 years ago)
- Branches:
- master
- Children:
- 1dd3554
- Parents:
- a5893e9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
logger.c
ra5893e9 r4e66b3a 18 18 #include <sys/param.h> 19 19 #include <sys/socket.h> 20 #include <sys/stat.h> 20 21 #include <sys/time.h> 21 22 #include <sys/types.h> … … 37 38 38 39 void logger_dispatch(int, short, void *); 40 FILE *logger_open(char *); 39 41 void logger_tick(int, short, void *); 40 void logger_set_ts(void); 41 42 int logger_pipe; 42 void logger_setts(void); 43 43 44 44 struct icbd_logentry { … … 48 48 }; 49 49 50 char line_ts[sizeof("[12:34 ]")]; 51 50 struct { 51 char group[ICB_MAXGRPLEN]; 52 FILE *fp; 53 } logfiles[10]; 54 int nlogfiles; 55 56 int logger_pipe; 57 58 char file_ts[sizeof "0000-00"]; 59 char line_ts[sizeof "[00:00] "]; 52 60 struct event ev_tick; 53 struct timeval tick;54 61 55 62 int … … 58 65 static struct event ev; 59 66 struct passwd *pw; 67 struct timeval tv = { 60, 0 }; 60 68 int pipes[2]; 61 69 … … 110 118 111 119 /* event for the tick */ 112 tick.tv_sec = 60;113 tick.tv_usec = 0;114 120 evtimer_set(&ev_tick, logger_tick, NULL); 115 if (evtimer_add(&ev_tick, &t ick) < 0) {121 if (evtimer_add(&ev_tick, &tv) < 0) { 116 122 syslog(LOG_ERR, "evtimer_add: %m"); 117 123 exit (EX_UNAVAILABLE); 118 124 } 119 logger_set _ts();125 logger_setts(); 120 126 return event_dispatch(); 121 127 } … … 124 130 logger_dispatch(int fd, short event, void *arg __attribute__((unused))) 125 131 { 126 char buf[ 512];132 char buf[ICB_MSGSIZE]; 127 133 struct icbd_logentry e; 128 134 struct iovec iov[2]; 135 FILE *fp = NULL; 136 int i; 129 137 130 138 if (event != EV_READ) … … 149 157 } 150 158 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 168 FILE * 169 logger_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); 154 189 } 155 190 … … 178 213 void *arg __attribute__((unused))) 179 214 { 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) { 182 219 syslog(LOG_ERR, "evtimer_add: %m"); 183 220 exit (EX_UNAVAILABLE); 184 221 } 185 222 } 186 void 187 logger_set_ts(void) 223 224 void 225 logger_setts(void) 188 226 { 189 227 struct tm *tm; 190 228 time_t t; 191 229 192 t = time(NULL);230 time(&t); 193 231 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, 195 235 tm->tm_min); 196 236 }
Note:
See TracChangeset
for help on using the changeset viewer.