Re: To go with Go or C/C++?
ram@zedat.fu-berlin.de (Stefan Ram) writes:
scott@slp53.sl.home (Scott Lurndal) writes:
I honestly don't see why the app can't have an array of char *'s
itself, indexed by the "single integer", that can be printed
when the error is finally handled.
if ((fd = open(argv[1], O_RDONLY)) == -1) {
fprintf(stderr, "%s: Unable to open '%s': %s\n", argv[0], argv[1], strerror(errno));
return EXIT_FAILURE;
}
Usually, one does not want to mix code for one system (here:
the filesystem) with code for another system (here: the
console, stderr).
What is ?fprintf(stderr,? supposed to do in a GUI program or
in a library, where you do not know whether it will be
called from a console program or a GUI programm or a daemon
or whatever?
This was a flipping example. I even said as much.
If this makes you feel better:
if ((fd = ::open(argv[1], O_RDONLY)) == -1) {
d_logger->log("%s: Unable to open '%s': %s\n", argv[0], argv[1], ::strerror(errno));
return;
}
Where Logger::log is a pure virtual method in an abstract Logger class.
Which can write to an application log file (FileLogger), the system log file
(SystemLogger) or an error dialog for GUI applications (which are no where
near the majority of C++ applications), and the generation code doesn't care which.
There's also a MuxLogger class which fans the output to multiple loggers.
This mechanism has been used in C++ applications ranging from operating systems
and hypervisors (bare-metal code, no libraries at all including std::) to
whole-system simulators to code that generates pretty much every SSL certificate
on most e-commerce sites.
scott
class c_logger {
bool l_debug;
public:
c_logger(bool d) { l_debug = d; }
virtual ~c_logger(void) {};
void log(const char *, ...)
__attribute__((format(printf, 2, 3)));
size_t trace(const char *, ...)
__attribute__((format(printf, 2, 3)));
virtual void do_log(const char *, va_list) = 0;
virtual size_t do_trace(const char *, va_list) = 0;
void set_tracing(bool d=false) { l_debug = d; }
bool is_tracing(void) { return l_debug; }
};