Logging.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "Logging.h"
  2. #include "Ast.h"
  3. using qlow::Logger;
  4. Logger Logger::instance(std::cout);
  5. Logger::Logger(std::ostream& target) :
  6. std::ostream{ this },
  7. target{ target },
  8. indentVal{ 0 },
  9. firstChar{ true }
  10. {
  11. }
  12. void Logger::foreground(Color color, bool bright)
  13. {
  14. using std::string;
  15. string cchar = string(1, '0' + color);
  16. string isbright = bright ? "9" : "3";
  17. *this << "\033[" << isbright << cchar << "m";
  18. }
  19. void Logger::background(Color color, bool bright)
  20. {
  21. using std::string;
  22. string cchar = string(1, '0' + color);
  23. string isbright = bright ? "10" : "4";
  24. *this << "\033[" << isbright << cchar << "m";
  25. }
  26. void Logger::bold(void)
  27. {
  28. *this << "\033[1m";
  29. }
  30. void Logger::removeFormatting(void)
  31. {
  32. *this << "\033[0m";
  33. }
  34. void Logger::logError(const std::string& errMsg)
  35. {
  36. bold();
  37. foreground(RED, true);
  38. *this << "error: ";
  39. removeFormatting();
  40. *this << errMsg << std::endl;
  41. }
  42. void Logger::logError(const std::string& errMsg, const qlow::CodePosition& cp)
  43. {
  44. bold();
  45. *this << cp.filename << ":" << cp.first_line << ":" << cp.first_column << ": ";
  46. foreground(RED, true);
  47. *this << "error: ";
  48. removeFormatting();
  49. *this << errMsg << std::endl;
  50. }
  51. std::ostream& Logger::operator()(LogLevel ll)
  52. {
  53. if (logLevel >= ll) {
  54. return *this;
  55. }
  56. else {
  57. return nullStream;
  58. }
  59. }
  60. int Logger::overflow(int c)
  61. {
  62. target.put(char(c));
  63. return 0;
  64. }
  65. std::ostream& operator << (std::ostream& o, qlow::LogLevel l)
  66. {
  67. switch(l)
  68. {
  69. case qlow::LogLevel::WARNING:
  70. try {
  71. dynamic_cast<qlow::Logger&>(o).setLogType(l);
  72. }
  73. catch(...) {}
  74. return o << "\033[33m";
  75. }
  76. return o;
  77. }
  78. /*std::streamsize Logger::xsputn(const char* s, std::streamsize n)
  79. {
  80. target.write(s, n);
  81. }*/
  82. /*int main()
  83. {
  84. qlow::Logger& l = Logger::stdout;
  85. l << "aha" << std::endl;
  86. l.indent(10);
  87. l << "aha" << std::endl;
  88. l << qlow::LogLevel::WARNING << "ee";
  89. l.unindent(10);
  90. l << "aha" << std::endl;
  91. }*/