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