Add method to close output stream and reset to default

reverse-scroll
nirenjan 2021-07-16 08:29:11 -07:00
parent ab946b4a1a
commit 0d407d77fe
4 changed files with 20 additions and 5 deletions

View File

@ -17,6 +17,7 @@ PKG_PROG_PKG_CONFIG
PKG_INSTALLDIR PKG_INSTALLDIR
AX_COMPILER_FLAGS AX_COMPILER_FLAGS
AX_GCC_FUNC_ATTRIBUTE([constructor]) AX_GCC_FUNC_ATTRIBUTE([constructor])
AX_GCC_FUNC_ATTRIBUTE([destructor])
AX_GCC_FUNC_ATTRIBUTE([format]) AX_GCC_FUNC_ATTRIBUTE([format])
AC_SUBST([PINELOG_CFLAGS]) AC_SUBST([PINELOG_CFLAGS])

View File

@ -87,16 +87,25 @@ void pinelog_set_defaults(void)
log_level = PINELOG_DEFAULT_LEVEL; 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) int pinelog_set_output_stream(FILE *stream)
{ {
if (stream == NULL) { if (stream == NULL) {
return EINVAL; return EINVAL;
} }
/* If current output stream is not stdout or stderr, then close it */ pinelog_close_output_stream();
if (output_stream != stdout && output_stream != stderr) {
fclose(output_stream);
}
setlinebuf(stream); setlinebuf(stream);
output_stream = stream; output_stream = stream;

View File

@ -62,6 +62,11 @@ enum {
*/ */
void pinelog_set_defaults(void); void pinelog_set_defaults(void);
/**
* @brief Close the output stream and terminate the logs
*/
void pinelog_close_output_stream(void);
#ifdef PINELOG_TEST #ifdef PINELOG_TEST
/** /**
* @brief Get the pointer to the output stream. Only used in test harness. * @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); printf("1..%u\n", test_id);
fclose(observed_stream_w); pinelog_close_output_stream();
fclose(observed_stream_r); fclose(observed_stream_r);
close(fifo_fd_w); close(fifo_fd_w);
close(fifo_fd_r); close(fifo_fd_r);