logger.cpp 1.8 KB

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