diff --git a/configure.ac b/configure.ac index 133e102..b9d83a7 100644 --- a/configure.ac +++ b/configure.ac @@ -17,6 +17,7 @@ PKG_PROG_PKG_CONFIG PKG_INSTALLDIR AX_COMPILER_FLAGS AX_GCC_FUNC_ATTRIBUTE([constructor]) +AX_GCC_FUNC_ATTRIBUTE([destructor]) AX_GCC_FUNC_ATTRIBUTE([format]) AC_SUBST([PINELOG_CFLAGS]) diff --git a/pinelog.c b/pinelog.c index 1c793c5..e2e7c42 100644 --- a/pinelog.c +++ b/pinelog.c @@ -87,16 +87,25 @@ void pinelog_set_defaults(void) log_level = PINELOG_DEFAULT_LEVEL; } +#if HAVE_FUNC_ATTRIBUTE_DESTRUCTOR +__attribute__((destructor)) +#endif +void pinelog_close_output_stream(void) +{ + /* If current output stream is not stdout or stderr, then close it */ + if (output_stream != NULL && output_stream != stdout && output_stream != stderr) { + fclose(output_stream); + } + output_stream = PINELOG_DEFAULT_STREAM; +} + int pinelog_set_output_stream(FILE *stream) { if (stream == NULL) { return EINVAL; } - /* If current output stream is not stdout or stderr, then close it */ - if (output_stream != stdout && output_stream != stderr) { - fclose(output_stream); - } + pinelog_close_output_stream(); setlinebuf(stream); output_stream = stream; diff --git a/pinelog.h b/pinelog.h index 0020ad9..dfd2b0c 100644 --- a/pinelog.h +++ b/pinelog.h @@ -62,6 +62,11 @@ enum { */ void pinelog_set_defaults(void); +/** + * @brief Close the output stream and terminate the logs + */ +void pinelog_close_output_stream(void); + #ifdef PINELOG_TEST /** * @brief Get the pointer to the output stream. Only used in test harness. diff --git a/test_pinelog.c b/test_pinelog.c index 8977367..0f59117 100644 --- a/test_pinelog.c +++ b/test_pinelog.c @@ -181,7 +181,7 @@ int main(int argc, char **argv) printf("1..%u\n", test_id); - fclose(observed_stream_w); + pinelog_close_output_stream(); fclose(observed_stream_r); close(fifo_fd_w); close(fifo_fd_r);