added (unused) rules.rc parser to so. it will be used later.
[umfw.git] / src / common.c
blobbe148b3cc0583b3e13e581bfcbe166f5f2e1c1f8
1 #ifndef COMMON_C
2 #define COMMON_C
4 #include "config.h"
6 #include <stdio.h>
7 #include <netdb.h>
8 #include <stdarg.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include <unistd.h>
14 #include <arpa/inet.h>
15 #include <netinet/in.h>
17 #include "common.h"
20 #ifndef OPT_RULEPARSER_TEST
21 /* globals */
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) {
40 loglevel = level;
41 if (loglevel < LOGMSG_ERR) loglevel = LOGMSG_NONE;
42 if (filename) {
43 strncpy(logfilename, filename, sizeof(logfilename));
44 logfilename[sizeof(logfilename)-1] = '\0';
46 logstamp = timestamp;
50 static void logMsg (int level, const char *fmt, ...) {
51 va_list ap;
52 int saveerr;
53 extern char *progname;
54 char timestring[20];
55 time_t timestamp;
57 if (loglevel == LOGMSG_NONE || level > loglevel) return;
59 saveerr = errno;
61 if (!logfile) {
62 if (logfilename[0]) {
63 logfile = fopen(logfilename, "a");
64 if (logfile == NULL) {
65 logfile = stderr;
66 logMsg(LOGMSG_ERR, "could not open log file, %s, %s\n", logfilename, strerror(errno));
68 } else logfile = stderr;
71 if (logstamp) {
72 timestamp = time(NULL);
73 strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(&timestamp));
74 fprintf(logfile, "%s ", timestring);
77 fputs(progname, logfile);
78 if (logstamp) {
79 fprintf(logfile, "(%d)[%s]", getpid(), getProcName());
82 fputs(": ", logfile);
83 va_start(ap, fmt);
84 vfprintf(logfile, fmt, ap);
85 va_end(ap);
86 fflush(logfile);
88 errno = saveerr;
91 #else
93 static void logMsg (int level, const char *fmt, ...) {
94 va_list ap;
95 int saveerr;
97 saveerr = errno;
99 fprintf(stderr, "[%s] ", getProcName());
100 va_start(ap, fmt);
101 vfprintf(stderr, fmt, ap);
102 va_end(ap);
103 fflush(stderr);
105 errno = saveerr;
108 #endif
111 static char *getProcName (void) {
112 static char name[4096] = {0};
113 char buf[512];
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;
120 return name;
124 #endif