]> git.tdb.fi Git - gldbg.git/blob - source/tracer.cpp
Replace per-file license notices with License.txt
[gldbg.git] / source / tracer.cpp
1 #include <stdexcept>
2 #include "gldbg.h"
3 #include "glprint.h"
4 #include "tracer.h"
5
6 using namespace std;
7
8 Tracer::Tracer(GlDbg &dbg):
9         glprint(new GlPrint(0, 16384)),
10         out(0),
11         close_out(true),
12         enabled(true)
13 {
14         dbg.get_command_interpreter().register_command("trace", this, &Tracer::cmd_trace)
15                 .set_help("Traces GL function calls",
16                         "trace\n"
17                         "  Send trace output to stdout.\n\n"
18                         "trace FILE\n"
19                         "  Send trace output to FILE (- for stdout).\n\n"
20                         "trace {off|on}\n"
21                         "  Temporarily suspend or resume trace without closing the file.\n\n"
22                         "trace end\n"
23                         "  Terminate trace, closing the file.\n");
24 }
25
26 Tracer::~Tracer()
27 {
28         delete glprint;
29         if(close_out && out)
30                 fclose(out);
31 }
32
33 void Tracer::set_output(FILE *o, bool d)
34 {
35         if(close_out && out)
36                 fclose(out);
37         out = o;
38         close_out = d;
39 }
40
41 void Tracer::decode(const char *data, unsigned len)
42 {
43         if(!enabled || !out)
44                 return;
45
46         int ret = glprint->decode(data, len);
47         if(ret>=0)
48         {
49                 const char *buf = glprint->get_buffer();
50                 if(buf[0])
51                         fprintf(out, "%s\n", buf);
52         }
53 }
54
55 void Tracer::cmd_trace(const string &args)
56 {
57         if(args.empty() || args=="-")
58         {
59                 set_output(stdout, false);
60                 printf("Tracing to stdout\n");
61         }
62         else if(args=="on")
63         {
64                 if(!out)
65                         throw runtime_error("Output is not set");
66                 enabled = true;
67                 printf("Tracing enabled\n");
68         }
69         else if(args=="off")
70         {
71                 enabled = false;
72                 printf("Tracing disabled\n");
73         }
74         else if(args=="end")
75         {
76                 set_output(0, true);
77                 printf("Tracing terminated\n");
78         }
79         else
80         {
81                 FILE *f = fopen(args.c_str(), "w");
82                 if(!f)
83                         throw runtime_error("Could not open trace file");
84
85                 set_output(f, true);
86                 printf("Tracing to %s\n", args.c_str());
87         }
88 }