]> git.cworth.org Git - apitrace/blob - common/log.hpp
Basic logging mechanism.
[apitrace] / common / log.hpp
1
2 #ifndef LOG_HPP_
3 #define LOG_HPP_
4
5 #include <windows.h>
6
7 #include <d3d8.h>
8
9 #include <stdio.h>
10
11
12 class Log
13 {
14 public:
15     Log(const char *filename) {
16         file = fopen(filename, "wt");
17         write("<?xml version='1.0' encoding='UTF-8'?>\n");
18         write("<?xml-stylesheet type='text/xsl' href='d3dtrace.xsl'?>\n");
19         write("<trace>\n");
20     }
21     
22     ~Log() {
23         write("</trace>\n");
24         fclose(file);
25     }
26     
27     void write(const char *s) {
28         fputs(s, file);
29     }
30     
31     void writef(const char *f, ...) {
32         va_list ap;
33         va_start(ap, f);
34         vfprintf(file, f, ap);
35         va_end(ap);
36     }
37     
38     void eol(void) {
39         fputs("\n", file);
40     }
41     
42     void tag(const char *name) {
43         write("<");
44         write(name);
45         write(">");
46     }
47     
48     void tag_begin(const char *name) {
49         write("<");
50         write(name);
51     }
52     
53     void tag_attr(const char *name, const char *value) {
54         write(" ");
55         write(name);
56         write("=\"");
57         write(value);
58         write("\"");
59     }
60     
61     void tag_end(void) {
62         write(">");
63     }
64     
65     void tag_close(const char *name) {
66         write("</");
67         write(name);
68         write(">");
69     }
70     
71     void call_begin(const char *function) {
72         write("\t");
73         tag_begin("call");
74         tag_attr("name", function);
75         tag_end();
76         eol();
77     }
78     
79     void call_end() {
80         write("\t");
81         tag_close("call");
82         eol();
83     }
84     
85     void param_begin(const char *type, const char *name) {
86         write("\t\t");
87         tag_begin("param");
88         tag_attr("type", type);
89         tag_attr("name", name);
90         tag_end();
91     }
92     
93     void param_end(void) {
94         tag_close("param");
95         eol();
96     }
97     
98     void param_uint(const char *name, UINT value) {
99         param_begin("UINT", name);
100         writef("%u", value);
101         param_end();
102     }
103     
104     void param_dword(const char *name, DWORD value) {
105         param_begin("DWORD", name);
106         writef("0x%08lx", value);
107         param_end();
108     }
109     
110 protected:
111     FILE *file;
112 };
113
114
115 #endif /* LOG_HPP_ */