summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/common/Logging.cpp148
-rw-r--r--lib/common/Logging.h31
-rw-r--r--lib/server/Daemon.cpp114
-rw-r--r--lib/server/Daemon.h2
4 files changed, 187 insertions, 108 deletions
diff --git a/lib/common/Logging.cpp b/lib/common/Logging.cpp
index af132fa3..97209104 100644
--- a/lib/common/Logging.cpp
+++ b/lib/common/Logging.cpp
@@ -554,3 +554,151 @@ bool HideSpecificExceptionGuard::IsHidden(int type, int subtype)
return false;
}
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: Logging::OptionParser::GetOptionString()
+// Purpose: Returns the valid Getopt command-line options
+// that Logging::OptionParser::ProcessOption will handle.
+// Created: 2014/04/09
+//
+// --------------------------------------------------------------------------
+std::string Logging::OptionParser::GetOptionString()
+{
+ return "PqQt:TUvVW:";
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: Logging::OptionParser::ProcessOption(signed int option)
+// Purpose: Processes the supplied option (equivalent to the
+// return code from getopt()). Return zero if the
+// option was handled successfully, or nonzero to
+// abort the program with that return value.
+// Created: 2007/09/18
+//
+// --------------------------------------------------------------------------
+int Logging::OptionParser::ProcessOption(signed int option)
+{
+ switch(option)
+ {
+ case 'P':
+ {
+ Console::SetShowPID(true);
+ }
+ break;
+
+ case 'q':
+ {
+ if(mCurrentLevel == Log::NOTHING)
+ {
+ BOX_FATAL("Too many '-q': "
+ "Cannot reduce logging "
+ "level any more");
+ return 2;
+ }
+ mCurrentLevel--;
+ }
+ break;
+
+ case 'Q':
+ {
+ mCurrentLevel = Log::NOTHING;
+ }
+ break;
+
+ case 't':
+ {
+ Logging::SetProgramName(optarg);
+ Console::SetShowTag(true);
+ }
+ break;
+
+ case 'T':
+ {
+ Console::SetShowTime(true);
+ }
+ break;
+
+ case 'U':
+ {
+ Console::SetShowTime(true);
+ Console::SetShowTimeMicros(true);
+ }
+ break;
+
+ case 'v':
+ {
+ if(mCurrentLevel == Log::EVERYTHING)
+ {
+ BOX_FATAL("Too many '-v': "
+ "Cannot increase logging "
+ "level any more");
+ return 2;
+ }
+ mCurrentLevel++;
+ }
+ break;
+
+ case 'V':
+ {
+ mCurrentLevel = Log::EVERYTHING;
+ }
+ break;
+
+ case 'W':
+ {
+ mCurrentLevel = Logging::GetNamedLevel(optarg);
+ if (mCurrentLevel == Log::INVALID)
+ {
+ BOX_FATAL("Invalid logging level: " << optarg);
+ return 2;
+ }
+ }
+ break;
+
+ case '?':
+ {
+ BOX_FATAL("Unknown option on command line: "
+ << "'" << (char)optopt << "'");
+ return 2;
+ }
+ break;
+
+ default:
+ {
+ BOX_FATAL("Unknown error in getopt: returned "
+ << "'" << option << "'");
+ return 1;
+ }
+ }
+
+ // If we didn't explicitly return an error code above, then the option
+ // was fine, so return 0 to continue processing.
+ return 0;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: Logging::OptionParser::GetUsageString()
+// Purpose: Returns a string suitable for displaying as part
+// of a program's command-line usage help message,
+// describing the logging options.
+// Created: 2014/04/09
+//
+// --------------------------------------------------------------------------
+std::string Logging::OptionParser::GetUsageString()
+{
+ return
+ " -P Show process ID (PID) in console output\n"
+ " -q Run more quietly, reduce verbosity level by one, can repeat\n"
+ " -Q Run at minimum verbosity, log nothing to console and system\n"
+ " -t <tag> Tag console output with specified marker\n"
+ " -T Timestamp console output\n"
+ " -U Timestamp console output with microseconds\n"
+ " -v Run more verbosely, increase verbosity level by one, can repeat\n"
+ " -V Run at maximum verbosity, log everything to console and system\n"
+ " -W <level> Set verbosity to error/warning/notice/info/trace/everything\n";
+}
diff --git a/lib/common/Logging.h b/lib/common/Logging.h
index d74ded61..7cf9d46c 100644
--- a/lib/common/Logging.h
+++ b/lib/common/Logging.h
@@ -446,6 +446,37 @@ class Logging
Logging::Remove(mpLogger);
}
};
+
+ // --------------------------------------------------------------------------
+ //
+ // Class
+ // Name: Logging::OptionParser
+ // Purpose: Process command-line options
+ // Created: 2014/04/09
+ //
+ // --------------------------------------------------------------------------
+ class OptionParser
+ {
+ public:
+ OptionParser(Log::Level InitialLevel =
+ #ifdef BOX_RELEASE_BUILD
+ Log::NOTICE
+ #else
+ Log::INFO
+ #endif
+ )
+ : mCurrentLevel(InitialLevel)
+ { }
+
+ static std::string GetOptionString();
+ int ProcessOption(signed int option);
+ static std::string GetUsageString();
+ int mCurrentLevel; // need an int to do math with
+ Log::Level GetCurrentLevel()
+ {
+ return (Log::Level) mCurrentLevel;
+ }
+ };
};
class FileLogger : public Logger
diff --git a/lib/server/Daemon.cpp b/lib/server/Daemon.cpp
index 1562b6e8..7f2e902c 100644
--- a/lib/server/Daemon.cpp
+++ b/lib/server/Daemon.cpp
@@ -106,11 +106,11 @@ Daemon::~Daemon()
// --------------------------------------------------------------------------
std::string Daemon::GetOptionString()
{
- return "c:"
+ return std::string("c:"
#ifndef WIN32
"DF"
#endif
- "hkKo:O:PqQt:TUvVW:";
+ "hkKo:O:") + Logging::OptionParser::GetOptionString();
}
void Daemon::Usage()
@@ -132,17 +132,8 @@ void Daemon::Usage()
" -k Keep console open after fork, keep writing log messages to it\n"
" -K Stop writing log messages to console while daemon is running\n"
" -o <file> Log to a file, defaults to maximum verbosity\n"
- " -O <level> Set file log verbosity to error/warning/notice/info/trace/everything\n"
- " -P Show process ID (PID) in console output\n"
- " -q Run more quietly, reduce verbosity level by one, can repeat\n"
- " -Q Run at minimum verbosity, log nothing to console and system\n"
- " -t <tag> Tag console output with specified marker\n"
- " -T Timestamp console output\n"
- " -U Timestamp console output with microseconds\n"
- " -v Run more verbosely, increase verbosity level by one, can repeat\n"
- " -V Run at maximum verbosity, log everything to console and system\n"
- " -W <level> Set verbosity to error/warning/notice/info/trace/everything\n"
- ;
+ " -O <level> Set file log verbosity to error/warning/notice/info/trace/everything\n" <<
+ Logging::OptionParser::GetUsageString();
}
// --------------------------------------------------------------------------
@@ -218,94 +209,9 @@ int Daemon::ProcessOption(signed int option)
}
break;
- case 'P':
- {
- Console::SetShowPID(true);
- }
- break;
-
- case 'q':
- {
- if(mLogLevel == Log::NOTHING)
- {
- BOX_FATAL("Too many '-q': "
- "Cannot reduce logging "
- "level any more");
- return 2;
- }
- mLogLevel--;
- }
- break;
-
- case 'Q':
- {
- mLogLevel = Log::NOTHING;
- }
- break;
-
- case 't':
- {
- Logging::SetProgramName(optarg);
- Console::SetShowTag(true);
- }
- break;
-
- case 'T':
- {
- Console::SetShowTime(true);
- }
- break;
-
- case 'U':
- {
- Console::SetShowTime(true);
- Console::SetShowTimeMicros(true);
- }
- break;
-
- case 'v':
- {
- if(mLogLevel == Log::EVERYTHING)
- {
- BOX_FATAL("Too many '-v': "
- "Cannot increase logging "
- "level any more");
- return 2;
- }
- mLogLevel++;
- }
- break;
-
- case 'V':
- {
- mLogLevel = Log::EVERYTHING;
- }
- break;
-
- case 'W':
- {
- mLogLevel = Logging::GetNamedLevel(optarg);
- if (mLogLevel == Log::INVALID)
- {
- BOX_FATAL("Invalid logging level: " << optarg);
- return 2;
- }
- }
- break;
-
- case '?':
- {
- BOX_FATAL("Unknown option on command line: "
- << "'" << (char)optopt << "'");
- return 2;
- }
- break;
-
default:
{
- BOX_FATAL("Unknown error in getopt: returned "
- << "'" << option << "'");
- return 1;
+ return mLogLevel.ProcessOption(option);
}
}
@@ -351,12 +257,6 @@ int Daemon::Main(const std::string& rDefaultConfigFile, int argc,
int Daemon::ProcessOptions(int argc, const char *argv[])
{
- #ifdef BOX_RELEASE_BUILD
- mLogLevel = Log::NOTICE;
- #else
- mLogLevel = Log::INFO;
- #endif
-
if (argc == 2 && strcmp(argv[1], "/?") == 0)
{
Usage();
@@ -406,8 +306,8 @@ int Daemon::ProcessOptions(int argc, const char *argv[])
return 2;
}
- Logging::FilterConsole((Log::Level)mLogLevel);
- Logging::FilterSyslog ((Log::Level)mLogLevel);
+ Logging::FilterConsole(mLogLevel.GetCurrentLevel());
+ Logging::FilterSyslog (mLogLevel.GetCurrentLevel());
if (mLogFileLevel != Log::INVALID)
{
diff --git a/lib/server/Daemon.h b/lib/server/Daemon.h
index 2718c288..923053c6 100644
--- a/lib/server/Daemon.h
+++ b/lib/server/Daemon.h
@@ -99,7 +99,7 @@ private:
bool mRunInForeground;
bool mKeepConsoleOpenAfterFork;
bool mHaveConfigFile;
- int mLogLevel; // need an int to do math with
+ Logging::OptionParser mLogLevel;
std::string mLogFile;
Log::Level mLogFileLevel;
std::auto_ptr<FileLogger> mapLogFileLogger;