Debian Codegraph @

external/portaudio/pa_trace.c

raw symbols SCIP
1 /*
2 * $Id$
3 * Portable Audio I/O Library Trace Facility
4 * Store trace information in real-time for later printing.
5 *
6 * Based on the Open Source API proposed by Ross Bencina
7 * Copyright (c) 1999-2000 Phil Burk
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files
11 * (the "Software"), to deal in the Software without restriction,
12 * including without limitation the rights to use, copy, modify, merge,
13 * publish, distribute, sublicense, and/or sell copies of the Software,
14 * and to permit persons to whom the Software is furnished to do so,
15 * subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 */
28
29 /*
30 * The text above constitutes the entire PortAudio license; however,
31 * the PortAudio community also makes the following non-binding requests:
32 *
33 * Any person wishing to distribute modifications to the Software is
34 * requested to send the modifications to the original developer so that
35 * they can be incorporated into the canonical version. It is also
36 * requested that these non-binding requests be included along with the
37 * license above.
38 */
39
40 /** @file
41 @ingroup common_src
42
43 @brief Real-time safe event trace logging facility for debugging.
44 */
45
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <stdarg.h>
50 #include <string.h>
51 #include <assert.h>
52 #include "pa_trace.h"
53 #include "pa_util.h"
54 #include "pa_debugprint.h"
55
56 #if PA_TRACE_REALTIME_EVENTS
57
58 static char const *traceTextArray[PA_MAX_TRACE_RECORDS];
59 static int traceIntArray[PA_MAX_TRACE_RECORDS];
60 static int traceIndex = 0;
61 static int traceBlock = 0;
62
63 /*********************************************************************/
64 void PaUtil_ResetTraceMessages()
65 {
66 traceIndex = 0;
67 }
68
69 /*********************************************************************/
70 void PaUtil_DumpTraceMessages()
71 {
72 int i;
73 int messageCount = (traceIndex < PA_MAX_TRACE_RECORDS) ? traceIndex : PA_MAX_TRACE_RECORDS;
74
75 printf("DumpTraceMessages: traceIndex = %d\n", traceIndex );
76 for( i=0; i<messageCount; i++ )
77 {
78 printf("%3d: %s = 0x%08X\n",
79 i, traceTextArray[i], traceIntArray[i] );
80 }
81 PaUtil_ResetTraceMessages();
82 fflush(stdout);
83 }
84
85 /*********************************************************************/
86 void PaUtil_AddTraceMessage( const char *msg, int data )
87 {
88 if( (traceIndex == PA_MAX_TRACE_RECORDS) && (traceBlock == 0) )
89 {
90 traceBlock = 1;
91 /* PaUtil_DumpTraceMessages(); */
92 }
93 else if( traceIndex < PA_MAX_TRACE_RECORDS )
94 {
95 traceTextArray[traceIndex] = msg;
96 traceIntArray[traceIndex] = data;
97 traceIndex++;
98 }
99 }
100
101 /************************************************************************/
102 /* High performance log alternative */
103 /************************************************************************/
104
105 typedef unsigned long long PaUint64;
106
107 typedef struct __PaHighPerformanceLog
108 {
109 unsigned magik;
110 int writePtr;
111 int readPtr;
112 int size;
113 double refTime;
114 char* data;
115 } PaHighPerformanceLog;
116
117 static const unsigned kMagik = 0xcafebabe;
118
119 #define USEC_PER_SEC (1000000ULL)
120
121 int PaUtil_InitializeHighSpeedLog( LogHandle* phLog, unsigned maxSizeInBytes )
122 {
123 PaHighPerformanceLog* pLog = (PaHighPerformanceLog*)PaUtil_AllocateMemory(sizeof(PaHighPerformanceLog));
124 if (pLog == 0)
125 {
126 return paInsufficientMemory;
127 }
128 assert(phLog != 0);
129 *phLog = pLog;
130
131 pLog->data = (char*)PaUtil_AllocateMemory(maxSizeInBytes);
132 if (pLog->data == 0)
133 {
134 PaUtil_FreeMemory(pLog);
135 return paInsufficientMemory;
136 }
137 pLog->magik = kMagik;
138 pLog->size = maxSizeInBytes;
139 pLog->refTime = PaUtil_GetTime();
140 return paNoError;
141 }
142
143 void PaUtil_ResetHighSpeedLogTimeRef( LogHandle hLog )
144 {
145 PaHighPerformanceLog* pLog = (PaHighPerformanceLog*)hLog;
146 assert(pLog->magik == kMagik);
147 pLog->refTime = PaUtil_GetTime();
148 }
149
150 typedef struct __PaLogEntryHeader
151 {
152 int size;
153 double timeStamp;
154 } PaLogEntryHeader;
155
156 #ifdef __APPLE__
157 #define _vsnprintf vsnprintf
158 #define min(a,b) ((a)<(b)?(a):(b))
159 #endif
160
161
162 int PaUtil_AddHighSpeedLogMessage( LogHandle hLog, const char* fmt, ... )
163 {
164 va_list l;
165 int n = 0;
166 PaHighPerformanceLog* pLog = (PaHighPerformanceLog*)hLog;
167 if (pLog != 0)
168 {
169 PaLogEntryHeader* pHeader;
170 char* p;
171 int maxN;
172 assert(pLog->magik == kMagik);
173 pHeader = (PaLogEntryHeader*)( pLog->data + pLog->writePtr );
174 p = (char*)( pHeader + 1 );
175 maxN = pLog->size - pLog->writePtr - 2 * sizeof(PaLogEntryHeader);
176
177 pHeader->timeStamp = PaUtil_GetTime() - pLog->refTime;
178 if (maxN > 0)
179 {
180 if (maxN > 32)
181 {
182 va_start(l, fmt);
183 n = _vsnprintf(p, min(1024, maxN), fmt, l);
184 va_end(l);
185 }
186 else {
187 n = sprintf(p, "End of log...");
188 }
189 n = ((n + sizeof(unsigned)) & ~(sizeof(unsigned)-1)) + sizeof(PaLogEntryHeader);
190 pHeader->size = n;
191 #if 0
192 PaUtil_DebugPrint("%05u.%03u: %s\n", pHeader->timeStamp/1000, pHeader->timeStamp%1000, p);
193 #endif
194 pLog->writePtr += n;
195 }
196 }
197 return n;
198 }
199
200 void PaUtil_DumpHighSpeedLog( LogHandle hLog, const char* fileName )
201 {
202 FILE* f = (fileName != NULL) ? fopen(fileName, "w") : stdout;
203 unsigned localWritePtr;
204 PaHighPerformanceLog* pLog = (PaHighPerformanceLog*)hLog;
205 assert(pLog->magik == kMagik);
206 localWritePtr = pLog->writePtr;
207 while (pLog->readPtr != localWritePtr)
208 {
209 const PaLogEntryHeader* pHeader = (const PaLogEntryHeader*)( pLog->data + pLog->readPtr );
210 const char* p = (const char*)( pHeader + 1 );
211 const PaUint64 ts = (const PaUint64)( pHeader->timeStamp * USEC_PER_SEC );
212 assert(pHeader->size < (1024+sizeof(unsigned)+sizeof(PaLogEntryHeader)));
213 fprintf(f, "%05u.%03u: %s\n", (unsigned)(ts/1000), (unsigned)(ts%1000), p);
214 pLog->readPtr += pHeader->size;
215 }
216 if (f != stdout)
217 {
218 fclose(f);
219 }
220 }
221
222 void PaUtil_DiscardHighSpeedLog( LogHandle hLog )
223 {
224 PaHighPerformanceLog* pLog = (PaHighPerformanceLog*)hLog;
225 assert(pLog->magik == kMagik);
226 PaUtil_FreeMemory(pLog->data);
227 PaUtil_FreeMemory(pLog);
228 }
229
230 #else
231 /* This stub was added so that this file will generate a symbol.
232 * Otherwise linker/archiver programs will complain.
233 */
234 int PaUtil_TraceStubToSatisfyLinker(void)
235 {
236 return 0;
237 }
238 #endif /* TRACE_REALTIME_EVENTS */