X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftracer.cpp;h=0d07761b36332375116d7eae1cffcc815831bb94;hb=a832996c884a0e0acc9a38ba4dd258edb75ec7af;hp=7ce00fea376d5052bb7f2d02bc34cf94bbf9f752;hpb=c6b2f7585d51164dc32f4dd2a05855913e464c58;p=gldbg.git diff --git a/source/tracer.cpp b/source/tracer.cpp index 7ce00fe..0d07761 100644 --- a/source/tracer.cpp +++ b/source/tracer.cpp @@ -1,61 +1,88 @@ -/* $Id$ - -This file is part of gldbg -Copyright © 2009 Mikko Rasa, Mikkosoft Productions -Distributed under the GPL -*/ - -#include +#include +#include "gldbg.h" #include "glprint.h" #include "tracer.h" -using namespace Msp; +using namespace std; -Tracer::Tracer(): +Tracer::Tracer(GlDbg &dbg): glprint(new GlPrint(0, 16384)), out(0), - delete_out(true), + close_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() { - if(delete_out) - delete out; + delete glprint; + if(close_out && out) + fclose(out); } -void Tracer::set_output(IO::Base *o) +void Tracer::set_output(FILE *o, bool d) { - if(delete_out) - delete out; + if(close_out && out) + fclose(out); out = o; - delete_out = true; + close_out = d; } -void Tracer::set_output(IO::Base &o) +void Tracer::decode(const char *data, unsigned len) { - set_output(&o); - delete_out = false; -} + if(!enabled || !out) + return; -void Tracer::enable() -{ - if(!out) - throw InvalidState("Output is not set"); - enabled = true; + int ret = glprint->decode(data, len); + if(ret>=0) + { + const char *buf = glprint->get_buffer(); + if(buf[0]) + fprintf(out, "%s\n", buf); + } } -void Tracer::disable() +void Tracer::cmd_trace(const string &args) { - enabled = false; -} + if(args.empty() || args=="-") + { + set_output(stdout, false); + printf("Tracing to stdout\n"); + } + else if(args=="on") + { + if(!out) + throw runtime_error("Output is not set"); + enabled = true; + printf("Tracing enabled\n"); + } + else if(args=="off") + { + enabled = false; + printf("Tracing disabled\n"); + } + else if(args=="end") + { + set_output(0, true); + printf("Tracing terminated\n"); + } + else + { + FILE *f = fopen(args.c_str(), "w"); + if(!f) + throw runtime_error("Could not open trace file"); -int Tracer::decode(const char *data, unsigned len) -{ - if(!enabled || !out) - return 0; - int ret = glprint->decode(data, len); - if(ret>=0) - IO::print(*out, "%s\n", glprint->get_buffer()); - return ret; + set_output(f, true); + printf("Tracing to %s\n", args.c_str()); + } }