Logging.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. int Logger::overflow(int c)
  52. {
  53. target.put(char(c));
  54. return 0;
  55. }
  56. std::ostream& operator << (std::ostream& o, qlow::LogLevel l)
  57. {
  58. switch(l)
  59. {
  60. case qlow::LogLevel::WARNING:
  61. try {
  62. dynamic_cast<qlow::Logger&>(o).setLogType(l);
  63. }
  64. catch(...) {}
  65. return o << "\033[33m";
  66. }
  67. return o;
  68. }
  69. /*std::streamsize Logger::xsputn(const char* s, std::streamsize n)
  70. {
  71. target.write(s, n);
  72. }*/
  73. /*int main()
  74. {
  75. qlow::Logger& l = Logger::stdout;
  76. l << "aha" << std::endl;
  77. l.indent(10);
  78. l << "aha" << std::endl;
  79. l << qlow::LogLevel::WARNING << "ee";
  80. l.unindent(10);
  81. l << "aha" << std::endl;
  82. }*/