-
Notifications
You must be signed in to change notification settings - Fork 1
/
dprintf.h
37 lines (37 loc) · 1.69 KB
/
dprintf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef ARR_DPRINTF_H
# define ARR_DPRINTF_H
# include <pthread.h>
# include <stdio.h>
# include <string.h>
/// If DPRINTF_ON is defined, DPRINTF((arg)) will acquire a mutex lock,
/// hand arg to printf, flush stdout, and release the lock. On error,
/// it prints an message and exits.
///
/// If DPRINTF_ON is not defined, DPRINTF is a noop.
///
/// The mutex "outputLock" is defined in dprintf.c, which must be linked
/// into executables that use DPRINTF.
# ifdef DPRINTF_ON
# define DPRINTF(arg) do{ \
int dprintf_status; \
dprintf_status=pthread_mutex_lock(&outputLock); \
if(dprintf_status) { \
fprintf(stderr,"%s:%s:%d: lock %d (%s)\n", \
__FILE__,__func__,__LINE__, \
dprintf_status, strerror(dprintf_status)); \
exit(1); \
} \
printf arg ; \
fflush(stdout); \
dprintf_status = pthread_mutex_unlock(&outputLock); \
if(dprintf_status) { \
fprintf(stderr,"%s:%s:%d: unlock %d (%s)\n", \
__FILE__,__func__,__LINE__, \
dprintf_status, strerror(dprintf_status)); \
exit(1); \
} \
}while(0)
# else
# define DPRINTF(arg)
# endif
#endif