c++ - The updating for "stat" function is slow? -


code snippet:

void cmylogger::log(     loglevel level, lpctstr file, int line, lpctstr func, lpctstr format, ...) {     time_t tcurrentlogtime;     time(&tcurrentlogtime);      tm tmcurrentlogtime;     localtime_s(&tmcurrentlogtime, &tcurrentlogtime);      // check date changed     if (m_tmcurrentlogtime.tm_year == tmcurrentlogtime.tm_year         && m_tmcurrentlogtime.tm_mon == tmcurrentlogtime.tm_mon         && m_tmcurrentlogtime.tm_mday == tmcurrentlogtime.tm_mday)     {         // check log file size has exceeded maximum size         struct _stat st;         _tstat(m_szcurrentlogfile, &st);         if (st.st_size > m_nmaxlogsize)         {             m_ncurrentlogindex++;              getcurrentlogfile();              clogger::clearoutputstreams();              // add logger             addoutputstream(new std::tofstream(m_szcurrentlogfile), true, loglevel::info);         }     }      // date changed     else     {         // current log time         memcpy(&m_tmcurrentlogtime, &tmcurrentlogtime, sizeof(tmcurrentlogtime));          m_ncurrentlogindex = 0;          deleteoldfiles();         getcurrentlogfile();          clogger::clearoutputstreams();          // add logger         addoutputstream(new std::tofstream(m_szcurrentlogfile), true, loglevel::info);     }       // log     va_list args;     va_start(args, format);     int length = _vsctprintf(format, args ) + 1;     tchar* text =  new tchar[length];     _vstprintf_s(text, length, format, args);     va_end(args);      clogger::log(level, file, line, func, text);      delete [] text; } 

the above code snippet of log program. if log file exceeds 1 mb, create new log file. (xxx_000.log => xxx_001.log ...) write following code test whether new log file created when log file exceeds 1 mb:

#ifdef unicode #define log(level, format, ...) ceaglelogger::getinstance()->log(level, __filew__, __line__, __functionw__, format, __va_args__); #else #define log(level, format, ...) ceaglelogger::getinstance()->log(level, __file__, __line__, __function__, format, __va_args__); #endif  // ...  while (1)     log(loglevel::info, _t("================================================================================")); 

but, log files not created every 1 mb. log file size checked stat function before write log. when actual log file size exceeded 1 mb, 'stat' function not exceed 1 mb. updating "stat" function slow?

the log size different.

stat posix function. _tstat windows function intended porting posix code windows (but _t stuff hinders porting other way). don't expect best performance such wrappers.

in particular, stat equivalent of findfirstfile on windows, , crt code included visual studio confirms this: first system call in _stat findfirstfileex(findexinfostandard ... findexsearchnamematch). . gives number of properties of directory entry, including file size in directory. file that's being written, information out of date. getfilesize has correct size

(note _stat doing quite bit of work kinds of properties don't want. pretty cheap on unix, on windows file access rights stuff has emulated. not fast either.)


Comments