summaryrefslogtreecommitdiff
path: root/lib/common
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2015-05-04 18:56:43 +0000
committerChris Wilson <chris+github@qwirx.com>2015-05-04 18:56:43 +0000
commite266b546a960ce6c7400ec546f7edce8f8fdd8a1 (patch)
tree35fb3a8771da2c38d0f56bc9b7ec7436c99ba370 /lib/common
parentffbdc492d317ea305c0462d0a0afbacc8c4a0a6c (diff)
Add command-line option to limit log messages to certain file(s).
E.g. you can run with -L NamedLock.cpp to only show messages logged in that file. You can also repeat it to only show messages from certain files.
Diffstat (limited to 'lib/common')
-rw-r--r--lib/common/Logging.cpp52
-rw-r--r--lib/common/Logging.h43
2 files changed, 87 insertions, 8 deletions
diff --git a/lib/common/Logging.cpp b/lib/common/Logging.cpp
index fd815132..93fd477b 100644
--- a/lib/common/Logging.cpp
+++ b/lib/common/Logging.cpp
@@ -45,6 +45,7 @@ Syslog* Logging::spSyslog = NULL;
Logging Logging::sGlobalLogging; // automatic initialisation
std::string Logging::sProgramName;
const Log::Category Logging::UNCATEGORISED("Uncategorised");
+std::auto_ptr<HideFileGuard> Logging::sapHideFileGuard;
HideSpecificExceptionGuard::SuppressedExceptions_t
HideSpecificExceptionGuard::sSuppressedExceptions;
@@ -566,7 +567,7 @@ bool HideSpecificExceptionGuard::IsHidden(int type, int subtype)
// --------------------------------------------------------------------------
std::string Logging::OptionParser::GetOptionString()
{
- return "NPqQt:TUvVW:";
+ return "L:NPqQt:TUvVW:";
}
// --------------------------------------------------------------------------
@@ -584,6 +585,20 @@ int Logging::OptionParser::ProcessOption(signed int option)
{
switch(option)
{
+ case 'L':
+ {
+ if(sapHideFileGuard.get())
+ {
+ sapHideFileGuard->Add(optarg);
+ }
+ else
+ {
+ sapHideFileGuard.reset(new HideFileGuard(
+ optarg, true)); // HideAllButSelected
+ }
+ }
+ break;
+
case 'N':
{
mTruncateLogFile = true;
@@ -698,7 +713,8 @@ int Logging::OptionParser::ProcessOption(signed int option)
// --------------------------------------------------------------------------
std::string Logging::OptionParser::GetUsageString()
{
- return
+ return
+ " -L <file> Filter out log messages except from specified file, can repeat\n"
" -N Truncate log file at startup and on backup start\n"
" -P Show process ID (PID) in console output\n"
" -q Run more quietly, reduce verbosity level by one, can repeat\n"
@@ -715,11 +731,35 @@ bool HideCategoryGuard::Log(Log::Level level, const std::string& file, int line,
const std::string& function, const Log::Category& category,
const std::string& message)
{
- // Return false if category is in our list, to suppress further
- // logging (thus, return true if it's not in our list, i.e. we
- // found nothing).
std::list<Log::Category>::iterator i = std::find(mCategories.begin(),
mCategories.end(), category);
- return i == mCategories.end();
+ // Return false if category is in our list, to suppress further
+ // logging (thus, return true if it's not in our list, i.e. we
+ // found nothing, to allow it).
+ return (i == mCategories.end());
+}
+
+bool HideFileGuard::Log(Log::Level level, const std::string& file, int line,
+ const std::string& function, const Log::Category& category,
+ const std::string& message)
+{
+ std::list<std::string>::iterator i = std::find(mFileNames.begin(),
+ mFileNames.end(), file);
+ bool allow_log_message;
+ if(mHideAllButSelected)
+ {
+ // Return true if filename is in our list, to allow further
+ // logging (thus, return false if it's not in our list, i.e. we
+ // found nothing, to suppress it).
+ allow_log_message = (i != mFileNames.end());
+ }
+ else
+ {
+ // Return false if filename is in our list, to suppress further
+ // logging (thus, return true if it's not in our list, i.e. we
+ // found nothing, to allow it).
+ allow_log_message = (i == mFileNames.end());
+ }
+ return allow_log_message;
}
diff --git a/lib/common/Logging.h b/lib/common/Logging.h
index 856c0499..f61bf1dc 100644
--- a/lib/common/Logging.h
+++ b/lib/common/Logging.h
@@ -385,6 +385,9 @@ class Capture : public Logger
}
};
+// Forward declaration
+class HideFileGuard;
+
// --------------------------------------------------------------------------
//
// Class
@@ -406,7 +409,8 @@ class Logging
static Syslog* spSyslog;
static Logging sGlobalLogging;
static std::string sProgramName;
-
+ static std::auto_ptr<HideFileGuard> sapHideFileGuard;
+
public:
Logging ();
~Logging();
@@ -501,11 +505,16 @@ class Logging
}
};
+ // Process global options
+ static std::string GetOptionString();
+ static int ProcessOption(signed int option);
+ static std::string GetUsageString();
+
// --------------------------------------------------------------------------
//
// Class
// Name: Logging::OptionParser
- // Purpose: Process command-line options
+ // Purpose: Process command-line options, some global, some local
// Created: 2014/04/09
//
// --------------------------------------------------------------------------
@@ -629,6 +638,36 @@ class HideCategoryGuard : public Logger
virtual void SetProgramName(const std::string& rProgramName) { }
};
+class HideFileGuard : public Logger
+{
+ private:
+ std::list<std::string> mFileNames;
+ HideFileGuard(const HideFileGuard& other); // no copying
+ HideFileGuard& operator=(const HideFileGuard& other); // no assignment
+ bool mHideAllButSelected;
+
+ public:
+ HideFileGuard(const std::string& rFileName, bool HideAllButSelected = false)
+ : mHideAllButSelected(HideAllButSelected)
+ {
+ mFileNames.push_back(rFileName);
+ Logging::Add(this);
+ }
+ ~HideFileGuard()
+ {
+ Logging::Remove(this);
+ }
+ void Add(const std::string& rFileName)
+ {
+ mFileNames.push_back(rFileName);
+ }
+ virtual bool Log(Log::Level level, const std::string& file, int line,
+ const std::string& function, const Log::Category& category,
+ const std::string& message);
+ virtual const char* GetType() { return "HideFileGuard"; }
+ virtual void SetProgramName(const std::string& rProgramName) { }
+};
+
std::string PrintEscapedBinaryData(const std::string& rInput);
#endif // LOGGING__H