-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to assert()? #28
Comments
Ideally, cloudlibc would have no observable global mutable state, and thus no global stdio streams (stdin, stdout, stderr). As you pointed out, this would be annoying for Though I think people should write their software in a dependency injected way, I think we can also agree that requiring software to have logging sinks injected (both for events and failure diagnostics) would be far too pedantic. This is why cloudlibc does provide a
By default, ISO C provides the void fswap(FILE *, FILE *); This function can be used to swap the state of two #include <argdata.h>
#include <assert.h>
#include <program.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
void program_main(const argdata_t *ad) {
// Reconfigure stderr.
int fd;
if (argdata_get_fd(ad, &fd) == 0) {
FILE *f = fdopen(fd, "w");
if (f != NULL) {
fswap(f, stderr);
fclose(f);
}
}
// Write various things over stderr.
fprintf(stderr, "I am now going to crash.\n");
syslog(LOG_INFO, "Also logging that I'm going to crash.");
assert(0 && "Crashing!");
fprintf(stderr, "Assertions disabled?\n");
exit(1);
} And here is what it looks like when invoked:
Hope that helps! |
@EdSchouten Thank you for the tip! Say, could we flesh out some similar |
I'm not convinced we should add those. Looking at the bulk of library C/C++ code out there, you see that Within user-interactive applications (e.g., UNIX shell tools) you do see |
Well in that case, let's at least offer an |
Could cloudlibc offer a
dassert()
macro that accepts a file descriptor for emitting assertion errors? Is this even possible, given thatassert()
tends to hook deep into C++throw
?Alternatively, NuxiNL/cloudabi-utils#11 would be fantastic!
I suspect this may be possible with
dup2()
, though I'm not sure how portable that function is.The text was updated successfully, but these errors were encountered: