~ubuntu-branches/ubuntu/saucy/libpthread-workqueue/saucy

« back to all changes in this revision

Viewing changes to src/debug.h

  • Committer: Bazaar Package Importer
  • Author(s): Mark Heily
  • Date: 2011-03-13 16:22:30 UTC
  • Revision ID: james.westby@ubuntu.com-20110313162230-yaiyoa7g3dk8xmww
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
23
extern char *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(_WIN32)
 
35
# define THREAD_ID (GetCurrentThreadId())
 
36
#else 
 
37
# error Unsupported platform
 
38
#endif
 
39
 
 
40
 
 
41
#ifndef NDEBUG
 
42
#define dbg_puts(str)           do {                                \
 
43
    if (DEBUG)                                                      \
 
44
      fprintf(stderr, "%s [%d]: %s(): %s\n",                        \
 
45
              DEBUG_IDENT, THREAD_ID, __func__, str);               \
 
46
} while (0)
 
47
 
 
48
#define dbg_printf(fmt,...)     do {                                \
 
49
    if (DEBUG)                                                      \
 
50
      fprintf(stderr, "%s [%d]: %s(): "fmt"\n",                     \
 
51
              DEBUG_IDENT, THREAD_ID, __func__, __VA_ARGS__);       \
 
52
} while (0)
 
53
 
 
54
#define dbg_perror(str)         do {                                \
 
55
    if (DEBUG)                                                      \
 
56
      fprintf(stderr, "%s [%d]: %s(): %s: %s (errno=%d)\n",         \
 
57
              DEBUG_IDENT, THREAD_ID, __func__, str,                \
 
58
              strerror(errno), errno);                              \
 
59
} while (0)
 
60
 
 
61
# define reset_errno()          do { errno = 0; } while (0)
 
62
 
 
63
# if defined(_WIN32)
 
64
#  define dbg_lasterror(str)     do {                                \
 
65
    if (DEBUG)                                                      \
 
66
      fprintf(stderr, "%s: [%d] %s(): %s: (LastError=%d)\n",        \
 
67
              THREAD_ID, __func__, str, GetLastError());            \
 
68
} while (0)
 
69
# else
 
70
#  define dbg_lasterror(str)     ;
 
71
# endif
 
72
 
 
73
#else /* NDEBUG */
 
74
# define dbg_puts(str)           ;
 
75
# define dbg_printf(fmt,...)     ;
 
76
# define dbg_perror(str)         ;
 
77
# define dbg_lasterror(str)      ;
 
78
# define reset_errno()           ;
 
79
#endif 
 
80
 
 
81
#endif  /* ! _DEBUG_H */