centralized printf formats and improved log_displayer performance
Before, log_displayer copied each message into the same structure that
gets put into queues etc so that it could be printed with the same
function. Now, it prints it directly, but using the same formats.
diff --git a/aos/common/logging/logging.h b/aos/common/logging/logging.h
index 673cc9a..114007b 100644
--- a/aos/common/logging/logging.h
+++ b/aos/common/logging/logging.h
@@ -8,6 +8,8 @@
#include <stdint.h>
#include <stdlib.h>
+#include "aos/common/macros.h"
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -28,9 +30,6 @@
DECL_LEVELS;
#undef DECL_LEVEL
-#define STRINGIFY(x) TO_STRING(x)
-#define TO_STRING(x) #x
-
// Not static const size_t for C code.
#define LOG_MESSAGE_LEN 400
diff --git a/aos/common/logging/logging_impl.cc b/aos/common/logging/logging_impl.cc
index b74b69d..e7b0a5e 100644
--- a/aos/common/logging/logging_impl.cc
+++ b/aos/common/logging/logging_impl.cc
@@ -7,6 +7,7 @@
#include "aos/common/inttypes.h"
#include "aos/common/once.h"
#include "aos/common/queue_types.h"
+#include "aos/common/logging/logging_printf_formats.h"
namespace aos {
namespace logging {
@@ -138,17 +139,13 @@
}
void PrintMessage(FILE *output, const LogMessage &message) {
-#define NSECONDS_DIGITS 5
-#define BASE_FORMAT \
- "%.*s(%" PRId32 ")(%05" PRIu16 "): %-7s at %010" PRId32 \
- ".%0" STRINGIFY(NSECONDS_DIGITS) PRId32 "s: "
-#define BASE_ARGS \
- static_cast<int>(message.name_length), message.name, \
- static_cast<int32_t>(message.source), message.sequence, \
- log_str(message.level), message.seconds, (message.nseconds + 5000) / 10000
- switch (message.type) {
+#define BASE_ARGS \
+ AOS_LOGGING_BASE_ARGS( \
+ message.name_length, message.name, static_cast<int32_t>(message.source), \
+ message.sequence, message.level, message.seconds, message.nseconds)
+ switch (message.type) {
case LogMessage::Type::kString:
- fprintf(output, BASE_FORMAT "%.*s", BASE_ARGS,
+ fprintf(output, AOS_LOGGING_BASE_FORMAT "%.*s", BASE_ARGS,
static_cast<int>(message.message_length), message.message);
break;
case LogMessage::Type::kStruct: {
@@ -169,7 +166,7 @@
LOG(WARNING, "%zu extra bytes on message of type %s\n", input_length,
type_cache::Get(message.structure.type_id).name.c_str());
}
- fprintf(output, BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS,
+ fprintf(output, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS,
static_cast<int>(message.structure.string_length),
message.structure.serialized,
static_cast<int>(sizeof(buffer) - output_length), buffer);
@@ -192,14 +189,12 @@
LOG(FATAL, "printing %dx%d matrix of type %" PRIu32 " failed\n",
message.matrix.rows, message.matrix.cols, message.matrix.type);
}
- fprintf(output, BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS,
+ fprintf(output, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS,
static_cast<int>(message.matrix.string_length),
message.matrix.data,
static_cast<int>(sizeof(buffer) - output_length), buffer);
} break;
}
-#undef NSECONDS_DIGITS
-#undef BASE_FORMAT
#undef BASE_ARGS
}
diff --git a/aos/common/logging/logging_printf_formats.h b/aos/common/logging/logging_printf_formats.h
new file mode 100644
index 0000000..be186bc
--- /dev/null
+++ b/aos/common/logging/logging_printf_formats.h
@@ -0,0 +1,30 @@
+#ifndef AOS_COMMON_LOGGING_LOGGING_PRINTF_FORMATS_H_
+#define AOS_COMMON_LOGGING_LOGGING_PRINTF_FORMATS_H_
+
+#include "aos/common/macros.h"
+
+// This file has printf(3) formats and corresponding arguments for printing out
+// times and log messages.
+// They are all split out as macros because there are 2 things that want to
+// print using the same format: log_displayer and PrintMessage in
+// logging_impl.cc.
+
+#define AOS_TIME_FORMAT \
+ "%010" PRId32 ".%0" STRINGIFY(AOS_TIME_NSECONDS_DIGITS) PRId32 "s"
+#define AOS_TIME_ARGS(sec, nsec) \
+ sec, (nsec + (AOS_TIME_NSECONDS_DENOMINATOR / 2)) / \
+ AOS_TIME_NSECONDS_DENOMINATOR
+
+#define AOS_LOGGING_BASE_FORMAT \
+ "%.*s(%" PRId32 ")(%05" PRIu16 "): %-7s at " AOS_TIME_FORMAT ": "
+#define AOS_LOGGING_BASE_ARGS(name_length, name, source, sequence, level, sec, \
+ nsec) \
+ static_cast<int>(name_length), name, source, sequence, \
+ ::aos::logging::log_str(level), AOS_TIME_ARGS(sec, nsec)
+
+// These 2 define how many digits we use to print out the nseconds fields of
+// times. They have to stay matching.
+#define AOS_TIME_NSECONDS_DIGITS 5
+#define AOS_TIME_NSECONDS_DENOMINATOR 10000
+
+#endif // AOS_COMMON_LOGGING_LOGGING_PRINTF_FORMATS_H_