util: Establish global logger object.
The object encapsulates logging configuration, and in a later commit,
set up routines will also be moved into the class.
Differential D1281
[Part 2] Refactor logging code into a global object jimpo on Apr 12 2018, 19:30. Authored by
Details
util: Establish global logger object. The object encapsulates logging configuration, and in a later commit, Run bitcoind with different permutations of:
Diff Detail
Event TimelineComment Actions The thread safety is tricky. No, the unique_ptr is not thread safe, but I'm hoping that it is in the way it is used. If the pointer is only assigned during initialization while there is only one thread and only released on program exit after all threads have been joined, it should only ever have one value when accessed concurrently. The reason it's a pointer and not just a global Logger value is because that way LogPrintf can check if the pointer is null and skip logging if so. On the flip side, there's no way to tell if an object has been constructed or destructed on program startup/exit. See the comment from the existing code about this: /**
I believe it unsafe for there to just be a global extern BCLog::Logger g_logger. That said, as long as the application joins all threads before exiting and destructing global objects, I think it ought to be safe to use a global unique_ptr. What do you think? I'd really rather not guard access to the logger with a mutex.
Comment Actions Using a unique_ptr is significantly more dangerous that the unique_ptr thing because you don't have the risk of a dangling pointer. Comment Actions Replaced std::unique_ptr to global logger with a thread-safe GetLogger method as discussed in chat. Comment Actions As discussed offline, this rely on the initialization order (using the logger before the pointer is initialized would cause a segfault). Going with a static and an accessor is best for now, using a boost::call_once to initialize what must be. Sadly, this is not ideal, but as long as there are globals using the logger in their constructor/destructor, there are not that many options. These globals need to be clean up first.
Comment Actions A few comments, but overall, it looks good.
Comment Actions @jasonbcox @schancel I completely agree on not using Hungarian notation for new code, but changing these variable names would increase the diff size. If you want, I would prefer to rename all of the variables in a future diff that just does the renaming.
Comment Actions I'm fine taking care of variable renaming later. I'm with @deadalnix on this, we shouldn't be burning too much time on inconsequential issues like naming. |