plog icon indicating copy to clipboard operation
plog copied to clipboard

Is it possible to truncate (or empty) log file while logging?

Open ChiftKey opened this issue 5 years ago • 3 comments

Hi! I'm using your library as a useful logging tool in my project.

I want to empty the log file (instead of DELETE) for test purposes.

Test Environment is below.

[Android NDK Project]

Target OS : Android Kit-Kat
NDK : Android NDK-R17C
C++ : stlport shared

I 've tried to empty a log file by bash redirection multiple times while my program was running.

My LogFile is hwCncLog.txt

root@tcc8930st:/sdcard/log # ls -l
-rw-rw---- root     sdcard_r    11834 2019-12-02 00:27 hwCncLog.txt
-rw-rw---- root     sdcard_r        6 2019-12-02 00:27 hwCncTestHandle.txt

and I tried to truncate file like this (shell redirection) in the adb shell

root@tcc8930st:/sdcard/log # > hwCncLog.txt

but the log file was still alive and being written

root@tcc8930st:/sdcard/log # ls -l
-rw-rw---- root     sdcard_r   176802 2019-12-02 00:30 hwCncLog.txt

My Logger setting in the code like this default logger is only used to print Android Logcat another logger is used to write file logger

//LOG PATH : /sdcard/log/hwCncLog.txt
//LOG_FILE_SIZE : 50 * 1048576 (50mb)
//LOG_FILE_NUM : 5
//
static plog::AndroidAppender<plog::FuncMessageFormatter> appender("Native-Log");
static plog::Logger<0>& logger = plog::init(plog::debug, &appender);
static plog::RollingFileAppender<plog::TxtFormatter> fileAppender(LOG_PATH, LOG_FILE_SIZE ,LOG_FILE_NUM);
static plog::Logger<1> &fileLogger = plog::init<1>(plog::debug, &fileAppender);
//FILE_LOGGER_NUM : 1 (file logger instance number)
//FILE_PRINT : bool variable (whether write log or not)
PLOGD_IF_(FILE_LOGGER_NUM, FILE_PRINT) << "cnc_rdexecprog"

my bottom line is... Is there a way to empty log file while writing file log in the process ??

ChiftKey avatar Dec 01 '19 15:12 ChiftKey

Hi @ChiftKey ,

Indeed it doesn't work. It looks that a file pointer continues to be incremented nevertheless the file is truncated. However if add O_APPEND flag (File class in Util.h) then it works fine:

m_file = ::open(fileName, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

I'll perform more tests and add it if everything will be good.

SergiusTheBest avatar Dec 01 '19 21:12 SergiusTheBest

Thanks a lot! After I modified the code that you mentioned, It works correctly.

ChiftKey avatar Dec 03 '19 02:12 ChiftKey

Excellent! I'll leave this open for a while until merging this changes into master.

SergiusTheBest avatar Dec 03 '19 07:12 SergiusTheBest