fork(1) download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. typedef int LogLevel;
  6.  
  7. bool isEnabled(LogLevel l){
  8. return true;
  9. }
  10.  
  11. namespace logger{
  12. void log(LogLevel l, std::string const& msg){
  13. std::cout << "(Level: " << l << "): " << msg << "\n";
  14. }
  15. }
  16.  
  17. #define DEBUG_LOG(Level, What) \
  18.   isEnabled(Level) && scoped_logger(Level, __FILE__, __LINE__).stream() << What
  19.  
  20. struct scoped_logger
  21. {
  22. scoped_logger(LogLevel level, char const* file, unsigned line)
  23. : _level(level)
  24. { _ss << file << "(" << line << ") [" << getpid() << "] : "; }
  25.  
  26. std::stringstream& stream(){ return _ss; }
  27. ~scoped_logger(){ logger::log(_level, _ss.str()); }
  28. private:
  29. std::stringstream _ss;
  30. LogLevel _level;
  31. };
  32.  
  33. int main(){
  34. DEBUG_LOG(0, "Hello, " << "World!");
  35. }
  36.  
Success #stdin #stdout 0.02s 2860KB
stdin
Standard input is empty
stdout
(Level: 0): prog.cpp(34) [11141] : Hello, World!