ros / rosconsole

17 stars 61 forks source link

Use ANSI escape sequence when output is a terminal #44

Closed at-wat closed 4 years ago

at-wat commented 4 years ago

ANSI escape sequence causes corrupted text if the output is not a terminal. (e.g. redirect to file, docker logs) Check output file descriptor is a terminal and skip applying text color if not.

dirk-thomas commented 4 years ago

How about the use case of redirecting the output to a file but still wanting to have colorization?

at-wat commented 4 years ago

Maybe better to read a environment variable like ROSCONSOLE_COLOR_ALWAY and force using text color if set. Is it better to keep always colored by default?

dirk-thomas commented 4 years ago

For your use case checking for an environment variable like NO_COLOR (https://no-color.org/) might be the best option. If it is present skip all colorization.

at-wat commented 4 years ago

Supporting NO_COLOR sounds good to me. I'm going to add a support for NO_COLOR environment variable.

By the way, rospy.log*() seems checking isatty() and doesn't use ANSI color when the output is not a terminal. https://github.com/ros/ros_comm/blob/c6ad71e7105f151d912c8c0db3bd18caad771e89/tools/rosgraph/src/rosgraph/roslogging.py#L296

at-wat commented 4 years ago

NO_COLOR environment variable version: https://github.com/ros/rosconsole/pull/46