Skip to content

Instantly share code, notes, and snippets.

@ranisalt
Created March 28, 2017 00:20
Show Gist options
  • Save ranisalt/639a4ab5717937c0bd349e95f8cb9dc7 to your computer and use it in GitHub Desktop.
Save ranisalt/639a4ab5717937c0bd349e95f8cb9dc7 to your computer and use it in GitHub Desktop.
Quantum debug
#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) {
os << " " << color << ns << detail::reset;
newline = false;
}
os << ' ' << value;
return *this;
}
Debug& operator<<(decltype(endl)) {
os << 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