Don't generate colorized output escapes when stderr isn't a TTY.

When piping to a pile, more/less, etc, this is generally undesirable.

This behavior can be overridden with the --colorized-output command-line
flag.
This commit is contained in:
Matt Pharr
2012-06-04 09:20:57 -07:00
parent 6118643232
commit 1397dbdabc
4 changed files with 15 additions and 0 deletions

View File

@@ -578,6 +578,7 @@ Globals::Globals() {
disableWarnings = false;
warningsAsErrors = false;
quiet = false;
forceColoredOutput = false;
disableLineWrap = false;
emitPerfWarnings = true;
emitInstrumentation = false;

4
ispc.h
View File

@@ -407,6 +407,10 @@ struct Globals {
/** Indicates whether all printed output should be surpressed. */
bool quiet;
/** Always use ANSI escape sequences to colorize warning and error
messages, even if piping output to a file, etc. */
bool forceColoredOutput;
/** Indicates whether calls should be emitted in the program to an
externally-defined program instrumentation function. (See the
"Instrumenting your ispc programs" section in the user's

View File

@@ -83,6 +83,9 @@ usage(int ret) {
printf(" [--arch={%s}]\t\tSelect target architecture\n",
Target::SupportedTargetArchs());
printf(" [--c++-include-file=<name>]\t\tSpecify name of file to emit in #include statement in generated C++ code.\n");
#ifndef ISPC_IS_WINDOWS
printf(" [--colored-output]\t\tAlways use terminal colors in error/warning messages.\n");
#endif
printf(" [--cpu=<cpu>]\t\t\tSelect target CPU type\n");
printf(" <cpu>={%s}\n", Target::SupportedTargetCPUs().c_str());
printf(" [-D<foo>]\t\t\t\t#define given value when running preprocessor\n");
@@ -401,6 +404,8 @@ int main(int Argc, char *Argv[]) {
#ifndef ISPC_IS_WINDOWS
else if (!strcmp(argv[i], "--pic"))
generatePIC = true;
else if (!strcmp(argv[i], "--colored-output"))
g->forceColoredOutput = true;
#endif // !ISPC_IS_WINDOWS
else if (!strcmp(argv[i], "--quiet"))
g->quiet = true;

View File

@@ -44,6 +44,7 @@
#endif
#else
#include <alloca.h>
#include <unistd.h>
#endif
#include <stdio.h>
@@ -94,6 +95,10 @@ static bool
lHaveANSIColors() {
static bool r = (getenv("TERM") != NULL &&
strcmp(getenv("TERM"), "dumb") != 0);
#ifndef ISPC_IS_WINDOWS
r &= isatty(2);
#endif // !ISPC_IS_WINDOWS
r |= g->forceColoredOutput;
return r;
}