Skip to content
Snippets Groups Projects
Commit 147c66d6 authored by Nat Goodspeed's avatar Nat Goodspeed
Browse files

SL-10297: Simplify implementation of LLSingletonBase::logwarns() etc.

Introduce 'string_params' typedef for std::initialization_list<std::string>,
and make logwarns(), loginfos(), logdebugs() and logerrs() accept const
string_params&.

Eliminate the central log() function in llsingleton.cpp that used LL_VLOGS().
To cache the result of a (moderately expensive) Log::shouldLog() call,
LL_VLOGS() wants its CallSite object to be static -- but of course the
shouldLog() result will differ for different ELevel values, so LL_VLOGS()
instantiates a static array of CallSite instances. It seems silly to funnel
distinct logwarns(), etc., functions through a common log() function only to
have LL_VLOGS() tease apart ELevel values again. Instead, make logwarns()
directly invoke LL_WARNS(), and similarly for the rest.

To reduce boilerplate in these distinct functions, teach std::ostream how to
stream a string_params instance by looping over its elements. Then each
logwarns(), etc., function can simply stream its string_params argument to
LL_WARNS() or whichever.

In particular, eliminate the LLERROR_CRASH macro in logerrs(). The fact that
it invokes LL_ERRS() ensures that its LL_ENDL macro will crash the viewer.
parent c9fc4349
No related branches found
No related tags found
No related merge requests found
...@@ -38,10 +38,6 @@ ...@@ -38,10 +38,6 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
namespace {
void log(LLError::ELevel level, std::initializer_list<std::string_view>);
} // anonymous namespace
// Our master list of all LLSingletons is itself an LLSingleton. We used to // Our master list of all LLSingletons is itself an LLSingleton. We used to
// store it in a function-local static, but that could get destroyed before // store it in a function-local static, but that could get destroyed before
// the last of the LLSingletons -- and ~LLSingletonBase() definitely wants to // the last of the LLSingletons -- and ~LLSingletonBase() definitely wants to
...@@ -450,43 +446,40 @@ void LLSingletonBase::deleteAll() ...@@ -450,43 +446,40 @@ void LLSingletonBase::deleteAll()
/*---------------------------- Logging helpers -----------------------------*/ /*---------------------------- Logging helpers -----------------------------*/
namespace { namespace {
void log(LLError::ELevel level, std::initializer_list<std::string_view> args) std::ostream& operator<<(std::ostream& out, const LLSingletonBase::string_params& args)
{ {
LL_VLOGS(level, "LLSingleton"); // However many args there are in args, stream each of them to 'out'.
for (auto arg : args) for (auto arg : args)
{ {
LL_CONT << arg; out << arg;
} }
LL_ENDL; return out;
} }
} // anonymous namespace } // anonymous namespace
//static //static
void LLSingletonBase::logwarns(std::initializer_list<std::string_view> args) void LLSingletonBase::logwarns(const string_params& args)
{ {
log(LLError::LEVEL_WARN, args); LL_WARNS("LLSingleton") << args << LL_ENDL;
} }
//static //static
void LLSingletonBase::loginfos(std::initializer_list<std::string_view> args) void LLSingletonBase::loginfos(const string_params& args)
{ {
log(LLError::LEVEL_INFO, args); LL_INFOS("LLSingleton") << args << LL_ENDL;
} }
//static //static
void LLSingletonBase::logdebugs(std::initializer_list<std::string_view> args) void LLSingletonBase::logdebugs(const string_params& args)
{ {
log(LLError::LEVEL_DEBUG, args); LL_DEBUGS("LLSingleton") << args << LL_ENDL;
} }
//static //static
void LLSingletonBase::logerrs(std::initializer_list<std::string_view> args) void LLSingletonBase::logerrs(const string_params& args)
{ {
log(LLError::LEVEL_ERROR, args); LL_ERRS("LLSingleton") << args << LL_ENDL;
// The other important side effect of LL_ERRS() is
// https://www.youtube.com/watch?v=OMG7paGJqhQ (emphasis on OMG)
LLERROR_CRASH;
} }
std::string LLSingletonBase::demangle(const char* mangled) std::string LLSingletonBase::demangle(const char* mangled)
......
...@@ -112,10 +112,13 @@ class LLSingletonBase: private boost::noncopyable ...@@ -112,10 +112,13 @@ class LLSingletonBase: private boost::noncopyable
void capture_dependency(); void capture_dependency();
// delegate logging calls to llsingleton.cpp // delegate logging calls to llsingleton.cpp
static void logerrs (std::initializer_list<std::string_view>); public:
static void logwarns (std::initializer_list<std::string_view>); typedef std::initializer_list<std::string_view> string_params;
static void loginfos (std::initializer_list<std::string_view>); protected:
static void logdebugs(std::initializer_list<std::string_view>); static void logerrs (const string_params&);
static void logwarns (const string_params&);
static void loginfos (const string_params&);
static void logdebugs(const string_params&);
static std::string demangle(const char* mangled); static std::string demangle(const char* mangled);
// these classname() declarations restate template functions declared in // these classname() declarations restate template functions declared in
// llerror.h because we avoid #including that here // llerror.h because we avoid #including that here
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment