Tizen 2.1 base
[platform/upstream/gcd.git] / pthread_workqueue-0.8.2 / src / debug.h
1 /*
2  * Copyright (c) 2011 Mark Heily <mark@heily.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #ifndef  _DEBUG_H
18 #define  _DEBUG_H
19
20 #include <assert.h>
21
22 extern int DEBUG_WORKQUEUE;
23 extern char *WORKQUEUE_DEBUG_IDENT;
24
25 #if defined(__linux__)
26
27 #include <linux/unistd.h>
28 #include <sys/syscall.h>
29 #include <unistd.h>
30
31 # define THREAD_ID ((pid_t)  syscall(__NR_gettid))
32 #elif defined(__sun)
33 # define THREAD_ID (pthread_self())
34 #elif defined(__FreeBSD__)  /* FIXME -- could use thr_self() */
35 # define THREAD_ID (0)
36 #elif defined(_WIN32)
37 # define THREAD_ID (int)(GetCurrentThreadId())
38 #else 
39 # error Unsupported platform
40 #endif
41
42
43 #ifndef NDEBUG
44 #define dbg_puts(str)           do {                                \
45     if (DEBUG_WORKQUEUE)                                                      \
46       fprintf(stderr, "%s [%d]: %s(): %s\n",                        \
47               WORKQUEUE_DEBUG_IDENT, THREAD_ID, __func__, str);               \
48 } while (0)
49
50 #define dbg_printf(fmt,...)     do {                                \
51     if (DEBUG_WORKQUEUE)                                                      \
52       fprintf(stderr, "%s [%d]: %s(): "fmt"\n",                     \
53               WORKQUEUE_DEBUG_IDENT, THREAD_ID, __func__, __VA_ARGS__);       \
54 } while (0)
55
56 #define dbg_perror(str)         do {                                \
57     if (DEBUG_WORKQUEUE)                                                      \
58       fprintf(stderr, "%s [%d]: %s(): %s: %s (errno=%d)\n",         \
59               WORKQUEUE_DEBUG_IDENT, THREAD_ID, __func__, str,                \
60               strerror(errno), errno);                              \
61 } while (0)
62
63 # define reset_errno()          do { errno = 0; } while (0)
64
65 # if defined(_WIN32)
66 #  define dbg_lasterror(str)     do {                                \
67     if (DEBUG_WORKQUEUE)                                                      \
68       fprintf(stderr, "%s: [%d] %s(): %s: (LastError=%d)\n",        \
69               THREAD_ID, __func__, str, GetLastError());            \
70 } while (0)
71 # else
72 #  define dbg_lasterror(str)     ;
73 # endif
74
75 #else /* NDEBUG */
76 # define dbg_puts(str)           ;
77 # define dbg_printf(fmt,...)     ;
78 # define dbg_perror(str)         ;
79 # define dbg_lasterror(str)      ;
80 # define reset_errno()           ;
81 #endif 
82
83 #endif  /* ! _DEBUG_H */