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/>.
22 #if !(defined(__sun) || defined(__linux) || defined(__GLIBC__))
23 #define NEED_IO_INTERLOCK
33 #include <linux/limits.h>
35 #include <sys/syslimits.h>
37 #include <sys/types.h>
40 int Verbose
= 3, Log
= STDERR_FILENO
, ErrorOccurred
= 0, ErrorsFatal
= 0;
42 extern size_t PrefixLen
;
44 #ifdef NEED_IO_INTERLOCK
47 LogMut
= PTHREAD_MUTEX_INITIALIZER
;
52 void logdebug(const char *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
64 (void) write(Log
,buf
,b
);
67 err
= pthread_mutex_lock(&LogMut
);
69 (void) write(Log
,buf
,b
);
70 err
= pthread_mutex_unlock(&LogMut
);
74 (void) write(Log
,buf
,b
);
78 #define debugmsg if (Verbose >= 5) logdebug
84 void infomsg(const char *msg
, ...)
88 char buf
[256], *b
= buf
+ PrefixLen
;
92 (void) memcpy(buf
,Prefix
,PrefixLen
);
93 b
+= vsnprintf(b
,sizeof(buf
)-(b
-buf
),msg
,val
);
95 assert(s
< sizeof(buf
));
96 #ifdef NEED_IO_INTERLOCK
98 (void) write(Log
,buf
,s
);
101 err
= pthread_mutex_lock(&LogMut
);
103 (void) write(Log
,buf
,s
);
104 err
= pthread_mutex_unlock(&LogMut
);
108 (void) write(Log
,buf
,s
);
115 void warningmsg(const char *msg
, ...)
119 char buf
[256], *b
= buf
+ PrefixLen
;
123 (void) memcpy(buf
,Prefix
,PrefixLen
);
124 (void) memcpy(b
,"warning: ",9);
126 b
+= vsnprintf(b
,sizeof(buf
)-(b
-buf
),msg
,val
);
128 assert(s
< sizeof(buf
));
129 #ifdef NEED_IO_INTERLOCK
131 (void) write(Log
,buf
,s
);
134 err
= pthread_mutex_lock(&LogMut
);
136 (void) write(Log
,buf
,s
);
137 err
= pthread_mutex_unlock(&LogMut
);
141 (void) write(Log
,buf
,s
);
148 void errormsg(const char *msg
, ...)
153 char buf
[256], *b
= buf
+ PrefixLen
;
157 (void) memcpy(buf
,Prefix
,PrefixLen
);
158 (void) memcpy(b
,"error: ",7);
160 b
+= vsnprintf(b
,sizeof(buf
)-(b
-buf
),msg
,val
);
162 assert(s
< sizeof(buf
));
163 #ifdef NEED_IO_INTERLOCK
165 (void) write(Log
,buf
,s
);
168 err
= pthread_mutex_lock(&LogMut
);
170 (void) write(Log
,buf
,s
);
171 err
= pthread_mutex_unlock(&LogMut
);
175 (void) write(Log
,buf
,s
);
186 void fatal(const char *msg
, ...)
190 char buf
[256], *b
= buf
+ PrefixLen
;
194 (void) memcpy(buf
,Prefix
,PrefixLen
);
195 (void) memcpy(b
,"fatal: ",7);
197 b
+= vsnprintf(b
,sizeof(buf
)-(b
-buf
),msg
,val
);
199 assert(s
< sizeof(buf
));
200 #ifdef NEED_IO_INTERLOCK
202 (void) write(Log
,buf
,s
);
205 err
= pthread_mutex_lock(&LogMut
);
207 (void) write(Log
,buf
,s
);
208 err
= pthread_mutex_unlock(&LogMut
);
212 (void) write(Log
,buf
,s
);
220 void printmsg(const char *msg
, ...)
223 char buf
[256], *b
= buf
+ PrefixLen
;
227 (void) memcpy(buf
,Prefix
,PrefixLen
);
228 b
+= vsnprintf(b
,sizeof(buf
)-(b
-buf
),msg
,val
);
230 assert(s
< sizeof(buf
));
231 #ifdef NEED_IO_INTERLOCK
233 (void) write(Log
,buf
,s
);
236 err
= pthread_mutex_lock(&LogMut
);
238 (void) write(Log
,buf
,s
);
239 err
= pthread_mutex_unlock(&LogMut
);
243 (void) write(Log
,buf
,s
);