| 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 | |
|---|
| 28 | int 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 | |
|---|
| 38 | bool BoxDebugTraceOn = true; |
|---|
| 39 | bool BoxDebugTraceToStdout = true; |
|---|
| 40 | bool BoxDebugTraceToSyslog = false; |
|---|
| 41 | |
|---|
| 42 | int 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 |
|---|