Skip to content

Commit

Permalink
lib/libc/syslog/lib_syslog.c : fix dbg and printf log mixup
Browse files Browse the repository at this point in the history
Use semaphore on stdout file stream, so that the entire dbg log is pushed to output
thus stopping the log mixup due to context switching.

PRINTING FROM THREAD
hello_main: printing from main
PRINTING FROM THREAD
hello_main: printing from main
PRINTING FROM THREAD
hello_main: printing from main
PRINTING FROM THREAD
hello_main: printing from main
  • Loading branch information
swasthik22 authored and sunghan-chang committed Jan 18, 2023
1 parent 62e84aa commit 7ce8b17
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lib/libc/syslog/lib_syslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@

#include <stdio.h>
#include <syslog.h>

#include <tinyara/arch.h>
#include <tinyara/clock.h>
#include <tinyara/streams.h>
#if defined(CONFIG_LOGM) && defined(CONFIG_SYSLOG2LOGM)
#include <tinyara/logm.h>
#endif
#include "syslog/syslog.h"
#if CONFIG_NFILE_STREAMS > 0
#include "lib_internal.h"
#endif

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -121,9 +124,10 @@ static inline int vsyslog_internal(FAR const char *fmt, va_list ap)
struct lib_outstream_s stream;
#endif

int ret;

#if defined(CONFIG_SYSLOG_TIMESTAMP)
struct timespec ts;
int ret;

/* Get the current time */

Expand Down Expand Up @@ -154,15 +158,31 @@ static inline int vsyslog_internal(FAR const char *fmt, va_list ap)

lib_rawoutstream(&stream, 1);

#if CONFIG_NFILE_STREAMS > 0 && (defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__))
if(!up_interrupt_context() && getpid() != 0){
lib_take_semaphore(stdout);
}
#elif CONFIG_NFILE_STREAMS > 0
lib_take_semaphore(stdout);
#endif

#if defined(CONFIG_SYSLOG_TIMESTAMP)
/* Pre-pend the message with the current time */

if (ret == OK) {
(void)lib_sprintf((FAR struct lib_outstream_s *)&stream, "[%6d.%06d]", ts.tv_sec, ts.tv_nsec / 1000);
}
#endif

return lib_vsprintf(&stream.public, fmt, ap);
ret = lib_vsprintf(&stream.public,fmt,ap);

#if CONFIG_NFILE_STREAMS > 0 && (defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__))
if(!up_interrupt_context() && getpid() != 0){
lib_give_semaphore(stdout);
}
#elif CONFIG_NFILE_STREAMS > 0
lib_give_semaphore(stdout);
#endif
return ret;

#elif defined(CONFIG_ARCH_LOWPUTC)
/* Wrap the low-level output in a stream object and let lib_vsprintf
Expand Down

0 comments on commit 7ce8b17

Please sign in to comment.