static inline int createFile(void) {

#ifdef _DEBUGFLAGS_H_
    {
        static unsigned int registered = 0;
        if (unlikely(0 == registered)) {
            registered = 1; /* dirty work around to avoid deadlock: syslogex->register->syslogex */
            registered = (registerLibraryDebugFlags(&debugFlags) == EXIT_SUCCESS);
        }
    }
#endif /*_DEBUGFLAGS_H_*/

    int error = pthread_mutex_lock(&fileLock);

    if (likely(EXIT_SUCCESS == error)) {
        int internalError = EXIT_SUCCESS;

        if (log_fd[current_log_fd] != -1) {
            WARNING_MSG("fd was NOT NULL");
            if (close(log_fd[current_log_fd]) != 0) {
                internalError = errno;
                ERROR_MSG("close %d error %d (%m)", log_fd[current_log_fd], internalError);
            }
            log_fd[current_log_fd] = -1;
        }

        if (unlikely(NULL == processName)) {
            setProcessName();
        }
        setFullFileName();

        log_fd[current_log_fd] = open(fullFileName, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR | S_IRGRP);
        if (likely(log_fd[current_log_fd] != -1)) {
            fileSize = 0;
        } else {
            error = errno;
            ERROR_MSG("open %s for creation error %d (%m)", fullFileName, error);
        }

        internalError = pthread_mutex_unlock(&fileLock);
        if (internalError != EXIT_SUCCESS) {
            ERROR_MSG("pthread_mutex_lock fileLock error %d (%s)", internalError, strerror(internalError));
            if (EXIT_SUCCESS == error) {
                error = internalError;
            }
        }

    } else {
        ERROR_MSG("pthread_mutex_lock fileLock error %d (%m)", error);
    }

    return error;
}
	void DailyRollingFileAppender::rollOver()
	{
	    // Q_ASSERT_X(, "DailyRollingFileAppender::rollOver()", "Lock must be held by caller")
	    Q_ASSERT_X(!mActiveDatePattern.isEmpty(), "DailyRollingFileAppender::rollOver()", "No active date pattern");
	
	    QString roll_over_suffix = mRollOverSuffix;
	    computeRollOverTime();
	    if (roll_over_suffix == mRollOverSuffix)
	        return;
	
	    closeFile();
	
        QString target_file_name = filePath() + fileName() + mRollOverSuffix + suffix();
        setFullFileName(target_file_name);
//	    QFile f(target_file_name);
//	    if (f.exists() && !removeFile(f))
//	        return;
//        f.setFileName(fullFileName());
//	    if (!renameFile(f, target_file_name))
//            return;
	    openFile();
	}
Exemple #3
0
static inline int createFile(void) {

#ifdef _DEBUGFLAGS_H_
	{
		static unsigned int registered = 0;
		if (unlikely(0 == registered)) {
			registered = 1; /* dirty work around to avoid deadlock: syslogex->register->syslogex */
			registered = (registerLibraryDebugFlags(&debugFlags) == EXIT_SUCCESS);
		}
	}
#endif /*_DEBUGFLAGS_H_*/

	int error = pthread_mutex_lock(&fileLock);

	if (likely(EXIT_SUCCESS == error)) {
		int internalError = EXIT_SUCCESS;
		int flags = O_WRONLY|O_CREAT|O_TRUNC;

		if (likely((!(LogStat & LOG_FILE_WITHOUT_SYNC)) && (!(LogStat & LOG_FILE_SYNC_ON_ERRORS_ONLY)))) {
			flags |= O_SYNC; /* enable synchronous I/O to avoid data lost in case of crash */
		}

		if (logFile != -1) {
			WARNING_MSG("logFile was NOT NULL");
			if (close(logFile) != 0) {
				internalError = errno;
				ERROR_MSG("close %d error %d (%m)", logFile, internalError);
			}
			logFile = -1;
		}

		if (unlikely(NULL == processName)) {
			setProcessName();
		}
		setFullFileName();

		logFile = open(fullFileName,flags,S_IRUSR|S_IWUSR|S_IRGRP);
		if (likely(logFile != -1)) {
			fileSize = 0;
			if (LOG_FILE_DURATION & LogStat) {
				startTime = time(NULL);
			}
		} else {
			error = errno;
			ERROR_MSG("open %s for creation error %d (%m)", fullFileName, error);
		}

		internalError = pthread_mutex_unlock(&fileLock);
		if (internalError != EXIT_SUCCESS) {
			ERROR_MSG("pthread_mutex_lock fileLock error %d (%s)", internalError, strerror(internalError));
			if (EXIT_SUCCESS == error) {
				error = internalError;
			}
		}

	} else {
		ERROR_MSG("pthread_mutex_lock fileLock error %d (%m)", error);
	}

	return error;
}