3

I've a C++ application that uses libespeak and, by extension, ALSA.

The first time this application produces audio, the following output is produced on stderr:

ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_dmix.c:961:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started

I have my own logging and would like to either disable these errors, or re-route them through my own logging framework for additional processing and reporting.

CL.
  • 173,858
  • 17
  • 217
  • 259
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • 1
    Only some of those messages come from ALSA. This is a [PortAudio bug](https://www.assembla.com/spaces/portaudio/tickets/163). – CL. Jul 16 '14 at 11:41

3 Answers3

3

It is possible to redirect the output of stderr using freopen.( Is it possible to disable stderr in C++?) In my case those errors were generated when Pa_Initialize(); was called, so I redirected stderr to /dev/null before it was called and redirected it back afterwards.

freopen("/dev/null","w",stderr);
PaError err= Pa_Initialize();
freopen("/dev/tty","w",stderr);
Community
  • 1
  • 1
  • Nice approach. Thanks, I'll give this a shot. – Drew Noakes Jun 11 '15 at 13:11
  • Judging by a comment from years ago [https://app.assembla.com/spaces/arhHGeUuSr4k70eJe4gwI3/tickets/163/details?comment=65633503] on the bug linked by CL, it doesn't seem to be a priority for PortAudio. Solutions like this may be best where the performance of the freopen's isn't going to be an issue. – cardiff space man Feb 17 '20 at 01:01
2

You may want to look at snd_lib_error_set_handler function to register an arbitrary error-handling callback.

http://www.alsa-project.org/alsa-doc/alsa-lib/group___error.html#ga6ba1f0aa6c6bc5d335ab297d6019cb03

smiszym
  • 133
  • 10
0

This works for me

#include <fcntl.h>

int main()
{
    int saved_stderr = dup(STDERR_FILENO);
    int devnull = open("/dev/null", O_RDWR);
    dup2(devnull, STDERR_FILENO);  // Replace standard out
    Pa_Initialize();
    dup2(saved_stderr, STDERR_FILENO);
}
Pazel1374
  • 218
  • 3
  • 14