blob: 08a65cba4c5e5d78f8ec798585972b6e16769491 [file] [log] [blame]
Brian Silvermanf665d692013-02-17 22:11:39 -08001#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
20namespace aos {
21namespace logging {
22namespace {
23
24using internal::Context;
25
26AOS_THREAD_LOCAL Context *my_context(NULL);
27
Brian Silvermanab6615c2013-03-05 20:29:29 -080028::std::string GetMyName() {
Brian Silvermanf665d692013-02-17 22:11:39 -080029 // 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 Silvermanab6615c2013-03-05 20:29:29 -080034 ::std::string process_name(program_invocation_short_name);
Brian Silvermanf665d692013-02-17 22:11:39 -080035
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 Silvermanab6615c2013-03-05 20:29:29 -080042 ::std::string thread_name(thread_name_array);
Brian Silvermanf665d692013-02-17 22:11:39 -080043
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 Silvermanab6615c2013-03-05 20:29:29 -080048 ::std::min(thread_name.length(), process_name.length())) == 0) {
Brian Silvermanf665d692013-02-17 22:11:39 -080049 // This thread doesn't have an actual name.
50 return process_name;
51 }
52
53 return process_name + '.' + thread_name;
54}
55
Brian Silvermanab6615c2013-03-05 20:29:29 -080056static const aos_type_sig message_sig = {sizeof(LogMessage), 1323, 1500};
Brian Silvermanf665d692013-02-17 22:11:39 -080057static aos_queue *queue;
58
59} // namespace
60namespace internal {
61
62Context *Context::Get() {
63 if (my_context == NULL) {
64 my_context = new Context();
Brian Silvermanab6615c2013-03-05 20:29:29 -080065 my_context->name = GetMyName();
66 if (my_context->name.size() + 1 > sizeof(LogMessage::name)) {
67 Die("logging: process/thread name '%s' is too long\n",
68 my_context->name.c_str());
69 }
Brian Silvermanf665d692013-02-17 22:11:39 -080070 my_context->source = getpid();
71 }
72 return my_context;
73}
74
75void Context::Delete() {
76 delete my_context;
77 my_context = NULL;
78}
79
80} // namespace internal
81namespace atom {
82namespace {
83
Brian Silvermanab6615c2013-03-05 20:29:29 -080084class AtomQueueLogImplementation : public LogImplementation {
Brian Silvermanf665d692013-02-17 22:11:39 -080085 virtual void DoLog(log_level level, const char *format, va_list ap) {
86 LogMessage *message = static_cast<LogMessage *>(aos_queue_get_msg(queue));
87 if (message == NULL) {
88 LOG(FATAL, "queue get message failed\n");
89 }
90
91 internal::FillInMessage(level, format, ap, message);
92
93 Write(message);
94 }
95};
96
97} // namespace
98
99void Register() {
100 Init();
101
102 queue = aos_fetch_queue("LoggingQueue", &message_sig);
103 if (queue == NULL) {
104 Die("logging: couldn't fetch queue\n");
105 }
106
Brian Silvermanab6615c2013-03-05 20:29:29 -0800107 AddImplementation(new AtomQueueLogImplementation());
Brian Silvermanf665d692013-02-17 22:11:39 -0800108}
109
110const LogMessage *ReadNext(int flags, int *index) {
111 return static_cast<const LogMessage *>(
112 aos_queue_read_msg_index(queue, flags, index));
113}
114
115const LogMessage *ReadNext() {
116 return ReadNext(BLOCK);
117}
118
119const LogMessage *ReadNext(int flags) {
120 const LogMessage *r = NULL;
121 do {
122 r = static_cast<const LogMessage *>(aos_queue_read_msg(queue, flags));
123 // not blocking means return a NULL if that's what it gets
124 } while ((flags & BLOCK) && r == NULL);
125 return r;
126}
127
128LogMessage *Get() {
129 return static_cast<LogMessage *>(aos_queue_get_msg(queue));
130}
131
132void Free(const LogMessage *msg) {
133 aos_queue_free_msg(queue, msg);
134}
135
136void Write(LogMessage *msg) {
137 if (aos_queue_write_msg_free(queue, msg, OVERRIDE) < 0) {
138 LOG(FATAL, "writing failed");
139 }
140}
141
142} // namespace atom
143} // namespace logging
144} // namespace aos