123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #include "logger.h"
- #include "utils.h"
- namespace graft_cv{
-
- CGcvLogger::CGcvLogger(ofstream& ofs)
- :m_target(terminal)
- ,m_level(debug)
- ,m_outfile(ofs)
- ,m_pMtx(new std::mutex())
- {
- cout<<currTime()<<" [WELCOME] ===========start logger==========="<<endl;
- }
- CGcvLogger::CGcvLogger(ofstream& ofs,log_target target, log_level level, string path)
- :m_target(target)
- ,m_level(level)
- ,m_path(path)
- ,m_outfile(ofs)
- , m_pMtx(new std::mutex())
- {
- string tmp = currTime() +" [WELCOME] ===========start logger===========\n";
- if(m_target !=terminal){
- //m_outfile.open(m_path, ios::out | ios::app);
- m_outfile.open(m_path, ofstream::out | ofstream::app);
- m_outfile<<tmp;
- }
- if(m_target != file){
- cout<<tmp<<endl;
- m_outfile.flush();
- }
- }
- void CGcvLogger::setPath(const string& path)
- {
- if (path !=m_path){
- m_path = path;
- }
- }
- string & CGcvLogger::getPath()
- {
- return m_path;
- }
- void CGcvLogger::output(string text, log_level act_level)
- {
- m_pMtx->lock();
- string prefix;
- if(act_level == debug){
- prefix = " [DEBUG] ";
- }
- else if(act_level== info){
- prefix =" [INFO] ";
- }
- else if(act_level == warning){
- prefix = " [WARNING] ";
- }
- else if(act_level == error){
- prefix= " [ERROR] ";
- }
- else {
- prefix ="";
- }
- string output_content= currTime() + prefix + text +"\n";
- if(this->m_level <= act_level && this->m_target!= file){
- cout <<output_content;
- }
- if(this->m_target != terminal){
- m_outfile<< output_content;
- m_outfile.flush();
- }
- m_pMtx->unlock();
- }
- void CGcvLogger::DEBUG(string text)
- {
- if(debug>=m_level){
- this->output(text,debug);
- }
- }
- void CGcvLogger::INFO(string text)
- {
- if(info>=m_level){
- this->output(text,info);
- }
- }
- void CGcvLogger::WARNING(string text)
- {
- if(warning>=m_level){
- this->output(text,warning);
- }
- }
- void CGcvLogger::ERRORINFO(string text)
- {
- if(error>=m_level){
- this->output(text,error);
- }
- }
- };
|