aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Łyszczek <michal.lyszczek@bofc.pl>2020-05-29 15:48:08 +0200
committerMichał Łyszczek <michal.lyszczek@bofc.pl>2020-05-29 15:48:08 +0200
commit9e84823607461ff43771c6f5c3f3a91ba8dbbf4a (patch)
tree01a51f9e8633ea0038eda11a76be38eb4e526438
parent25362541b8902b82210a9e59fd969594d06f5d5f (diff)
downloadembedlog-9e84823607461ff43771c6f5c3f3a91ba8dbbf4a.tar.gz
embedlog-9e84823607461ff43771c6f5c3f3a91ba8dbbf4a.tar.bz2
embedlog-9e84823607461ff43771c6f5c3f3a91ba8dbbf4a.zip
src/el-file.c: create symlink file to most recent log file
embedlog creates rotated log files from 0 (oldest) to N (newest), this leads to situation when name of the most recent log file is not deterministic (it may be log.1, and some time later log.2). For this option EL_FROTATE_SYMLINK has been added, and when this is enabled (default) embedlog will always keep symlink with file basename to most recent log file like: log.0 log.1 log.3 log -> log.3 Signed-off-by: Michał Łyszczek <michal.lyszczek@bofc.pl>
-rw-r--r--configure.ac2
-rw-r--r--include/embedlog.h.in4
-rw-r--r--src/el-file.c40
-rw-r--r--src/el-options.c21
4 files changed, 64 insertions, 3 deletions
diff --git a/configure.ac b/configure.ac
index 9d1a00c..e27ded9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -107,7 +107,7 @@ AS_IF([test "x$enable_out_file" = "xyes"],
[
AC_DEFINE([ENABLE_OUT_FILE], [1], [Enable printing to file])
ENABLE_OUT_FILE=1
- AC_CHECK_FUNCS(access stat fsync fileno)
+ AC_CHECK_FUNCS(access stat fsync fileno symlink)
])
AC_SUBST(ENABLE_OUT_FILE)
diff --git a/include/embedlog.h.in b/include/embedlog.h.in
index bd91415..575094e 100644
--- a/include/embedlog.h.in
+++ b/include/embedlog.h.in
@@ -118,6 +118,7 @@ enum el_option
EL_FPATH = 13,
EL_FROTATE_NUMBER = 14,
EL_FROTATE_SIZE = 15,
+ EL_FROTATE_SYMLINK= 19,
EL_FSYNC_EVERY = 16,
EL_FSYNC_LEVEL = 17,
EL_FILE_SYNC_EVERY= 16, /* deprecated */
@@ -130,7 +131,7 @@ enum el_option
* options you add).
*/
- EL_OPT_ERROR = 19
+ EL_OPT_ERROR = 20
};
enum el_option_timestamp
@@ -209,6 +210,7 @@ struct el
#if @ENABLE_OUT_FILE@
unsigned int fsync_level:3;
+ unsigned int frotate_symlink:1;
unsigned int frotate_number;
unsigned int fcurrent_rotate;
unsigned long frotate_size;
diff --git a/src/el-file.c b/src/el-file.c
index 675b74f..21a8014 100644
--- a/src/el-file.c
+++ b/src/el-file.c
@@ -36,6 +36,7 @@
#include "el-private.h"
+#include "el-utils.h"
#if HAVE_UNISTD_H
# include <unistd.h>
@@ -161,6 +162,40 @@ static int el_file_exists
/* ==========================================================================
+ create symlink without any suffix to newest log file. Rationale: it's
+ very often that user wants to view newest log file, but with the fact
+ that newest file has non deterministic suffix, it may be hard and
+ unconvenient to first check newest file and then read it, so we create
+ that nice symlink in a way:
+
+ log.0
+ log.1
+ log.5
+ log.7
+ log -> log.7
+
+ Function does not return anything as errors are not checked here.
+ Symlink is optional and if it is not created no data is lost and
+ returning error would confuse caller.
+ ========================================================================== */
+
+
+static void el_symlink_to_newest_log
+(
+ struct el *el /* embedlog object to work on */
+)
+{
+#if HAVE_SYMLINK
+ if (el->frotate_symlink)
+ {
+ (void)remove(el->fname);
+ (void)symlink(el_basename(el->fcurrent_log), el->fname);
+ }
+#endif
+}
+
+
+/* ==========================================================================
function rotates file, meaning if currently opened file has suffix .3,
function will close that file and will open file with suffix .4. If
creating new suffix is impossible (we reached frotate_number of
@@ -258,6 +293,7 @@ skip_rotate:
return -1;
}
+ el_symlink_to_newest_log(el);
el->fpos = 0;
return 0;
@@ -476,10 +512,12 @@ int el_file_open
#endif /* HAVE_STAT */
+
/* oldest file found, file is already opened so we
- * simply return from the function
+ * simply update symlink to newest log file and return.
*/
+ el_symlink_to_newest_log(el);
el->fcurrent_rotate = i;
el->fpos = fsize;
el->file = f;
diff --git a/src/el-options.c b/src/el-options.c
index b1de908..0694991 100644
--- a/src/el-options.c
+++ b/src/el-options.c
@@ -491,6 +491,25 @@ static int el_vooption
/* ==================================================================
+ ___ __ __ __ _ __
+ / _/____ ___ / /_ ___ _ / /_ ___ ___ __ __ __ _ / /(_)___ / /__
+ / _// __// _ \/ __// _ `// __// -_) (_-</ // // ' \ / // // _ \ / '_/
+ /_/ /_/ \___/\__/ \_,_/ \__/ \__/ /___/\_, //_/_/_//_//_//_//_//_/\_\
+ /___/
+ ================================================================== */
+
+
+ case EL_FROTATE_SYMLINK:
+ value_int = va_arg(ap, int);
+ VALID(EINVAL, (value_int & ~1) == 0);
+
+ el_lock(el);
+ el->frotate_symlink = value_int;
+ el_unlock(el);
+ return 0;
+
+
+ /* ==================================================================
___
/ _/___ __ __ ___ ____ ___ _ __ ___ ____ __ __
/ _/(_-</ // // _ \/ __/ / -_)| |/ // -_)/ __// // /
@@ -507,6 +526,7 @@ static int el_vooption
el_unlock(el);
return 0;
+
# endif /* ENABLE_OUT_FILE */
@@ -711,6 +731,7 @@ static int el_vooption
#if ENABLE_OUT_FILE
el->fsync_every = 32768;
el->fsync_level = EL_FATAL;
+ el->frotate_symlink = 1;
#endif
#if ENABLE_PTHREAD