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" |
| 16 | #include "aos/common/logging/logging_impl.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 | a33af74 | 2014-03-08 12:44:12 -0800 | [diff] [blame] | 28 | int32_t dropped_start_seconds, dropped_start_nseconds; |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 29 | |
| 30 | LogMessage *GetMessageOrDie() { |
| 31 | LogMessage *message = static_cast<LogMessage *>(queue->GetMessage()); |
| 32 | if (message == NULL) { |
| 33 | LOG(FATAL, "%p->GetMessage() failed\n", queue); |
| 34 | } else { |
| 35 | return message; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | class LinuxQueueLogImplementation : public LogImplementation { |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 40 | virtual void DoLog(log_level level, const char *format, va_list ap) override { |
| 41 | LogMessage *message = GetMessageOrDie(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 42 | internal::FillInMessage(level, format, ap, message); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 43 | Write(message); |
| 44 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 45 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 46 | virtual void LogStruct(log_level level, const ::std::string &message_string, |
| 47 | size_t size, const MessageType *type, |
| 48 | const ::std::function<size_t(char *)> &serialize) |
| 49 | override { |
| 50 | LogMessage *message = GetMessageOrDie(); |
| 51 | internal::FillInMessageStructure(level, message_string, size, type, |
| 52 | serialize, message); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 53 | Write(message); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | } // namespace |
| 58 | |
| 59 | void Register() { |
| 60 | Init(); |
| 61 | |
Brian Silverman | a70144e | 2014-03-01 22:38:54 -0800 | [diff] [blame] | 62 | queue = RawQueue::Fetch("LoggingQueue", sizeof(LogMessage), 1323, 80000); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 63 | if (queue == NULL) { |
| 64 | Die("logging: couldn't fetch queue\n"); |
| 65 | } |
| 66 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 67 | AddImplementation(new LinuxQueueLogImplementation()); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | const LogMessage *ReadNext(int flags, int *index) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 71 | return static_cast<const LogMessage *>(queue->ReadMessageIndex(flags, index)); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | const LogMessage *ReadNext() { |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 75 | return ReadNext(RawQueue::kBlock); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | const LogMessage *ReadNext(int flags) { |
| 79 | const LogMessage *r = NULL; |
| 80 | do { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 81 | r = static_cast<const LogMessage *>(queue->ReadMessage(flags)); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 82 | // not blocking means return a NULL if that's what it gets |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 83 | } while ((flags & RawQueue::kBlock) && r == NULL); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 84 | return r; |
| 85 | } |
| 86 | |
| 87 | LogMessage *Get() { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 88 | return static_cast<LogMessage *>(queue->GetMessage()); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void Free(const LogMessage *msg) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 92 | queue->FreeMessage(msg); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void Write(LogMessage *msg) { |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 96 | if (__builtin_expect(dropped_messages > 0, 0)) { |
| 97 | LogMessage *dropped_message = GetMessageOrDie(); |
Brian Silverman | a33af74 | 2014-03-08 12:44:12 -0800 | [diff] [blame] | 98 | internal::FillInMessageVarargs( |
| 99 | ERROR, dropped_message, |
| 100 | "%d logs starting at %" PRId32 ".%" PRId32 " dropped\n", |
| 101 | dropped_messages, dropped_start_seconds, dropped_start_nseconds); |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 102 | if (queue->WriteMessage(dropped_message, RawQueue::kNonBlock)) { |
| 103 | dropped_messages = 0; |
Brian Silverman | a33af74 | 2014-03-08 12:44:12 -0800 | [diff] [blame] | 104 | internal::PrintMessage(stderr, *dropped_message); |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 105 | } else { |
| 106 | // Don't even bother trying to write this message because it's not likely |
| 107 | // to work and it would be confusing to have one log in the middle of a |
| 108 | // string of failures get through. |
| 109 | ++dropped_messages; |
Brian Silverman | a33af74 | 2014-03-08 12:44:12 -0800 | [diff] [blame] | 110 | queue->FreeMessage(msg); |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | } |
| 114 | if (!queue->WriteMessage(msg, RawQueue::kNonBlock)) { |
| 115 | if (dropped_messages == 0) { |
Brian Silverman | a33af74 | 2014-03-08 12:44:12 -0800 | [diff] [blame] | 116 | dropped_start_seconds = msg->seconds; |
| 117 | dropped_start_nseconds = msg->nseconds; |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 118 | } |
| 119 | ++dropped_messages; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 123 | } // namespace linux_code |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 124 | } // namespace logging |
| 125 | } // namespace aos |