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