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