]> git.cworth.org Git - apitrace/blob - log.cpp
Rename GL spec parser.
[apitrace] / log.cpp
1 /**************************************************************************
2  *
3  * Copyright 2007-2009 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <map>
34
35 #include <zlib.h>
36
37 #include "os.hpp"
38 #include "log.hpp"
39 #include "trace_format.hpp"
40
41
42 namespace Log {
43
44
45 static gzFile g_gzFile = NULL;
46 static void _Close(void) {
47    if(g_gzFile != NULL) {
48       gzclose(g_gzFile);
49       g_gzFile = NULL;
50    }
51 }
52
53 static int reentrancy = 0;
54
55 static void _Open(const char *szExtension) {
56    _Close();
57    
58    static unsigned dwCounter = 0;
59
60    char szFileName[PATH_MAX];
61    const char *lpFileName;
62
63    lpFileName = getenv("TRACE_PATH");
64    if (lpFileName) {
65        strncpy(szFileName, lpFileName, PATH_MAX);
66    }
67    else {
68        char szProcessName[PATH_MAX];
69        char szCurrentDir[PATH_MAX];
70        OS::GetProcessName(szProcessName, PATH_MAX);
71        OS::GetCurrentDir(szCurrentDir, PATH_MAX);
72
73        for(;;) {
74           FILE *file;
75           
76           if (dwCounter)
77              snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension);
78           else
79              snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension);
80           
81           file = fopen(szFileName, "rb");
82           if(file == NULL)
83              break;
84           
85           fclose(file);
86           
87           ++dwCounter;
88        }
89    }
90
91    {
92        char szMessage[PATH_MAX];
93        snprintf(szMessage, PATH_MAX, "Tracing to %s\n", szFileName);
94        OS::DebugMessage(szMessage);
95    }
96
97    g_gzFile = gzopen(szFileName, "wb");
98 }
99
100 static inline void Write(const void *sBuffer, size_t dwBytesToWrite) {
101    if (g_gzFile == NULL)
102       return;
103    
104    if (reentrancy > 1)
105       return;
106
107    gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
108 }
109
110 static inline void 
111 WriteByte(char c) {
112    Write(&c, 1);
113 }
114
115 void inline 
116 WriteUInt(unsigned long long value) {
117    char buf[2 * sizeof value];
118    unsigned len;
119
120    len = 0;
121    do {
122       assert(len < sizeof buf);
123       buf[len] = 0x80 | (value & 0x7f);
124       value >>= 7;
125       ++len;
126    } while (value);
127
128    assert(len);
129    buf[len - 1] &= 0x7f;
130
131    Write(buf, len);
132 }
133
134 static inline void 
135 WriteFloat(float value) {
136    assert(sizeof value == 4);
137    Write((const char *)&value, sizeof value);
138 }
139
140 static inline void 
141 WriteDouble(double value) {
142    assert(sizeof value == 8);
143    Write((const char *)&value, sizeof value);
144 }
145
146 static inline void 
147 WriteString(const char *str) {
148    size_t len = strlen(str);
149    WriteUInt(len);
150    Write(str, len);
151 }
152
153 typedef std::map<const char *, size_t> namemap;
154 static namemap names;
155
156 static inline void 
157 WriteName(const char *name) {
158    namemap::iterator it = names.find(name);
159    if (it == names.end()) {
160        size_t name_id = names.size();
161        WriteUInt(name_id);
162        WriteString(name);
163        names[name] = name_id;
164    } else {
165        WriteUInt(it->second);
166    }
167 }
168
169 void Open(void) {
170     if (!g_gzFile) {
171         _Open("trace");
172         WriteUInt(TRACE_VERSION);
173     }
174 }
175
176 void Close(void) {
177    _Close();
178 }
179
180 void BeginCall(const char *function) {
181    OS::AcquireMutex();
182    Open();
183    ++reentrancy;
184    WriteName(function);
185 }
186
187 void EndCall(void) {
188    WriteByte(Trace::CALL_END);
189    --reentrancy;
190    gzflush(g_gzFile, Z_SYNC_FLUSH);
191    OS::ReleaseMutex();
192 }
193
194 void BeginArg(unsigned index, const char *name) {
195    WriteByte(Trace::CALL_ARG);
196    WriteUInt(index);
197    WriteName(name);
198 }
199
200 void BeginReturn(void) {
201    WriteByte(Trace::CALL_RET);
202 }
203
204 void BeginArray(size_t length) {
205    WriteByte(Trace::TYPE_ARRAY);
206    WriteUInt(length);
207 }
208
209 void BeginStruct(size_t length) {
210    WriteByte(Trace::TYPE_STRUCT);
211    WriteUInt(length);
212 }
213
214 void BeginMember(const char *name) {
215    WriteName(name);
216 }
217
218 void BeginBitmask(void) {
219    WriteByte(Trace::TYPE_BITMASK);
220 }
221
222 void EndBitmask(void) {
223    WriteByte(Trace::TYPE_NULL);
224 }
225
226 void LiteralBool(bool value) {
227    WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
228 }
229
230 void LiteralSInt(signed long long value) {
231    if (value < 0) {
232       WriteByte(Trace::TYPE_SINT);
233       WriteUInt(-value);
234    } else {
235       WriteByte(Trace::TYPE_UINT);
236       WriteUInt(value);
237    }
238 }
239
240 void LiteralUInt(unsigned long long value) {
241    WriteByte(Trace::TYPE_UINT);
242    WriteUInt(value);
243 }
244
245 void LiteralFloat(float value) {
246    WriteByte(Trace::TYPE_FLOAT);
247    WriteFloat(value);
248 }
249
250 void LiteralFloat(double value) {
251    WriteByte(Trace::TYPE_DOUBLE);
252    WriteDouble(value);
253 }
254
255 void LiteralString(const char *str) {
256    if (!str) {
257       LiteralNull();
258       return;
259    }
260    WriteByte(Trace::TYPE_STRING);
261    WriteString(str);
262 }
263
264 void LiteralString(const char *str, size_t len) {
265    if (!str) {
266       LiteralNull();
267       return;
268    }
269    WriteByte(Trace::TYPE_STRING);
270    WriteUInt(len);
271    Write(str, len);
272 }
273
274 void LiteralWString(const wchar_t *str) {
275    if (!str) {
276       LiteralNull();
277       return;
278    }
279    WriteByte(Trace::TYPE_STRING);
280    WriteString("<wide-string>");
281 }
282    
283 void LiteralBlob(const void *data, size_t size) {
284    if (!data) {
285       LiteralNull();
286       return;
287    }
288    WriteByte(Trace::TYPE_BLOB);
289    WriteUInt(size);
290    if (size) {
291       Write(data, size);
292    }
293 }
294
295 void LiteralNamedConstant(const char *name, long long value) {
296    WriteByte(Trace::TYPE_CONST);
297    WriteName(name);
298    LiteralSInt(value);
299 }
300
301 void LiteralNull(void) {
302    WriteByte(Trace::TYPE_NULL);
303 }
304
305 void LiteralOpaque(const void *addr) {
306    if (!addr) {
307       LiteralNull();
308       return;
309    }
310    WriteByte(Trace::TYPE_OPAQUE);
311    WriteUInt((size_t)addr);
312 }
313
314 void Abort(void) {
315     Close();
316     OS::Abort();
317 }
318
319 } /* namespace Log */