Commit 3ca90e7a authored by Eric Cano's avatar Eric Cano
Browse files

Added -l option to taped so logs go to a file.

parent b2005bbb
......@@ -17,6 +17,7 @@
*/
#include "common/Configuration.hpp"
#include "common/log/FileLogger.hpp"
#include "common/log/StdoutLogger.hpp"
#include "common/log/SyslogLogger.hpp"
#include "common/processCap/ProcessCap.hpp"
......@@ -56,10 +57,11 @@ std::string gHelpString =
"\n"
"where options can be:\n"
"\n"
"\t--foreground or -f \tRemain in the Foreground\n"
"\t--stdout or -s \tPrint logs to standard output. Required --foreground\n"
"\t--config <config-file> or -c \tConfiguration file\n"
"\t--help or -h \tPrint this help and exit\n"
"\t--foreground or -f \tRemain in the Foreground\n"
"\t--stdout or -s \tPrint logs to standard output. Required --foreground\n"
"\t--log-to-file <log-file> or -l \tLogs to a given file (instead of default syslog)\n"
"\t--config <config-file> or -c \tConfiguration file\n"
"\t--help or -h \tPrint this help and exit\n"
"\n"
"Comments to: Castor.Support@cern.ch\n";
......@@ -214,6 +216,8 @@ int main(const int argc, char **const argv) {
try {
if (commandLine->logToStdout) {
logPtr.reset(new log::StdoutLogger("cta-taped"));
} else if (commandLine->logToFile) {
logPtr.reset(new log::FileLogger("cta-taped", commandLine->logFilePath, log::DEBUG));
} else {
logPtr.reset(new log::SyslogLogger("cta-taped", log::DEBUG));
}
......
......@@ -24,10 +24,10 @@
namespace cta { namespace daemon {
CommandLineParams::CommandLineParams(int argc, char** argv):
foreground(false), logToStdout(false),
foreground(false), logToStdout(false), logToFile(false),
configFileLocation("/etc/cta/cta.conf"),
helpRequested(false){
struct ::option longopts[5];
struct ::option longopts[6];
longopts[0].name = "foreground";
longopts[0].has_arg = no_argument;
......@@ -49,6 +49,11 @@ CommandLineParams::CommandLineParams(int argc, char** argv):
longopts[3].flag = NULL;
longopts[3].val = 's';
longopts[4].name = "log-to-file";
longopts[3].has_arg = required_argument;
longopts[3].flag = NULL;
longopts[3].val = 'l';
memset(&longopts[4], 0, sizeof(struct ::option));
char c;
......@@ -57,7 +62,7 @@ CommandLineParams::CommandLineParams(int argc, char** argv):
// Prevent getopt from printing out errors on stdout
opterr=0;
// We ask getopt to not reshuffle argv ('+')
while ((c = getopt_long(argc, argv, "+fsc:h", longopts, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "+fsc:l:h", longopts, NULL)) != -1) {
switch (c) {
case 'f':
foreground = true;
......@@ -71,6 +76,10 @@ CommandLineParams::CommandLineParams(int argc, char** argv):
case 'h':
helpRequested = true;
break;
case 'l':
logFilePath = optarg;
logToFile = true;
break;
default:
break;
}
......@@ -78,14 +87,19 @@ CommandLineParams::CommandLineParams(int argc, char** argv):
if (logToStdout && !foreground) {
throw cta::exception::Exception("In CommandLineParams::CommandLineParams(): cannot log to stdout without running in the foreground");
}
if (logToFile && logToStdout) {
throw cta::exception::Exception("In CommandLineParams::CommandLineParams(): cannot log to both stdout and file");
}
}
std::list<cta::log::Param> CommandLineParams::toLogParams() const {
std::list<cta::log::Param> ret;
ret.push_back(cta::log::Param("foreground", foreground));
ret.push_back(cta::log::Param("logToStdout", logToStdout));
ret.push_back(cta::log::Param("configFileLocation", configFileLocation));
ret.push_back(cta::log::Param("helpRequested", helpRequested));
ret.push_back({"foreground", foreground});
ret.push_back({"logToStdout", logToStdout});
ret.push_back({"logToFile", logToFile});
ret.push_back({"logFilePath", logFilePath});
ret.push_back({"configFileLocation", configFileLocation});
ret.push_back({"helpRequested", helpRequested});
return ret;
}
......
......@@ -33,6 +33,8 @@ struct CommandLineParams{
CommandLineParams(int argc, char **argv);
bool foreground; ///< Prevents daemonisation
bool logToStdout; ///< Log to stdout instead of syslog. Foreground is required.
bool logToFile; ///< Log to file intead of syslog.
std::string logFilePath;
std::string configFileLocation; ///< Location of the configuration file. Defaults to /etc/cta/cta.conf
bool helpRequested; ///< Help requested: will print out help and exit.
std::list<cta::log::Param> toLogParams() const; ///< Convert the command line into set of parameters for logging.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment