/* * Asterisk -- An open source telephony toolkit. * * Copyright (c) 2007 Tilghman Lesher. * * Tilghman Lesher * * This code is released by the author with no restrictions on usage. * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * */ #include #include #include #include #include "asterisk.h" ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $") #include "asterisk/file.h" #include "asterisk/logger.h" #include "asterisk/options.h" #include "asterisk/channel.h" #include "asterisk/pbx.h" #include "asterisk/module.h" #include "asterisk/say.h" static char *tdesc = "stderr"; static int old_stderr, new_stderr; static pthread_t thread = -1; static void *stderr_monitor(void *data) { int piped[2]; FILE *STDERR; char buf[512]; dup2(STDERR_FILENO, old_stderr); pipe(piped); dup2(piped[1], STDERR_FILENO); close(piped[1]); new_stderr = piped[0]; STDERR = fdopen(new_stderr, "r"); for (;/* ever */;) { pthread_testcancel(); while (fgets(buf, sizeof(buf) - 1, STDERR)) { ast_log(LOG_DEBUG, "%s", buf); } usleep(1); } /* Never reached */ return NULL; } int unload_module(void) { dup2(old_stderr, STDERR_FILENO); /* Give other threads time to stop writing to the monitor thread */ usleep(1); if (thread > -1) pthread_cancel(thread); /* Give monitor thread a chance to go away */ usleep(1); thread = -1; return 0; } int load_module(void) { if (ast_pthread_create(&thread, NULL, stderr_monitor, NULL)) { ast_log(LOG_WARNING, "Unable to create stderr monitor thread\n"); return -1; } return 0; } char *description(void) { return tdesc; } int usecount(void) { return 0; } char *key() { return ASTERISK_GPL_KEY; }