blob: 10641fb4c590ff43b8811bcbfa77cd147122af76 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <errno.h>
4#include <time.h>
5#include <string.h>
6#include <string>
7#include <unistd.h>
8#include <sys/types.h>
9#include <pwd.h>
10#include <fcntl.h>
Ben Fredricksona04c1752014-03-02 22:54:07 +000011#include <dirent.h>
Austin Schuhc5982cb2014-10-25 18:04:36 -070012#include <mntent.h>
brians343bc112013-02-10 01:53:46 +000013
14#include <map>
Brian Silverman88471dc2014-02-15 22:35:42 -080015#include <unordered_set>
brians343bc112013-02-10 01:53:46 +000016
Brian Silvermancb5da1f2015-12-05 22:19:58 -050017#include "aos/common/logging/implementations.h"
Brian Silvermanf44f1242015-12-05 20:19:48 -050018#include "aos/common/logging/binary_log_file.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -080019#include "aos/linux_code/init.h"
20#include "aos/linux_code/configuration.h"
Brian Silvermancb5da1f2015-12-05 22:19:58 -050021#include "aos/linux_code/ipc_lib/queue.h"
Brian Silverman88471dc2014-02-15 22:35:42 -080022#include "aos/common/queue_types.h"
Ben Fredricksona04c1752014-03-02 22:54:07 +000023#include "aos/common/die.h"
Brian Silverman18c2c362016-01-02 14:18:32 -080024#include "aos/common/time.h"
brians343bc112013-02-10 01:53:46 +000025
Brian Silvermanf665d692013-02-17 22:11:39 -080026namespace aos {
27namespace logging {
Brian Silverman14fd0fb2014-01-14 21:42:01 -080028namespace linux_code {
Brian Silvermanf665d692013-02-17 22:11:39 -080029namespace {
brians343bc112013-02-10 01:53:46 +000030
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050031void CheckTypeWritten(uint32_t type_id, LogFileWriter *writer,
32 ::std::unordered_set<uint32_t> *written_type_ids) {
33 if (written_type_ids->count(type_id) > 0) return;
Brian Silverman88471dc2014-02-15 22:35:42 -080034 if (MessageType::IsPrimitive(type_id)) return;
35
36 const MessageType &type = type_cache::Get(type_id);
37 for (int i = 0; i < type.number_fields; ++i) {
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050038 CheckTypeWritten(type.fields[i]->type, writer, written_type_ids);
Brian Silverman88471dc2014-02-15 22:35:42 -080039 }
40
41 char buffer[1024];
42 ssize_t size = type.Serialize(buffer, sizeof(buffer));
43 if (size == -1) {
44 LOG(WARNING, "%zu-byte buffer is too small to serialize type %s\n",
45 sizeof(buffer), type.name.c_str());
46 return;
47 }
48 LogFileMessageHeader *const output =
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050049 writer->GetWritePosition(sizeof(LogFileMessageHeader) + size);
Brian Silverman88471dc2014-02-15 22:35:42 -080050
51 output->time_sec = output->time_nsec = 0;
52 output->source = getpid();
53 output->name_size = 0;
54 output->sequence = 0;
55 output->level = FATAL;
56
57 memcpy(output + 1, buffer, size);
58 output->message_size = size;
59
60 output->type = LogFileMessageHeader::MessageType::kStructType;
61 futex_set(&output->marker);
Brian Silvermanf7780312014-02-16 17:26:15 -080062
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050063 written_type_ids->insert(type_id);
Brian Silverman88471dc2014-02-15 22:35:42 -080064}
65
Ben Fredricksona04c1752014-03-02 22:54:07 +000066void AllocateLogName(char **filename, const char *directory) {
67 int fileindex = 0;
Brian Silverman2adb1452014-05-13 08:43:38 -070068 DIR *const d = opendir(directory);
69 if (d == nullptr) {
70 PDie("could not open directory %s", directory);
71 }
72 int index = 0;
73 while (true) {
74 errno = 0;
75 struct dirent *const dir = readdir(d);
76 if (dir == nullptr) {
77 if (errno == 0) {
78 break;
79 } else {
80 PLOG(FATAL, "readdir(%p) failed", d);
81 }
82 } else {
Ben Fredricksona04c1752014-03-02 22:54:07 +000083 if (sscanf(dir->d_name, "aos_log-%d", &index) == 1) {
84 if (index >= fileindex) {
85 fileindex = index + 1;
86 }
87 }
88 }
Ben Fredricksona04c1752014-03-02 22:54:07 +000089 }
Brian Silverman2adb1452014-05-13 08:43:38 -070090 closedir(d);
Ben Fredricksona04c1752014-03-02 22:54:07 +000091
92 char previous[512];
Brian Silvermanf574e732014-03-02 17:35:04 -080093 ::std::string path = ::std::string(directory) + "/aos_log-current";
94 ssize_t len = ::readlink(path.c_str(), previous, sizeof(previous));
Ben Fredricksona04c1752014-03-02 22:54:07 +000095 if (len != -1) {
96 previous[len] = '\0';
97 } else {
Brian Silvermanf574e732014-03-02 17:35:04 -080098 previous[0] = '\0';
99 LOG(INFO, "Could not find aos_log-current\n");
100 printf("Could not find aos_log-current\n");
Ben Fredricksona04c1752014-03-02 22:54:07 +0000101 }
Brian Silverman09e85ed2014-03-15 08:26:29 -0700102 if (asprintf(filename, "%s/aos_log-%03d", directory, fileindex) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700103 PDie("couldn't create final name");
Ben Fredricksona04c1752014-03-02 22:54:07 +0000104 }
Brian Silvermanf574e732014-03-02 17:35:04 -0800105 LOG(INFO, "Created log file (aos_log-%d) in directory (%s). Previous file "
106 "was (%s).\n",
107 fileindex, directory, previous);
108 printf("Created log file (aos_log-%d) in directory (%s). Previous file was "
109 "(%s).\n",
110 fileindex, directory, previous);
Ben Fredricksona04c1752014-03-02 22:54:07 +0000111}
112
Brian Silverman9f330492015-03-01 17:37:02 -0500113#ifdef AOS_ARCHITECTURE_arm_frc
Austin Schuhc5982cb2014-10-25 18:04:36 -0700114bool FoundThumbDrive(const char *path) {
115 FILE *mnt_fp = setmntent("/etc/mtab", "r");
116 if (mnt_fp == nullptr) {
117 Die("Could not open /etc/mtab");
118 }
119
120 bool found = false;
121 struct mntent mntbuf;
122 char buf[256];
123 while (!found) {
124 struct mntent *mount_list = getmntent_r(mnt_fp, &mntbuf, buf, sizeof(buf));
125 if (mount_list == nullptr) {
126 break;
127 }
128 if (strcmp(mount_list->mnt_dir, path) == 0) {
129 found = true;
130 }
131 }
132 endmntent(mnt_fp);
133 return found;
134}
135
136bool FindDevice(char *device, size_t device_size) {
137 char test_device[10];
138 for (char i = 'a'; i < 'z'; ++i) {
139 snprintf(test_device, sizeof(test_device), "/dev/sd%c", i);
140 LOG(INFO, "Trying to access %s\n", test_device);
141 if (access(test_device, F_OK) != -1) {
142 snprintf(device, device_size, "sd%c", i);
143 return true;
144 }
145 }
146 return false;
147}
148#endif
149
Brian Silvermanab6615c2013-03-05 20:29:29 -0800150int BinaryLogReaderMain() {
Brian Silvermanf665d692013-02-17 22:11:39 -0800151 InitNRT();
brians343bc112013-02-10 01:53:46 +0000152
Brian Silverman9f330492015-03-01 17:37:02 -0500153#ifdef AOS_ARCHITECTURE_arm_frc
Austin Schuhc5982cb2014-10-25 18:04:36 -0700154 char folder[128];
155
156 {
157 char dev_name[8];
158 while (!FindDevice(dev_name, sizeof(dev_name))) {
159 LOG(INFO, "Waiting for a device\n");
160 printf("Waiting for a device\n");
161 sleep(5);
162 }
163 snprintf(folder, sizeof(folder), "/media/%s1", dev_name);
164 while (!FoundThumbDrive(folder)) {
165 LOG(INFO, "Waiting for %s\n", folder);
166 printf("Waiting for %s\n", folder);
167 sleep(1);
168 }
169 snprintf(folder, sizeof(folder), "/media/%s1/", dev_name);
170 }
171
172 if (access(folder, F_OK) == -1) {
173#else
Brian Silvermanf665d692013-02-17 22:11:39 -0800174 const char *folder = configuration::GetLoggingDirectory();
brians343bc112013-02-10 01:53:46 +0000175 if (access(folder, R_OK | W_OK) == -1) {
Austin Schuhc5982cb2014-10-25 18:04:36 -0700176#endif
brians2fdfc072013-02-26 05:35:15 +0000177 LOG(FATAL, "folder '%s' does not exist. please create it\n", folder);
brians343bc112013-02-10 01:53:46 +0000178 }
179 LOG(INFO, "logging to folder '%s'\n", folder);
180
brians343bc112013-02-10 01:53:46 +0000181 char *tmp;
Ben Fredricksona04c1752014-03-02 22:54:07 +0000182 AllocateLogName(&tmp, folder);
brians343bc112013-02-10 01:53:46 +0000183 char *tmp2;
184 if (asprintf(&tmp2, "%s/aos_log-current", folder) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700185 PLOG(WARNING, "couldn't create current symlink name");
brians343bc112013-02-10 01:53:46 +0000186 } else {
187 if (unlink(tmp2) == -1 && (errno != EROFS && errno != ENOENT)) {
Brian Silverman01be0002014-05-10 15:44:38 -0700188 LOG(WARNING, "unlink('%s') failed", tmp2);
brians343bc112013-02-10 01:53:46 +0000189 }
190 if (symlink(tmp, tmp2) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700191 PLOG(WARNING, "symlink('%s', '%s') failed", tmp, tmp2);
brians343bc112013-02-10 01:53:46 +0000192 }
193 free(tmp2);
194 }
195 int fd = open(tmp, O_SYNC | O_APPEND | O_RDWR | O_CREAT,
196 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
197 free(tmp);
198 if (fd == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700199 PLOG(FATAL, "opening file '%s' failed", tmp);
brians343bc112013-02-10 01:53:46 +0000200 }
Brian Silvermanab5ba472014-04-18 15:26:14 -0700201 LogFileWriter writer(fd);
brians343bc112013-02-10 01:53:46 +0000202
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500203 RawQueue *queue = GetLoggingQueue();
204
Brian Silvermanf5ca4d02015-03-01 16:52:24 -0500205 ::std::unordered_set<uint32_t> written_type_ids;
206 off_t clear_type_ids_cookie = 0;
207
brians343bc112013-02-10 01:53:46 +0000208 while (true) {
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500209 const LogMessage *const msg =
Brian Silverman18c2c362016-01-02 14:18:32 -0800210 static_cast<const LogMessage *>(queue->ReadMessage(RawQueue::kNonBlock));
211 if (msg == NULL) {
212 // If we've emptied the queue, then wait for a bit before starting to read
213 // again so the queue can buffer up some logs. This avoids lots of context
214 // switches and mutex contention which happens if we're constantly reading
215 // new messages as they come in.
216 static constexpr auto kSleepTime = ::aos::time::Time::InSeconds(0.1);
217 ::aos::time::SleepFor(kSleepTime);
218 continue;
219 }
brians343bc112013-02-10 01:53:46 +0000220
Brian Silverman664db1a2014-03-20 17:06:29 -0700221 const size_t raw_output_length =
Brian Silverman88471dc2014-02-15 22:35:42 -0800222 sizeof(LogFileMessageHeader) + msg->name_length + msg->message_length;
Brian Silverman664db1a2014-03-20 17:06:29 -0700223 size_t output_length = raw_output_length;
Brian Silverman88471dc2014-02-15 22:35:42 -0800224 if (msg->type == LogMessage::Type::kStruct) {
225 output_length += sizeof(msg->structure.type_id) + sizeof(uint32_t) +
226 msg->structure.string_length;
Brian Silvermanf5ca4d02015-03-01 16:52:24 -0500227 if (writer.ShouldClearSeekableData(&clear_type_ids_cookie,
228 output_length)) {
229 writer.ForceNewPage();
230 written_type_ids.clear();
231 }
232 CheckTypeWritten(msg->structure.type_id, &writer, &written_type_ids);
Brian Silverman664db1a2014-03-20 17:06:29 -0700233 } else if (msg->type == LogMessage::Type::kMatrix) {
234 output_length +=
235 sizeof(msg->matrix.type) + sizeof(uint32_t) + sizeof(uint16_t) +
236 sizeof(uint16_t) + msg->matrix.string_length;
Brian Silvermanab5ba472014-04-18 15:26:14 -0700237 CHECK(MessageType::IsPrimitive(msg->matrix.type));
Brian Silverman88471dc2014-02-15 22:35:42 -0800238 }
Brian Silverman91660632014-03-21 20:52:03 -0700239 LogFileMessageHeader *const output = writer.GetWritePosition(output_length);
brians343bc112013-02-10 01:53:46 +0000240 char *output_strings = reinterpret_cast<char *>(output) + sizeof(*output);
Brian Silverman88471dc2014-02-15 22:35:42 -0800241 output->name_size = msg->name_length;
242 output->message_size = msg->message_length;
brians343bc112013-02-10 01:53:46 +0000243 output->source = msg->source;
Brian Silvermanf665d692013-02-17 22:11:39 -0800244 output->level = msg->level;
245 output->time_sec = msg->seconds;
246 output->time_nsec = msg->nseconds;
247 output->sequence = msg->sequence;
Brian Silverman88471dc2014-02-15 22:35:42 -0800248 memcpy(output_strings, msg->name, msg->name_length);
249
250 switch (msg->type) {
251 case LogMessage::Type::kString:
252 memcpy(output_strings + msg->name_length, msg->message,
253 msg->message_length);
254 output->type = LogFileMessageHeader::MessageType::kString;
255 break;
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700256 case LogMessage::Type::kStruct: {
Brian Silverman88471dc2014-02-15 22:35:42 -0800257 char *position = output_strings + msg->name_length;
258
Brian Silverman4dd06242014-04-08 19:10:17 -0700259 memcpy(position, &msg->structure.type_id,
260 sizeof(msg->structure.type_id));
Brian Silverman88471dc2014-02-15 22:35:42 -0800261 position += sizeof(msg->structure.type_id);
262 output->message_size += sizeof(msg->structure.type_id);
263
Brian Silvermana7234c62014-03-24 20:23:25 -0700264 const uint32_t length = msg->structure.string_length;
Brian Silverman88471dc2014-02-15 22:35:42 -0800265 memcpy(position, &length, sizeof(length));
266 position += sizeof(length);
Brian Silverman4dd06242014-04-08 19:10:17 -0700267 memcpy(position, msg->structure.serialized,
268 length + msg->message_length);
Brian Silvermana7234c62014-03-24 20:23:25 -0700269 position += length + msg->message_length;
Brian Silverman88471dc2014-02-15 22:35:42 -0800270 output->message_size += sizeof(length) + length;
271
Brian Silverman88471dc2014-02-15 22:35:42 -0800272 output->type = LogFileMessageHeader::MessageType::kStruct;
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700273 } break;
274 case LogMessage::Type::kMatrix: {
275 char *position = output_strings + msg->name_length;
276
277 memcpy(position, &msg->matrix.type, sizeof(msg->matrix.type));
278 position += sizeof(msg->matrix.type);
279 output->message_size += sizeof(msg->matrix.type);
280
281 uint32_t length = msg->matrix.string_length;
282 memcpy(position, &length, sizeof(length));
283 position += sizeof(length);
Brian Silverman664db1a2014-03-20 17:06:29 -0700284 output->message_size += sizeof(length);
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700285
286 uint16_t rows = msg->matrix.rows, cols = msg->matrix.cols;
287 memcpy(position, &rows, sizeof(rows));
288 position += sizeof(rows);
289 memcpy(position, &cols, sizeof(cols));
290 position += sizeof(cols);
291 output->message_size += sizeof(rows) + sizeof(cols);
292 CHECK_EQ(msg->message_length,
293 MessageType::Sizeof(msg->matrix.type) * rows * cols);
294
Brian Silverman664db1a2014-03-20 17:06:29 -0700295 memcpy(position, msg->matrix.data, msg->message_length + length);
296 output->message_size += length;
297
298 output->type = LogFileMessageHeader::MessageType::kMatrix;
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700299 } break;
Brian Silverman88471dc2014-02-15 22:35:42 -0800300 }
301
Brian Silverman664db1a2014-03-20 17:06:29 -0700302 if (output->message_size - msg->message_length !=
303 output_length - raw_output_length) {
304 LOG(FATAL, "%zu != %zu\n", output->message_size - msg->message_length,
305 output_length - raw_output_length);
306 }
307
Brian Silvermanaf221b82013-09-01 13:57:50 -0700308 futex_set(&output->marker);
brians343bc112013-02-10 01:53:46 +0000309
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500310 queue->FreeMessage(msg);
brians343bc112013-02-10 01:53:46 +0000311 }
312
Brian Silvermanf665d692013-02-17 22:11:39 -0800313 Cleanup();
314 return 0;
315}
316
317} // namespace
Brian Silverman14fd0fb2014-01-14 21:42:01 -0800318} // namespace linux_code
Brian Silvermanf665d692013-02-17 22:11:39 -0800319} // namespace logging
320} // namespace aos
321
322int main() {
Brian Silverman14fd0fb2014-01-14 21:42:01 -0800323 return ::aos::logging::linux_code::BinaryLogReaderMain();
brians343bc112013-02-10 01:53:46 +0000324}