mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-20 14:43:34 +08:00
Always use FILE* for standard output.
The threads running commands for clients through the Posix daemon used to write to a char buffer through snprintf (etc.) which was then written directly to the file descriptor, whereas in the other case printf (etc.) was used to write to stdout (FILE*). Both versions used some macro's and repeated code to have the same output. This change unifies these two cases by using a FILE* in both cases. The (line) buffering is done by the standard C library's implementation (just like with stdout), and px4_log.c now uses the same code in all cases (using fprintf, etc.) for printing (colored) output.
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <px4_log.h>
|
||||
|
||||
using namespace linux_pwm_out;
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace linux_pwm_out;
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <px4_config.h>
|
||||
#include <px4_workqueue.h>
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <px4_config.h>
|
||||
#include <px4_workqueue.h>
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <uORB/topics/vehicle_gps_position.h>
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include "../uORB.h"
|
||||
#include <px4_time.h>
|
||||
#include <px4_tasks.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct orb_test {
|
||||
int val;
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
|
||||
set(SRCS)
|
||||
|
||||
if (NOT "${OS}" MATCHES "qurt")
|
||||
if (NOT "${OS}" MATCHES "qurt" AND NOT "${CONFIG}" MATCHES "px4io")
|
||||
list(APPEND SRCS
|
||||
px4_log.c
|
||||
)
|
||||
|
||||
+41
-107
@@ -31,6 +31,7 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <px4_log.h>
|
||||
@@ -95,78 +96,38 @@ void px4_backtrace()
|
||||
|
||||
__EXPORT void px4_log_modulename(int level, const char *moduleName, const char *fmt, ...)
|
||||
{
|
||||
FILE *out = stdout;
|
||||
bool use_color = true;
|
||||
|
||||
#ifdef __PX4_POSIX
|
||||
char *buffer;
|
||||
unsigned max_length;
|
||||
bool is_atty = false;
|
||||
|
||||
if (get_stdout_pipe_buffer(&buffer, &max_length, &is_atty) == 0) {
|
||||
if (level >= _PX4_LOG_LEVEL_INFO) {
|
||||
|
||||
unsigned pos = 0;
|
||||
|
||||
if (is_atty) { pos += snprintf(buffer + pos, max_length - pos, "%s", __px4_log_level_color[level]); }
|
||||
|
||||
if (pos >= max_length) { return; }
|
||||
|
||||
pos += snprintf(buffer + pos, max_length - pos, __px4__log_level_fmt __px4__log_level_arg(level));
|
||||
|
||||
if (pos >= max_length) { return; }
|
||||
|
||||
if (is_atty) { pos += snprintf(buffer + pos, max_length - pos, "%s", PX4_ANSI_COLOR_GRAY); }
|
||||
|
||||
if (pos >= max_length) { return; }
|
||||
|
||||
pos += snprintf(buffer + pos, max_length - pos, __px4__log_modulename_pfmt, moduleName);
|
||||
va_list argptr;
|
||||
|
||||
if (pos >= max_length) { return; }
|
||||
|
||||
if (is_atty) { pos += snprintf(buffer + pos, max_length - pos, "%s", __px4_log_level_color[level]); }
|
||||
|
||||
if (pos >= max_length) { return; }
|
||||
|
||||
va_start(argptr, fmt);
|
||||
pos += vsnprintf(buffer + pos, max_length - pos, fmt, argptr);
|
||||
|
||||
if (pos >= max_length) { return; }
|
||||
|
||||
va_end(argptr);
|
||||
pos += snprintf(buffer + pos, max_length - pos, "\n");
|
||||
|
||||
if (pos >= max_length) { return; }
|
||||
|
||||
if (is_atty) { pos += snprintf(buffer + pos, max_length - pos, "%s", PX4_ANSI_COLOR_RESET); }
|
||||
|
||||
if (pos >= max_length) { return; }
|
||||
|
||||
// +1 for the terminating 0 char.
|
||||
send_stdout_pipe_buffer(pos + 1);
|
||||
}
|
||||
|
||||
} else {
|
||||
out = get_stdout(&use_color);
|
||||
#endif
|
||||
|
||||
if (level >= _PX4_LOG_LEVEL_INFO) {
|
||||
PX4_LOG_COLOR_START
|
||||
printf(__px4__log_level_fmt __px4__log_level_arg(level));
|
||||
PX4_LOG_COLOR_MODULE
|
||||
printf(__px4__log_modulename_pfmt, moduleName);
|
||||
PX4_LOG_COLOR_MESSAGE
|
||||
va_list argptr;
|
||||
va_start(argptr, fmt);
|
||||
vprintf(fmt, argptr);
|
||||
va_end(argptr);
|
||||
PX4_LOG_COLOR_END
|
||||
printf("\n");
|
||||
}
|
||||
#ifndef PX4_LOG_COLORIZED_OUTPUT
|
||||
use_color = false;
|
||||
#endif
|
||||
|
||||
#ifdef __PX4_POSIX
|
||||
if (level >= _PX4_LOG_LEVEL_INFO) {
|
||||
if (use_color) { fputs(__px4_log_level_color[level], out); }
|
||||
|
||||
fprintf(out, __px4__log_level_fmt __px4__log_level_arg(level));
|
||||
|
||||
if (use_color) { fputs(PX4_ANSI_COLOR_GRAY, out); }
|
||||
|
||||
fprintf(out, __px4__log_modulename_pfmt, moduleName);
|
||||
|
||||
if (use_color) { fputs(__px4_log_level_color[level], out); }
|
||||
|
||||
va_list argptr;
|
||||
va_start(argptr, fmt);
|
||||
vfprintf(out, fmt, argptr);
|
||||
va_end(argptr);
|
||||
|
||||
if (use_color) { fputs(PX4_ANSI_COLOR_RESET, out); }
|
||||
|
||||
fputc('\n', out);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* publish an orb log message */
|
||||
if (level >= _PX4_LOG_LEVEL_WARN && orb_log_message_pub) { //only publish important messages
|
||||
|
||||
@@ -201,52 +162,25 @@ __EXPORT void px4_log_modulename(int level, const char *moduleName, const char *
|
||||
|
||||
__EXPORT void px4_log_raw(int level, const char *fmt, ...)
|
||||
{
|
||||
FILE *out = stdout;
|
||||
bool use_color = true;
|
||||
|
||||
#ifdef __PX4_POSIX
|
||||
char *buffer;
|
||||
unsigned max_length;
|
||||
bool is_atty = false;
|
||||
|
||||
if (get_stdout_pipe_buffer(&buffer, &max_length, &is_atty) == 0) {
|
||||
if (level >= _PX4_LOG_LEVEL_INFO) {
|
||||
|
||||
unsigned pos = 0;
|
||||
|
||||
va_list argptr;
|
||||
|
||||
if (is_atty) { pos += snprintf(buffer + pos, max_length - pos, "%s", __px4_log_level_color[level]); }
|
||||
|
||||
if (pos >= max_length) { return; }
|
||||
|
||||
va_start(argptr, fmt);
|
||||
pos += vsnprintf(buffer + pos, max_length - pos, fmt, argptr);
|
||||
va_end(argptr);
|
||||
|
||||
if (pos >= max_length) { return; }
|
||||
|
||||
if (is_atty) { pos += snprintf(buffer + pos, max_length - pos, "%s", PX4_ANSI_COLOR_RESET); }
|
||||
|
||||
if (pos >= max_length) { return; }
|
||||
|
||||
// +1 for the terminating 0 char.
|
||||
send_stdout_pipe_buffer(pos + 1);
|
||||
}
|
||||
|
||||
} else {
|
||||
out = get_stdout(&use_color);
|
||||
#endif
|
||||
|
||||
if (level >= _PX4_LOG_LEVEL_INFO) {
|
||||
PX4_LOG_COLOR_START
|
||||
PX4_LOG_COLOR_MESSAGE
|
||||
va_list argptr;
|
||||
va_start(argptr, fmt);
|
||||
vprintf(fmt, argptr);
|
||||
va_end(argptr);
|
||||
PX4_LOG_COLOR_END
|
||||
}
|
||||
#ifndef PX4_LOG_COLORIZED_OUTPUT
|
||||
use_color = false;
|
||||
#endif
|
||||
|
||||
#ifdef __PX4_POSIX
|
||||
if (level >= _PX4_LOG_LEVEL_INFO) {
|
||||
if (use_color) { fputs(__px4_log_level_color[level], out); }
|
||||
|
||||
va_list argptr;
|
||||
va_start(argptr, fmt);
|
||||
vfprintf(out, fmt, argptr);
|
||||
va_end(argptr);
|
||||
|
||||
if (use_color) { fputs(PX4_ANSI_COLOR_RESET, out); }
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -196,24 +196,6 @@ __END_DECLS
|
||||
#endif /* __PX4_POSIX */
|
||||
|
||||
|
||||
#ifdef PX4_LOG_COLORIZED_OUTPUT
|
||||
#include <unistd.h>
|
||||
#define PX4_LOG_COLOR_START \
|
||||
int use_color = isatty(STDOUT_FILENO); \
|
||||
if (use_color) printf("%s", __px4_log_level_color[level]);
|
||||
#define PX4_LOG_COLOR_MODULE \
|
||||
if (use_color) printf(PX4_ANSI_COLOR_GRAY);
|
||||
#define PX4_LOG_COLOR_MESSAGE \
|
||||
if (use_color) printf("%s", __px4_log_level_color[level]);
|
||||
#define PX4_LOG_COLOR_END \
|
||||
if (use_color) printf(PX4_ANSI_COLOR_RESET);
|
||||
#else
|
||||
#define PX4_LOG_COLOR_START
|
||||
#define PX4_LOG_COLOR_MODULE
|
||||
#define PX4_LOG_COLOR_MESSAGE
|
||||
#define PX4_LOG_COLOR_END
|
||||
#endif /* PX4_LOG_COLORIZED_OUTPUT */
|
||||
|
||||
/****************************************************************************
|
||||
* Output format macros
|
||||
* Use these to implement the code level macros below
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <unit_test.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <systemlib/hysteresis/hysteresis.h>
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <perf/perf_counter.h>
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <perf/perf_counter.h>
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <perf/perf_counter.h>
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <perf/perf_counter.h>
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
#include <limits>
|
||||
#include <dirent.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <px4_config.h>
|
||||
#include <mixer/mixer.h>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <px4_defines.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
class ParameterTest : public UnitTest
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user