From 1a8703ec1c0aee86aa2440fc8b7cd627f65854a9 Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Wed, 17 Jun 2015 13:28:27 -0700 Subject: [PATCH 1/8] Improved logging with both compile and runtime level filtering The device level debug will have to be removed and the debugging can be based on this new logging structure which can tell where an error (or debug output) occured whch the current implmentation cannot. The one limitation is the new macros cannot take a char* for the format parameter. It must be an actual string literal because it is concatenated with other strings. Signed-off-by: Mark Charlebois --- src/drivers/device/device_posix.cpp | 17 +- .../commander/state_machine_helper.cpp | 7 +- src/platforms/common/module.mk | 3 +- src/platforms/common/px4_log.c | 5 + src/platforms/px4_log.h | 184 +++++++++++------- 5 files changed, 132 insertions(+), 84 deletions(-) create mode 100644 src/platforms/common/px4_log.c diff --git a/src/drivers/device/device_posix.cpp b/src/drivers/device/device_posix.cpp index 7d83842266..771bee2471 100644 --- a/src/drivers/device/device_posix.cpp +++ b/src/drivers/device/device_posix.cpp @@ -85,11 +85,10 @@ Device::log(const char *fmt, ...) PX4_INFO("[%s] ", _name); va_start(ap, fmt); - PX4_INFO( fmt, ap ); - //vprintf(fmt, ap); + vprintf(fmt, ap); va_end(ap); - //printf("\n"); - //fflush(stdout); + printf("\n"); + fflush(stdout); } void @@ -98,14 +97,12 @@ Device::debug(const char *fmt, ...) va_list ap; if (_debug_enabled) { - PX4_INFO("<%s> ", _name); - //printf("<%s> ", _name); + printf("<%s> ", _name); va_start(ap, fmt); - //vprintf(fmt, ap); - PX4_INFO(fmt, ap); + vprintf(fmt, ap); va_end(ap); - //printf("\n"); - //fflush(stdout); + printf("\n"); + fflush(stdout); } } diff --git a/src/modules/commander/state_machine_helper.cpp b/src/modules/commander/state_machine_helper.cpp index 8d49c305e1..ba5dee9be9 100644 --- a/src/modules/commander/state_machine_helper.cpp +++ b/src/modules/commander/state_machine_helper.cpp @@ -269,14 +269,15 @@ arming_state_transition(struct vehicle_status_s *status, ///< current vehicle s } if (ret == TRANSITION_DENIED) { - const char * str = "INVAL: %s - %s"; +#define WARNSTR "INVAL: %s - %s" /* only print to console here by default as this is too technical to be useful during operation */ - warnx(str, state_names[status->arming_state], state_names[new_arming_state]); + warnx(WARNSTR, state_names[status->arming_state], state_names[new_arming_state]); /* print to MAVLink if we didn't provide any feedback yet */ if (!feedback_provided) { - mavlink_log_critical(mavlink_fd, str, state_names[status->arming_state], state_names[new_arming_state]); + mavlink_log_critical(mavlink_fd, WARNSTR, state_names[status->arming_state], state_names[new_arming_state]); } +#undef WARNSTR } return ret; diff --git a/src/platforms/common/module.mk b/src/platforms/common/module.mk index 0e5f463e69..472dc7dba6 100644 --- a/src/platforms/common/module.mk +++ b/src/platforms/common/module.mk @@ -2,5 +2,6 @@ # Common OS porting APIs # -SRCS = px4_getopt.c +SRCS = px4_getopt.c \ + px4_log.c diff --git a/src/platforms/common/px4_log.c b/src/platforms/common/px4_log.c new file mode 100644 index 0000000000..0ee4c2a6ad --- /dev/null +++ b/src/platforms/common/px4_log.c @@ -0,0 +1,5 @@ +#include + +__EXPORT unsigned int __px4_log_level_current = PX4_LOG_LEVEL_AT_RUN_TIME; + +__EXPORT const char *__px4_log_level_str[_PX4_LOG_LEVEL_DEBUG+1] = { "INFO", "PANIC", "ERROR", "WARN", "DEBUG" }; diff --git a/src/platforms/px4_log.h b/src/platforms/px4_log.h index 5576025362..57dbf3e503 100644 --- a/src/platforms/px4_log.h +++ b/src/platforms/px4_log.h @@ -32,92 +32,136 @@ ****************************************************************************/ /** - * @file px4_log.h - * Platform dependant logging/debug + * @file px4_log_os_impl.h + * Platform dependant logging/debug implementation */ #pragma once -#define __px4_log_omit(level, ...) { } - -#define __px4_log(level, ...) { \ - printf("%-5s ", level);\ - printf(__VA_ARGS__);\ - printf("\n");\ -} -#define __px4_log_verbose(level, ...) { \ - printf("%-5s ", level);\ - printf(__VA_ARGS__);\ - printf(" (file %s line %d)\n", __FILE__, __LINE__);\ -} -#if defined(__PX4_QURT) +#define __STDC_FORMAT_MACROS +#include +#include +#include #include -#define FARF printf - -#define __FARF_omit(level, ...) { } -#define __FARF_log(level, ...) { \ - FARF("%-5s ", level);\ - FARF(__VA_ARGS__);\ - FARF("\n");\ -} -#define __FARF_log_verbose(level, ...) { \ - FARF("%-5s ", level);\ - FARF(__VA_ARGS__);\ - FARF(" (file %s line %d)\n", __FILE__, __LINE__);\ -} - -#define PX4_DEBUG(...) __FARF_omit("DEBUG", __VA_ARGS__) -#define PX4_INFO(...) __FARF_log("INFO", __VA_ARGS__) -#define PX4_WARN(...) __FARF_log_verbose("WARN", __VA_ARGS__) -#define PX4_ERR(...) __FARF_log_verbose("ERROR", __VA_ARGS__) - -#elif defined(__PX4_LINUX) -#include -#include -#define __px4_log_threads(level, ...) { \ - printf("%-5s %ld ", level, pthread_self());\ - printf(__VA_ARGS__);\ - printf(" (file %s line %d)\n", __FILE__, __LINE__);\ -} +__BEGIN_DECLS +__EXPORT extern uint64_t hrt_absolute_time(void); +//__EXPORT extern unsigned long pthread_self(); -#define PX4_DEBUG(...) __px4_log_omit("DEBUG", __VA_ARGS__) -#define PX4_INFO(...) __px4_log("INFO", __VA_ARGS__) -#define PX4_WARN(...) __px4_log_verbose("WARN", __VA_ARGS__) -#define PX4_ERR(...) __px4_log_verbose("ERROR", __VA_ARGS__) +#define _PX4_LOG_LEVEL_ALWAYS 0 +#define _PX4_LOG_LEVEL_PANIC 1 +#define _PX4_LOG_LEVEL_ERROR 2 +#define _PX4_LOG_LEVEL_WARN 3 +#define _PX4_LOG_LEVEL_DEBUG 4 -#elif defined(__PX4_DARWIN) -#include -#include +extern const char *__px4_log_level_str[5]; +extern unsigned int __px4_log_level_current; + +#define PX4_LOG_LEVEL_AT_RUN_TIME _PX4_LOG_LEVEL_WARN -#define __px4_log_threads(level, ...) { \ - printf("%-5s %ld ", level, pthread_self());\ - printf(__VA_ARGS__);\ - printf(" (file %s line %d)\n", __FILE__, __LINE__);\ -} +#define _PX4_LOG_LEVEL_STR(level) __px4_log_level_str[level]; + +/**************************************************************************** + * Implementation of log section formatting based on printf + ****************************************************************************/ +#if defined(__PX4_ROS) +#define __px4__log_startline(level) if (level <= __px4_log_level_current) ROS_WARN( +#else +#define __px4__log_startline(level) if (level <= __px4_log_level_current) printf( +#endif +#define __px4__log_timestamp_fmt "%-10" PRIu64 +#define __px4__log_timestamp_arg ,hrt_absolute_time() +#define __px4__log_level_fmt "%-5s " +#define __px4__log_level_arg(level) ,__px4_log_level_str[level] +#define __px4__log_thread_fmt "%ld " +#define __px4__log_thread_arg ,pthread_self() + +#define __px4__log_file_and_line_fmt " (file %s line %d)" +#define __px4__log_file_and_line_arg , __FILE__, __LINE__ +#define __px4__log_end_fmt "\n" +#define __px4__log_endline ) -#define PX4_DEBUG(...) __px4_log_omit("DEBUG", __VA_ARGS__) -#define PX4_INFO(...) __px4_log("INFO", __VA_ARGS__) -#define PX4_WARN(...) __px4_log_verbose("WARN", __VA_ARGS__) -#define PX4_ERR(...) __px4_log_verbose("ERROR", __VA_ARGS__) +/**************************************************************************** + * Output format macros + * Use these to implement the code level macros below + ****************************************************************************/ +#define __px4_log_omit(level, FMT, ...) { } + +#define __px4_log(level, FMT, ...) \ + __px4__log_startline(level)\ + __px4__log_level_fmt \ + FMT\ + __px4__log_end_fmt \ + __px4__log_level_arg(level), ##__VA_ARGS__\ + __px4__log_endline + +#define __px4_log_timestamp(level, FMT, ...) \ + __px4__log_startline(level)\ + __px4__log_timestamp_fmt\ + __px4__log_level_fmt\ + FMT\ + __px4__log_end_fmt\ + __px4__log_timestamp_arg\ + __px4__log_level_arg(level), ##__VA_ARGS__\ + __px4__log_endline + +#define __px4_log_file_and_line(level, FMT, ...) \ + __px4__log_startline(level)\ + __px4__log_timestamp_fmt\ + __px4__log_level_fmt\ + FMT\ + __px4__log_file_and_line_fmt\ + __px4__log_end_fmt\ + __px4__log_timestamp_arg\ + __px4__log_level_arg(level), ##__VA_ARGS__\ + __px4__log_file_and_line_arg\ + __px4__log_endline + +#define __px4_log_timestamp_file_and_line(level, FMT, ...) \ + __px4__log_startline(level)\ + __px4__log_timestamp_fmt\ + __px4__log_level_fmt\ + FMT\ + __px4__log_file_and_line_fmt\ + __px4__log_end_fmt\ + __px4__log_timestamp_arg\ + __px4__log_level_arg(level) , ##__VA_ARGS__\ + __px4__log_file_and_line_arg\ + __px4__log_endline + +#define __px4_log_thread_file_and_line(level, FMT, ...) \ + __px4__log_startline(level)\ + __px4__log_thread_fmt\ + __px4__log_level_fmt\ + FMT\ + __px4__log_file_and_line_fmt\ + __px4__log_end_fmt\ + __px4__log_thread_arg\ + __px4__log_level_arg(level) , ##__VA_ARGS__\ + __px4__log_file_and_line_arg\ + __px4__log_endline -#elif defined(__PX4_ROS) -#define PX4_DBG(...) -#define PX4_INFO(...) ROS_WARN(__VA_ARGS__) -#define PX4_WARN(...) ROS_WARN(__VA_ARGS__) -#define PX4_ERR(...) ROS_WARN(__VA_ARGS__) +/**************************************************************************** + * Code level macros + * These are the log APIs that should be used by the code + ****************************************************************************/ +#define PX4_LOG(FMT, ...) __px4_log(_PX4_LOG_LEVEL_ALWAYS, FMT, __VA_ARGS__) -#elif defined(__PX4_NUTTX) -#include +#if defined(DEBUG_BUILD) -#define PX4_DBG(...) -#define PX4_INFO(...) warnx(__VA_ARGS__) -#define PX4_WARN(...) warnx(__VA_ARGS__) -#define PX4_ERR(...) warnx(__VA_ARGS__) +#define PX4_PANIC(FMT, ...) __px4_log_timestamp_file_and_line(_PX4_LOG_LEVEL_PANIC, FMT, ##__VA_ARGS__) +#define PX4_ERR(FMT, ...) __px4_log_timestamp_file_and_line(_PX4_LOG_LEVEL_ERROR, FMT, ##__VA_ARGS__) +#define PX4_WARN(FMT, ...) __px4_log_timestamp_file_and_line(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__) +#define PX4_DEBUG(FMT, ...) __px4_log_timestamp(_PX4_LOG_LEVEL_DEBUG, FMT, __VA_ARGS__) #else -#error "Target platform unknown" +#define PX4_PANIC(FMT, ...) __px4_log_file_and_line(_PX4_LOG_LEVEL_PANIC, FMT, ##__VA_ARGS__) +#define PX4_ERR(FMT, ...) __px4_log_file_and_line(_PX4_LOG_LEVEL_ERROR, FMT, ##__VA_ARGS__) +#define PX4_WARN(FMT, ...) __px4_log_file_and_line(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__) +#define PX4_INFO(FMT, ...) __px4_log(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__) +#define PX4_DEBUG(FMT, ...) __px4_log_omit(_PX4_LOG_LEVEL_DEBUG, FMT, ##__VA_ARGS__) #endif +__END_DECLS From 65e9fd9dd8df747541d2d3ee44fc88b3c7b90c90 Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Wed, 17 Jun 2015 13:37:27 -0700 Subject: [PATCH 2/8] px4_log: minor fixes to logging header file Signed-off-by: Mark Charlebois --- src/platforms/px4_log.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/platforms/px4_log.h b/src/platforms/px4_log.h index 57dbf3e503..d18026b07e 100644 --- a/src/platforms/px4_log.h +++ b/src/platforms/px4_log.h @@ -32,7 +32,7 @@ ****************************************************************************/ /** - * @file px4_log_os_impl.h + * @file px4_log.h * Platform dependant logging/debug implementation */ @@ -46,7 +46,6 @@ __BEGIN_DECLS __EXPORT extern uint64_t hrt_absolute_time(void); -//__EXPORT extern unsigned long pthread_self(); #define _PX4_LOG_LEVEL_ALWAYS 0 #define _PX4_LOG_LEVEL_PANIC 1 @@ -54,8 +53,8 @@ __EXPORT extern uint64_t hrt_absolute_time(void); #define _PX4_LOG_LEVEL_WARN 3 #define _PX4_LOG_LEVEL_DEBUG 4 -extern const char *__px4_log_level_str[5]; -extern unsigned int __px4_log_level_current; +__EXPORT extern const char *__px4_log_level_str[5]; +__EXPORT extern unsigned int __px4_log_level_current; #define PX4_LOG_LEVEL_AT_RUN_TIME _PX4_LOG_LEVEL_WARN From a2297aa950c6adf499fa8f225e9702e4b342c34c Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Wed, 17 Jun 2015 13:49:34 -0700 Subject: [PATCH 3/8] px4_log: Fixed ROS build Signed-off-by: Mark Charlebois --- src/platforms/px4_log.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/platforms/px4_log.h b/src/platforms/px4_log.h index d18026b07e..91107b5490 100644 --- a/src/platforms/px4_log.h +++ b/src/platforms/px4_log.h @@ -44,6 +44,16 @@ #include #include +#if defined(__PX4_ROS) + +#define PX4_PANIC(...) ROS_WARN(__VA_ARGS__) +#define PX4_ERR(...) ROS_WARN(__VA_ARGS__) +#define PX4_WARN(...) ROS_WARN(__VA_ARGS__) +#define PX4_INFO(...) ROS_WARN(__VA_ARGS__) +#define PX4_DEBUG(...) + +#else + __BEGIN_DECLS __EXPORT extern uint64_t hrt_absolute_time(void); @@ -164,3 +174,4 @@ __EXPORT extern unsigned int __px4_log_level_current; #endif __END_DECLS +#endif From dad0526a9975a5fb053484815bf332b58c1410a6 Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Wed, 17 Jun 2015 13:50:49 -0700 Subject: [PATCH 4/8] px4_log: Added include for ROS Signed-off-by: Mark Charlebois --- src/platforms/px4_log.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/platforms/px4_log.h b/src/platforms/px4_log.h index 91107b5490..7e89dbe11d 100644 --- a/src/platforms/px4_log.h +++ b/src/platforms/px4_log.h @@ -46,6 +46,7 @@ #if defined(__PX4_ROS) +#include #define PX4_PANIC(...) ROS_WARN(__VA_ARGS__) #define PX4_ERR(...) ROS_WARN(__VA_ARGS__) #define PX4_WARN(...) ROS_WARN(__VA_ARGS__) From 29a36da22c8c5e79b46e6a454538fbc982ab05cc Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Wed, 17 Jun 2015 17:11:21 -0700 Subject: [PATCH 5/8] px4_log: Added documentation and handled unused variables Added __attribute__ ((unused)) for variables used only for log output and flagged as unused if the message log level is compiled out. Signed-off-by: Mark Charlebois --- src/modules/commander/commander.cpp | 2 +- src/modules/sdlog2/sdlog2.c | 4 +- src/modules/sensors/sensors.cpp | 2 +- src/platforms/px4_log.h | 174 ++++++++++++++++++++++++---- 4 files changed, 153 insertions(+), 29 deletions(-) diff --git a/src/modules/commander/commander.cpp b/src/modules/commander/commander.cpp index d47b45d89f..38a547282f 100644 --- a/src/modules/commander/commander.cpp +++ b/src/modules/commander/commander.cpp @@ -361,7 +361,7 @@ int commander_main(int argc, char *argv[]) if (!strcmp(argv[1], "check")) { int mavlink_fd_local = open(MAVLINK_LOG_DEVICE, 0); - int checkres = prearm_check(&status, mavlink_fd_local); + int checkres __attribute__ ((unused)) = prearm_check(&status, mavlink_fd_local); close(mavlink_fd_local); warnx("FINAL RESULT: %s", (checkres == 0) ? "OK" : "FAILED"); return 0; diff --git a/src/modules/sdlog2/sdlog2.c b/src/modules/sdlog2/sdlog2.c index 270804075e..67e45154cb 100644 --- a/src/modules/sdlog2/sdlog2.c +++ b/src/modules/sdlog2/sdlog2.c @@ -1936,8 +1936,8 @@ void sdlog2_status() } else { float kibibytes = log_bytes_written / 1024.0f; - float mebibytes = kibibytes / 1024.0f; - float seconds = ((float)(hrt_absolute_time() - start_time)) / 1000000.0f; + float mebibytes __attribute__ ((unused)) = kibibytes / 1024.0f; + float seconds __attribute__ ((unused)) = ((float)(hrt_absolute_time() - start_time)) / 1000000.0f; warnx("wrote %lu msgs, %4.2f MiB (average %5.3f KiB/s), skipped %lu msgs", log_msgs_written, (double)mebibytes, (double)(kibibytes / seconds), log_msgs_skipped); mavlink_log_info(mavlink_fd, "[sdlog2] wrote %lu msgs, skipped %lu msgs", log_msgs_written, log_msgs_skipped); diff --git a/src/modules/sensors/sensors.cpp b/src/modules/sensors/sensors.cpp index 0be31046b1..484092c747 100644 --- a/src/modules/sensors/sensors.cpp +++ b/src/modules/sensors/sensors.cpp @@ -706,7 +706,7 @@ Sensors::parameters_update() warnx("WARNING WARNING WARNING\n\nRC CALIBRATION NOT SANE!\n\n"); } - const char *paramerr = "FAIL PARM LOAD"; + const char *paramerr __attribute__ ((unused)) = "FAIL PARM LOAD"; /* channel mapping */ if (param_get(_parameter_handles.rc_map_roll, &(_parameters.rc_map_roll)) != OK) { diff --git a/src/platforms/px4_log.h b/src/platforms/px4_log.h index 7e89dbe11d..d52582f15e 100644 --- a/src/platforms/px4_log.h +++ b/src/platforms/px4_log.h @@ -38,12 +38,6 @@ #pragma once -#define __STDC_FORMAT_MACROS -#include -#include -#include -#include - #if defined(__PX4_ROS) #include @@ -55,6 +49,12 @@ #else +#define __STDC_FORMAT_MACROS +#include +#include +#include +#include + __BEGIN_DECLS __EXPORT extern uint64_t hrt_absolute_time(void); @@ -67,26 +67,29 @@ __EXPORT extern uint64_t hrt_absolute_time(void); __EXPORT extern const char *__px4_log_level_str[5]; __EXPORT extern unsigned int __px4_log_level_current; +// __px4_log_level_current will be initialized to PX4_LOG_LEVEL_AT_RUN_TIME #define PX4_LOG_LEVEL_AT_RUN_TIME _PX4_LOG_LEVEL_WARN -#define _PX4_LOG_LEVEL_STR(level) __px4_log_level_str[level]; - /**************************************************************************** * Implementation of log section formatting based on printf + * + * To write to a specific stream for each message type, open the streams and + * set __px4__log_startline to something like: + * if (level <= __px4_log_level_current) printf(_px4_fd[level], + * + * Additional behavior can be added using "{\" for __px4__log_startline and + * "}" for __px4__log_endline and any other required setup or teardown steps ****************************************************************************/ -#if defined(__PX4_ROS) -#define __px4__log_startline(level) if (level <= __px4_log_level_current) ROS_WARN( -#else #define __px4__log_startline(level) if (level <= __px4_log_level_current) printf( -#endif -#define __px4__log_timestamp_fmt "%-10" PRIu64 + +#define __px4__log_timestamp_fmt "%-10" PRIu64 " " #define __px4__log_timestamp_arg ,hrt_absolute_time() #define __px4__log_level_fmt "%-5s " #define __px4__log_level_arg(level) ,__px4_log_level_str[level] -#define __px4__log_thread_fmt "%ld " +#define __px4__log_thread_fmt "%#X " #define __px4__log_thread_arg ,pthread_self() -#define __px4__log_file_and_line_fmt " (file %s line %d)" +#define __px4__log_file_and_line_fmt " (file %s line %u)" #define __px4__log_file_and_line_arg , __FILE__, __LINE__ #define __px4__log_end_fmt "\n" #define __px4__log_endline ) @@ -95,8 +98,20 @@ __EXPORT extern unsigned int __px4_log_level_current; * Output format macros * Use these to implement the code level macros below ****************************************************************************/ + +/**************************************************************************** + * __px4_log_omit: + * Compile out the message + ****************************************************************************/ #define __px4_log_omit(level, FMT, ...) { } +/**************************************************************************** + * __px4_log: + * Convert a message in the form: + * PX4_WARN("val is %d", val); + * to + * printf("%-5s val is %d\n", __px4_log_level_str[3], val); + ****************************************************************************/ #define __px4_log(level, FMT, ...) \ __px4__log_startline(level)\ __px4__log_level_fmt \ @@ -105,49 +120,132 @@ __EXPORT extern unsigned int __px4_log_level_current; __px4__log_level_arg(level), ##__VA_ARGS__\ __px4__log_endline +/**************************************************************************** + * __px4_log_timestamp: + * Convert a message in the form: + * PX4_WARN("val is %d", val); + * to + * printf("%-5s %10lu val is %d\n", __px4_log_level_str[3], + * hrt_absolute_time(), val); + ****************************************************************************/ #define __px4_log_timestamp(level, FMT, ...) \ __px4__log_startline(level)\ + __px4__log_level_fmt\ __px4__log_timestamp_fmt\ + FMT\ + __px4__log_end_fmt\ + __px4__log_level_arg(level)\ + __px4__log_timestamp_arg\ + , ##__VA_ARGS__\ + __px4__log_endline + +/**************************************************************************** + * __px4_log_timestamp_thread: + * Convert a message in the form: + * PX4_WARN("val is %d", val); + * to + * printf("%-5s %10lu %#X val is %d\n", __px4_log_level_str[3], + * hrt_absolute_time(), pthread_self(), val); + ****************************************************************************/ +#define __px4_log_timestamp_thread(level, FMT, ...) \ + __px4__log_startline(level)\ __px4__log_level_fmt\ + __px4__log_timestamp_fmt\ + __px4__log_thread_fmt\ FMT\ __px4__log_end_fmt\ + __px4__log_level_arg(level)\ __px4__log_timestamp_arg\ - __px4__log_level_arg(level), ##__VA_ARGS__\ + __px4__log_thread_arg\ + , ##__VA_ARGS__\ __px4__log_endline +/**************************************************************************** + * __px4_log_file_and_line: + * Convert a message in the form: + * PX4_WARN("val is %d", val); + * to + * printf("%-5s val is %d (file %s line %u)\n", + * __px4_log_level_str[3], val, __FILE__, __LINE__); + ****************************************************************************/ #define __px4_log_file_and_line(level, FMT, ...) \ __px4__log_startline(level)\ - __px4__log_timestamp_fmt\ __px4__log_level_fmt\ + __px4__log_timestamp_fmt\ FMT\ __px4__log_file_and_line_fmt\ __px4__log_end_fmt\ + __px4__log_level_arg(level)\ __px4__log_timestamp_arg\ - __px4__log_level_arg(level), ##__VA_ARGS__\ + , ##__VA_ARGS__\ __px4__log_file_and_line_arg\ __px4__log_endline +/**************************************************************************** + * __px4_log_timestamp_file_and_line: + * Convert a message in the form: + * PX4_WARN("val is %d", val); + * to + * printf("%-5s %-10lu val is %d (file %s line %u)\n", + * __px4_log_level_str[3], hrt_absolute_time(), + * val, __FILE__, __LINE__); + ****************************************************************************/ #define __px4_log_timestamp_file_and_line(level, FMT, ...) \ __px4__log_startline(level)\ - __px4__log_timestamp_fmt\ __px4__log_level_fmt\ + __px4__log_timestamp_fmt\ FMT\ __px4__log_file_and_line_fmt\ __px4__log_end_fmt\ + __px4__log_level_arg(level)\ __px4__log_timestamp_arg\ - __px4__log_level_arg(level) , ##__VA_ARGS__\ + , ##__VA_ARGS__\ __px4__log_file_and_line_arg\ __px4__log_endline +/**************************************************************************** + * __px4_log_thread_file_and_line: + * Convert a message in the form: + * PX4_WARN("val is %d", val); + * to + * printf("%-5s %#X val is %d (file %s line %u)\n", + * __px4_log_level_str[3], pthread_self(), + * val, __FILE__, __LINE__); + ****************************************************************************/ #define __px4_log_thread_file_and_line(level, FMT, ...) \ __px4__log_startline(level)\ + __px4__log_level_fmt\ __px4__log_thread_fmt\ + FMT\ + __px4__log_file_and_line_fmt\ + __px4__log_end_fmt\ + __px4__log_level_arg(level)\ + __px4__log_thread_arg\ + , ##__VA_ARGS__\ + __px4__log_file_and_line_arg\ + __px4__log_endline + +/**************************************************************************** + * __px4_log_timestamp_thread_file_and_line: + * Convert a message in the form: + * PX4_WARN("val is %d", val); + * to + * printf("%-5s %-10lu %#X val is %d (file %s line %u)\n", + * __px4_log_level_str[3], hrt_absolute_time(), + * pthread_self(), val, __FILE__, __LINE__); + ****************************************************************************/ +#define __px4_log_timestamp_thread_file_and_line(level, FMT, ...) \ + __px4__log_startline(level)\ __px4__log_level_fmt\ + __px4__log_timestamp_fmt\ + __px4__log_thread_fmt\ FMT\ __px4__log_file_and_line_fmt\ __px4__log_end_fmt\ + __px4__log_level_arg(level)\ + __px4__log_timestamp_arg\ __px4__log_thread_arg\ - __px4__log_level_arg(level) , ##__VA_ARGS__\ + , ##__VA_ARGS__\ __px4__log_file_and_line_arg\ __px4__log_endline @@ -156,21 +254,47 @@ __EXPORT extern unsigned int __px4_log_level_current; * Code level macros * These are the log APIs that should be used by the code ****************************************************************************/ -#define PX4_LOG(FMT, ...) __px4_log(_PX4_LOG_LEVEL_ALWAYS, FMT, __VA_ARGS__) -#if defined(DEBUG_BUILD) +/**************************************************************************** + * Messages that should never be filtered or compiled out + ****************************************************************************/ +#define PX4_LOG(FMT, ...) __px4_log(_PX4_LOG_LEVEL_ALWAYS, FMT, ##__VA_ARGS__) +#define PX4_INFO(FMT, ...) __px4_log(_PX4_LOG_LEVEL_ALWAYS, FMT, ##__VA_ARGS__) + +#if defined(TRACE_BUILD) +/**************************************************************************** + * Extremely Verbose settings for a Trace build + ****************************************************************************/ +#define PX4_PANIC(FMT, ...) __px4_log_timestamp_thread_file_and_line(_PX4_LOG_LEVEL_PANIC, FMT, ##__VA_ARGS__) +#define PX4_ERR(FMT, ...) __px4_log_timestamp_thread_file_and_line(_PX4_LOG_LEVEL_ERROR, FMT, ##__VA_ARGS__) +#define PX4_WARN(FMT, ...) __px4_log_timestamp_thread_file_and_line(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__) +#define PX4_DEBUG(FMT, ...) __px4_log_timestamp_thread(_PX4_LOG_LEVEL_DEBUG, FMT, __VA_ARGS__) +#elif defined(DEBUG_BUILD) +/**************************************************************************** + * Verbose settings for a Debug build + ****************************************************************************/ #define PX4_PANIC(FMT, ...) __px4_log_timestamp_file_and_line(_PX4_LOG_LEVEL_PANIC, FMT, ##__VA_ARGS__) #define PX4_ERR(FMT, ...) __px4_log_timestamp_file_and_line(_PX4_LOG_LEVEL_ERROR, FMT, ##__VA_ARGS__) #define PX4_WARN(FMT, ...) __px4_log_timestamp_file_and_line(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__) #define PX4_DEBUG(FMT, ...) __px4_log_timestamp(_PX4_LOG_LEVEL_DEBUG, FMT, __VA_ARGS__) -#else +#elif defined(RELEASE_BUILD) +/**************************************************************************** + * Non-verbose settings for a Release build to minimize strings in build + ****************************************************************************/ +#define PX4_PANIC(FMT, ...) __px4_log_file_and_line(_PX4_LOG_LEVEL_PANIC, FMT, ##__VA_ARGS__) +#define PX4_ERR(FMT, ...) __px4_log_file_and_line(_PX4_LOG_LEVEL_ERROR, FMT, ##__VA_ARGS__) +#define PX4_WARN(FMT, ...) __px4_log_omit(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__) +#define PX4_DEBUG(FMT, ...) __px4_log_omit(_PX4_LOG_LEVEL_DEBUG, FMT, ##__VA_ARGS__) +#else +/**************************************************************************** + * Medium verbose settings for a default build + ****************************************************************************/ #define PX4_PANIC(FMT, ...) __px4_log_file_and_line(_PX4_LOG_LEVEL_PANIC, FMT, ##__VA_ARGS__) #define PX4_ERR(FMT, ...) __px4_log_file_and_line(_PX4_LOG_LEVEL_ERROR, FMT, ##__VA_ARGS__) #define PX4_WARN(FMT, ...) __px4_log_file_and_line(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__) -#define PX4_INFO(FMT, ...) __px4_log(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__) #define PX4_DEBUG(FMT, ...) __px4_log_omit(_PX4_LOG_LEVEL_DEBUG, FMT, ##__VA_ARGS__) #endif From fc5eb7af6f2e30243835beac68e21b91ab319915 Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Wed, 17 Jun 2015 18:05:04 -0700 Subject: [PATCH 6/8] unittests: Fixed dependency on px4_log.c px4_log.c was added to px4_platform library and the library was added to unit tests that use the log macros. There is also a dependency on hrt_absolute_time() as well which requires px4_platform. Signed-off-by: Mark Charlebois --- unittests/CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt index b4e505209d..580d9e5d81 100644 --- a/unittests/CMakeLists.txt +++ b/unittests/CMakeLists.txt @@ -75,6 +75,7 @@ endfunction() add_library( px4_platform # ${PX_SRC}/platforms/common/px4_getopt.c + ${PX_SRC}/platforms/common/px4_log.c ${PX_SRC}/platforms/posix/px4_layer/px4_posix_impl.cpp ${PX_SRC}/platforms/posix/px4_layer/px4_posix_tasks.cpp ${PX_SRC}/platforms/posix/px4_layer/work_lock.c @@ -130,22 +131,27 @@ add_gtest(mixer_test) # conversion_test add_executable(conversion_test conversion_test.cpp ${PX_SRC}/systemcmds/tests/test_conv.cpp) +target_link_libraries( conversion_test px4_platform ) add_gtest(conversion_test) # sbus2_test add_executable(sbus2_test sbus2_test.cpp hrt.cpp) +target_link_libraries( sbus2_test px4_platform ) add_gtest(sbus2_test) # st24_test add_executable(st24_test st24_test.cpp hrt.cpp ${PX_SRC}/lib/rc/st24.c) +target_link_libraries( st24_test px4_platform ) add_gtest(st24_test) # sumd_test add_executable(sumd_test sumd_test.cpp hrt.cpp ${PX_SRC}/lib/rc/sumd.c) +target_link_libraries( sumd_test px4_platform ) add_gtest(sumd_test) # sf0x_test add_executable(sf0x_test sf0x_test.cpp ${PX_SRC}/drivers/sf0x/sf0x_parser.cpp) +target_link_libraries( sf0x_test px4_platform ) add_gtest(sf0x_test) # param_test From 552c9800a9a394e5ad351309d62278aecd44073f Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Wed, 17 Jun 2015 19:04:57 -0700 Subject: [PATCH 7/8] px4_log: Fixed compiler warning when using PX4_LOG If __px4_log_level_current is unsigned then the runtime filter comparison warns because an unsigned value can't be less than zero. Changed typed to signed so compiler will not issue a warning. Signed-off-by: Mark Charlebois --- src/platforms/common/px4_log.c | 2 +- src/platforms/px4_log.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/platforms/common/px4_log.c b/src/platforms/common/px4_log.c index 0ee4c2a6ad..a2c61ab297 100644 --- a/src/platforms/common/px4_log.c +++ b/src/platforms/common/px4_log.c @@ -1,5 +1,5 @@ #include -__EXPORT unsigned int __px4_log_level_current = PX4_LOG_LEVEL_AT_RUN_TIME; +__EXPORT int __px4_log_level_current = PX4_LOG_LEVEL_AT_RUN_TIME; __EXPORT const char *__px4_log_level_str[_PX4_LOG_LEVEL_DEBUG+1] = { "INFO", "PANIC", "ERROR", "WARN", "DEBUG" }; diff --git a/src/platforms/px4_log.h b/src/platforms/px4_log.h index d52582f15e..05278131c8 100644 --- a/src/platforms/px4_log.h +++ b/src/platforms/px4_log.h @@ -65,7 +65,7 @@ __EXPORT extern uint64_t hrt_absolute_time(void); #define _PX4_LOG_LEVEL_DEBUG 4 __EXPORT extern const char *__px4_log_level_str[5]; -__EXPORT extern unsigned int __px4_log_level_current; +__EXPORT extern int __px4_log_level_current; // __px4_log_level_current will be initialized to PX4_LOG_LEVEL_AT_RUN_TIME #define PX4_LOG_LEVEL_AT_RUN_TIME _PX4_LOG_LEVEL_WARN From 785053e4f100cde01a32b29133918e0ba150b115 Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Thu, 18 Jun 2015 07:41:22 -0700 Subject: [PATCH 8/8] px4_log: reverted unused attribute annotations Used a do_nothing() function for px4_omit() that will satisfy the compiler so it will not report unused variables when a debug message is compiled out. Signed-off-by: Mark Charlebois --- src/modules/commander/commander.cpp | 2 +- src/modules/sdlog2/sdlog2.c | 4 ++-- src/modules/sensors/sensors.cpp | 2 +- src/platforms/px4_log.h | 8 +++++++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/modules/commander/commander.cpp b/src/modules/commander/commander.cpp index 38a547282f..d47b45d89f 100644 --- a/src/modules/commander/commander.cpp +++ b/src/modules/commander/commander.cpp @@ -361,7 +361,7 @@ int commander_main(int argc, char *argv[]) if (!strcmp(argv[1], "check")) { int mavlink_fd_local = open(MAVLINK_LOG_DEVICE, 0); - int checkres __attribute__ ((unused)) = prearm_check(&status, mavlink_fd_local); + int checkres = prearm_check(&status, mavlink_fd_local); close(mavlink_fd_local); warnx("FINAL RESULT: %s", (checkres == 0) ? "OK" : "FAILED"); return 0; diff --git a/src/modules/sdlog2/sdlog2.c b/src/modules/sdlog2/sdlog2.c index 67e45154cb..270804075e 100644 --- a/src/modules/sdlog2/sdlog2.c +++ b/src/modules/sdlog2/sdlog2.c @@ -1936,8 +1936,8 @@ void sdlog2_status() } else { float kibibytes = log_bytes_written / 1024.0f; - float mebibytes __attribute__ ((unused)) = kibibytes / 1024.0f; - float seconds __attribute__ ((unused)) = ((float)(hrt_absolute_time() - start_time)) / 1000000.0f; + float mebibytes = kibibytes / 1024.0f; + float seconds = ((float)(hrt_absolute_time() - start_time)) / 1000000.0f; warnx("wrote %lu msgs, %4.2f MiB (average %5.3f KiB/s), skipped %lu msgs", log_msgs_written, (double)mebibytes, (double)(kibibytes / seconds), log_msgs_skipped); mavlink_log_info(mavlink_fd, "[sdlog2] wrote %lu msgs, skipped %lu msgs", log_msgs_written, log_msgs_skipped); diff --git a/src/modules/sensors/sensors.cpp b/src/modules/sensors/sensors.cpp index 484092c747..0be31046b1 100644 --- a/src/modules/sensors/sensors.cpp +++ b/src/modules/sensors/sensors.cpp @@ -706,7 +706,7 @@ Sensors::parameters_update() warnx("WARNING WARNING WARNING\n\nRC CALIBRATION NOT SANE!\n\n"); } - const char *paramerr __attribute__ ((unused)) = "FAIL PARM LOAD"; + const char *paramerr = "FAIL PARM LOAD"; /* channel mapping */ if (param_get(_parameter_handles.rc_map_roll, &(_parameters.rc_map_roll)) != OK) { diff --git a/src/platforms/px4_log.h b/src/platforms/px4_log.h index 05278131c8..327df7abe1 100644 --- a/src/platforms/px4_log.h +++ b/src/platforms/px4_log.h @@ -58,6 +58,12 @@ __BEGIN_DECLS __EXPORT extern uint64_t hrt_absolute_time(void); +// Used to silence unused variable warning +static inline void do_nothing(int level, ...) +{ + (void)level; +} + #define _PX4_LOG_LEVEL_ALWAYS 0 #define _PX4_LOG_LEVEL_PANIC 1 #define _PX4_LOG_LEVEL_ERROR 2 @@ -103,7 +109,7 @@ __EXPORT extern int __px4_log_level_current; * __px4_log_omit: * Compile out the message ****************************************************************************/ -#define __px4_log_omit(level, FMT, ...) { } +#define __px4_log_omit(level, FMT, ...) do_nothing(level, ##__VA_ARGS__) /**************************************************************************** * __px4_log: