source: box/trunk/lib/common/DebugPrintf.cpp @ 2415

Revision 2415, 1.6 KB checked in by chris, 3 years ago (diff)

Rename NDEBUG flag to BOX_RELEASE_BUILD, as other projects use NDEBUG as
well (e.g. wxWidgets) and it causes conflicts which are difficult to
resolve.

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    DebugPrintf.cpp
5//              Purpose: Implementation of a printf function, to avoid a stdio.h include in Box.h
6//              Created: 2003/09/06
7//
8// --------------------------------------------------------------------------
9
10#ifndef BOX_RELEASE_BUILD
11
12#include "Box.h"
13
14#include <stdio.h>
15#include <stdarg.h>
16
17#ifdef WIN32
18        #include "emu.h"
19#else
20        #include <syslog.h>
21#endif
22
23#include "MemLeakFindOn.h"
24
25// Use this apparently superflous printf function to avoid having to
26// include stdio.h in every file in the project.
27
28int BoxDebug_printf(const char *format, ...)
29{
30        va_list ap;
31        va_start(ap, format);
32        int r = vprintf(format, ap);
33        va_end(ap);
34        return r;
35}
36
37
38bool BoxDebugTraceOn = true;
39bool BoxDebugTraceToStdout = true;
40bool BoxDebugTraceToSyslog = false;
41
42int BoxDebugTrace(const char *format, ...)
43{
44        char text[512];
45        int r = 0;
46        if(BoxDebugTraceOn || BoxDebugTraceToSyslog)
47        {
48                va_list ap;
49                va_start(ap, format);
50                r = vsnprintf(text, sizeof(text), format, ap);
51                va_end(ap);
52        }
53
54        // Send to stdout if trace is on and std out is enabled
55        if(BoxDebugTraceOn && BoxDebugTraceToStdout)
56        {
57                printf("%s", text);             
58        }
59
60        // But tracing to syslog is independent of tracing being on or not
61        if(BoxDebugTraceToSyslog)
62        {
63#ifdef WIN32           
64                // Remove trailing '\n', if it's there
65                if(r > 0 && text[r-1] == '\n')
66                {
67                        text[r-1] = '\0';
68#else
69                if(r > 0 && text[r] == '\n')
70                {
71                        text[r] = '\0';
72#endif
73                        --r;
74                }
75                // Log it
76                ::syslog(LOG_INFO, "TRACE: %s", text);
77        }
78
79        return r;
80}
81
82
83#endif // BOX_RELEASE_BUILD
Note: See TracBrowser for help on using the repository browser.