fork download
  1. #pragma once
  2. #include <iostream>
  3. #include <sstream>
  4. #include <cstdarg>
  5. #include <windows.h>
  6.  
  7. //usage:
  8. // DEBUGOUT("HI %s", username);
  9. // DEBUGOUT << "HI " << username;
  10. //results:
  11. // C:/users/dev/code/mpd/file_ptr.h(9): HI smith
  12. // C:/users/dev/code/mpd/file_ptr.h(10): HI smith
  13. //double click a line to be taken directly to that line of code
  14.  
  15. __declspec(dllimport) void __stdcall OutputDebugStringA(_In_opt_ const char* lpOutputString);
  16. struct DebugOutBuffer {
  17. DebugOutBuffer(const char* file, int line, bool enabled)
  18. {if (enabled) ss<<file<<'('<<line<<"): "; else ss.setstate(std::ios_base::badbit); }
  19. ~DebugOutBuffer()
  20. {if (ss) {ss<<'\n'; OutputDebugStringW(ss.str().c_str());}}
  21.  
  22. operator std::wostream&() {return ss;}
  23. template<class T> std::wostream& operator<<(T&& t) {return ss << t;}
  24.  
  25. DebugOutBuffer& operator()(_In_z_ const char* text)
  26. {if (ss) ss<<text; return *this;}
  27. template<class T, class enabled=typename std::enable_if<!std::is_same<T,va_list>::value>::type>
  28. DebugOutBuffer& operator()(_In_z_ _Printf_format_string_ const char* format, T, ...)
  29. {
  30. if (!ss) return *this;
  31. va_list arglist;
  32. va_start(arglist, format);
  33. operator()(format, arglist);
  34. va_end(arglist);
  35. return *this;
  36. }
  37. DebugOutBuffer& operator()(_In_z_ _Printf_format_string_ const char* format, va_list args)
  38. {
  39. if (!ss) return *this;
  40. char buffer[1024];
  41. va_list arglist;
  42. va_start(arglist, format);
  43. vsnprintf(buffer, 1024, format, args);
  44. buffer[1023] = '\0';
  45. ss<<buffer;
  46. return *this;
  47. }
  48. DebugOutBuffer& operator()(_In_z_ const wchar_t* text)
  49. {if (ss) ss<<text; return *this;}
  50. template<class T, class enabled=typename std::enable_if<!std::is_same<T,va_list>::value>::type>
  51. DebugOutBuffer& operator()(_In_z_ _Printf_format_string_ const wchar_t* format, T, ...)
  52. {
  53. if (!ss) return *this;
  54. va_list arglist;
  55. va_start(arglist, format);
  56. operator()(format, arglist);
  57. va_end(arglist);
  58. return *this;
  59. }
  60. DebugOutBuffer& operator()(_In_z_ _Printf_format_string_ const wchar_t* format, va_list args)
  61. {
  62. if (!ss) return *this;
  63. wchar_t buffer[1024];
  64. va_list arglist;
  65. va_start(arglist, format);
  66. _vsnwprintf(buffer, 1024, format, args);
  67. buffer[1023] = '\0';
  68. ss<<buffer;
  69. return *this;
  70. }
  71. private:
  72. std::wostringstream ss;
  73. };
  74. #define DEBUGOUT DebugOutBuffer(__FILE__, __LINE__, true)
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:9: warning: #pragma once in main file
 #pragma once
         ^
prog.cpp:5:21: fatal error: windows.h: No such file or directory
 #include <windows.h>
                     ^
compilation terminated.
stdout
Standard output is empty