Add print_set_logfile() function to specify a log file as another possible output for messages, beside the standard output and the system log.
Signed-off-by: Miroslav Lichvar <mlich...@redhat.com> --- print.c | 22 ++++++++++++++++++++++ print.h | 1 + 2 files changed, 23 insertions(+) diff --git a/print.c b/print.c index 6c48e1e..3e8c587 100644 --- a/print.c +++ b/print.c @@ -29,6 +29,7 @@ static int print_level = LOG_INFO; static int use_syslog = 1; static const char *progname; static const char *message_tag; +static FILE *log_file = NULL; void print_set_progname(const char *name) { @@ -55,6 +56,20 @@ void print_set_verbose(int value) verbose = value ? 1 : 0; } +void print_set_logfile(const char *path) +{ + if (log_file) { + fclose(log_file); + log_file = NULL; + } + + if (path) { + log_file = fopen(path, "a"); + if (!log_file) + pr_err("failed to open %s: %m", path); + } +} + void print(int level, char const *format, ...) { struct timespec ts; @@ -80,6 +95,13 @@ void print(int level, char const *format, ...) buf); fflush(f); } + if (log_file) { + fprintf(log_file, "[%ld.%03ld] %s%s%s\n", + ts.tv_sec, ts.tv_nsec / 1000000, + message_tag ? message_tag : "", message_tag ? " " : "", + buf); + fflush(log_file); + } if (use_syslog) { syslog(level, "[%ld.%03ld] %s%s%s", ts.tv_sec, ts.tv_nsec / 1000000, diff --git a/print.h b/print.h index 1723d8a..338a921 100644 --- a/print.h +++ b/print.h @@ -37,6 +37,7 @@ void print_set_tag(const char *tag); void print_set_syslog(int value); void print_set_level(int level); void print_set_verbose(int value); +void print_set_logfile(const char *path); #define pr_emerg(x...) print(LOG_EMERG, x) #define pr_alert(x...) print(LOG_ALERT, x) -- 2.9.3 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Linuxptp-devel mailing list Linuxptp-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linuxptp-devel