fix(log): normalize colorized shell output

Route plain text script output through the PX4 log formatter when it already follows the INFO/WARN/ERROR module format.

This keeps color selection centralized, preserves tty-aware color decisions across daemon and shell output, and adds a stdout tty override for embedded-script execution where output is intentionally captured.

Signed-off-by: Nuno Marques <n.marques21@hotmail.com>
This commit is contained in:
Nuno Marques
2026-04-27 15:25:56 -07:00
parent 1ed35593a7
commit 4916c9e0cf
4 changed files with 582 additions and 24 deletions
@@ -123,14 +123,24 @@ __END_DECLS
#include <px4_platform_common/defines.h> #include <px4_platform_common/defines.h>
#include <drivers/drv_hrt.h> #include <drivers/drv_hrt.h>
#if defined(_MSC_VER) && !defined(__clang__)
# define __px4_log_format(_fmt, _args)
#elif defined(__PX4_WINDOWS)
# define __px4_log_format(_fmt, _args) __attribute__((format(gnu_printf, _fmt, _args)))
#else
# define __px4_log_format(_fmt, _args) __attribute__((format(printf, _fmt, _args)))
#endif
__BEGIN_DECLS __BEGIN_DECLS
__EXPORT extern const char *__px4_log_level_str[_PX4_LOG_LEVEL_PANIC + 1]; __EXPORT extern const char *__px4_log_level_str[_PX4_LOG_LEVEL_PANIC + 1];
__EXPORT void px4_log_modulename(int level, const char *moduleName, const char *fmt, ...) __EXPORT void px4_log_modulename(int level, const char *moduleName, const char *fmt, ...)
__attribute__((format(printf, 3, 4))); __px4_log_format(3, 4);
__EXPORT void px4_log_raw(int level, const char *fmt, ...) __EXPORT void px4_log_raw(int level, const char *fmt, ...)
__attribute__((format(printf, 2, 3))); __px4_log_format(2, 3);
__EXPORT void px4_log_history(FILE *out); __EXPORT void px4_log_history(FILE *out);
__EXPORT int px4_log_modulename_from_text(const char *line);
__EXPORT void px4_log_write_text(FILE *out, const char *data, size_t length);
#if __GNUC__ #if __GNUC__
// Allow empty format strings. // Allow empty format strings.
@@ -168,8 +178,9 @@ __END_DECLS
#define __px4__log_end_fmt "\n" #define __px4__log_end_fmt "\n"
#ifdef __PX4_POSIX #ifdef __PX4_POSIX
#define PX4_LOG_COLORIZED_OUTPUT //if defined and output is a tty, colorize the output according to the log level // If defined and output is a tty, colorize the output according to the log level.
#endif /* __PX4_POSIX */ #define PX4_LOG_COLORIZED_OUTPUT
#endif
/**************************************************************************** /****************************************************************************
File diff suppressed because it is too large Load Diff
@@ -48,5 +48,7 @@ __BEGIN_DECLS
* @return The FILE* which represents the standard output of the current thread. * @return The FILE* which represents the standard output of the current thread.
*/ */
__EXPORT FILE *get_stdout(bool *isatty_); __EXPORT FILE *get_stdout(bool *isatty_);
__EXPORT void set_stdout_isatty_override(bool isatty_);
__EXPORT void clear_stdout_isatty_override();
__END_DECLS __END_DECLS
@@ -57,12 +57,22 @@
using namespace px4_daemon; using namespace px4_daemon;
namespace
{
thread_local int stdout_isatty_override = -1;
}
FILE *get_stdout(bool *isatty_) FILE *get_stdout(bool *isatty_)
{ {
if (stdout_isatty_override >= 0) {
if (isatty_) {
*isatty_ = stdout_isatty_override != 0;
}
}
// If the server is not running, we are not in a thread that has been started // If the server is not running, we are not in a thread that has been started
if (!Server::is_running()) { if (!Server::is_running()) {
if (isatty_) { *isatty_ = isatty(1); } if (isatty_ && stdout_isatty_override < 0) { *isatty_ = isatty(1); }
return stdout; return stdout;
} }
@@ -74,12 +84,22 @@ FILE *get_stdout(bool *isatty_)
// have any thread specific data set and we won't have a pipe to write // have any thread specific data set and we won't have a pipe to write
// stdout to. // stdout to.
if (thread_data_ptr == nullptr || thread_data_ptr->thread_stdout == nullptr) { if (thread_data_ptr == nullptr || thread_data_ptr->thread_stdout == nullptr) {
if (isatty_) { *isatty_ = isatty(1); } if (isatty_ && stdout_isatty_override < 0) { *isatty_ = isatty(1); }
return stdout; return stdout;
} }
if (isatty_) { *isatty_ = thread_data_ptr->is_atty; } if (isatty_ && stdout_isatty_override < 0) { *isatty_ = thread_data_ptr->is_atty; }
return thread_data_ptr->thread_stdout; return thread_data_ptr->thread_stdout;
} }
void set_stdout_isatty_override(bool isatty_)
{
stdout_isatty_override = isatty_ ? 1 : 0;
}
void clear_stdout_isatty_override()
{
stdout_isatty_override = -1;
}