Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 1 | #include <dirent.h> |
| 2 | #include <errno.h> |
| 3 | #include <fcntl.h> |
| 4 | #include <mntent.h> |
| 5 | #include <pwd.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | #include <string.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | #include <sys/types.h> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 10 | #include <time.h> |
| 11 | #include <unistd.h> |
| 12 | #include <string> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 14 | #include <chrono> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | #include <map> |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 16 | #include <unordered_set> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 17 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 18 | #include "aos/die.h" |
| 19 | #include "aos/logging/binary_log_file.h" |
| 20 | #include "aos/logging/implementations.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 21 | #include "aos/time/time.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 22 | #include "aos/configuration.h" |
| 23 | #include "aos/init.h" |
| 24 | #include "aos/ipc_lib/queue.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 25 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 26 | namespace aos { |
| 27 | namespace logging { |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 28 | namespace linux_code { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 29 | namespace { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 30 | |
Ben Fredrickson | a04c175 | 2014-03-02 22:54:07 +0000 | [diff] [blame] | 31 | void AllocateLogName(char **filename, const char *directory) { |
| 32 | int fileindex = 0; |
Brian Silverman | 2adb145 | 2014-05-13 08:43:38 -0700 | [diff] [blame] | 33 | DIR *const d = opendir(directory); |
| 34 | if (d == nullptr) { |
| 35 | PDie("could not open directory %s", directory); |
| 36 | } |
| 37 | int index = 0; |
| 38 | while (true) { |
| 39 | errno = 0; |
| 40 | struct dirent *const dir = readdir(d); |
| 41 | if (dir == nullptr) { |
| 42 | if (errno == 0) { |
| 43 | break; |
| 44 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 45 | AOS_PLOG(FATAL, "readdir(%p) failed", d); |
Brian Silverman | 2adb145 | 2014-05-13 08:43:38 -0700 | [diff] [blame] | 46 | } |
| 47 | } else { |
Ben Fredrickson | a04c175 | 2014-03-02 22:54:07 +0000 | [diff] [blame] | 48 | if (sscanf(dir->d_name, "aos_log-%d", &index) == 1) { |
| 49 | if (index >= fileindex) { |
| 50 | fileindex = index + 1; |
| 51 | } |
| 52 | } |
| 53 | } |
Ben Fredrickson | a04c175 | 2014-03-02 22:54:07 +0000 | [diff] [blame] | 54 | } |
Brian Silverman | 2adb145 | 2014-05-13 08:43:38 -0700 | [diff] [blame] | 55 | closedir(d); |
Ben Fredrickson | a04c175 | 2014-03-02 22:54:07 +0000 | [diff] [blame] | 56 | |
| 57 | char previous[512]; |
Brian Silverman | f574e73 | 2014-03-02 17:35:04 -0800 | [diff] [blame] | 58 | ::std::string path = ::std::string(directory) + "/aos_log-current"; |
| 59 | ssize_t len = ::readlink(path.c_str(), previous, sizeof(previous)); |
Ben Fredrickson | a04c175 | 2014-03-02 22:54:07 +0000 | [diff] [blame] | 60 | if (len != -1) { |
| 61 | previous[len] = '\0'; |
| 62 | } else { |
Brian Silverman | f574e73 | 2014-03-02 17:35:04 -0800 | [diff] [blame] | 63 | previous[0] = '\0'; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 64 | AOS_LOG(INFO, "Could not find aos_log-current\n"); |
Brian Silverman | f574e73 | 2014-03-02 17:35:04 -0800 | [diff] [blame] | 65 | printf("Could not find aos_log-current\n"); |
Ben Fredrickson | a04c175 | 2014-03-02 22:54:07 +0000 | [diff] [blame] | 66 | } |
Brian Silverman | 09e85ed | 2014-03-15 08:26:29 -0700 | [diff] [blame] | 67 | if (asprintf(filename, "%s/aos_log-%03d", directory, fileindex) == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 68 | PDie("couldn't create final name"); |
Ben Fredrickson | a04c175 | 2014-03-02 22:54:07 +0000 | [diff] [blame] | 69 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 70 | AOS_LOG(INFO, |
| 71 | "Created log file (aos_log-%d) in directory (%s). Previous file " |
| 72 | "was (%s).\n", |
| 73 | fileindex, directory, previous); |
Brian Silverman | f574e73 | 2014-03-02 17:35:04 -0800 | [diff] [blame] | 74 | printf("Created log file (aos_log-%d) in directory (%s). Previous file was " |
| 75 | "(%s).\n", |
| 76 | fileindex, directory, previous); |
Ben Fredrickson | a04c175 | 2014-03-02 22:54:07 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Brian Silverman | 9f33049 | 2015-03-01 17:37:02 -0500 | [diff] [blame] | 79 | #ifdef AOS_ARCHITECTURE_arm_frc |
Austin Schuh | c5982cb | 2014-10-25 18:04:36 -0700 | [diff] [blame] | 80 | bool FoundThumbDrive(const char *path) { |
| 81 | FILE *mnt_fp = setmntent("/etc/mtab", "r"); |
| 82 | if (mnt_fp == nullptr) { |
| 83 | Die("Could not open /etc/mtab"); |
| 84 | } |
| 85 | |
| 86 | bool found = false; |
| 87 | struct mntent mntbuf; |
| 88 | char buf[256]; |
| 89 | while (!found) { |
| 90 | struct mntent *mount_list = getmntent_r(mnt_fp, &mntbuf, buf, sizeof(buf)); |
| 91 | if (mount_list == nullptr) { |
| 92 | break; |
| 93 | } |
| 94 | if (strcmp(mount_list->mnt_dir, path) == 0) { |
| 95 | found = true; |
| 96 | } |
| 97 | } |
| 98 | endmntent(mnt_fp); |
| 99 | return found; |
| 100 | } |
| 101 | |
| 102 | bool FindDevice(char *device, size_t device_size) { |
| 103 | char test_device[10]; |
| 104 | for (char i = 'a'; i < 'z'; ++i) { |
| 105 | snprintf(test_device, sizeof(test_device), "/dev/sd%c", i); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 106 | AOS_LOG(INFO, "Trying to access %s\n", test_device); |
Austin Schuh | c5982cb | 2014-10-25 18:04:36 -0700 | [diff] [blame] | 107 | if (access(test_device, F_OK) != -1) { |
| 108 | snprintf(device, device_size, "sd%c", i); |
| 109 | return true; |
| 110 | } |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | #endif |
| 115 | |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 116 | int BinaryLogReaderMain() { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 117 | InitNRT(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 118 | |
Brian Silverman | 9f33049 | 2015-03-01 17:37:02 -0500 | [diff] [blame] | 119 | #ifdef AOS_ARCHITECTURE_arm_frc |
Austin Schuh | c5982cb | 2014-10-25 18:04:36 -0700 | [diff] [blame] | 120 | char folder[128]; |
| 121 | |
| 122 | { |
| 123 | char dev_name[8]; |
| 124 | while (!FindDevice(dev_name, sizeof(dev_name))) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 125 | AOS_LOG(INFO, "Waiting for a device\n"); |
Austin Schuh | c5982cb | 2014-10-25 18:04:36 -0700 | [diff] [blame] | 126 | printf("Waiting for a device\n"); |
| 127 | sleep(5); |
| 128 | } |
| 129 | snprintf(folder, sizeof(folder), "/media/%s1", dev_name); |
| 130 | while (!FoundThumbDrive(folder)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 131 | AOS_LOG(INFO, "Waiting for %s\n", folder); |
Austin Schuh | c5982cb | 2014-10-25 18:04:36 -0700 | [diff] [blame] | 132 | printf("Waiting for %s\n", folder); |
| 133 | sleep(1); |
| 134 | } |
| 135 | snprintf(folder, sizeof(folder), "/media/%s1/", dev_name); |
| 136 | } |
| 137 | |
| 138 | if (access(folder, F_OK) == -1) { |
| 139 | #else |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 140 | const char *folder = configuration::GetLoggingDirectory(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 141 | if (access(folder, R_OK | W_OK) == -1) { |
Austin Schuh | c5982cb | 2014-10-25 18:04:36 -0700 | [diff] [blame] | 142 | #endif |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 143 | AOS_LOG(FATAL, "folder '%s' does not exist. please create it\n", folder); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 144 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 145 | AOS_LOG(INFO, "logging to folder '%s'\n", folder); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 146 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 147 | char *tmp; |
Ben Fredrickson | a04c175 | 2014-03-02 22:54:07 +0000 | [diff] [blame] | 148 | AllocateLogName(&tmp, folder); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 149 | char *tmp2; |
| 150 | if (asprintf(&tmp2, "%s/aos_log-current", folder) == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 151 | AOS_PLOG(WARNING, "couldn't create current symlink name"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 152 | } else { |
| 153 | if (unlink(tmp2) == -1 && (errno != EROFS && errno != ENOENT)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 154 | AOS_LOG(WARNING, "unlink('%s') failed", tmp2); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 155 | } |
| 156 | if (symlink(tmp, tmp2) == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 157 | AOS_PLOG(WARNING, "symlink('%s', '%s') failed", tmp, tmp2); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 158 | } |
| 159 | free(tmp2); |
| 160 | } |
| 161 | int fd = open(tmp, O_SYNC | O_APPEND | O_RDWR | O_CREAT, |
| 162 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 163 | free(tmp); |
| 164 | if (fd == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 165 | AOS_PLOG(FATAL, "opening file '%s' failed", tmp); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 166 | } |
Brian Silverman | ab5ba47 | 2014-04-18 15:26:14 -0700 | [diff] [blame] | 167 | LogFileWriter writer(fd); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 168 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 169 | RawQueue *queue = GetLoggingQueue(); |
| 170 | |
Brian Silverman | f5ca4d0 | 2015-03-01 16:52:24 -0500 | [diff] [blame] | 171 | ::std::unordered_set<uint32_t> written_type_ids; |
Brian Silverman | f5ca4d0 | 2015-03-01 16:52:24 -0500 | [diff] [blame] | 172 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 173 | while (true) { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 174 | const LogMessage *const msg = |
Brian Silverman | 18c2c36 | 2016-01-02 14:18:32 -0800 | [diff] [blame] | 175 | static_cast<const LogMessage *>(queue->ReadMessage(RawQueue::kNonBlock)); |
| 176 | if (msg == NULL) { |
| 177 | // If we've emptied the queue, then wait for a bit before starting to read |
| 178 | // again so the queue can buffer up some logs. This avoids lots of context |
| 179 | // switches and mutex contention which happens if we're constantly reading |
| 180 | // new messages as they come in. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 181 | ::std::this_thread::sleep_for(::std::chrono::milliseconds(100)); |
Brian Silverman | 18c2c36 | 2016-01-02 14:18:32 -0800 | [diff] [blame] | 182 | continue; |
| 183 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 184 | |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 185 | const size_t raw_output_length = |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 186 | sizeof(LogFileMessageHeader) + msg->name_length + msg->message_length; |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 187 | size_t output_length = raw_output_length; |
Brian Silverman | 9166063 | 2014-03-21 20:52:03 -0700 | [diff] [blame] | 188 | LogFileMessageHeader *const output = writer.GetWritePosition(output_length); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 189 | char *output_strings = reinterpret_cast<char *>(output) + sizeof(*output); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 190 | output->name_size = msg->name_length; |
| 191 | output->message_size = msg->message_length; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 192 | output->source = msg->source; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 193 | output->level = msg->level; |
| 194 | output->time_sec = msg->seconds; |
| 195 | output->time_nsec = msg->nseconds; |
| 196 | output->sequence = msg->sequence; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 197 | memcpy(output_strings, msg->name, msg->name_length); |
| 198 | |
| 199 | switch (msg->type) { |
| 200 | case LogMessage::Type::kString: |
| 201 | memcpy(output_strings + msg->name_length, msg->message, |
| 202 | msg->message_length); |
| 203 | output->type = LogFileMessageHeader::MessageType::kString; |
| 204 | break; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 207 | if (output->message_size - msg->message_length != |
| 208 | output_length - raw_output_length) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 209 | AOS_LOG(FATAL, "%zu != %zu\n", output->message_size - msg->message_length, |
| 210 | output_length - raw_output_length); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Brian Silverman | af221b8 | 2013-09-01 13:57:50 -0700 | [diff] [blame] | 213 | futex_set(&output->marker); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 214 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 215 | queue->FreeMessage(msg); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 218 | Cleanup(); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | } // namespace |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 223 | } // namespace linux_code |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 224 | } // namespace logging |
| 225 | } // namespace aos |
| 226 | |
| 227 | int main() { |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 228 | return ::aos::logging::linux_code::BinaryLogReaderMain(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 229 | } |