Skip to content

Commit 5f4812c

Browse files
Add optional colour to the logging helper prefixes
1 parent 66ceeed commit 5f4812c

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/ammonite/utils/debug.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#ifdef AMMONITE_DEBUG
1111
/*NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables,
1212
cppcoreguidelines-interfaces-global-init)*/
13-
ammonite::utils::OutputHelper ammoniteInternalDebug(std::cout, "DEBUG: ");
13+
ammonite::utils::OutputHelper ammoniteInternalDebug(std::cout, "DEBUG: ",
14+
ammonite::utils::colour::magenta);
1415
/*NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables,
1516
cppcoreguidelines-interfaces-global-init)*/
1617
#endif

src/ammonite/utils/logging.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ namespace ammonite {
99
namespace utils {
1010
namespace {
1111
std::mutex outputLock;
12+
13+
const std::string reset = "\033[0m";
1214
}
1315

1416
OutputHelper::OutputHelper(std::ostream& output, const std::string& pre):
1517
outputStream(output), prefix(pre) {}
1618

19+
OutputHelper::OutputHelper(std::ostream& output, const std::string& pre, const std::string& colour):
20+
outputStream(output), prefix(colour + pre + reset) {}
21+
1722
//Output the stored string
1823
OutputHelper& OutputHelper::operator << (std::ostream& (*)(std::ostream&)) {
1924
//Safely output the prefix, buffered string and new line
@@ -32,9 +37,9 @@ namespace ammonite {
3237
cppcoreguidelines-interfaces-global-init)*/
3338
thread_local std::stringstream OutputHelper::storageStream = std::stringstream("");
3439

35-
OutputHelper error(std::cerr, "ERROR: ");
36-
OutputHelper warning(std::cerr, "WARNING: ");
37-
OutputHelper status(std::cout, "STATUS: ");
40+
OutputHelper error(std::cerr, "ERROR: ", colour::red);
41+
OutputHelper warning(std::cerr, "WARNING: ", colour::yellow);
42+
OutputHelper status(std::cout, "STATUS: ", colour::green);
3843
/*NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables,
3944
cppcoreguidelines-interfaces-global-init)*/
4045
}

src/include/ammonite/utils/logging.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@
88

99
namespace ammonite {
1010
namespace utils {
11+
namespace colour {
12+
const std::string black = "\033[30m";
13+
const std::string red = "\033[31m";
14+
const std::string green = "\033[32m";
15+
const std::string yellow = "\033[33m";
16+
const std::string blue = "\033[34m";
17+
const std::string magenta = "\033[35m";
18+
const std::string cyan = "\033[36m";
19+
const std::string white = "\033[37m";
20+
}
21+
1122
/*
1223
- Output helper to buffer output data and synchronise with other helpers to display
1324
- Prefixes the output with the prefix of the instant used to flush
1425
- Buffer is shared between all instances on the same thread
26+
- Colour is only used for the prefix
1527
*/
1628
class OutputHelper {
1729
private:
@@ -23,6 +35,7 @@ namespace ammonite {
2335

2436
public:
2537
OutputHelper(std::ostream& output, const std::string& pre);
38+
OutputHelper(std::ostream& output, const std::string& pre, const std::string& colour);
2639
template<typename T> OutputHelper& operator << (T&& input);
2740
OutputHelper& operator << (std::ostream& (*)(std::ostream&));
2841
};

0 commit comments

Comments
 (0)