]> git.tdb.fi Git - gldbg.git/blobdiff - source/tracer.cpp
Replace per-file license notices with License.txt
[gldbg.git] / source / tracer.cpp
index 8aa12de25fd629b362c4b4eaaae3af4e3708ac85..0d07761b36332375116d7eae1cffcc815831bb94 100644 (file)
@@ -1,23 +1,14 @@
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2009-2010  Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#include <msp/io/file.h>
-#include <msp/io/print.h>
+#include <stdexcept>
 #include "gldbg.h"
 #include "glprint.h"
 #include "tracer.h"
 
 using namespace std;
-using namespace Msp;
 
 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)
@@ -35,16 +26,16 @@ Tracer::Tracer(GlDbg &dbg):
 Tracer::~Tracer()
 {
        delete glprint;
-       if(delete_out)
-               delete out;
+       if(close_out && out)
+               fclose(out);
 }
 
-void Tracer::set_output(IO::Base *o, bool d)
+void Tracer::set_output(FILE *o, bool d)
 {
-       if(delete_out)
-               delete out;
+       if(close_out && out)
+               fclose(out);
        out = o;
-       delete_out = d;
+       close_out = d;
 }
 
 void Tracer::decode(const char *data, unsigned len)
@@ -54,36 +45,44 @@ void Tracer::decode(const char *data, unsigned len)
 
        int ret = glprint->decode(data, len);
        if(ret>=0)
-               IO::print(*out, "%s\n", glprint->get_buffer());
+       {
+               const char *buf = glprint->get_buffer();
+               if(buf[0])
+                       fprintf(out, "%s\n", buf);
+       }
 }
 
 void Tracer::cmd_trace(const string &args)
 {
        if(args.empty() || args=="-")
        {
-               set_output(&IO::cout, false);
-               IO::print("Tracing to stdout\n");
+               set_output(stdout, false);
+               printf("Tracing to stdout\n");
        }
        else if(args=="on")
        {
                if(!out)
-                       throw InvalidState("Output is not set");
+                       throw runtime_error("Output is not set");
                enabled = true;
-               IO::print("Tracing enabled\n");
+               printf("Tracing enabled\n");
        }
        else if(args=="off")
        {
                enabled = false;
-               IO::print("Tracing disabled\n");
+               printf("Tracing disabled\n");
        }
        else if(args=="end")
        {
                set_output(0, true);
-               IO::print("Tracing terminated\n");
+               printf("Tracing terminated\n");
        }
        else
        {
-               set_output(new IO::File(args, IO::M_WRITE), true);
-               IO::print("Tracing to %s\n", args);
+               FILE *f = fopen(args.c_str(), "w");
+               if(!f)
+                       throw runtime_error("Could not open trace file");
+
+               set_output(f, true);
+               printf("Tracing to %s\n", args.c_str());
        }
 }