root/honeytrap/trunk/src/logging.c

Revision 1292, 1.6 kB (checked in by till, 2 years ago)

- safe signal handling through per-process signal pipes
- increased verbosity of debug log messages during initialization phase
- silly pitfalls during build process removed

Line 
1 /* logging.c
2  * Copyright (C) 2005-2007 Tillmann Werner <tillmann.werner@gmx.de>
3  *
4  * This file is free software; as a special exception the author gives
5  * unlimited permission to copy and/or distribute it, with or without
6  * modifications, as long as this notice is preserved.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  */
12
13 #include <stdio.h>
14 #include <stdarg.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <strings.h>
19
20 #include "honeytrap.h"
21 #include "logging.h"
22
23
24 void logmsg(int level, int add_time, const char *format, ...) {
25         char logline[LOGLINE_SIZE];
26         va_list ap;
27         int bytes_written, logline_size;
28         time_t timeval;
29        
30         if(level <= log_level) {
31                 bzero(&logline, LOGLINE_SIZE);
32                 va_start(ap, format);
33                 if (add_time) {
34                         time(&timeval);
35                         strftime(logline, 23, "[%F %T] ", localtime(&timeval));
36                         if (log_level == LOG_DEBUG)
37                                 snprintf(logline + strlen(logline), LOGLINE_SIZE - strlen(logline), "%5d  ", getpid());
38                 }
39                 vsnprintf(logline + strlen(logline), LOGLINE_SIZE - strlen(logline), format, ap);
40                 logline_size = strlen(logline);
41
42                 if ((bytes_written = write(logfile_fd, logline, logline_size)) != logline_size)
43                         perror("Error while writing logfile");
44
45                 /* log to stdout as well if we are not running as daemon */
46                 if ((STDOUT_FILENO != logfile_fd) && (daemonize != 1)) {
47                         if ((bytes_written = write(STDOUT_FILENO, logline, logline_size)) != logline_size)
48                                 perror("Error while logging to stdout");
49                 }
50                 va_end(ap);
51         }
52         return;
53 }
Note: See TracBrowser for help on using the browser.