Skip to content

Instantly share code, notes, and snippets.

@ArthurSonzogni
Last active March 3, 2016 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArthurSonzogni/60ac2ecf42f16195f82e to your computer and use it in GitHub Desktop.
Save ArthurSonzogni/60ac2ecf42f16195f82e to your computer and use it in GitHub Desktop.
qdebug() like object that avoid interleaving issues in a multiprocess / multithread environment.
/* ---------------------------------------------------------------------------
* ** This software is in the public domain, furnished "as is", without technical
* ** support, and with no warranty, express or implied, as to its usefulness for
* ** any purpose.
* **
* ** The file provide a way to avoid interleaved printing in a multithreaded
* ** environment. It works like qDebug() for the Qt framework
* **
* ** Usage :
* ** Print() << " The debug value = " << 42;
* ** Print() << " The debug value = " << 56;
* **
* ** Author: Arthur Sonzogni
* ** -------------------------------------------------------------------------*/
#ifndef PRINT_HEADER_GUARD_823
#define PRINT_HEADER_GUARD_823
#include <iostream>
#include <sstream>
class Print
{
public:
template<typename T> Print& operator<<(const T& x)
{
ss << x;
return *this;
}
~Print()
{
std::cerr << ss.rdbuf() << std::endl;
}
private:
std::stringstream ss;
};
#endif /* end of include guard: PRINT_HEADER_GUARD_823 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment