1 /**
2 * Error notification
3 * Library: joedog
4 *
5 * Copyright (C) 2000-2013 by
6 * Jeffrey Fulmer - <jeff@joedog.org>, et. al.
7 * This file is distributed as part of Siege
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif/*HAVE_CONFIG_H*/
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <syslog.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <notify.h>
36
37 #define BUFSIZE 40000
38
39 #define RESET 0
40 #define BRIGHT 1
41 #define DIM 2
42 #define UNDERLINE 3
43 #define BLINK 4
44 #define REVERSE 7
45 #define HIDDEN 8
46
47 #define UNCOLOR -1
48 #define BLACK 0
49 #define RED 1
50 #define GREEN 2
51 #define YELLOW 3
52 #define BLUE 4
53 #define MAGENTA 5
54 #define CYAN 6
55 #define WHITE 7
56
57 typedef enum {
58 __LOG = 1,
59 __OUT = 2,
60 } METHOD;
61
62 static void __message(METHOD M, LEVEL L, const char *fmt, va_list ap);
63
64 void
OPENLOG(char * program)65 OPENLOG(char *program)
66 {
67 openlog(program, LOG_PID, LOG_DAEMON);
68 return;
69 }
70
71 void
CLOSELOG(void)72 CLOSELOG(void)
73 {
74 closelog();
75 return;
76 }
77
78 static void
__message(METHOD M,LEVEL L,const char * fmt,va_list ap)79 __message(METHOD M, LEVEL L, const char *fmt, va_list ap)
80 {
81 char buf[BUFSIZE];
82 char msg[BUFSIZE+1024];
83 LEVEL level = WARNING;
84 char pmode[64];
85 char lmode[64];
86 memset(lmode, '\0', 64);
87 memset(pmode, '\0', 64);
88
89 vsprintf(buf, fmt, ap);
90 if (errno == 0 || errno == ENOSYS || L == DEBUG) {
91 snprintf(msg, sizeof msg, "%s\n", buf);
92 } else {
93 snprintf(msg, sizeof msg, "%s: %s\n", buf, strerror(errno));
94 }
95
96 switch (L) {
97 case DEBUG:
98 sprintf(pmode, "[%c[%d;%dmdebug%c[%dm]", 0x1B, BRIGHT, BLUE+30, 0x1B, RESET);
99 strcpy(lmode, "[debug]");
100 level = LOG_WARNING;
101 break;
102 case WARNING:
103 sprintf(pmode, "[%c[%d;%dmalert%c[%dm]", 0x1B, BRIGHT, GREEN+30, 0x1B, RESET);
104 strcpy(lmode, "[alert] ");
105 level = LOG_WARNING;
106 break;
107 case ERROR:
108 sprintf(pmode, "[%c[%d;%dmerror%c[%dm]", 0x1B, BRIGHT, YELLOW+30, 0x1B, RESET);
109 strcpy(lmode, "[error]");
110 level = LOG_ERR;
111 break;
112 case FATAL:
113 sprintf(pmode, "[%c[%d;%dmfatal%c[%dm]", 0x1B, BRIGHT, RED+30, 0x1B, RESET);
114 strcpy(lmode, "[fatal]");
115 level = LOG_CRIT;
116 break;
117 }
118
119 if (M == __LOG) {
120 syslog(level, "%s %s", lmode, msg);
121 } else {
122 fflush(stdout);
123 fprintf(stderr, "%s %s", pmode, msg);
124 }
125 if (L==FATAL) { exit(1); }
126 return;
127 }
128
129 void
SYSLOG(LEVEL L,const char * fmt,...)130 SYSLOG(LEVEL L, const char *fmt, ...)
131 {
132 va_list ap;
133 va_start(ap, fmt);
134
135 __message(__LOG, L, fmt, ap);
136 va_end(ap);
137
138 return;
139 }
140
141 void
NOTIFY(LEVEL L,const char * fmt,...)142 NOTIFY(LEVEL L, const char *fmt, ...)
143 {
144 va_list ap;
145 va_start(ap, fmt);
146
147 __message(__OUT, L, fmt, ap);
148 va_end(ap);
149
150 return;
151 }
152
153 void
__display(int color,const char * fmt,va_list ap)154 __display(int color, const char *fmt, va_list ap)
155 {
156 char buf[BUFSIZE];
157 char msg[BUFSIZE+1024];
158
159 vsprintf(buf, fmt, ap);
160 if (color == UNCOLOR) {
161 snprintf(msg, sizeof msg,"%s\n", buf);
162 } else {
163 snprintf(msg, sizeof msg, "%c[%d;%dm%s%c[%dm\n", 0x1B, RESET, color+30, buf, 0x1B, RESET);
164 }
165 printf("%s", msg);
166 return;
167 }
168
169 void
DISPLAY(int color,const char * fmt,...)170 DISPLAY(int color, const char *fmt, ...)
171 {
172 va_list ap;
173 va_start(ap, fmt);
174 __display(color, fmt, ap);
175 va_end(ap);
176 return;
177 }
178
179