In a program i want all the printfs to be written to syslog. I replace all printf to syslog so i thought of redirecting stdout and stderr to syslog. For that I tried the following code
int main()
{
FILE *fl;
fl = popen("logger","w");
if(fl == NULL)
return 1;
fprintf(fl,"logger test new");//this goes to /var/log/messages
int nf;
nf = fileno(fl);
dup2(nf,STDOUT_FILENO);
dup2(nf,STDERR_FILENO);
fprintf(stdout,"Wriiten in stdout\n");
fprintf(stderr,"Wriiten in stderr\n");
pclose(fl);
}
Issue is stderr goes to syslog and nothing is printed on the screen and program is hung. Please suggest.