summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorSamuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>2016-12-21 21:15:49 +0100
committerThorsten Behrens <Thorsten.Behrens@CIB.de>2018-01-17 20:58:19 +0100
commit9f027559557cb132835d8a13cdc0281ad4e757ae (patch)
treeefa7e2d0ec59085b7d2f2fe775a782625902025f /sal
parentcb3e9b7023160508094d54fe5f4f9db11f2a6d2f (diff)
WNT: allow to set log level/path from file
Expects a file logging.ini in the program/ directory with the keys LogFilePath and LogLevel, e.g.: LogFilePath=C:\log.txt LogLevel=info Note: This is Windows-only for now. En passant remove extraneous newlines from syslog and android log calls. Change-Id: I35c730469e4079139da908aa287b989dc98e0f9d
Diffstat (limited to 'sal')
-rw-r--r--sal/osl/all/log.cxx90
1 files changed, 76 insertions, 14 deletions
diff --git a/sal/osl/all/log.cxx b/sal/osl/all/log.cxx
index 636429c6992e..211fcb6fb9bd 100644
--- a/sal/osl/all/log.cxx
+++ b/sal/osl/all/log.cxx
@@ -33,6 +33,7 @@
#include <android/log.h>
#elif defined WNT
#include <process.h>
+#include <windows.h>
#define OSL_DETAIL_GETPID _getpid()
#else
#include <unistd.h>
@@ -81,13 +82,13 @@ char const * toString(sal_detail_LogLevel level) {
// the process is running":
#if defined ANDROID
-char const * getEnvironmentVariable() {
+char const * getLogLevel() {
return std::getenv("SAL_LOG");
}
#else
-char const * getEnvironmentVariable_(const char* env) {
+char const * getEnvironmentVariable(const char* env) {
char const * p1 = std::getenv(env);
if (p1 == nullptr) {
return nullptr;
@@ -99,18 +100,78 @@ char const * getEnvironmentVariable_(const char* env) {
return p2;
}
-char const * getEnvironmentVariable() {
- static char const * env = getEnvironmentVariable_("SAL_LOG");
- return env;
+#ifdef WNT
+bool getValueFromLoggingIniFile(const char* key, char* value) {
+ char buffer[MAX_PATH];
+ GetModuleFileName(NULL, buffer, MAX_PATH);
+ std::string sProgramDirectory = std::string(buffer);
+ std::string::size_type pos = sProgramDirectory.find_last_of( "\\/" );
+ sProgramDirectory = sProgramDirectory.substr(0, pos+1);
+ sProgramDirectory += "logging.ini";
+
+ std::ifstream logFileStream(sProgramDirectory);
+ if (!logFileStream.good())
+ return false;
+
+ std::size_t n;
+ std::string aKey;
+ std::string aValue;
+ std::string sWantedKey(key);
+ std::string sLine;
+ while (std::getline(logFileStream, sLine)) {
+ if (sLine.find('#') == 0)
+ continue;
+ if ( ( n = sLine.find('=') ) != std::string::npos) {
+ aKey = sLine.substr(0, n);
+ if (aKey != sWantedKey)
+ continue;
+ aValue = sLine.substr(n+1, sLine.length());
+ sprintf(value, "%s", aValue.c_str());
+ return true;
+ }
+ }
+ return false;
}
+#endif
+
+char const * getLogLevel() {
+ // First check the environment variable, then the setting in logging.ini
+ static char const * env = getEnvironmentVariable("SAL_LOG");
+
+ if (env != nullptr)
+ return env;
+
+#ifdef WNT
+ static char logLevel[1024];
+ if (getValueFromLoggingIniFile("LogLevel", logLevel))
+ return logLevel;
+#endif
-char const * getLogFile() {
- static char const * logFile = getEnvironmentVariable_("SAL_LOG_FILE");
- return logFile;
+ return nullptr;
+}
+
+std::ofstream * getLogFile() {
+ // First check the environment variable, then the setting in logging.ini
+ char const * logFile = getEnvironmentVariable("SAL_LOG_FILE");
+ if (!logFile)
+ return nullptr;
+
+#ifdef WNT
+ static char logFilePath[1024];
+ if (getValueFromLoggingIniFile("LogFilePath", logFilePath))
+ logFile = logFilePath;
+ else
+ return nullptr;
+#endif
+
+ // stays until process exits
+ static std::ofstream file(logFile, std::ios::app | std::ios::out);
+
+ return &file;
}
void maybeOutputTimestamp(std::ostringstream &s) {
- char const * env = getEnvironmentVariable();
+ static char const * env = getLogLevel();
if (env == nullptr)
return;
bool outputTimestamp = false;
@@ -217,7 +278,7 @@ void sal_detail_log(
if (backtraceDepth != 0) {
s << " at:\n" << osl::detail::backtraceAsString(backtraceDepth);
}
- s << '\n';
+
#if defined ANDROID
int android_log_level;
switch (level) {
@@ -258,12 +319,13 @@ void sal_detail_log(
syslog(prio, "%s", s.str().c_str());
#endif
} else {
- const char* logFile = getLogFile();
+ // avoid calling getLogFile() more than once
+ static std::ofstream * logFile = getLogFile();
if (logFile) {
- std::ofstream file(logFile, std::ios::app | std::ios::out);
- file << s.str();
+ *logFile << s.str() << std::endl;
}
else {
+ s << '\n';
std::fputs(s.str().c_str(), stderr);
std::fflush(stderr);
}
@@ -296,7 +358,7 @@ sal_Bool sal_detail_log_report(sal_detail_LogLevel level, char const * area) {
return true;
}
assert(area != nullptr);
- char const * env = getEnvironmentVariable();
+ static char const * env = getLogLevel();
if (env == nullptr) {
env = "+WARN";
}