modified: nfig1.py
[GalaxyCodeBases.git] / c_cpp / etc / mbuffer / log.c
blob85888a6ec032aa1ae82c1c6c497d35f0264617e5
1 /*
2 * Copyright (C) 2000-2009, Thomas Maier-Komor
4 * This is the source code of mbuffer.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "log.h"
22 #if !(defined(__sun) || defined(__linux) || defined(__GLIBC__))
23 #define NEED_IO_INTERLOCK
24 #endif
26 #include <assert.h>
27 #include <limits.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #if defined(__linux)
33 #include <linux/limits.h>
34 #elif defined(__bsd)
35 #include <sys/syslimits.h>
36 #endif
37 #include <sys/types.h>
38 #include <unistd.h>
40 int Verbose = 3, Log = STDERR_FILENO, ErrorOccurred = 0, ErrorsFatal = 0;
41 extern char *Prefix;
42 extern size_t PrefixLen;
44 #ifdef NEED_IO_INTERLOCK
45 #include <pthread.h>
46 pthread_mutex_t
47 LogMut = PTHREAD_MUTEX_INITIALIZER;
48 #endif
51 #ifdef DEBUG
52 void logdebug(const char *msg, ...)
54 va_list val;
55 char buf[256];
56 size_t b = PrefixLen;
58 va_start(val,msg);
59 (void) memcpy(buf,Prefix,b);
60 b += vsnprintf(buf + b,sizeof(buf)-b,msg,val);
61 assert(b < sizeof(buf));
62 #ifdef NEED_IO_INTERLOCK
63 if (b <= PIPE_BUF) {
64 (void) write(Log,buf,b);
65 } else {
66 int err;
67 err = pthread_mutex_lock(&LogMut);
68 assert(err == 0);
69 (void) write(Log,buf,b);
70 err = pthread_mutex_unlock(&LogMut);
71 assert(err == 0);
73 #else
74 (void) write(Log,buf,b);
75 #endif
76 va_end(val);
78 #define debugmsg if (Verbose >= 5) logdebug
79 #else
80 #define debugmsg
81 #endif
84 void infomsg(const char *msg, ...)
86 if (Verbose >= 4) {
87 va_list val;
88 char buf[256], *b = buf + PrefixLen;
89 size_t s;
91 va_start(val,msg);
92 (void) memcpy(buf,Prefix,PrefixLen);
93 b += vsnprintf(b,sizeof(buf)-(b-buf),msg,val);
94 s = b - buf;
95 assert(s < sizeof(buf));
96 #ifdef NEED_IO_INTERLOCK
97 if (s <= PIPE_BUF) {
98 (void) write(Log,buf,s);
99 } else {
100 int err;
101 err = pthread_mutex_lock(&LogMut);
102 assert(err == 0);
103 (void) write(Log,buf,s);
104 err = pthread_mutex_unlock(&LogMut);
105 assert(err == 0);
107 #else
108 (void) write(Log,buf,s);
109 #endif
110 va_end(val);
115 void warningmsg(const char *msg, ...)
117 if (Verbose >= 3) {
118 va_list val;
119 char buf[256], *b = buf + PrefixLen;
120 size_t s;
122 va_start(val,msg);
123 (void) memcpy(buf,Prefix,PrefixLen);
124 (void) memcpy(b,"warning: ",9);
125 b += 9;
126 b += vsnprintf(b,sizeof(buf)-(b-buf),msg,val);
127 s = b - buf;
128 assert(s < sizeof(buf));
129 #ifdef NEED_IO_INTERLOCK
130 if (s <= PIPE_BUF) {
131 (void) write(Log,buf,s);
132 } else {
133 int err;
134 err = pthread_mutex_lock(&LogMut);
135 assert(err == 0);
136 (void) write(Log,buf,s);
137 err = pthread_mutex_unlock(&LogMut);
138 assert(err == 0);
140 #else
141 (void) write(Log,buf,s);
142 #endif
143 va_end(val);
148 void errormsg(const char *msg, ...)
150 ErrorOccurred = 1;
151 if (Verbose >= 2) {
152 va_list val;
153 char buf[256], *b = buf + PrefixLen;
154 size_t s;
156 va_start(val,msg);
157 (void) memcpy(buf,Prefix,PrefixLen);
158 (void) memcpy(b,"error: ",7);
159 b += 7;
160 b += vsnprintf(b,sizeof(buf)-(b-buf),msg,val);
161 s = b - buf;
162 assert(s < sizeof(buf));
163 #ifdef NEED_IO_INTERLOCK
164 if (s <= PIPE_BUF) {
165 (void) write(Log,buf,s);
166 } else {
167 int err;
168 err = pthread_mutex_lock(&LogMut);
169 assert(err == 0);
170 (void) write(Log,buf,s);
171 err = pthread_mutex_unlock(&LogMut);
172 assert(err == 0);
174 #else
175 (void) write(Log,buf,s);
176 #endif
177 va_end(val);
179 if (ErrorsFatal) {
180 close(Log);
181 exit(EXIT_FAILURE);
186 void fatal(const char *msg, ...)
188 if (Verbose >= 1) {
189 va_list val;
190 char buf[256], *b = buf + PrefixLen;
191 size_t s;
193 va_start(val,msg);
194 (void) memcpy(buf,Prefix,PrefixLen);
195 (void) memcpy(b,"fatal: ",7);
196 b += 7;
197 b += vsnprintf(b,sizeof(buf)-(b-buf),msg,val);
198 s = b - buf;
199 assert(s < sizeof(buf));
200 #ifdef NEED_IO_INTERLOCK
201 if (s <= PIPE_BUF) {
202 (void) write(Log,buf,s);
203 } else {
204 int err;
205 err = pthread_mutex_lock(&LogMut);
206 assert(err == 0);
207 (void) write(Log,buf,s);
208 err = pthread_mutex_unlock(&LogMut);
209 assert(err == 0);
211 #else
212 (void) write(Log,buf,s);
213 #endif
214 va_end(val);
216 exit(EXIT_FAILURE);
220 void printmsg(const char *msg, ...)
222 va_list val;
223 char buf[256], *b = buf + PrefixLen;
224 size_t s;
226 va_start(val,msg);
227 (void) memcpy(buf,Prefix,PrefixLen);
228 b += vsnprintf(b,sizeof(buf)-(b-buf),msg,val);
229 s = b - buf;
230 assert(s < sizeof(buf));
231 #ifdef NEED_IO_INTERLOCK
232 if (s <= PIPE_BUF) {
233 (void) write(Log,buf,s);
234 } else {
235 int err;
236 err = pthread_mutex_lock(&LogMut);
237 assert(err == 0);
238 (void) write(Log,buf,s);
239 err = pthread_mutex_unlock(&LogMut);
240 assert(err == 0);
242 #else
243 (void) write(Log,buf,s);
244 #endif
245 va_end(val);