A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

log.h 777B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #pragma once
  2. #include <iostream>
  3. namespace Swan {
  4. class Logger {
  5. public:
  6. class NewlineStream {
  7. public:
  8. NewlineStream(std::ostream &os): os_(os) {}
  9. ~NewlineStream() {
  10. os_ << '\n' << std::flush;
  11. }
  12. template<typename T>
  13. NewlineStream &operator<<(const T &val) {
  14. os_ << val;
  15. return *this;
  16. }
  17. private:
  18. std::ostream &os_;
  19. };
  20. Logger(std::ostream &os, std::string name): os_(os), name_(std::move(name)) {}
  21. template<typename T>
  22. NewlineStream operator<<(const T &val) {
  23. os_ << name_ << ": " << val;
  24. return NewlineStream(os_);
  25. }
  26. private:
  27. std::ostream &os_;
  28. std::string name_;
  29. };
  30. static Logger log(std::clog, "log");
  31. static Logger info(std::clog, "info");
  32. static Logger warning(std::clog, "warning");
  33. static Logger panic(std::clog, "panic");
  34. }