Re: Maximum possible lifetime
Am 21.07.2013 08:49, schrieb fmatthew5876:
I once wrote a logging system for a project that used the Schwarz
counter technique (used by iostream) to stay alive as long as possible.
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Nifty_Counter
This worked very well to for ensuring the lifetime of the logging
system with regards to anyone who used it.
However, there was still a hole if someone did an assert()/abort(), the
logging system would not clean up correctly and thus log messages might
not get flushed to the console to debug the crash.
Is there any way to hook these methods as well? assert() calls abort() which
does a SIGABRT, but messing with signal handlers behind the scenes seems
somewhat risky for a library.
There is no standardized way to replace what abort() or assert() does.
If you have to handle such very low-level situations, you need to use
extreme low-level tools or non-standard means for this. You have to
accept this risk, I think: Chances are good that the state of the
program is possibly only partially defined, when abort() is called.
First, because of the reason for calling abort (presumably invariants
are broken somewhere) and second, because the state during the abort()
call provides only little guarantees you can rely on anyway.
How about using macros?
Do you mean your own macros such as fmatthew5876_assert, fmatthew5876_abort?
Redefine these 3 functions to macros which first
call some log flushing function and then do the requested task.
Redefining standard library names is undefined behaviour, see 17.6.4.3
p2 (see also 17.6.4.3.1), I don't recommend to do that. Beside the UB
aspect, this approach won't help you for all usages of the standard
macros that don't see you revised definition (This could be the binary
parts of your standard library or of any third-party library).
Furthermore, in C++11/C99 we also have _Exit. How will you handle this?
(No atexit handler, no at_quick_exit handler, and no destructors will be
invoked)
This still doesn't help for other crashes like segfaults. Is there anyway
to absolutely ensure a system shuts down properly in all possible cases?
I doubt that you can ensure that, simply based on the fact that C++
programs can invoke undefined behaviour and the effects of this is not
predictable in general.
How does stdio/iostream/ handle it? Magic? Operating system hooks? Or do
they often lose messages on crashes on a lot of platforms?
I guess that all of this can be true. Surely, the lowest levels of most
(if not all) standard library implementations cannot be distinguished
very much from magic ;-)
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]