cannam@124
|
1 /*
|
cannam@124
|
2 * $Id: pa_log.c $
|
cannam@124
|
3 * Portable Audio I/O Library Multi-Host API front end
|
cannam@124
|
4 * Validate function parameters and manage multiple host APIs.
|
cannam@124
|
5 *
|
cannam@124
|
6 * Based on the Open Source API proposed by Ross Bencina
|
cannam@124
|
7 * Copyright (c) 1999-2006 Ross Bencina, Phil Burk
|
cannam@124
|
8 *
|
cannam@124
|
9 * Permission is hereby granted, free of charge, to any person obtaining
|
cannam@124
|
10 * a copy of this software and associated documentation files
|
cannam@124
|
11 * (the "Software"), to deal in the Software without restriction,
|
cannam@124
|
12 * including without limitation the rights to use, copy, modify, merge,
|
cannam@124
|
13 * publish, distribute, sublicense, and/or sell copies of the Software,
|
cannam@124
|
14 * and to permit persons to whom the Software is furnished to do so,
|
cannam@124
|
15 * subject to the following conditions:
|
cannam@124
|
16 *
|
cannam@124
|
17 * The above copyright notice and this permission notice shall be
|
cannam@124
|
18 * included in all copies or substantial portions of the Software.
|
cannam@124
|
19 *
|
cannam@124
|
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
cannam@124
|
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
cannam@124
|
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
cannam@124
|
23 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
cannam@124
|
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
cannam@124
|
25 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
cannam@124
|
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
cannam@124
|
27 */
|
cannam@124
|
28
|
cannam@124
|
29 /*
|
cannam@124
|
30 * The text above constitutes the entire PortAudio license; however,
|
cannam@124
|
31 * the PortAudio community also makes the following non-binding requests:
|
cannam@124
|
32 *
|
cannam@124
|
33 * Any person wishing to distribute modifications to the Software is
|
cannam@124
|
34 * requested to send the modifications to the original developer so that
|
cannam@124
|
35 * they can be incorporated into the canonical version. It is also
|
cannam@124
|
36 * requested that these non-binding requests be included along with the
|
cannam@124
|
37 * license above.
|
cannam@124
|
38 */
|
cannam@124
|
39
|
cannam@124
|
40 /** @file
|
cannam@124
|
41 @ingroup common_src
|
cannam@124
|
42
|
cannam@124
|
43 @brief Implements log function.
|
cannam@124
|
44
|
cannam@124
|
45 PaUtil_SetLogPrintFunction can be user called to replace the provided
|
cannam@124
|
46 DefaultLogPrint function, which writes to stderr.
|
cannam@124
|
47 One can NOT pass var_args across compiler/dll boundaries as it is not
|
cannam@124
|
48 "byte code/abi portable". So the technique used here is to allocate a local
|
cannam@124
|
49 a static array, write in it, then callback the user with a pointer to its
|
cannam@124
|
50 start.
|
cannam@124
|
51 */
|
cannam@124
|
52
|
cannam@124
|
53 #include <stdio.h>
|
cannam@124
|
54 #include <stdarg.h>
|
cannam@124
|
55
|
cannam@124
|
56 #include "pa_debugprint.h"
|
cannam@124
|
57
|
cannam@124
|
58 // for OutputDebugStringA
|
cannam@124
|
59 #if defined(_MSC_VER) && defined(PA_ENABLE_MSVC_DEBUG_OUTPUT)
|
cannam@124
|
60 #define WIN32_LEAN_AND_MEAN // exclude rare headers
|
cannam@124
|
61 #include "windows.h"
|
cannam@124
|
62 #endif
|
cannam@124
|
63
|
cannam@124
|
64 // User callback
|
cannam@124
|
65 static PaUtilLogCallback userCB = NULL;
|
cannam@124
|
66
|
cannam@124
|
67 // Sets user callback
|
cannam@124
|
68 void PaUtil_SetDebugPrintFunction(PaUtilLogCallback cb)
|
cannam@124
|
69 {
|
cannam@124
|
70 userCB = cb;
|
cannam@124
|
71 }
|
cannam@124
|
72
|
cannam@124
|
73 /*
|
cannam@124
|
74 If your platform doesn’t have vsnprintf, you are stuck with a
|
cannam@124
|
75 VERY dangerous alternative, vsprintf (with no n)
|
cannam@124
|
76 */
|
cannam@124
|
77 #if _MSC_VER
|
cannam@124
|
78 /* Some Windows Mobile SDKs don't define vsnprintf but all define _vsnprintf (hopefully).
|
cannam@124
|
79 According to MSDN "vsnprintf is identical to _vsnprintf". So we use _vsnprintf with MSC.
|
cannam@124
|
80 */
|
cannam@124
|
81 #define VSNPRINTF _vsnprintf
|
cannam@124
|
82 #else
|
cannam@124
|
83 #define VSNPRINTF vsnprintf
|
cannam@124
|
84 #endif
|
cannam@124
|
85
|
cannam@124
|
86 #define PA_LOG_BUF_SIZE 2048
|
cannam@124
|
87
|
cannam@124
|
88 void PaUtil_DebugPrint( const char *format, ... )
|
cannam@124
|
89 {
|
cannam@124
|
90 // Optional logging into Output console of Visual Studio
|
cannam@124
|
91 #if defined(_MSC_VER) && defined(PA_ENABLE_MSVC_DEBUG_OUTPUT)
|
cannam@124
|
92 {
|
cannam@124
|
93 char buf[PA_LOG_BUF_SIZE];
|
cannam@124
|
94 va_list ap;
|
cannam@124
|
95 va_start(ap, format);
|
cannam@124
|
96 VSNPRINTF(buf, sizeof(buf), format, ap);
|
cannam@124
|
97 buf[sizeof(buf)-1] = 0;
|
cannam@124
|
98 OutputDebugStringA(buf);
|
cannam@124
|
99 va_end(ap);
|
cannam@124
|
100 }
|
cannam@124
|
101 #endif
|
cannam@124
|
102
|
cannam@124
|
103 // Output to User-Callback
|
cannam@124
|
104 if (userCB != NULL)
|
cannam@124
|
105 {
|
cannam@124
|
106 char strdump[PA_LOG_BUF_SIZE];
|
cannam@124
|
107 va_list ap;
|
cannam@124
|
108 va_start(ap, format);
|
cannam@124
|
109 VSNPRINTF(strdump, sizeof(strdump), format, ap);
|
cannam@124
|
110 strdump[sizeof(strdump)-1] = 0;
|
cannam@124
|
111 userCB(strdump);
|
cannam@124
|
112 va_end(ap);
|
cannam@124
|
113 }
|
cannam@124
|
114 else
|
cannam@124
|
115 // Standard output to stderr
|
cannam@124
|
116 {
|
cannam@124
|
117 va_list ap;
|
cannam@124
|
118 va_start(ap, format);
|
cannam@124
|
119 vfprintf(stderr, format, ap);
|
cannam@124
|
120 va_end(ap);
|
cannam@124
|
121 fflush(stderr);
|
cannam@124
|
122 }
|
cannam@124
|
123 }
|