]> git.notmuchmail.org Git - apitrace/blob - log.hpp
Dump arguments.
[apitrace] / log.hpp
1
2 #ifndef _LOG_HPP_
3 #define _LOG_HPP_
4
5 #include <windows.h>
6 #include <tchar.h>
7 #include <stdio.h>
8
9
10 class File
11 {
12 public:
13     File(const TCHAR *szName, const TCHAR *szExtension) {
14         m_hFile = INVALID_HANDLE_VALUE;
15         Open(szName, szExtension);
16     }
17     
18     ~File() {
19         Close();
20     }
21
22     void Open(const TCHAR *szName, const TCHAR *szExtension) {
23         Close();
24         
25         DWORD dwCounter = 0;
26         do {
27             if(dwCounter)
28                 _sntprintf(szFileName, MAX_PATH, TEXT("%s.%u.%s"), szName, dwCounter, szExtension);
29             else
30                 _sntprintf(szFileName, MAX_PATH, TEXT("%s.%s"), szName, szExtension);
31
32             m_hFile = CreateFile(szFileName,
33                                  GENERIC_WRITE,
34                                  FILE_SHARE_WRITE,
35                                  NULL,
36                                  CREATE_NEW,
37                                  FILE_ATTRIBUTE_NORMAL,
38                                  NULL);
39             ++dwCounter;
40         } while(m_hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS);
41     }
42     
43     void ReOpen(void) {
44         Close();
45         
46         m_hFile = CreateFile(szFileName,
47                              GENERIC_WRITE,
48                              0,
49                              NULL,
50                              OPEN_EXISTING,
51                              FILE_ATTRIBUTE_NORMAL,
52                              NULL);
53     }
54     
55     void Close(void) {
56         if(m_hFile != INVALID_HANDLE_VALUE) {
57             CloseHandle(m_hFile);
58             m_hFile = INVALID_HANDLE_VALUE;
59         }
60     }
61     
62     void Write(const char *szText) {
63         if(m_hFile == INVALID_HANDLE_VALUE)
64             return;
65         
66         DWORD dwBytesToWrite = (DWORD)strlen(szText);
67         DWORD dwBytesWritten = 0;
68         
69         while (dwBytesWritten < dwBytesToWrite) {
70             OVERLAPPED overlapped;
71             memset(&overlapped, 0, sizeof(OVERLAPPED));
72
73             /* Write to end of file */
74             overlapped.Offset = 0xffffffff;
75             overlapped.OffsetHigh = 0xffffffff;
76             
77             if(WriteFile(m_hFile,
78                          szText + dwBytesWritten,
79                          dwBytesToWrite - dwBytesWritten,
80                          &dwBytesWritten,
81                          &overlapped) == FALSE) {
82                 Close();
83                 Open(TEXT("extra"), TEXT("xml"));
84                 return;
85             }
86         }
87     }
88     
89 private:
90     HANDLE m_hFile;
91     TCHAR szFileName[MAX_PATH];
92 };
93
94
95 class Log : public File
96 {
97 public:
98     Log(const TCHAR *szName) : File(szName, TEXT("xml")) {
99         Write("<?xml version='1.0' encoding='UTF-8'?>");
100         NewLine();
101         Write("<?xml-stylesheet type='text/xsl' href='d3dtrace.xsl'?>");
102         NewLine();
103         Write("<trace>");
104         NewLine();
105     }
106     
107     ~Log() {
108         Write("</trace>");
109         NewLine();
110     }
111     
112     void NewLine(void) {
113         Write("\r\n");
114     }
115     
116     void Tag(const char *name) {
117         Write("<");
118         Write(name);
119         Write("/>");
120     }
121     
122     void BeginTag(const char *name) {
123         Write("<");
124         Write(name);
125         Write(">");
126     }
127     
128     void BeginTag(const char *name, 
129                   const char *attr1, const char *value1) {
130         Write("<");
131         Write(name);
132         Write(" ");
133         Write(attr1);
134         Write("=\"");
135         Escape(value1);
136         Write("\">");
137     }
138     
139     void BeginTag(const char *name, 
140                   const char *attr1, const char *value1,
141                   const char *attr2, const char *value2) {
142         Write("<");
143         Write(name);
144         Write(" ");
145         Write(attr1);
146         Write("=\"");
147         Escape(value1);
148         Write("\" ");
149         Write(attr2);
150         Write("=\"");
151         Escape(value2);
152         Write("\">");
153     }
154     
155     void EndTag(const char *name) {
156         Write("</");
157         Write(name);
158         Write(">");
159     }
160     
161     void Text(const char *text) {
162         Escape(text);
163     }
164     
165     void TextF(const char *format, ...) {
166         char szBuffer[4196];
167         va_list ap;
168         va_start(ap, format);
169         vsnprintf(szBuffer, sizeof(szBuffer), format, ap);
170         va_end(ap);
171         Escape(szBuffer);
172     }
173     
174     void BeginCall(const char *function) {
175         Write("\t");
176         BeginTag("call", "name", function);
177         NewLine();
178     }
179     
180     void EndCall(void) {
181         Write("\t");
182         EndTag("call");
183         NewLine();
184     }
185     
186     void BeginParam(const char *name, const char *type) {
187         Write("\t\t");
188         BeginTag("param", "name", name, "type", type);
189     }
190     
191     void EndParam(void) {
192         EndTag("param");
193         NewLine();
194     }
195     
196     void BeginReturn(const char *type) {
197         Write("\t\t");
198         BeginTag("return", "type", type);
199     }
200     
201     void EndReturn(void) {
202         EndTag("return");
203         NewLine();
204     }
205     
206 protected:
207     void Escape(const char *s) {
208         /* FIXME */
209         Write(s);
210     }
211 };
212
213
214 extern Log * g_pLog;
215
216
217 #endif /* _LOG_HPP_ */