blob: d4e668539d1b51e36f2469798eda39a932226622 [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 Silvermanf44f1242015-12-05 20:19:48 -050017#include "aos/common/logging/linux_logging.h"
18#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 Silverman88471dc2014-02-15 22:35:42 -080021#include "aos/common/queue_types.h"
Ben Fredricksona04c1752014-03-02 22:54:07 +000022#include "aos/common/die.h"
brians343bc112013-02-10 01:53:46 +000023
Brian Silvermanf665d692013-02-17 22:11:39 -080024namespace aos {
25namespace logging {
Brian Silverman14fd0fb2014-01-14 21:42:01 -080026namespace linux_code {
Brian Silvermanf665d692013-02-17 22:11:39 -080027namespace {
brians343bc112013-02-10 01:53:46 +000028
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050029void CheckTypeWritten(uint32_t type_id, LogFileWriter *writer,
30 ::std::unordered_set<uint32_t> *written_type_ids) {
31 if (written_type_ids->count(type_id) > 0) return;
Brian Silverman88471dc2014-02-15 22:35:42 -080032 if (MessageType::IsPrimitive(type_id)) return;
33
34 const MessageType &type = type_cache::Get(type_id);
35 for (int i = 0; i < type.number_fields; ++i) {
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050036 CheckTypeWritten(type.fields[i]->type, writer, written_type_ids);
Brian Silverman88471dc2014-02-15 22:35:42 -080037 }
38
39 char buffer[1024];
40 ssize_t size = type.Serialize(buffer, sizeof(buffer));
41 if (size == -1) {
42 LOG(WARNING, "%zu-byte buffer is too small to serialize type %s\n",
43 sizeof(buffer), type.name.c_str());
44 return;
45 }
46 LogFileMessageHeader *const output =
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050047 writer->GetWritePosition(sizeof(LogFileMessageHeader) + size);
Brian Silverman88471dc2014-02-15 22:35:42 -080048
49 output->time_sec = output->time_nsec = 0;
50 output->source = getpid();
51 output->name_size = 0;
52 output->sequence = 0;
53 output->level = FATAL;
54
55 memcpy(output + 1, buffer, size);
56 output->message_size = size;
57
58 output->type = LogFileMessageHeader::MessageType::kStructType;
59 futex_set(&output->marker);
Brian Silvermanf7780312014-02-16 17:26:15 -080060
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050061 written_type_ids->insert(type_id);
Brian Silverman88471dc2014-02-15 22:35:42 -080062}
63
Ben Fredricksona04c1752014-03-02 22:54:07 +000064void AllocateLogName(char **filename, const char *directory) {
65 int fileindex = 0;
Brian Silverman2adb1452014-05-13 08:43:38 -070066 DIR *const d = opendir(directory);
67 if (d == nullptr) {
68 PDie("could not open directory %s", directory);
69 }
70 int index = 0;
71 while (true) {
72 errno = 0;
73 struct dirent *const dir = readdir(d);
74 if (dir == nullptr) {
75 if (errno == 0) {
76 break;
77 } else {
78 PLOG(FATAL, "readdir(%p) failed", d);
79 }
80 } else {
Ben Fredricksona04c1752014-03-02 22:54:07 +000081 if (sscanf(dir->d_name, "aos_log-%d", &index) == 1) {
82 if (index >= fileindex) {
83 fileindex = index + 1;
84 }
85 }
86 }
Ben Fredricksona04c1752014-03-02 22:54:07 +000087 }
Brian Silverman2adb1452014-05-13 08:43:38 -070088 closedir(d);
Ben Fredricksona04c1752014-03-02 22:54:07 +000089
90 char previous[512];
Brian Silvermanf574e732014-03-02 17:35:04 -080091 ::std::string path = ::std::string(directory) + "/aos_log-current";
92 ssize_t len = ::readlink(path.c_str(), previous, sizeof(previous));
Ben Fredricksona04c1752014-03-02 22:54:07 +000093 if (len != -1) {
94 previous[len] = '\0';
95 } else {
Brian Silvermanf574e732014-03-02 17:35:04 -080096 previous[0] = '\0';
97 LOG(INFO, "Could not find aos_log-current\n");
98 printf("Could not find aos_log-current\n");
Ben Fredricksona04c1752014-03-02 22:54:07 +000099 }
Brian Silverman09e85ed2014-03-15 08:26:29 -0700100 if (asprintf(filename, "%s/aos_log-%03d", directory, fileindex) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700101 PDie("couldn't create final name");
Ben Fredricksona04c1752014-03-02 22:54:07 +0000102 }
Brian Silvermanf574e732014-03-02 17:35:04 -0800103 LOG(INFO, "Created log file (aos_log-%d) in directory (%s). Previous file "
104 "was (%s).\n",
105 fileindex, directory, previous);
106 printf("Created log file (aos_log-%d) in directory (%s). Previous file was "
107 "(%s).\n",
108 fileindex, directory, previous);
Ben Fredricksona04c1752014-03-02 22:54:07 +0000109}
110
Brian Silverman9f330492015-03-01 17:37:02 -0500111#ifdef AOS_ARCHITECTURE_arm_frc
Austin Schuhc5982cb2014-10-25 18:04:36 -0700112bool FoundThumbDrive(const char *path) {
113 FILE *mnt_fp = setmntent("/etc/mtab", "r");
114 if (mnt_fp == nullptr) {
115 Die("Could not open /etc/mtab");
116 }
117
118 bool found = false;
119 struct mntent mntbuf;
120 char buf[256];
121 while (!found) {
122 struct mntent *mount_list = getmntent_r(mnt_fp, &mntbuf, buf, sizeof(buf));
123 if (mount_list == nullptr) {
124 break;
125 }
126 if (strcmp(mount_list->mnt_dir, path) == 0) {
127 found = true;
128 }
129 }
130 endmntent(mnt_fp);
131 return found;
132}
133
134bool FindDevice(char *device, size_t device_size) {
135 char test_device[10];
136 for (char i = 'a'; i < 'z'; ++i) {
137 snprintf(test_device, sizeof(test_device), "/dev/sd%c", i);
138 LOG(INFO, "Trying to access %s\n", test_device);
139 if (access(test_device, F_OK) != -1) {
140 snprintf(device, device_size, "sd%c", i);
141 return true;
142 }
143 }
144 return false;
145}
146#endif
147
Brian Silvermanab6615c2013-03-05 20:29:29 -0800148int BinaryLogReaderMain() {
Brian Silvermanf665d692013-02-17 22:11:39 -0800149 InitNRT();
brians343bc112013-02-10 01:53:46 +0000150
Brian Silverman9f330492015-03-01 17:37:02 -0500151#ifdef AOS_ARCHITECTURE_arm_frc
Austin Schuhc5982cb2014-10-25 18:04:36 -0700152 char folder[128];
153
154 {
155 char dev_name[8];
156 while (!FindDevice(dev_name, sizeof(dev_name))) {
157 LOG(INFO, "Waiting for a device\n");
158 printf("Waiting for a device\n");
159 sleep(5);
160 }
161 snprintf(folder, sizeof(folder), "/media/%s1", dev_name);
162 while (!FoundThumbDrive(folder)) {
163 LOG(INFO, "Waiting for %s\n", folder);
164 printf("Waiting for %s\n", folder);
165 sleep(1);
166 }
167 snprintf(folder, sizeof(folder), "/media/%s1/", dev_name);
168 }
169
170 if (access(folder, F_OK) == -1) {
171#else
Brian Silvermanf665d692013-02-17 22:11:39 -0800172 const char *folder = configuration::GetLoggingDirectory();
brians343bc112013-02-10 01:53:46 +0000173 if (access(folder, R_OK | W_OK) == -1) {
Austin Schuhc5982cb2014-10-25 18:04:36 -0700174#endif
brians2fdfc072013-02-26 05:35:15 +0000175 LOG(FATAL, "folder '%s' does not exist. please create it\n", folder);
brians343bc112013-02-10 01:53:46 +0000176 }
177 LOG(INFO, "logging to folder '%s'\n", folder);
178
brians343bc112013-02-10 01:53:46 +0000179 char *tmp;
Ben Fredricksona04c1752014-03-02 22:54:07 +0000180 AllocateLogName(&tmp, folder);
brians343bc112013-02-10 01:53:46 +0000181 char *tmp2;
182 if (asprintf(&tmp2, "%s/aos_log-current", folder) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700183 PLOG(WARNING, "couldn't create current symlink name");
brians343bc112013-02-10 01:53:46 +0000184 } else {
185 if (unlink(tmp2) == -1 && (errno != EROFS && errno != ENOENT)) {
Brian Silverman01be0002014-05-10 15:44:38 -0700186 LOG(WARNING, "unlink('%s') failed", tmp2);
brians343bc112013-02-10 01:53:46 +0000187 }
188 if (symlink(tmp, tmp2) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700189 PLOG(WARNING, "symlink('%s', '%s') failed", tmp, tmp2);
brians343bc112013-02-10 01:53:46 +0000190 }
191 free(tmp2);
192 }
193 int fd = open(tmp, O_SYNC | O_APPEND | O_RDWR | O_CREAT,
194 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
195 free(tmp);
196 if (fd == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700197 PLOG(FATAL, "opening file '%s' failed", tmp);
brians343bc112013-02-10 01:53:46 +0000198 }
Brian Silvermanab5ba472014-04-18 15:26:14 -0700199 LogFileWriter writer(fd);
brians343bc112013-02-10 01:53:46 +0000200
Brian Silvermanf5ca4d02015-03-01 16:52:24 -0500201 ::std::unordered_set<uint32_t> written_type_ids;
202 off_t clear_type_ids_cookie = 0;
203
brians343bc112013-02-10 01:53:46 +0000204 while (true) {
Brian Silvermanf665d692013-02-17 22:11:39 -0800205 const LogMessage *const msg = ReadNext();
brians343bc112013-02-10 01:53:46 +0000206 if (msg == NULL) continue;
brians343bc112013-02-10 01:53:46 +0000207
Brian Silverman664db1a2014-03-20 17:06:29 -0700208 const size_t raw_output_length =
Brian Silverman88471dc2014-02-15 22:35:42 -0800209 sizeof(LogFileMessageHeader) + msg->name_length + msg->message_length;
Brian Silverman664db1a2014-03-20 17:06:29 -0700210 size_t output_length = raw_output_length;
Brian Silverman88471dc2014-02-15 22:35:42 -0800211 if (msg->type == LogMessage::Type::kStruct) {
212 output_length += sizeof(msg->structure.type_id) + sizeof(uint32_t) +
213 msg->structure.string_length;
Brian Silvermanf5ca4d02015-03-01 16:52:24 -0500214 if (writer.ShouldClearSeekableData(&clear_type_ids_cookie,
215 output_length)) {
216 writer.ForceNewPage();
217 written_type_ids.clear();
218 }
219 CheckTypeWritten(msg->structure.type_id, &writer, &written_type_ids);
Brian Silverman664db1a2014-03-20 17:06:29 -0700220 } else if (msg->type == LogMessage::Type::kMatrix) {
221 output_length +=
222 sizeof(msg->matrix.type) + sizeof(uint32_t) + sizeof(uint16_t) +
223 sizeof(uint16_t) + msg->matrix.string_length;
Brian Silvermanab5ba472014-04-18 15:26:14 -0700224 CHECK(MessageType::IsPrimitive(msg->matrix.type));
Brian Silverman88471dc2014-02-15 22:35:42 -0800225 }
Brian Silverman91660632014-03-21 20:52:03 -0700226 LogFileMessageHeader *const output = writer.GetWritePosition(output_length);
brians343bc112013-02-10 01:53:46 +0000227 char *output_strings = reinterpret_cast<char *>(output) + sizeof(*output);
Brian Silverman88471dc2014-02-15 22:35:42 -0800228 output->name_size = msg->name_length;
229 output->message_size = msg->message_length;
brians343bc112013-02-10 01:53:46 +0000230 output->source = msg->source;
Brian Silvermanf665d692013-02-17 22:11:39 -0800231 output->level = msg->level;
232 output->time_sec = msg->seconds;
233 output->time_nsec = msg->nseconds;
234 output->sequence = msg->sequence;
Brian Silverman88471dc2014-02-15 22:35:42 -0800235 memcpy(output_strings, msg->name, msg->name_length);
236
237 switch (msg->type) {
238 case LogMessage::Type::kString:
239 memcpy(output_strings + msg->name_length, msg->message,
240 msg->message_length);
241 output->type = LogFileMessageHeader::MessageType::kString;
242 break;
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700243 case LogMessage::Type::kStruct: {
Brian Silverman88471dc2014-02-15 22:35:42 -0800244 char *position = output_strings + msg->name_length;
245
Brian Silverman4dd06242014-04-08 19:10:17 -0700246 memcpy(position, &msg->structure.type_id,
247 sizeof(msg->structure.type_id));
Brian Silverman88471dc2014-02-15 22:35:42 -0800248 position += sizeof(msg->structure.type_id);
249 output->message_size += sizeof(msg->structure.type_id);
250
Brian Silvermana7234c62014-03-24 20:23:25 -0700251 const uint32_t length = msg->structure.string_length;
Brian Silverman88471dc2014-02-15 22:35:42 -0800252 memcpy(position, &length, sizeof(length));
253 position += sizeof(length);
Brian Silverman4dd06242014-04-08 19:10:17 -0700254 memcpy(position, msg->structure.serialized,
255 length + msg->message_length);
Brian Silvermana7234c62014-03-24 20:23:25 -0700256 position += length + msg->message_length;
Brian Silverman88471dc2014-02-15 22:35:42 -0800257 output->message_size += sizeof(length) + length;
258
Brian Silverman88471dc2014-02-15 22:35:42 -0800259 output->type = LogFileMessageHeader::MessageType::kStruct;
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700260 } break;
261 case LogMessage::Type::kMatrix: {
262 char *position = output_strings + msg->name_length;
263
264 memcpy(position, &msg->matrix.type, sizeof(msg->matrix.type));
265 position += sizeof(msg->matrix.type);
266 output->message_size += sizeof(msg->matrix.type);
267
268 uint32_t length = msg->matrix.string_length;
269 memcpy(position, &length, sizeof(length));
270 position += sizeof(length);
Brian Silverman664db1a2014-03-20 17:06:29 -0700271 output->message_size += sizeof(length);
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700272
273 uint16_t rows = msg->matrix.rows, cols = msg->matrix.cols;
274 memcpy(position, &rows, sizeof(rows));
275 position += sizeof(rows);
276 memcpy(position, &cols, sizeof(cols));
277 position += sizeof(cols);
278 output->message_size += sizeof(rows) + sizeof(cols);
279 CHECK_EQ(msg->message_length,
280 MessageType::Sizeof(msg->matrix.type) * rows * cols);
281
Brian Silverman664db1a2014-03-20 17:06:29 -0700282 memcpy(position, msg->matrix.data, msg->message_length + length);
283 output->message_size += length;
284
285 output->type = LogFileMessageHeader::MessageType::kMatrix;
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700286 } break;
Brian Silverman88471dc2014-02-15 22:35:42 -0800287 }
288
Brian Silverman664db1a2014-03-20 17:06:29 -0700289 if (output->message_size - msg->message_length !=
290 output_length - raw_output_length) {
291 LOG(FATAL, "%zu != %zu\n", output->message_size - msg->message_length,
292 output_length - raw_output_length);
293 }
294
Brian Silvermanaf221b82013-09-01 13:57:50 -0700295 futex_set(&output->marker);
brians343bc112013-02-10 01:53:46 +0000296
Brian Silverman14fd0fb2014-01-14 21:42:01 -0800297 logging::linux_code::Free(msg);
brians343bc112013-02-10 01:53:46 +0000298 }
299
Brian Silvermanf665d692013-02-17 22:11:39 -0800300 Cleanup();
301 return 0;
302}
303
304} // namespace
Brian Silverman14fd0fb2014-01-14 21:42:01 -0800305} // namespace linux_code
Brian Silvermanf665d692013-02-17 22:11:39 -0800306} // namespace logging
307} // namespace aos
308
309int main() {
Brian Silverman14fd0fb2014-01-14 21:42:01 -0800310 return ::aos::logging::linux_code::BinaryLogReaderMain();
brians343bc112013-02-10 01:53:46 +0000311}