Single header library with simple, thread-safe methods for printing colorized debug info to std::cout.
- prints to
std::cout - thread-safe
- prints the thread's id
- each thread a different colour
Just
#include "mt_color_print.hpp"and do not forget to link with pthread
C++17
template<typename ... Ts>
void print(Ts && ... args)Print args to std::cout, without being interrupted by other threads, and preceded by a (perhaps colorized) string representation of the thread's id, between brackets.
Print a space between each adjacent arg.
Params:
...argsarguments, of printable types (i.e. having an extraction tostd::ostreamoperator overload)
#include "mt_color_print.hpp"
int x = -82;
bool b = true;
mt_color_print::print(3.14f,x,b,std::string("hello"),"hello again");output:
[140485091915520] 3.14 -82 1 hello hello again
template<typename Iter>
void print_range(Iter from, Iter to)Print items in the iterator range [from,to) to std::cout, without being interrupted by other threads, and preceded by a (perhaps colorized) string representation of the thread's id, between brackets. Each item on its own line.
Params:
fromiterator pointing to the first element, which must be of a printable type (i.e. having an extraction tostd::ostreamoperator overload)toiterator pointing to one past the last element, which must be of a printable type (i.e. having an extraction tostd::ostreamoperator overload)
#include "mt_color_print.hpp"
std::vector<int> v{1,2,3,4};
mt_color_print::print_range(v.cbegin(),v.cend());output:
[140485091915520]
1
2
3
4
void set_color_enabled(bool enable)Enable/disable colorized output. Thread-safe, valid for every thread
Params:
enableboolean value
bool is_color_enabled()Tell if coloring of output is enabled.
See demo/mt_color_print-demo.cpp (use demo/compile.sh to compile)
Distributed under the MIT License. See LICENSE.txt for more information.
