daemon.cpp
/**
 * Loginimas i faila. Galima naudoti printf() stiliaus argumentus
 *
 * @param int logLevel - pranesimo tipas (LogLevelDebug, LogLevelError, LogLevelWarning, LogLevelInfo)
 * @param string text - tekstas su printf() formatu
 */
Loginimas i faila. Galima naudoti printf() stiliaus argumentus
@param int logLevel - pranesimo tipas (LogLevelDebug, LogLevelError, LogLevelWarning, LogLevelInfo)
@param string text - tekstas su printf() formatu
Loginimas i faila . Galima naudoti printf () stiliaus argumentus @param int logLevel - pranesimo tipas ( LogLevelDebug LogLevelError LogLevelWarning LogLevelInfo ) @param string text - tekstas su printf () formatu
void Daemon::_log(int logLevel, std::string text, ...)
{
    if (logLevel == LogLevelDebug && !_debug)
        return;
    _logFileFd = fopen(_logFile.c_str(), "a+");
    if (_logFileFd) {
        FILE *fd = _logFileFd;
        time_t now;
        time(&now);
        char timeNow[30];
        strftime(timeNow, 30, "[%Y-%m-%d %X] ", localtime(&now));
        string logLevelStr;
        switch (logLevel) {
        case LogLevelDebug:
            logLevelStr = "(DEBUG) ";
            break;
        case LogLevelError:
            logLevelStr = "(ERROR) ";
            break;
        case LogLevelWarning:
            logLevelStr = "(WARNING) ";
            break;
        case LogLevelInfo:
            logLevelStr = "(INFO) ";
            break;
        default:
            logLevelStr = "(DEBUG) ";
        }
        text = string(timeNow) + logLevelStr + text + "\n";
        va_list args;
        va_start(args, text.c_str());
        va_end(args);
        vfprintf(fd, text.c_str(), args);
        fflush(fd);
    }
    fclose(_logFileFd);
}
void Daemon :: _log ( int logLevel , std :: string text , ... ) { if ( logLevel == LogLevelDebug && ! _debug ) return ; _logFileFd = fopen ( _logFile . c_str ( ) , " " ) ; if ( _logFileFd ) { FILE * fd = _logFileFd ; time_t now ; time ( & now ) ; char timeNow [ 30 ] ; strftime ( timeNow , 30 , " " , localtime ( & now ) ) ; string logLevelStr ; switch ( logLevel ) { case LogLevelDebug : logLevelStr = " " ; break ; case LogLevelError : logLevelStr = " " ; break ; case LogLevelWarning : logLevelStr = " " ; break ; case LogLevelInfo : logLevelStr = " " ; break ; default : logLevelStr = " " ; } text = string ( timeNow ) + logLevelStr + text + " \n " ; va_list args ; va_start ( args , text . c_str ( ) ) ; va_end ( args ) ; vfprintf ( fd , text . c_str ( ) , args ) ; fflush ( fd ) ; } fclose ( _logFileFd ) ; }
Loginimas i faila.
Loginimas i faila .