]> git.cworth.org Git - apitrace/commitdiff
Allow to disable ANSI escape codes on tracedump.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Sat, 20 Aug 2011 12:49:40 +0000 (13:49 +0100)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Sat, 20 Aug 2011 12:53:55 +0000 (13:53 +0100)
formatter.hpp
trace_model.cpp
trace_model.hpp
tracedump.cpp

index 040d06d49084feafa191c8232ad708dea3d1e3bb..181e2d1539d5c14bc14e070db1325a31d2593ad4 100644 (file)
@@ -155,12 +155,16 @@ public:
 #endif
 
 
-inline Formatter *defaultFormatter(void) {
+inline Formatter *defaultFormatter(bool color = true) {
+    if (color) {
 #ifdef _WIN32
-    return new WindowsFormatter;
+        return new WindowsFormatter;
 #else
-    return new AnsiFormatter;
+        return new AnsiFormatter;
 #endif
+    } else {
+        return new Formatter;
+    }
 }
 
 
index 5651f64b506b842bf0033aa6a454f8cae2971fb5..79bb7575801b4842c2bb23be04deacc9986a5c9d 100644 (file)
@@ -177,8 +177,8 @@ protected:
     Formatter::Attribute *literal;
 
 public:
-    Dumper(std::ostream &_os) : os(_os) {
-        formatter = Formatter::defaultFormatter();
+    Dumper(std::ostream &_os, bool color) : os(_os) {
+        formatter = Formatter::defaultFormatter(color);
         normal = formatter->normal();
         bold = formatter->bold();
         italic = formatter->italic();
@@ -334,12 +334,9 @@ public:
 };
 
 
-std::ostream & operator <<(std::ostream &os, Value *value) {
-    Dumper d(os);
-    if (value) {
-        value->visit(d);
-    }
-    return os;
+void Value::dump(std::ostream &os, bool color) {
+    Dumper d(os, color);
+    visit(d);
 }
 
 
@@ -355,11 +352,10 @@ const Value & Value::operator[](size_t index) const {
     return null;
 }
 
-std::ostream & operator <<(std::ostream &os, Call &call) {
-    Dumper d(os);
-    os << call.no << " ";
-    d.visit(&call);
-    return os;
+void Call::dump(std::ostream &os, bool color) {
+    Dumper d(os, color);
+    os << no << " ";
+    d.visit(this);
 }
 
 
index ddbcabd2993e357687588f7d3c0c549b609e715a..5c51bba7d06c345a541b8dca59cebe1291d546f6 100644 (file)
@@ -100,6 +100,8 @@ public:
     virtual const char *toString(void) const;
 
     const Value & operator[](size_t index) const;
+
+    void dump(std::ostream &os, bool color=true);
 };
 
 
@@ -305,7 +307,12 @@ protected:
 };
 
 
-std::ostream & operator <<(std::ostream &os, Value *value);
+inline std::ostream & operator <<(std::ostream &os, Value *value) {
+    if (value) {
+        value->dump(os);
+    }
+    return os;
+}
 
 
 class Call
@@ -327,10 +334,15 @@ public:
         assert(index < args.size());
         return *(args[index]);
     }
+
+    void dump(std::ostream &os, bool color=true);
 };
 
 
-std::ostream & operator <<(std::ostream &os, Call &call);
+inline std::ostream & operator <<(std::ostream &os, Call &call) {
+    call.dump(os);
+    return os;
+}
 
 
 } /* namespace Trace */
index 1548880fd916be9d076f98818b5e19b085cefa0f..c210a0c9dd097119076f38642398c2673087283b 100644 (file)
  */
 
 
+#include <string.h>
+
 #include "trace_parser.hpp"
 
 
+static bool color = true;
+
+
+static void usage(void) {
+    std::cout <<
+        "Usage: tracedump [OPTION] [TRACE...]\n"
+        "Dump TRACE to standard output.\n"
+        "\n"
+        "  --no-color   no colored syntax highlightint\n"
+        "  --no-colour  alias for --no-color\n"
+    ;
+}
+
+
 int main(int argc, char **argv)
 {
-    for (int i = 1; i < argc; ++i) {
+    int i;
+
+    for (i = 1; i < argc; ++i) {
+        const char *arg = argv[i];
+
+        if (arg[0] != '-') {
+            break;
+        }
+
+        if (!strcmp(arg, "--")) {
+            break;
+        } else if (!strcmp(arg, "--no-color") ||
+                   !strcmp(arg, "--no-colour")) {
+            color = false;
+        } else {
+            std::cerr << "error: unknown option " << arg << "\n";
+            usage();
+            return 1;
+        }
+    }
+
+    for (; i < argc; ++i) {
         Trace::Parser p;
 
         if (!p.open(argv[i])) {
@@ -44,9 +81,10 @@ int main(int argc, char **argv)
 
         Trace::Call *call;
         while ((call = p.parse_call())) {
-            std::cout << *call;
+            call->dump(std::cout, color);
             delete call;
         }
     }
+
     return 0;
 }