14 #include <arpa/inet.h>
15 #include <netinet/in.h>
20 #ifndef OPT_RULEPARSER_TEST
22 static int loglevel
= LOGMSG_ERR
; /* the default logging level is to only log error messages */
23 static char logfilename
[4096]; /* name of file to which log messages should be redirected */
24 static FILE *logfile
= NULL
; /* file to which messages should be logged */
25 static int logstamp
= 0; /* timestamp (and pid stamp) messages */
28 /* Set logging options, the options are as follows: */
29 /* level - This sets the logging threshold, messages with */
30 /* a higher level (i.e lower importance) will not be */
31 /* output. For example, if the threshold is set to */
32 /* MSGWARN a call to log a message of level MSGDEBUG */
33 /* would be ignored. This can be set to -1 to disable */
34 /* messages entirely */
35 /* filename - This is a filename to which the messages should */
36 /* be logged instead of to standard error */
37 /* timestamp - This indicates that messages should be prefixed */
38 /* with timestamps (and the process id) */
39 static void setLogOptions (int level
, const char *filename
, int timestamp
) {
41 if (loglevel
< LOGMSG_ERR
) loglevel
= LOGMSG_NONE
;
43 strncpy(logfilename
, filename
, sizeof(logfilename
));
44 logfilename
[sizeof(logfilename
)-1] = '\0';
50 static void logMsg (int level
, const char *fmt
, ...) {
53 extern char *progname
;
57 if (loglevel
== LOGMSG_NONE
|| level
> loglevel
) return;
63 logfile
= fopen(logfilename
, "a");
64 if (logfile
== NULL
) {
66 logMsg(LOGMSG_ERR
, "could not open log file, %s, %s\n", logfilename
, strerror(errno
));
68 } else logfile
= stderr
;
72 timestamp
= time(NULL
);
73 strftime(timestring
, sizeof(timestring
), "%H:%M:%S", localtime(×tamp
));
74 fprintf(logfile
, "%s ", timestring
);
77 fputs(progname
, logfile
);
79 fprintf(logfile
, "(%d)[%s]", getpid(), getProcName());
84 vfprintf(logfile
, fmt
, ap
);
93 static void logMsg (int level
, const char *fmt
, ...) {
99 fprintf(stderr
, "[%s] ", getProcName());
101 vfprintf(stderr
, fmt
, ap
);
111 static char *getProcName (void) {
112 static char name
[4096] = {0};
115 if (name
[0]) return name
;
116 pid_t pid
= getpid();
117 sprintf(buf
, "/proc/%u/exe", pid
);
118 ssize_t len
= readlink(buf
, name
, sizeof(name
));
119 if (len
< 1) strcpy(name
, "<unknown>"); else name
[len
] = 0;