Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 1 | #include "aos/linux_code/logging/linux_logging.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 2 | |
| 3 | #include <stdarg.h> |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | #include <time.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <errno.h> |
| 9 | #include <unistd.h> |
| 10 | #include <limits.h> |
Brian Silverman | a33af74 | 2014-03-08 12:44:12 -0800 | [diff] [blame] | 11 | #include <inttypes.h> |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | |
| 15 | #include "aos/common/die.h" |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame^] | 16 | #include "aos/common/logging/logging_interface.h" |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 17 | #include "aos/linux_code/ipc_lib/queue.h" |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 18 | #include "aos/common/time.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 19 | |
| 20 | namespace aos { |
| 21 | namespace logging { |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 22 | namespace linux_code { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 23 | namespace { |
| 24 | |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 25 | RawQueue *queue = NULL; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 26 | |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 27 | int dropped_messages = 0; |
Brian Silverman | 75e9530 | 2015-02-22 00:43:59 -0500 | [diff] [blame] | 28 | ::aos::time::Time dropped_start, backoff_start; |
| 29 | // Wait this long after dropping a message before even trying to write any more. |
| 30 | constexpr ::aos::time::Time kDropBackoff = ::aos::time::Time::InSeconds(0.1); |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 31 | |
| 32 | LogMessage *GetMessageOrDie() { |
| 33 | LogMessage *message = static_cast<LogMessage *>(queue->GetMessage()); |
| 34 | if (message == NULL) { |
| 35 | LOG(FATAL, "%p->GetMessage() failed\n", queue); |
| 36 | } else { |
| 37 | return message; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | class LinuxQueueLogImplementation : public LogImplementation { |
Brian Silverman | f798614 | 2014-04-21 17:42:35 -0700 | [diff] [blame] | 42 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 43 | virtual void DoLog(log_level level, const char *format, va_list ap) override { |
| 44 | LogMessage *message = GetMessageOrDie(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 45 | internal::FillInMessage(level, format, ap, message); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 46 | Write(message); |
| 47 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 48 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 49 | virtual void LogStruct(log_level level, const ::std::string &message_string, |
| 50 | size_t size, const MessageType *type, |
| 51 | const ::std::function<size_t(char *)> &serialize) |
| 52 | override { |
| 53 | LogMessage *message = GetMessageOrDie(); |
| 54 | internal::FillInMessageStructure(level, message_string, size, type, |
| 55 | serialize, message); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 56 | Write(message); |
| 57 | } |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 58 | |
| 59 | virtual void LogMatrix(log_level level, const ::std::string &message_string, |
| 60 | uint32_t type_id, int rows, int cols, const void *data) |
| 61 | override { |
| 62 | LogMessage *message = GetMessageOrDie(); |
| 63 | internal::FillInMessageMatrix(level, message_string, type_id, rows, cols, |
| 64 | data, message); |
| 65 | Write(message); |
| 66 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } // namespace |
| 70 | |
| 71 | void Register() { |
| 72 | Init(); |
| 73 | |
Austin Schuh | 0480bc8 | 2014-10-25 18:01:51 -0700 | [diff] [blame] | 74 | queue = RawQueue::Fetch("LoggingQueue", sizeof(LogMessage), 1323, 40000); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 75 | if (queue == NULL) { |
| 76 | Die("logging: couldn't fetch queue\n"); |
| 77 | } |
| 78 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 79 | AddImplementation(new LinuxQueueLogImplementation()); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Brian Silverman | 7faaec7 | 2014-05-26 16:25:38 -0700 | [diff] [blame] | 82 | const LogMessage *ReadNext(Options<RawQueue> flags, int *index) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 83 | return static_cast<const LogMessage *>(queue->ReadMessageIndex(flags, index)); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | const LogMessage *ReadNext() { |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 87 | return ReadNext(RawQueue::kBlock); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Brian Silverman | 7faaec7 | 2014-05-26 16:25:38 -0700 | [diff] [blame] | 90 | const LogMessage *ReadNext(Options<RawQueue> flags) { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 91 | const LogMessage *r = NULL; |
| 92 | do { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 93 | r = static_cast<const LogMessage *>(queue->ReadMessage(flags)); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 94 | // not blocking means return a NULL if that's what it gets |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 95 | } while ((flags & RawQueue::kBlock) && r == NULL); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 96 | return r; |
| 97 | } |
| 98 | |
| 99 | LogMessage *Get() { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 100 | return static_cast<LogMessage *>(queue->GetMessage()); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void Free(const LogMessage *msg) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 104 | queue->FreeMessage(msg); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void Write(LogMessage *msg) { |
Brian Silverman | 75e9530 | 2015-02-22 00:43:59 -0500 | [diff] [blame] | 108 | if (__builtin_expect(dropped_messages > 0, false)) { |
| 109 | ::aos::time::Time message_time = |
| 110 | ::aos::time::Time(msg->seconds, msg->nseconds); |
Brian Silverman | 36ee13b | 2015-03-30 01:18:14 -0400 | [diff] [blame] | 111 | if (message_time - backoff_start < kDropBackoff) { |
Brian Silverman | 75e9530 | 2015-02-22 00:43:59 -0500 | [diff] [blame] | 112 | ++dropped_messages; |
| 113 | queue->FreeMessage(msg); |
| 114 | return; |
| 115 | } |
| 116 | |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 117 | LogMessage *dropped_message = GetMessageOrDie(); |
Brian Silverman | a33af74 | 2014-03-08 12:44:12 -0800 | [diff] [blame] | 118 | internal::FillInMessageVarargs( |
| 119 | ERROR, dropped_message, |
| 120 | "%d logs starting at %" PRId32 ".%" PRId32 " dropped\n", |
Brian Silverman | 75e9530 | 2015-02-22 00:43:59 -0500 | [diff] [blame] | 121 | dropped_messages, dropped_start.sec(), dropped_start.nsec()); |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 122 | if (queue->WriteMessage(dropped_message, RawQueue::kNonBlock)) { |
| 123 | dropped_messages = 0; |
| 124 | } else { |
| 125 | // Don't even bother trying to write this message because it's not likely |
| 126 | // to work and it would be confusing to have one log in the middle of a |
| 127 | // string of failures get through. |
| 128 | ++dropped_messages; |
Brian Silverman | 75e9530 | 2015-02-22 00:43:59 -0500 | [diff] [blame] | 129 | backoff_start = message_time; |
Brian Silverman | a33af74 | 2014-03-08 12:44:12 -0800 | [diff] [blame] | 130 | queue->FreeMessage(msg); |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | } |
| 134 | if (!queue->WriteMessage(msg, RawQueue::kNonBlock)) { |
| 135 | if (dropped_messages == 0) { |
Brian Silverman | 75e9530 | 2015-02-22 00:43:59 -0500 | [diff] [blame] | 136 | dropped_start = backoff_start = |
| 137 | ::aos::time::Time(msg->seconds, msg->nseconds); |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 138 | } |
| 139 | ++dropped_messages; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 143 | } // namespace linux_code |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 144 | } // namespace logging |
| 145 | } // namespace aos |