Skip to content

Commit

Permalink
Allow using LogLevelOverrideByFileGuard as a guard
Browse files Browse the repository at this point in the history
After calling its Install() method, the guard will be installed in the central
logging configuration, and will automatically be uninstalled when the object
goes out of scope and is destroyed.
  • Loading branch information
qris committed Nov 17, 2017
1 parent beefd5c commit fac7c23
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
24 changes: 21 additions & 3 deletions lib/common/Logging.cpp
Expand Up @@ -775,10 +775,10 @@ std::string Logging::OptionParser::GetUsageString()

std::ostringstream buf;
buf <<
" -L <file>[/<category>]=<level> Override log level for specified file\n"
" (for example, -L '" << current_file << "=trace')\n"
" -L [<file>][/<category>]=<level> Override log level for specified file or\n"
" category (for example, -L '" << current_file << "/Configuration=trace').\n"
" <category> can be one of: {Uncategorised, Backtrace,\n"
" Configuration, RaidFileRead}\n"
" Configuration, RaidFileRead, FileSystem/Locking}\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"
Expand All @@ -804,6 +804,24 @@ bool HideCategoryGuard::Log(Log::Level level, const std::string& file, int line,
return (i == mCategories.end());
}

LogLevelOverrideByFileGuard::~LogLevelOverrideByFileGuard()
{
if(mInstalled)
{
auto this_pos = std::find(Logging::sLogLevelOverrideByFileGuards.begin(),
Logging::sLogLevelOverrideByFileGuards.end(), *this);
ASSERT(this_pos != Logging::sLogLevelOverrideByFileGuards.end());
Logging::sLogLevelOverrideByFileGuards.erase(this_pos);
}
}

void LogLevelOverrideByFileGuard::Install()
{
ASSERT(!mInstalled);
Logging::sLogLevelOverrideByFileGuards.push_back(*this);
mInstalled = true;
}

bool LogLevelOverrideByFileGuard::IsOverridden(Log::Level level, const std::string& file, int line,
const std::string& function, const Log::Category& category,
const std::string& message)
Expand Down
17 changes: 13 additions & 4 deletions lib/common/Logging.h
Expand Up @@ -423,31 +423,39 @@ class LogLevelOverrideByFileGuard
std::string mCategoryNamePrefix;
Log::Level mNewLevel;
bool mOverrideAllButSelected;
bool mInstalled;

public:
LogLevelOverrideByFileGuard(const std::string& rFileName,
const std::string& rCategoryNamePrefix, Log::Level NewLevel,
bool OverrideAllButSelected = false)
: mCategoryNamePrefix(rCategoryNamePrefix),
mNewLevel(NewLevel),
mOverrideAllButSelected(OverrideAllButSelected)
mOverrideAllButSelected(OverrideAllButSelected),
mInstalled(false)
{
if(rFileName.size() > 0)
{
mFileNames.push_back(rFileName);
}
}
~LogLevelOverrideByFileGuard()
{
}
virtual ~LogLevelOverrideByFileGuard();
void Add(const std::string& rFileName)
{
mFileNames.push_back(rFileName);
}
void Install();
bool IsOverridden(Log::Level level, const std::string& file, int line,
const std::string& function, const Log::Category& category,
const std::string& message);
Log::Level GetNewLevel() { return mNewLevel; }
bool operator==(const LogLevelOverrideByFileGuard& rOther)
{
return (mFileNames == rOther.mFileNames) &&
(mCategoryNamePrefix == rOther.mCategoryNamePrefix) &&
(mNewLevel == rOther.mNewLevel) &&
(mOverrideAllButSelected == rOther.mOverrideAllButSelected);
}
};


Expand All @@ -473,6 +481,7 @@ class Logging
static Logging sGlobalLogging;
static std::string sProgramName;
static std::vector<LogLevelOverrideByFileGuard> sLogLevelOverrideByFileGuards;
friend class LogLevelOverrideByFileGuard;

public:
Logging ();
Expand Down

0 comments on commit fac7c23

Please sign in to comment.