Skip to content

Commit 8403922

Browse files
committed
Conditionally print ANSI escape codes based on terminal support
1 parent 5d2425f commit 8403922

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/log.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
#include <iomanip>
99
#include <sstream>
1010

11+
#ifdef _WIN32
12+
#include <io.h>
13+
#define isatty _isatty
14+
#ifndef STDOUT_FILENO
15+
#define STDOUT_FILENO _fileno(stdout)
16+
#endif
17+
#else
18+
#include <unistd.h>
19+
#endif
20+
1121
#ifdef ANDROID
1222
#include <android/log.h>
1323
#endif
@@ -139,7 +149,12 @@ void log(const std::string& appName, const std::string& level, const std::string
139149
if (firstLine) {
140150
tmp << *firstLine << '\n';
141151
}
142-
printMessage(tmp.str());
152+
if (isatty(STDOUT_FILENO) != 0 || std::getenv("FORCE_COLOR") != nullptr ||
153+
std::getenv("CLICOLOR_FORCE") != nullptr) {
154+
printMessage(tmp.str());
155+
} else {
156+
printMessage(stripAnsiEscapeCodes(tmp.str()));
157+
}
143158
#endif
144159
}
145160

0 commit comments

Comments
 (0)