Created
August 24, 2016 05:46
-
-
Save ranisalt/f0deaf76f0a863a2e0750d2815f3afe2 to your computer and use it in GitHub Desktop.
C++ version of visionmedia/debug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef QUANTUM_DEBUG_H | |
#define QUANTUM_DEBUG_H | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
namespace quantum { | |
namespace tools { | |
namespace detail { | |
const auto colors = std::vector<std::string>{ | |
"\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m", | |
}; | |
const auto reset = std::string{"\033[00m"}; | |
std::string get_color() { | |
static auto color_index = 0; | |
return colors[color_index++ % colors.size()]; | |
} | |
} | |
class Debug { | |
public: | |
struct { | |
} endl; | |
Debug(std::string ns, std::ostream& os = std::cout) : | |
os{os}, ns{std::move(ns)}, color{detail::get_color()} {} | |
template<class T> | |
Debug& operator<<(const T& value) { | |
if (not enabled) { | |
return *this; | |
} | |
if (newline) { | |
std::cout << " " << color << ns << detail::reset; | |
newline = false; | |
} | |
std::cout << ' ' << value; | |
} | |
Debug& operator<<(decltype(endl)) { | |
std::cout << std::endl; | |
newline = true; | |
} | |
private: | |
std::ostream& os; | |
std::string ns; | |
std::string color; | |
bool enabled = true; | |
bool newline = true; | |
}; | |
} | |
} | |
#endif //QUANTUM_DEBUG_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment