logger.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "logger.h"
  2. #include "utils.h"
  3. namespace graft_cv{
  4. CGcvLogger::CGcvLogger(ofstream& ofs)
  5. :m_target(terminal)
  6. ,m_level(debug)
  7. ,m_outfile(ofs)
  8. ,m_pMtx(new std::mutex())
  9. {
  10. cout<<currTime()<<" [WELCOME] ===========start logger==========="<<endl;
  11. }
  12. CGcvLogger::CGcvLogger(ofstream& ofs,log_target target, log_level level, string path)
  13. :m_target(target)
  14. ,m_level(level)
  15. ,m_path(path)
  16. ,m_outfile(ofs)
  17. , m_pMtx(new std::mutex())
  18. {
  19. string tmp = currTime() +" [WELCOME] ===========start logger===========\n";
  20. if(m_target !=terminal){
  21. //m_outfile.open(m_path, ios::out | ios::app);
  22. m_outfile.open(m_path, ofstream::out | ofstream::app);
  23. m_outfile<<tmp;
  24. }
  25. if(m_target != file){
  26. cout<<tmp<<endl;
  27. m_outfile.flush();
  28. }
  29. }
  30. void CGcvLogger::setPath(const string& path)
  31. {
  32. if (path !=m_path){
  33. m_path = path;
  34. }
  35. }
  36. string & CGcvLogger::getPath()
  37. {
  38. return m_path;
  39. }
  40. void CGcvLogger::output(string text, log_level act_level)
  41. {
  42. m_pMtx->lock();
  43. string prefix;
  44. if(act_level == debug){
  45. prefix = " [DEBUG] ";
  46. }
  47. else if(act_level== info){
  48. prefix =" [INFO] ";
  49. }
  50. else if(act_level == warning){
  51. prefix = " [WARNING] ";
  52. }
  53. else if(act_level == error){
  54. prefix= " [ERROR] ";
  55. }
  56. else {
  57. prefix ="";
  58. }
  59. string output_content= currTime() + prefix + text +"\n";
  60. if(this->m_level <= act_level && this->m_target!= file){
  61. cout <<output_content;
  62. }
  63. if(this->m_target != terminal){
  64. m_outfile<< output_content;
  65. m_outfile.flush();
  66. }
  67. m_pMtx->unlock();
  68. }
  69. void CGcvLogger::DEBUG(string text)
  70. {
  71. if(debug>=m_level){
  72. this->output(text,debug);
  73. }
  74. }
  75. void CGcvLogger::INFO(string text)
  76. {
  77. if(info>=m_level){
  78. this->output(text,info);
  79. }
  80. }
  81. void CGcvLogger::WARNING(string text)
  82. {
  83. if(warning>=m_level){
  84. this->output(text,warning);
  85. }
  86. }
  87. void CGcvLogger::ERRORINFO(string text)
  88. {
  89. if(error>=m_level){
  90. this->output(text,error);
  91. }
  92. }
  93. };