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.

(cherry picked from commit 2b5b65b)
  • Loading branch information
qris committed Dec 12, 2017
1 parent 4623e1d commit c5c1864
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions infrastructure/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ file(TO_NATIVE_PATH "${PERL_EXECUTABLE}" perl_executable_native)
string(REPLACE "\\" "\\\\" perl_path_escaped ${perl_executable_native})
target_compile_definitions(test_backupstorefix PRIVATE -DPERL_EXECUTABLE="${perl_path_escaped}")

target_compile_features(lib_common PRIVATE cxx_auto_type)
target_compile_features(lib_httpserver PRIVATE cxx_auto_type)
target_compile_features(lib_backupstore PRIVATE cxx_auto_type)

Expand Down
24 changes: 21 additions & 3 deletions lib/common/Logging.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}
virtual ~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 c5c1864

Please sign in to comment.