MXS-2491 Remove global destructor inter-dependency

log.cc:this_unit.slogger depends upon logger.cc:this_unit.ident.
Depending on which one is destructed first, there will be a crash
or then not.

It seems that the destruction order when returning from main()
is not necessarily the same as that when calling exit() in main,
as this bug is triggered with compilers versions and not with other.
This commit is contained in:
Johan Wikman
2019-05-15 13:43:53 +03:00
parent 8a67c702b8
commit 0f85baabe1

View File

@ -70,17 +70,20 @@ bool should_log_error()
struct this_unit
{
std::string ident;
static const int MAX_IDENT_LEN = 256;
// Don't change to std::string. Order of destruction issue with logger.cc:this_unit.
char ident[MAX_IDENT_LEN + 1];
} this_unit;
std::string get_ident()
{
if (this_unit.ident.empty())
if (!this_unit.ident[0])
{
#ifdef __GNUC__
this_unit.ident = program_invocation_short_name;
return program_invocation_short_name;
#else
this_unit.ident = "The Program";
return "The Program";
#endif
}
@ -98,7 +101,15 @@ namespace maxbase
// static
void Logger::set_ident(const std::string& ident)
{
this_unit.ident = ident;
int len = ident.length();
if (len > this_unit.MAX_IDENT_LEN)
{
len = this_unit.MAX_IDENT_LEN;
}
this_unit.ident[len] = 0;
memcpy(this_unit.ident, ident.c_str(), len);
}
std::unique_ptr<Logger> FileLogger::create(const std::string& filename)