Add method to close output stream and reset to default

master
nirenjan 2021-07-16 08:29:11 -07:00
parent fb9b51a130
commit a3eb2c03d7
4 changed files with 20 additions and 5 deletions

View File

@ -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])

View File

@ -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;

View File

@ -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.

View File

@ -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);