fork(2) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template<typename TF>
  5. void write_debug_output( std::ostream & out, TF const& f ) {
  6. out << f;
  7. }
  8.  
  9. struct tracer {
  10. std::ostream & out;
  11. tracer( std::ostream & out, char const * file, int line )
  12. : out( out ) {
  13. out << file << ":" << line << ": ";
  14. }
  15. ~tracer() {
  16. out << std::endl;
  17. }
  18.  
  19. template<typename TF, typename ... TR>
  20. void write( TF const& f, TR const& ... rest ) {
  21. write_debug_output( out, f );
  22. out << " ";
  23. write( rest... );
  24. }
  25. template<typename TF>
  26. void write( TF const& f ) {
  27. write_debug_output( out, f );
  28. }
  29. void write() {
  30. //handle the empty params case
  31. }
  32. };
  33.  
  34. #define TRACE(...) tracer( std::cout, __FILE__, __LINE__ ).write( __VA_ARGS__ )
  35.  
  36. struct my_object { };
  37. void write_debug_output( std::ostream & out, my_object const & f ) {
  38. out << "**Mine**";
  39. }
  40.  
  41. int main() {
  42. my_object a;
  43. TRACE( 1, a, "okay" );
  44. TRACE( 5.6, my_object() );
  45. }
  46.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
prog.cpp:43: 1 **Mine** okay
prog.cpp:44: 5.6 **Mine**