Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture standard error output from a Windows service?

I have an application that makes use of the Mozilla LDAP library. We're diagnosing a problem involving the LDAP library failing to make a connection to the server. I'm attempting to get additional information from the LDAP library by tossing a debug version of the lib in with the application and enabling debug using ldap_set_opt. Unfortunately, I think the debug library is sending debug strings to standard error.

While I'm working on recompiling the LDAP client library again, hopefully enabling the option that makes it call OutputDebugString instead of streaming to stderr, a nice solution would be to capture the stderr output to a file. The application, though, is running as a Windows service.

Anyone know how I could redirect stderr to a file for an application running as a service?

edit

I'm hoping not to have to modify any more of the service source code than I already have. Options in the service configuration would be ideal.

like image 588
veefu Avatar asked May 06 '09 17:05

veefu


2 Answers

Can you try manually redirecting stderr?

    FILE* stderr_redirect = freopen( "C:/stderr.log", "w", stderr );

    // Code that writes to stderr

    fclose( stderr_redirect );

Edit:

There is no way to redirect stdout or stderr for a service other than to handle those streams inside your service yourself. Some services provide an option to redirect these messages to a file. You could either temporarily redirect these streams or add an option to your service to make it configurable next time you have a problem.

like image 137
Naaff Avatar answered Oct 16 '22 04:10

Naaff


If you are modifying the code to the service, you could call SetStdHandle in your ServiceMain:

SetStdHandle(STD_ERROR_HANDLE, logFileHandle);
like image 39
Michael Avatar answered Oct 16 '22 04:10

Michael