From: José Fonseca Date: Sat, 20 Aug 2011 12:49:40 +0000 (+0100) Subject: Allow to disable ANSI escape codes on tracedump. X-Git-Tag: 2.0_alpha^2~725 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=822d20a5ea51eb70bc9e2e937f70374c106fa06b;p=tools%2Fapitrace.git Allow to disable ANSI escape codes on tracedump. --- diff --git a/formatter.hpp b/formatter.hpp index 040d06d..181e2d1 100644 --- a/formatter.hpp +++ b/formatter.hpp @@ -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; + } } diff --git a/trace_model.cpp b/trace_model.cpp index 5651f64..79bb757 100644 --- a/trace_model.cpp +++ b/trace_model.cpp @@ -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); } diff --git a/trace_model.hpp b/trace_model.hpp index ddbcabd..5c51bba 100644 --- a/trace_model.hpp +++ b/trace_model.hpp @@ -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 */ diff --git a/tracedump.cpp b/tracedump.cpp index 1548880..c210a0c 100644 --- a/tracedump.cpp +++ b/tracedump.cpp @@ -29,12 +29,49 @@ */ +#include + #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; }