]> git.tdb.fi Git - gldbg.git/blobdiff - source/tracer.cpp
Decouple the existing tools from the debugger core
[gldbg.git] / source / tracer.cpp
index d92da29b92c84cf3e08bcc5c1e4e1b610df75284..8aa12de25fd629b362c4b4eaaae3af4e3708ac85 100644 (file)
@@ -1,22 +1,36 @@
 /* $Id$
 
 This file is part of gldbg
-Copyright © 2009  Mikko Rasa, Mikkosoft Productions
+Copyright © 2009-2010  Mikko Rasa, Mikkosoft Productions
 Distributed under the GPL
 */
 
+#include <msp/io/file.h>
 #include <msp/io/print.h>
+#include "gldbg.h"
 #include "glprint.h"
 #include "tracer.h"
 
+using namespace std;
 using namespace Msp;
 
-Tracer::Tracer():
+Tracer::Tracer(GlDbg &dbg):
        glprint(new GlPrint(0, 16384)),
        out(0),
        delete_out(true),
        enabled(true)
-{ }
+{
+       dbg.get_command_interpreter().register_command("trace", this, &Tracer::cmd_trace)
+               .set_help("Traces GL function calls",
+                       "trace\n"
+                       "  Send trace output to stdout.\n\n"
+                       "trace FILE\n"
+                       "  Send trace output to FILE (- for stdout).\n\n"
+                       "trace {off|on}\n"
+                       "  Temporarily suspend or resume trace without closing the file.\n\n"
+                       "trace end\n"
+                       "  Terminate trace, closing the file.\n");
+}
 
 Tracer::~Tracer()
 {
@@ -25,38 +39,51 @@ Tracer::~Tracer()
                delete out;
 }
 
-void Tracer::set_output(IO::Base *o)
+void Tracer::set_output(IO::Base *o, bool d)
 {
        if(delete_out)
                delete out;
        out = o;
-       delete_out = true;
-}
-
-void Tracer::set_output(IO::Base &o)
-{
-       set_output(&o);
-       delete_out = false;
-}
-
-void Tracer::enable()
-{
-       if(!out)
-               throw InvalidState("Output is not set");
-       enabled = true;
+       delete_out = d;
 }
 
-void Tracer::disable()
-{
-       enabled = false;
-}
-
-int Tracer::decode(const char *data, unsigned len)
+void Tracer::decode(const char *data, unsigned len)
 {
        if(!enabled || !out)
-               return 0;
+               return;
+
        int ret = glprint->decode(data, len);
        if(ret>=0)
                IO::print(*out, "%s\n", glprint->get_buffer());
-       return ret;
+}
+
+void Tracer::cmd_trace(const string &args)
+{
+       if(args.empty() || args=="-")
+       {
+               set_output(&IO::cout, false);
+               IO::print("Tracing to stdout\n");
+       }
+       else if(args=="on")
+       {
+               if(!out)
+                       throw InvalidState("Output is not set");
+               enabled = true;
+               IO::print("Tracing enabled\n");
+       }
+       else if(args=="off")
+       {
+               enabled = false;
+               IO::print("Tracing disabled\n");
+       }
+       else if(args=="end")
+       {
+               set_output(0, true);
+               IO::print("Tracing terminated\n");
+       }
+       else
+       {
+               set_output(new IO::File(args, IO::M_WRITE), true);
+               IO::print("Tracing to %s\n", args);
+       }
 }