From 0a0eebb33f363f0f5c25af34910b8aefcb75e5da Mon Sep 17 00:00:00 2001 From: Frank Celler Date: Wed, 28 Jan 2015 23:58:43 +0100 Subject: [PATCH] log to stderr if on a tty --- .../Books/Users/ConfigureArango/Logging.mdpp | 18 ++- lib/ApplicationServer/ApplicationServer.cpp | 134 ++++++++---------- lib/ApplicationServer/ApplicationServer.h | 19 +-- lib/Rest/AnyServer.cpp | 12 +- 4 files changed, 85 insertions(+), 98 deletions(-) diff --git a/Documentation/Books/Users/ConfigureArango/Logging.mdpp b/Documentation/Books/Users/ConfigureArango/Logging.mdpp index f091f3ee7c..2f5b1930a1 100644 --- a/Documentation/Books/Users/ConfigureArango/Logging.mdpp +++ b/Documentation/Books/Users/ConfigureArango/Logging.mdpp @@ -8,8 +8,22 @@ statistics about executed requests and timings about computation steps. !SECTION General Logging Options !SUBSECTION Logfile - -@startDocuBlock logFile +`--log.file filename` + +This option allows the user to specify the name of a file to which information +is logged. By default, if no log file is specified, the standard output is +used. Note that if the file named by *filename* does not exist, it will be +created. If the file cannot be created (e.g. due to missing file privileges), +the server will refuse to start. If the specified file already exists, output is +appended to that file. + +Use *+* to log to standard error. Use *-* to log to standard output. +Use *""* to disable logging to file. + +`--log.tty filename` + +Be default, if started on a tty, the log output will also go to the ttyp. +Use *""* to disable. !SUBSECTION Request diff --git a/lib/ApplicationServer/ApplicationServer.cpp b/lib/ApplicationServer/ApplicationServer.cpp index ec3f9038a1..65856df38a 100644 --- a/lib/ApplicationServer/ApplicationServer.cpp +++ b/lib/ApplicationServer/ApplicationServer.cpp @@ -97,86 +97,50 @@ string const ApplicationServer::OPTIONS_SSL = "SSL Options"; /// @brief constructor //////////////////////////////////////////////////////////////////////////////// +ApplicationServer::ApplicationServer (std::string const& name, std::string const& title, std::string const& version) + : _options(), + _description(), + _descriptionFile(), + _arguments(), + _features(), + _exitOnParentDeath(false), + _watchParent(0), + _stopping(0), + _name(name), + _title(title), + _version(version), + _configFile(), + _userConfigFile(), + _systemConfigFile(), + _uid(), + _realUid(0), + _effectiveUid(0), + _gid(), + _realGid(0), + _effectiveGid(0), + _logApplicationName("arangod"), + _logFacility(""), + _logLevel("info"), + _logSeverity("human"), + _logFile("+"), + _logTty("+"), + _logRequestsFile(""), + _logPrefix(), + _logThreadId(false), + _logLineNumber(false), + _logLocalTime(false), + _logSourceFilter(), + _logContentFilter(), #ifdef _WIN32 - -ApplicationServer::ApplicationServer (std::string const& name, std::string const& title, std::string const& version) - : _options(), - _description(), - _descriptionFile(), - _arguments(), - _features(), - _exitOnParentDeath(false), - _watchParent(0), - _stopping(0), - _name(name), - _title(title), - _version(version), - _configFile(), - _userConfigFile(), - _systemConfigFile(), - _uid(), - _realUid(0), - _effectiveUid(0), - _gid(), - _realGid(0), - _effectiveGid(0), - _logApplicationName("arangod"), - _logFacility(""), - _logLevel("info"), - _logSeverity("human"), - _logFile("+"), - _logRequestsFile(""), - _logPrefix(), - _logThreadId(false), - _logLineNumber(false), - _logLocalTime(false), - _logSourceFilter(), - _logContentFilter(), _randomGenerator(5), - _finishedCondition() { -} - #else - -ApplicationServer::ApplicationServer (std::string const& name, std::string const& title, std::string const& version) - : _options(), - _description(), - _descriptionFile(), - _arguments(), - _features(), - _exitOnParentDeath(false), - _watchParent(0), - _stopping(0), - _name(name), - _title(title), - _version(version), - _configFile(), - _userConfigFile(), - _systemConfigFile(), - _uid(), - _realUid(0), - _effectiveUid(0), - _gid(), - _realGid(0), - _effectiveGid(0), - _logApplicationName("arangod"), - _logFacility(""), - _logLevel("info"), - _logSeverity("human"), - _logFile("+"), - _logRequestsFile(""), - _logPrefix(), - _logThreadId(false), - _logLineNumber(false), - _logLocalTime(false), - _logSourceFilter(), - _logContentFilter(), _randomGenerator(3), - _finishedCondition() { - storeRealPrivileges(); -} - #endif + _finishedCondition() { +#ifndef _WIN32 + storeRealPrivileges(); +#endif +} //////////////////////////////////////////////////////////////////////////////// /// @brief destructor @@ -227,7 +191,7 @@ string const& ApplicationServer::getName () const { /// @brief sets up the logging //////////////////////////////////////////////////////////////////////////////// -void ApplicationServer::setupLogging (bool threaded, bool daemon) { +void ApplicationServer::setupLogging (bool threaded, bool daemon, bool backgrounded) { TRI_ShutdownLogging(false); TRI_InitialiseLogging(threaded); @@ -305,6 +269,19 @@ void ApplicationServer::setupLogging (bool threaded, bool daemon) { } } + // additional log file in case of tty + if (! backgrounded && isatty(STDOUT_FILENO) != 0 && ! _logTty.empty()) { + bool regularOut = (_logFile == "+" || _logFile == "-"); + bool ttyOut = (_logTty == "+" || _logTty == "-"); + + if (! regularOut || ! ttyOut) { + TRI_CreateLogAppenderFile(_logTty.c_str(), + contentFilter, + TRI_LOG_SEVERITY_UNKNOWN, + false); + } + } + #ifdef TRI_ENABLE_SYSLOG if (! _logFacility.empty()) { TRI_CreateLogAppenderSyslog(_logApplicationName.c_str(), @@ -446,7 +423,7 @@ bool ApplicationServer::parse (int argc, // setup logging // ............................................................................. - setupLogging(false, false); + setupLogging(false, false, false); // ............................................................................. // parse phase 2 @@ -811,7 +788,7 @@ void ApplicationServer::setupOptions (map& op options[OPTIONS_LOGGER] ("log.file", &_logFile, "log to file") ("log.requests-file", &_logRequestsFile, "log requests to file") - ("log.level,l", &_logLevel, "log level for severity 'human'") + ("log.level,l", &_logLevel, "log level") ; options[OPTIONS_LOGGER + ":help-log"] @@ -824,6 +801,7 @@ void ApplicationServer::setupOptions (map& op ("log.severity", &_logSeverity, "log severities") ("log.thread", "log the thread identifier for severity 'human'") ("log.use-local-time", "use local dates and times in log messages") + ("log.tty", &_logTty, "additional log file if started on tty") ; options[OPTIONS_HIDDEN] diff --git a/lib/ApplicationServer/ApplicationServer.h b/lib/ApplicationServer/ApplicationServer.h index ef5a6f3b5f..7ef1a840dd 100644 --- a/lib/ApplicationServer/ApplicationServer.h +++ b/lib/ApplicationServer/ApplicationServer.h @@ -147,7 +147,7 @@ namespace triagens { /// @brief sets up the logging //////////////////////////////////////////////////////////////////////////////// - void setupLogging (bool thread, bool daemon); + void setupLogging (bool thread, bool daemon, bool backgrounded); //////////////////////////////////////////////////////////////////////////////// /// @brief returns the command line options @@ -660,22 +660,17 @@ namespace triagens { //////////////////////////////////////////////////////////////////////////////// /// @brief log file /// @startDocuBlock logFile -/// `--log.file filename` -/// -/// This option allows the user to specify the name of a file to which -/// information is logged. By default, if no log file is specified, the standard -/// output is used. Note that if the file named by *filename* does not -/// exist, it will be created. If the file cannot be created (e.g. due to -/// missing file privileges), the server will refuse to start. If the specified -/// file already exists, output is appended to that file. -/// -/// Use *+* to log to standard error. Use *-* to log to standard output. -/// Use *""* to disable logging to file. /// @endDocuBlock //////////////////////////////////////////////////////////////////////////////// std::string _logFile; +//////////////////////////////////////////////////////////////////////////////// +/// @brief log file +//////////////////////////////////////////////////////////////////////////////// + + std::string _logTty; + //////////////////////////////////////////////////////////////////////////////// /// @brief log file for requests /// @startDocuBlock logRequests diff --git a/lib/Rest/AnyServer.cpp b/lib/Rest/AnyServer.cpp index 5a36d67893..c70a361fef 100644 --- a/lib/Rest/AnyServer.cpp +++ b/lib/Rest/AnyServer.cpp @@ -274,7 +274,7 @@ int AnyServer::start () { return startupDaemon(); } else { - _applicationServer->setupLogging(true, false); + _applicationServer->setupLogging(true, false, false); if (! _pidFile.empty()) { CheckPidFile(_pidFile); @@ -331,7 +331,7 @@ int AnyServer::startupSupervisor () { CheckPidFile(_pidFile); - _applicationServer->setupLogging(false, true); + _applicationServer->setupLogging(false, true, true); string current; int result = forkProcess(_workingDirectory, current); @@ -359,7 +359,7 @@ int AnyServer::startupSupervisor () { // parent if (0 < pid) { - _applicationServer->setupLogging(false, true); + _applicationServer->setupLogging(false, true, true); TRI_SetProcessTitle("arangodb [supervisor]"); int status; @@ -430,7 +430,7 @@ int AnyServer::startupSupervisor () { // child else { - _applicationServer->setupLogging(true, false); + _applicationServer->setupLogging(true, false, true); // write the pid file WritePidFile(_pidFile, TRI_CurrentProcessId()); @@ -467,7 +467,7 @@ int AnyServer::startupDaemon () { CheckPidFile(_pidFile); - _applicationServer->setupLogging(false, true); + _applicationServer->setupLogging(false, true, true); string current; int result = forkProcess(_workingDirectory, current); @@ -483,7 +483,7 @@ int AnyServer::startupDaemon () { // child process else { - _applicationServer->setupLogging(true, false); + _applicationServer->setupLogging(true, false, true); // and startup server prepareServer();