Пример #1
0
void Lvk::Cmn::Logger::init()
{
    if (m_logFile) {
        return; // Already initialized
    }

    m_strPid = QString::number(QCoreApplication::applicationPid());

    QString logFilename = chatbotLogFilename();

    if (makeLogsPath()) {
        rotateLog(logFilename);

        m_logFile = new QFile(logFilename);

        if (m_logFile->open(QFile::Append)) {
            qInstallMsgHandler(msgHandler);
            qDebug() << "Logger initialized on"
                     << APP_NAME " v" APP_VERSION_STR " rev:" APP_VERSION_REV;
            qDebug() << "OS Type:" << getOSType();
        } else {
            delete m_logFile;
            m_logFile = 0;
            qCritical() << CRITICAL_STR "Cannot open" << logFilename << " in append mode.";
        }
    } else {
        qCritical() << CRITICAL_STR "Cannot create path for log file" << logFilename;
    }
}
Пример #2
0
void CLogger::logMessage(int level,const char *file, int line, const char *function,const char *fmt, ...) {
    if (level>_level) return;
    
    if (_check && _name) {
        checkFile();
    }
    
    time_t t;
    time(&t);
    struct tm tm;
    ::localtime_r((const time_t*)&t, &tm);
    
    char data1[4000];
    char buffer[5000];

    va_list args;
    va_start(args, fmt);
    vsnprintf(data1, 4000, fmt, args);
    va_end(args);
    
    int size;
    if (level < TBSYS_LOG_LEVEL_INFO) {
        size = snprintf(buffer,5000,"[%04d-%02d-%02d %02d:%02d:%02d] %-5s %s (%s:%d) %s\n",
            tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
            tm.tm_hour, tm.tm_min, tm.tm_sec,
            _errstr[level], function, file, line, data1);
    } else {
        size = snprintf(buffer,5000,"[%04d-%02d-%02d %02d:%02d:%02d] %-5s %s:%d %s\n",
            tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
            tm.tm_hour, tm.tm_min, tm.tm_sec,
            _errstr[level], file, line, data1);
    }
    // 去掉过多的换行
    while (buffer[size-2] == '\n') size --;
    buffer[size] = '\0';
    while (size > 0) {
        ssize_t success = ::write(_fd, buffer, size);
        if (success == -1) break;    
        size -= success;
    }

    if ( _maxFileSize ){
        pthread_mutex_lock(&_fileSizeMutex);
        off_t offset = ::lseek(_fd, 0, SEEK_END);
        if ( offset < 0 ){
            // we got an error , ignore for now
        } else {
            if ( static_cast<size_t>(offset) >= _maxFileSize ) {
                rotateLog(NULL);
            }
        }
        pthread_mutex_unlock(&_fileSizeMutex);
    }
}
Пример #3
0
void openLog() {
    int tries = 0;
    while (tries++ < 3 && !dataFile.open(root, logFilename, O_CREAT | O_WRITE)) {
        Serial.println(F("Unable to create log file, rotating..."));
        rotateLog();
    }
    if (!dataFile.isOpen()) {
        Serial.println(F("Unable to create log file, disabling data logger."));
        disableLogging = true;
        return;
    }
}
Пример #4
0
int MaHost::start()
{
	char	*hex = "0123456789abcdef";
	uchar	bytes[MPR_HTTP_MAX_SECRET];
	char	ascii[MPR_HTTP_MAX_SECRET * 2 + 1], *ap;
	int		i;

	//
	//	Create a random secret for use in authentication. Don't block.
	//	FUTURE -- conditional on Auth
	//
	mprLog(7, "Get random bytes\n");
	if (mprGetRandomBytes(bytes, sizeof(bytes), 0) < 0) {
		mprError(MPR_L, MPR_LOG, "Can't generate local secret");
		return MPR_ERR_CANT_INITIALIZE;
	}
	ap = ascii;
	for (i = 0; i < (int) sizeof(bytes); i++) {
		*ap++ = hex[bytes[i] >> 4];
		*ap++ = hex[bytes[i] & 0xf];
	}
	*ap = '\0';
	secret = mprStrdup(ascii);
	mprLog(7, "Got %d random bytes\n", sizeof(bytes));

#if BLD_FEATURE_ACCESS_LOG && !BLD_FEATURE_ROMFS
	if (logPath) {
		logFd = open(logPath, O_CREAT | O_APPEND | O_WRONLY | O_TEXT, 0664);
		if (logFd < 0) {
			mprError(MPR_L, MPR_LOG, "Can't open log file %s", logPath);
		}
#if FUTURE
		rotateLog();
#endif
	}
#endif
	return 0;
}
Пример #5
0
  void CLogger::logMessage(int level,const char *file, int line, const char *function, pthread_t tid, const char *fmt, ...) {
    if (level>_level) return;

    if (_check && _name) {
        checkFile();
    }

    struct timeval tv;
    gettimeofday(&tv, NULL);
    struct tm tm;
    ::localtime_r((const time_t*)&tv.tv_sec, &tm);
    const int max_log_size = 10240;

    char data1[max_log_size];
    char head[128];

    va_list args;
    va_start(args, fmt);
    int data_size = vsnprintf(data1, max_log_size, fmt, args);
    va_end(args);
    if (data_size >= max_log_size)
    {
      data_size = max_log_size - 1;
    }
    // remove trailing '\n'
    while (data1[data_size-1] == '\n') data_size --;
    data1[data_size] = '\0';

    int head_size;
    if (level < TBSYS_LOG_LEVEL_INFO) {
        head_size = snprintf(head,128,"[%04d-%02d-%02d %02d:%02d:%02d.%06ld] %-5s %s (%s:%d) [%ld] ",
                        tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
                        tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec,
                        _errstr[level], function, file, line, tid);
    } else {
        head_size = snprintf(head,128,"[%04d-%02d-%02d %02d:%02d:%02d.%06ld] %-5s %s:%d [%ld] ",
                        tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
                        tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec,
                        _errstr[level], file, line, tid);
    }
    struct iovec vec[3];
    vec[0].iov_base = head;
    vec[0].iov_len = head_size;
    vec[1].iov_base = data1;
    vec[1].iov_len = data_size;
    vec[2].iov_base = NEWLINE;
    vec[2].iov_len = sizeof(NEWLINE);
    if (data_size > 0)
    {
      ::writev(_fd, vec, 3);
    }
    if ( _maxFileSize ){
        pthread_mutex_lock(&_fileSizeMutex);
        off_t offset = ::lseek(_fd, 0, SEEK_END);
        if ( offset < 0 ){
            // we got an error , ignore for now
        } else {
            if ( static_cast<int64_t>(offset) >= _maxFileSize ) {
                rotateLog(NULL);
            }
        }
        pthread_mutex_unlock(&_fileSizeMutex);
    }

    // write data to warning buffer for SQL
    if (WarningBuffer::is_warn_log_on() && data_size > 0)
    {
      if (level == TBSYS_LOG_LEVEL_WARN)
      { // WARN only
        WarningBuffer *wb = get_tsi_warning_buffer();
        if (NULL != wb)
        {
          wb->append_warning(data1);
        }
      }
      else if (level == TBSYS_LOG_LEVEL_USER_ERROR)
      {
        WarningBuffer *wb = get_tsi_warning_buffer();
        if (NULL != wb)
        {
          wb->set_err_msg(data1);
        }
      }
    }
    return;
}
Пример #6
0
void writeLog() {
    if (disableLogging) {
        return;
    }

    if (!dataFile.isOpen()) {
        if (!dataFile.open(root, logFilename, O_WRITE | O_APPEND)) {
#ifdef DEBUG
            Serial.println(F("Could not open file for writing"));
#endif
            disableLogging = true;
            return;
        }
    }

    // Local sensor readings
    logFileSize += dataFile.print(lastUpdate);
    logFileSize += dataFile.print(F("\t"));
    logFileSize += dataFile.print(sinceLastAck);
    logFileSize += dataFile.print(F("\t"));
    logFileSize += dataFile.print(lastVcc);
    logFileSize += dataFile.print(F("\t"));
    if (validReadingi(lastRoundtrip)) {
        logFileSize += dataFile.print(lastRoundtrip);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingi(lastRssi)) {
        logFileSize += dataFile.print(lastRssi);
    }
    logFileSize += dataFile.print(F("\t"));

    // Remote sensor readings
    if (validReadingi(m.vcc)) {
        logFileSize += dataFile.print(m.vcc);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingi(m.rssi)) {
        logFileSize += dataFile.print(m.rssi);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingi(m.vibration)) {
        logFileSize += dataFile.print(m.vibration);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.altitudeGps)) {
        logFileSize += dataFile.print(m.altitudeGps);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.altitude)) {
        logFileSize += dataFile.print(m.altitude);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.temp)) {
        logFileSize += dataFile.print(m.temp);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.temp2)) {
        logFileSize += dataFile.print(m.temp2);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.latitude)) {
        logFileSize += dataFile.print(m.latitude, 6);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.longitude)) {
        logFileSize += dataFile.print(m.longitude, 6);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.accelX)) {
        logFileSize += dataFile.print(m.accelX);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.accelY)) {
        logFileSize += dataFile.print(m.accelY);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.accelZ)) {
        logFileSize += dataFile.print(m.accelZ);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.magX)) {
        logFileSize += dataFile.print(m.magX);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.magY)) {
        logFileSize += dataFile.print(m.magY);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.magZ)) {
        logFileSize += dataFile.print(m.magZ);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.gyroX)) {
        logFileSize += dataFile.print(m.gyroX);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.gyroY)) {
        logFileSize += dataFile.print(m.gyroY);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.gyroZ)) {
        logFileSize += dataFile.print(m.gyroZ);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.accel2X)) {
        logFileSize += dataFile.print(m.accel2X);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.accel2Y)) {
        logFileSize += dataFile.print(m.accel2Y);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.accel2Z)) {
        logFileSize += dataFile.print(m.accel2Z);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.mag2X)) {
        logFileSize += dataFile.print(m.mag2X);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.mag2Y)) {
        logFileSize += dataFile.print(m.mag2Y);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.mag2Z)) {
        logFileSize += dataFile.print(m.mag2Z);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.gyro2X)) {
        logFileSize += dataFile.print(m.gyro2X);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.gyro2Y)) {
        logFileSize += dataFile.print(m.gyro2Y);
    }
    logFileSize += dataFile.print(F("\t"));
    if (validReadingf(m.gyro2Z)) {
        logFileSize += dataFile.print(m.gyro2Z);
    }
    logFileSize += dataFile.println(F("\t"));
    dataFile.sync();
    if (logFileSize >= logFileMax) {
        dataFile.close();
        rotateLog();
        openLog();
    }
}