mirror of
https://github.com/apache/nuttx.git
synced 2026-06-04 06:42:32 +08:00
Added automatic log rotation, when log file is opened.
This commit is contained in:
committed by
Alan Carvalho de Assis
parent
2e54df0f35
commit
216194d31b
+33
-1
@@ -13,6 +13,8 @@ config ARCH_SYSLOG
|
|||||||
|
|
||||||
# Selected if the SYSLOG device supports multi-byte write operations
|
# Selected if the SYSLOG device supports multi-byte write operations
|
||||||
|
|
||||||
|
comment "SYSLOG options"
|
||||||
|
|
||||||
config SYSLOG_MAX_CHANNELS
|
config SYSLOG_MAX_CHANNELS
|
||||||
int "Maximum SYSLOG channels"
|
int "Maximum SYSLOG channels"
|
||||||
default 1
|
default 1
|
||||||
@@ -105,6 +107,8 @@ config SYSLOG_INTBUFSIZE
|
|||||||
---help---
|
---help---
|
||||||
The size of the interrupt buffer in bytes.
|
The size of the interrupt buffer in bytes.
|
||||||
|
|
||||||
|
comment "Formatting options"
|
||||||
|
|
||||||
config SYSLOG_TIMESTAMP
|
config SYSLOG_TIMESTAMP
|
||||||
bool "Prepend timestamp to syslog message"
|
bool "Prepend timestamp to syslog message"
|
||||||
default n
|
default n
|
||||||
@@ -188,6 +192,8 @@ config SYSLOG_COLOR_OUTPUT
|
|||||||
---help---
|
---help---
|
||||||
Enables colored output in syslog, according to message priority.
|
Enables colored output in syslog, according to message priority.
|
||||||
|
|
||||||
|
comment "SYSLOG channels"
|
||||||
|
|
||||||
if !ARCH_SYSLOG
|
if !ARCH_SYSLOG
|
||||||
config SYSLOG_CHAR
|
config SYSLOG_CHAR
|
||||||
bool "Log to a character device"
|
bool "Log to a character device"
|
||||||
@@ -251,7 +257,7 @@ config SYSLOG_RPMSG_SERVER
|
|||||||
---help---
|
---help---
|
||||||
Use rpmsg to receive message from remote proc.
|
Use rpmsg to receive message from remote proc.
|
||||||
|
|
||||||
config SYSLOG_FILE
|
menuconfig SYSLOG_FILE
|
||||||
bool "Syslog file output"
|
bool "Syslog file output"
|
||||||
default n
|
default n
|
||||||
---help---
|
---help---
|
||||||
@@ -265,6 +271,32 @@ config SYSLOG_FILE
|
|||||||
NOTE interrupt level SYSLOG output will be lost in this case unless
|
NOTE interrupt level SYSLOG output will be lost in this case unless
|
||||||
the interrupt buffer is used.
|
the interrupt buffer is used.
|
||||||
|
|
||||||
|
if SYSLOG_FILE
|
||||||
|
|
||||||
|
config SYSLOG_FILE_ROTATE
|
||||||
|
bool "Log file rotate"
|
||||||
|
default n
|
||||||
|
depends on SYSLOG_FILE
|
||||||
|
---help---
|
||||||
|
If enabled, the log file size will be checked before opening.
|
||||||
|
If it is larger than the specified limit it will be "rotated",
|
||||||
|
i.e. the old file will be kept as a backup, and a new empty
|
||||||
|
file will be created.
|
||||||
|
|
||||||
|
This option is useful to ensure that log files do not get
|
||||||
|
huge after prolonged periods of system operation.
|
||||||
|
|
||||||
|
config SYSLOG_FILE_SIZE_LIMIT
|
||||||
|
int "Log file size limit"
|
||||||
|
default 524288
|
||||||
|
depends on SYSLOG_FILE_ROTATE
|
||||||
|
---help---
|
||||||
|
File size limit when the log is rotated automatically.
|
||||||
|
If a log file is found larger than this limit, it will
|
||||||
|
be rotated.
|
||||||
|
|
||||||
|
endif # SYSLOG_FILE
|
||||||
|
|
||||||
config CONSOLE_SYSLOG
|
config CONSOLE_SYSLOG
|
||||||
bool "Use SYSLOG for /dev/console"
|
bool "Use SYSLOG for /dev/console"
|
||||||
default n
|
default n
|
||||||
|
|||||||
@@ -25,9 +25,13 @@
|
|||||||
#include <nuttx/config.h>
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <nuttx/syslog/syslog.h>
|
#include <nuttx/syslog/syslog.h>
|
||||||
|
|
||||||
@@ -50,6 +54,62 @@
|
|||||||
|
|
||||||
FAR static struct syslog_channel_s *g_syslog_file_channel;
|
FAR static struct syslog_channel_s *g_syslog_file_channel;
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifdef CONFIG_SYSLOG_FILE_ROTATE
|
||||||
|
static void log_rotate(FAR const char *log_file)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
off_t size;
|
||||||
|
struct stat f_stat;
|
||||||
|
char *backup_file;
|
||||||
|
|
||||||
|
/* Get the size of the current log file. */
|
||||||
|
|
||||||
|
fd = open(log_file, O_RDONLY);
|
||||||
|
if (fd < 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fstat(fd, &f_stat);
|
||||||
|
size = f_stat.st_size;
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
/* If it does not exceed the limit we are OK. */
|
||||||
|
|
||||||
|
if (size < CONFIG_SYSLOG_FILE_SIZE_LIMIT)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Construct the backup file name. */
|
||||||
|
|
||||||
|
backup_file = malloc(strlen(log_file) + 3);
|
||||||
|
if (backup_file == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sprintf(backup_file, "%s.0", log_file);
|
||||||
|
|
||||||
|
/* Delete any old backup files. */
|
||||||
|
|
||||||
|
if (access(backup_file, F_OK) == 0)
|
||||||
|
{
|
||||||
|
remove(backup_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Rotate the log. */
|
||||||
|
|
||||||
|
rename(log_file, backup_file);
|
||||||
|
|
||||||
|
free(backup_file);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Functions
|
* Public Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@@ -108,6 +168,12 @@ FAR struct syslog_channel_s *syslog_file_channel(FAR const char *devpath)
|
|||||||
syslog_dev_uninitialize(g_syslog_file_channel);
|
syslog_dev_uninitialize(g_syslog_file_channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Rotate the log file, if needed. */
|
||||||
|
|
||||||
|
#ifdef CONFIG_SYSLOG_FILE_ROTATE
|
||||||
|
log_rotate(devpath);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Then initialize the file interface */
|
/* Then initialize the file interface */
|
||||||
|
|
||||||
g_syslog_file_channel = syslog_dev_initialize(devpath, OPEN_FLAGS,
|
g_syslog_file_channel = syslog_dev_initialize(devpath, OPEN_FLAGS,
|
||||||
|
|||||||
Reference in New Issue
Block a user