]> git.cworth.org Git - apitrace/blob - log.cpp
Cleanup files.
[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 <zlib.h>
34
35 #include "os.hpp"
36 #include "log.hpp"
37 #include "trace_format.hpp"
38
39
40 namespace Log {
41
42
43 static gzFile g_gzFile = NULL;
44 static void _Close(void) {
45    if(g_gzFile != NULL) {
46       gzclose(g_gzFile);
47       g_gzFile = NULL;
48    }
49 }
50
51 static void _Open(const char *szName, const char *szExtension) {
52    _Close();
53    
54    static unsigned dwCounter = 0;
55
56    char szProcessName[PATH_MAX];
57    char szFileName[PATH_MAX];
58
59    OS::GetProcessName(szProcessName, PATH_MAX);
60
61    for(;;) {
62       FILE *file;
63       
64       if(dwCounter)
65          snprintf(szFileName, PATH_MAX, "%s.%s.%u.%s", szProcessName, szName, dwCounter, szExtension);
66       else
67          snprintf(szFileName, PATH_MAX, "%s.%s.%s", szProcessName, szName, szExtension);
68       
69       file = fopen(szFileName, "rb");
70       if(file == NULL)
71          break;
72       
73       fclose(file);
74       
75       ++dwCounter;
76    }
77
78    fprintf(stderr, "Logging to %s\n", szFileName);
79    g_gzFile = gzopen(szFileName, "wb");
80 }
81
82 static inline void Write(const char *sBuffer, size_t dwBytesToWrite) {
83    if(g_gzFile == NULL)
84       return;
85    
86    gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
87 }
88
89 static inline void 
90 WriteByte(char c) {
91    Write(&c, 1);
92 }
93
94 void inline 
95 WriteUInt(unsigned long long value) {
96    char buf[2 * sizeof value];
97    unsigned len;
98
99    len = 0;
100    do {
101       assert(len < sizeof buf);
102       buf[len] = 0x80 | (value & 0x7f);
103       value >>= 7;
104       ++len;
105    } while (value);
106
107    assert(len);
108    buf[len - 1] &= 0x7f;
109
110    Write(buf, len);
111 }
112
113 static inline void 
114 WriteFloat(float value) {
115    assert(sizeof value == 4);
116    Write((const char *)&value, sizeof value);
117 }
118
119 static inline void 
120 WriteDouble(double value) {
121    assert(sizeof value == 8);
122    Write((const char *)&value, sizeof value);
123 }
124
125 static inline void 
126 WriteString(const char *str) {
127    size_t len = strlen(str);
128    WriteUInt(len);
129    Write(str, len);
130 }
131
132 void Open(const char *name) {
133    _Open(name, "trace");
134    WriteUInt(TRACE_VERSION);
135 }
136
137 void Close(void) {
138    _Close();
139 }
140
141 void BeginCall(const char *function) {
142    OS::AcquireMutex();
143    WriteString(function);
144 }
145
146 void EndCall(void) {
147    WriteByte(Trace::CALL_END);
148    gzflush(g_gzFile, Z_SYNC_FLUSH);
149    OS::ReleaseMutex();
150 }
151
152 void BeginArg(const char *type, const char *name) {
153    WriteByte(Trace::CALL_ARG);
154    WriteString(name);
155 }
156
157 void EndArg(void) { }
158
159 void BeginReturn(const char *type) {
160    WriteByte(Trace::CALL_RET);
161 }
162
163 void EndReturn(void) { }
164
165 void BeginArray(const char *type, size_t length) {
166    WriteByte(Trace::TYPE_ARRAY);
167    WriteUInt(length);
168 }
169
170 void EndArray(void) { }
171
172 void BeginElement(const char *type) { }
173
174 void EndElement(void) { }
175
176 void BeginStruct(const char *type) {
177    WriteByte(Trace::TYPE_STRUCT);
178 }
179
180 void EndStruct(void) {
181    WriteString("");
182 }
183
184 void BeginMember(const char *type, const char *name) {
185    WriteString(name);
186 }
187
188 void EndMember(void) { }
189
190 void BeginBitmask(const char *type) {
191    WriteByte(Trace::TYPE_BITMASK);
192 }
193
194 void EndBitmask(void) {
195    WriteByte(Trace::TYPE_VOID);
196 }
197
198 void BeginPointer(const char *type, const void *addr)
199 {
200    WriteByte(Trace::TYPE_POINTER);
201    WriteUInt((size_t)addr);
202 }
203
204 void EndPointer(void) { }
205
206 void LiteralBool(bool value) {
207    WriteByte(Trace::TYPE_BOOL);
208    WriteByte(value ? 0 : 1);
209 }
210
211 void LiteralSInt(signed long long value) {
212    if (value < 0) {
213       WriteByte(Trace::TYPE_SINT);
214       WriteUInt(-value);
215    } else {
216       WriteByte(Trace::TYPE_UINT);
217       WriteUInt(value);
218    }
219 }
220
221 void LiteralUInt(unsigned long long value) {
222    WriteByte(Trace::TYPE_UINT);
223    WriteUInt(value);
224 }
225
226 void LiteralFloat(float value) {
227    WriteByte(Trace::TYPE_FLOAT);
228    WriteFloat(value);
229 }
230
231 void LiteralFloat(double value) {
232    WriteByte(Trace::TYPE_DOUBLE);
233    WriteDouble(value);
234 }
235
236 void LiteralString(const char *str) {
237    if (!str) {
238       LiteralNull();
239       return;
240    }
241    WriteByte(Trace::TYPE_STRING);
242    WriteString(str);
243 }
244
245 void LiteralWString(const wchar_t *str) {
246
247    if (!str) {
248       LiteralNull();
249       return;
250    }
251    WriteByte(Trace::TYPE_STRING);
252    WriteString("<wide-string>");
253 }
254    
255 void LiteralNamedConstant(const char *name, long long value) {
256    WriteByte(Trace::TYPE_CONST);
257    WriteString(name);
258    LiteralSInt(value);
259 }
260
261 void LiteralNull(void) {
262    WriteByte(Trace::TYPE_POINTER);
263    WriteUInt(0);
264    WriteByte(Trace::TYPE_OPAQUE);
265 }
266
267 void LiteralOpaque(void) {
268    WriteByte(Trace::TYPE_OPAQUE);
269 }
270
271 } /* namespace Log */