Open Source Tomb Raider Engine
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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*!
  2. * \file include/Log.h
  3. * \brief Global Logging Utility
  4. *
  5. * \author xythobuz
  6. */
  7. #ifndef _LOG_H_
  8. #define _LOG_H_
  9. #include <iostream>
  10. #include <string>
  11. #include <sstream>
  12. #include <vector>
  13. #include <glm/gtc/type_precision.hpp>
  14. #define LOG_USER 0
  15. #define LOG_ERROR 1
  16. #define LOG_WARNING 2
  17. #define LOG_INFO 3
  18. #define LOG_DEBUG 4
  19. #define LOG_COUNT 5
  20. struct LogEntry {
  21. public:
  22. std::string text;
  23. int level;
  24. LogEntry(std::string t, int l) : text(t), level(l) { }
  25. };
  26. class LogLevel;
  27. class Log {
  28. public:
  29. const static char endl = '\n';
  30. static void initialize();
  31. static LogLevel& get(int level);
  32. static unsigned long size() { return wholeLog.size(); }
  33. static LogEntry& getEntry(unsigned long i) { return wholeLog.at(i); }
  34. private:
  35. static std::vector<LogLevel> logs;
  36. static std::vector<LogEntry> wholeLog;
  37. friend class LogLevel;
  38. };
  39. class LogLevel {
  40. public:
  41. explicit LogLevel(int l) : level(l) { printBuffer << std::boolalpha; }
  42. LogLevel(LogLevel&& l) : level(l.level) { printBuffer << std::boolalpha; }
  43. LogLevel& operator<< (const glm::vec2& v) {
  44. return (*this) << v.x << " " << v.y;
  45. }
  46. LogLevel& operator<< (const glm::i32vec2& v) {
  47. return (*this) << v.x << " " << v.y;
  48. }
  49. LogLevel& operator<< (const glm::vec3& v) {
  50. return (*this) << v.x << " " << v.y << " " << v.z;
  51. }
  52. template<typename T>
  53. LogLevel& operator<< (const T t) {
  54. printBuffer << t;
  55. if ((printBuffer.str().length() > 0) && (printBuffer.str().back() == Log::endl)) {
  56. std::string s = printBuffer.str().substr(0, printBuffer.str().length() - 1);
  57. printBuffer.str("");
  58. Log::wholeLog.emplace_back(s, level);
  59. #ifdef DEBUG
  60. std::cout << s << std::endl;
  61. #endif
  62. }
  63. return (*this);
  64. }
  65. private:
  66. int level;
  67. std::ostringstream printBuffer;
  68. };
  69. #endif