fork(1) download
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. #define DebugVars(...) DEBUG_VARS(__FILE__, __LINE__, __FUNCTION__, #__VA_ARGS__,__VA_ARGS__)
  5.  
  6. void Log(const char* file, const int line, const char* func, const std::string& message)
  7. {
  8. printf("file:%s, line:%d, func:%s, message:%s \n", file, line, func, message.c_str());
  9. }
  10.  
  11. template < typename... Args>
  12. void DEBUG_VARS(const char* file, const int line, const char* func, const std::string& names, Args&&... args)
  13. {
  14.  
  15. std::stringstream names_ss;
  16. for (const char& c : names )
  17. {
  18. if (c == ','){
  19. names_ss << " ";
  20. continue;
  21. }
  22. names_ss << c;
  23. }
  24.  
  25. std::string name;
  26. std::ostringstream ss;
  27. ss << "\n";
  28. using expander = int[];
  29. (void) expander { 0, (
  30. names_ss >> name, ss << name << ": " << args << "\n"
  31.  
  32. ,0) ...};
  33. Log(file, line, func, ss.str());
  34.  
  35. }
  36.  
  37. int main()
  38. {
  39. int number = 37;
  40. float pie = 3.14;
  41. std::string str = "test string";
  42.  
  43. DebugVars(number, pie, str);
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
file:prog.cpp, line:43, func:main, message:
number: 37
pie: 3.14
str: test string