From b85699a02706ee6d0b322888bf5da1c8906134dd Mon Sep 17 00:00:00 2001 From: Markus Mohrhard Date: Mon, 19 Dec 2016 12:54:34 +0100 Subject: add an option to pipe log output to file The target file and the option are controlled by SAL_LOG_FILE. The variable should contain the target file in a system dependent notation. Change-Id: Ie1ce9c39740c8b7a989e5af222be21a3e3142084 Reviewed-on: https://gerrit.libreoffice.org/32176 Tested-by: Jenkins Reviewed-by: Markus Mohrhard --- sal/osl/all/log.cxx | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'sal') diff --git a/sal/osl/all/log.cxx b/sal/osl/all/log.cxx index 00bd67cbb759..b400e43ab164 100644 --- a/sal/osl/all/log.cxx +++ b/sal/osl/all/log.cxx @@ -19,6 +19,7 @@ #include #include +#include #include "osl/thread.hxx" #include "rtl/string.h" @@ -86,8 +87,8 @@ char const * getEnvironmentVariable() { #else -char const * getEnvironmentVariable_() { - char const * p1 = std::getenv("SAL_LOG"); +char const * getEnvironmentVariable_(const char* env) { + char const * p1 = std::getenv(env); if (p1 == nullptr) { return nullptr; } @@ -99,10 +100,15 @@ char const * getEnvironmentVariable_() { } char const * getEnvironmentVariable() { - static char const * env = getEnvironmentVariable_(); + static char const * env = getEnvironmentVariable_("SAL_LOG"); return env; } +char const * getLogFile() { + static char const * logFile = getEnvironmentVariable_("SAL_LOG_FILE"); + return logFile; +} + void maybeOutputTimestamp(std::ostringstream &s) { char const * env = getEnvironmentVariable(); if (env == nullptr) @@ -339,8 +345,15 @@ void log( syslog(prio, "%s", s.str().c_str()); #endif } else { - std::fputs(s.str().c_str(), stderr); - std::fflush(stderr); + const char* logFile = getLogFile(); + if (logFile) { + std::ofstream file(logFile, std::ios::app | std::ios::out); + file << s.str(); + } + else { + std::fputs(s.str().c_str(), stderr); + std::fflush(stderr); + } } #endif } -- cgit v1.2.3