I'm currently trying to define log macro for a program which runs as a deamon.
As it's a deamon, I redirect both stdout and stderr to a specific file. This step is okay and I have two macro for logging which write in the same file.
define LOG(fmt, args...) fprintf(stdout, fmt, ## args); \
fflush(stdout)
#define LOG_ERR(fmt, args...) fprintf(stderr, fmt, ## args);
My point is I would like to have a "perror-like" trace :
<Message with arguments> : <errno associated message>
So I searched a little and if I modified LOG_ERR to :
#define LOG_ERR(fmt, args...) fprintf(stderr, fmt, ## args); \
perror("")
It works but the errno associated message is sent after a \n caracter (I think because stderr is buffered) So I obtain :
<Message with arguments>
<errno associated message>
Another solution could be to use strerror function as last parameter but I really don't know if it's possible to do so. It would look like :
#define LOG_ERR(fmt, args...) fprintf(stderr, fmt, ## args, strerror(errno));
But this solution also fail as errno signification is not inserted and even the end of line caracter is not written.
Does someone know how I could achieve it without using a function?