[PATCH] tracedump: Add tri-state --color option (auto, always, or never)

José Fonseca jose.r.fonseca at gmail.com
Mon Oct 24 05:35:43 PDT 2011


Carl,

The formatter.hpp/trace_model.cpp code can be used with streams other
than stdout, as there's no standard way to get filenos out of C++
ostreams , so the autodetection is better done by the caller. I've
done that and commited.

Thanks.

Jose

On Thu, Oct 20, 2011 at 11:22 PM, Carl Worth <cworth at cworth.org> wrote:
> This follows a pattern similar to that provided by other tools, (such
> as grep). The default auto mode colorizes if stdout is a tty (except
> on Windows where it always colorizes). The always and never modes
> explicit enable or disable colorizing.
>
> The old --no-color and --no-colour options are deprecated, (no longer
> documented but still supported to avoid any regressions of scripts).
> ---
>
> I assume that isatty isn't something we should call on Windows, but
> that's honestly just guessing on my part. Let me know if I got that
> wrong.
>
> The support for the old option syntax is perhaps gratuitous, but was
> easy enough to maintain. Let me know if you would prefer to drop that
> instead.
>
> -Carl
>
>  common/formatter.hpp   |   12 ++++++++++--
>  common/trace_model.cpp |    6 +++---
>  common/trace_model.hpp |    9 +++++++--
>  tracedump.cpp          |   24 ++++++++++++++----------
>  4 files changed, 34 insertions(+), 17 deletions(-)
>
> diff --git a/common/formatter.hpp b/common/formatter.hpp
> index 181e2d1..34b1cba 100644
> --- a/common/formatter.hpp
> +++ b/common/formatter.hpp
> @@ -33,6 +33,7 @@
>
>  #include <iostream>
>
> +#include "trace_model.hpp"
>
>  namespace Formatter {
>
> @@ -155,8 +156,15 @@ public:
>  #endif
>
>
> -inline Formatter *defaultFormatter(bool color = true) {
> -    if (color) {
> +inline Formatter *defaultFormatter(Trace::ColorOption color = Trace::COLOR_OPTION_AUTO) {
> +    bool auto_means_yes = true;
> +
> +#ifndef _WIN32
> +    auto_means_yes = isatty(1);
> +#endif
> +
> +    if (color == Trace::COLOR_OPTION_ALWAYS ||
> +       (color == Trace::COLOR_OPTION_AUTO && auto_means_yes)) {
>  #ifdef _WIN32
>         return new WindowsFormatter;
>  #else
> diff --git a/common/trace_model.cpp b/common/trace_model.cpp
> index 306b9e7..c31eb60 100644
> --- a/common/trace_model.cpp
> +++ b/common/trace_model.cpp
> @@ -194,7 +194,7 @@ protected:
>     Formatter::Attribute *literal;
>
>  public:
> -    Dumper(std::ostream &_os, bool color) : os(_os) {
> +    Dumper(std::ostream &_os, Trace::ColorOption color) : os(_os) {
>         formatter = Formatter::defaultFormatter(color);
>         normal = formatter->normal();
>         bold = formatter->bold();
> @@ -351,7 +351,7 @@ public:
>  };
>
>
> -void Value::dump(std::ostream &os, bool color) {
> +void Value::dump(std::ostream &os, ColorOption color) {
>     Dumper d(os, color);
>     visit(d);
>  }
> @@ -369,7 +369,7 @@ const Value & Value::operator[](size_t index) const {
>     return null;
>  }
>
> -void Call::dump(std::ostream &os, bool color) {
> +void Call::dump(std::ostream &os, ColorOption color) {
>     Dumper d(os, color);
>     os << no << " ";
>     d.visit(this);
> diff --git a/common/trace_model.hpp b/common/trace_model.hpp
> index a74508e..0d939fb 100644
> --- a/common/trace_model.hpp
> +++ b/common/trace_model.hpp
> @@ -43,6 +43,11 @@ namespace Trace {
>
>  typedef unsigned Id;
>
> +enum ColorOption {
> +    COLOR_OPTION_AUTO,
> +    COLOR_OPTION_ALWAYS,
> +    COLOR_OPTION_NEVER
> +};
>
>  struct FunctionSig {
>     Id id;
> @@ -102,7 +107,7 @@ public:
>
>     const Value & operator[](size_t index) const;
>
> -    void dump(std::ostream &os, bool color=true);
> +    void dump(std::ostream &os, ColorOption color=COLOR_OPTION_AUTO);
>  };
>
>
> @@ -342,7 +347,7 @@ public:
>         return *(args[index]);
>     }
>
> -    void dump(std::ostream &os, bool color=true);
> +    void dump(std::ostream &os, ColorOption color=COLOR_OPTION_AUTO);
>  };
>
>
> diff --git a/tracedump.cpp b/tracedump.cpp
> index c210a0c..876e5ee 100644
> --- a/tracedump.cpp
> +++ b/tracedump.cpp
> @@ -33,24 +33,20 @@
>
>  #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"
> +        "  --color=<WHEN>   Colored syntax highlighting\n"
> +        "  --colour=<WHEN>  WHEN is 'auto', 'always', or 'never'\n"
>     ;
>  }
>
> -
>  int main(int argc, char **argv)
>  {
>     int i;
> +    Trace::ColorOption color = Trace::COLOR_OPTION_AUTO;
>
>     for (i = 1; i < argc; ++i) {
>         const char *arg = argv[i];
> @@ -61,9 +57,17 @@ int main(int argc, char **argv)
>
>         if (!strcmp(arg, "--")) {
>             break;
> -        } else if (!strcmp(arg, "--no-color") ||
> -                   !strcmp(arg, "--no-colour")) {
> -            color = false;
> +        } else if (!strcmp(arg, "--color=auto") ||
> +                   !strcmp(arg, "--colour=auto")) {
> +            color = Trace::COLOR_OPTION_AUTO;
> +        } else if (!strcmp(arg, "--color=always") ||
> +                   !strcmp(arg, "--colour=always")) {
> +            color = Trace::COLOR_OPTION_ALWAYS;
> +       } else if (!strcmp(arg, "--color=never") ||
> +                   !strcmp(arg, "--colour=never") ||
> +                  !strcmp(arg, "--no-color") ||
> +                  !strcmp(arg, "--no-colour")) {
> +           color = Trace::COLOR_OPTION_NEVER;
>         } else {
>             std::cerr << "error: unknown option " << arg << "\n";
>             usage();
> --
> 1.7.7
>
> _______________________________________________
> apitrace mailing list
> apitrace at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/apitrace
>


More information about the apitrace mailing list