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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. LogLevel(int l) : level(l) { printBuffer << std::boolalpha; }
  42. LogLevel& operator<< (const glm::vec2& v) {
  43. return (*this) << v.x << " " << v.y;
  44. }
  45. LogLevel& operator<< (const glm::i32vec2& v) {
  46. return (*this) << v.x << " " << v.y;
  47. }
  48. LogLevel& operator<< (const glm::vec3& v) {
  49. return (*this) << v.x << " " << v.y << " " << v.z;
  50. }
  51. template<typename T>
  52. LogLevel& operator<< (const T t) {
  53. printBuffer << t;
  54. if (printBuffer.str().back() == Log::endl) {
  55. std::string s = printBuffer.str().substr(0, printBuffer.str().length() - 1);
  56. printBuffer.str("");
  57. Log::wholeLog.emplace_back(s, level);
  58. #ifdef DEBUG
  59. std::cout << s << std::endl;
  60. #endif
  61. }
  62. return (*this);
  63. }
  64. private:
  65. int level;
  66. std::ostringstream printBuffer;
  67. };
  68. #endif