Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 1 | #include "aos/atom_code/logging/atom_logging.h" |
| 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> |
| 11 | #include <sys/prctl.h> |
| 12 | |
| 13 | #include <algorithm> |
| 14 | |
| 15 | #include "aos/common/die.h" |
| 16 | #include "aos/common/logging/logging_impl.h" |
| 17 | #include "aos/atom_code/thread_local.h" |
| 18 | #include "aos/atom_code/ipc_lib/queue.h" |
| 19 | |
| 20 | namespace aos { |
| 21 | namespace logging { |
| 22 | namespace { |
| 23 | |
| 24 | using internal::Context; |
| 25 | |
| 26 | AOS_THREAD_LOCAL Context *my_context(NULL); |
| 27 | |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 28 | ::std::string GetMyName() { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 29 | // The maximum number of characters that can make up a thread name. |
| 30 | // The docs are unclear if it can be 16 characters with no '\0', so we'll be |
| 31 | // safe by adding our own where necessary. |
| 32 | static const size_t kThreadNameLength = 16; |
| 33 | |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 34 | ::std::string process_name(program_invocation_short_name); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 35 | |
| 36 | char thread_name_array[kThreadNameLength + 1]; |
| 37 | if (prctl(PR_GET_NAME, thread_name_array) != 0) { |
| 38 | Die("prctl(PR_GET_NAME, %p) failed with %d: %s\n", |
| 39 | thread_name_array, errno, strerror(errno)); |
| 40 | } |
| 41 | thread_name_array[sizeof(thread_name_array) - 1] = '\0'; |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 42 | ::std::string thread_name(thread_name_array); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 43 | |
| 44 | // If the first bunch of characters are the same. |
| 45 | // We cut off comparing at the shorter of the 2 strings because one or the |
| 46 | // other often ends up cut off. |
| 47 | if (strncmp(thread_name.c_str(), process_name.c_str(), |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 48 | ::std::min(thread_name.length(), process_name.length())) == 0) { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 49 | // This thread doesn't have an actual name. |
| 50 | return process_name; |
| 51 | } |
| 52 | |
| 53 | return process_name + '.' + thread_name; |
| 54 | } |
| 55 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame^] | 56 | RawQueue *queue; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 57 | |
| 58 | } // namespace |
| 59 | namespace internal { |
| 60 | |
| 61 | Context *Context::Get() { |
| 62 | if (my_context == NULL) { |
| 63 | my_context = new Context(); |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 64 | my_context->name = GetMyName(); |
| 65 | if (my_context->name.size() + 1 > sizeof(LogMessage::name)) { |
| 66 | Die("logging: process/thread name '%s' is too long\n", |
| 67 | my_context->name.c_str()); |
| 68 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 69 | my_context->source = getpid(); |
| 70 | } |
| 71 | return my_context; |
| 72 | } |
| 73 | |
| 74 | void Context::Delete() { |
| 75 | delete my_context; |
| 76 | my_context = NULL; |
| 77 | } |
| 78 | |
| 79 | } // namespace internal |
| 80 | namespace atom { |
| 81 | namespace { |
| 82 | |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 83 | class AtomQueueLogImplementation : public LogImplementation { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 84 | virtual void DoLog(log_level level, const char *format, va_list ap) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 85 | LogMessage *message = static_cast<LogMessage *>(queue->GetMessage()); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 86 | if (message == NULL) { |
| 87 | LOG(FATAL, "queue get message failed\n"); |
| 88 | } |
| 89 | |
| 90 | internal::FillInMessage(level, format, ap, message); |
| 91 | |
| 92 | Write(message); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | } // namespace |
| 97 | |
| 98 | void Register() { |
| 99 | Init(); |
| 100 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame^] | 101 | queue = RawQueue::Fetch("LoggingQueue", sizeof(LogMessage), 1323, 1500); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 102 | if (queue == NULL) { |
| 103 | Die("logging: couldn't fetch queue\n"); |
| 104 | } |
| 105 | |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 106 | AddImplementation(new AtomQueueLogImplementation()); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | const LogMessage *ReadNext(int flags, int *index) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 110 | return static_cast<const LogMessage *>(queue->ReadMessageIndex(flags, index)); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | const LogMessage *ReadNext() { |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame^] | 114 | return ReadNext(RawQueue::kBlock); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | const LogMessage *ReadNext(int flags) { |
| 118 | const LogMessage *r = NULL; |
| 119 | do { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 120 | r = static_cast<const LogMessage *>(queue->ReadMessage(flags)); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 121 | // not blocking means return a NULL if that's what it gets |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame^] | 122 | } while ((flags & RawQueue::kBlock) && r == NULL); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 123 | return r; |
| 124 | } |
| 125 | |
| 126 | LogMessage *Get() { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 127 | return static_cast<LogMessage *>(queue->GetMessage()); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void Free(const LogMessage *msg) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 131 | queue->FreeMessage(msg); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void Write(LogMessage *msg) { |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame^] | 135 | if (!queue->WriteMessage(msg, RawQueue::kOverride)) { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 136 | LOG(FATAL, "writing failed"); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } // namespace atom |
| 141 | } // namespace logging |
| 142 | } // namespace aos |