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" |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame^] | 21 | #include "aos/logging/log_namer.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 22 | #include "aos/time/time.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 23 | #include "aos/configuration.h" |
| 24 | #include "aos/init.h" |
| 25 | #include "aos/ipc_lib/queue.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 26 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 27 | namespace aos { |
| 28 | namespace logging { |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 29 | namespace linux_code { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 30 | namespace { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 31 | |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 32 | int BinaryLogReaderMain() { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 33 | InitNRT(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 34 | |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame^] | 35 | const std::string file_name = GetLogName("aos_log"); |
| 36 | int fd = open(file_name.c_str(), O_SYNC | O_APPEND | O_RDWR | O_CREAT, |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 37 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 38 | if (fd == -1) { |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame^] | 39 | AOS_PLOG(FATAL, "opening file '%s' failed", file_name.c_str()); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 40 | } |
Brian Silverman | ab5ba47 | 2014-04-18 15:26:14 -0700 | [diff] [blame] | 41 | LogFileWriter writer(fd); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 42 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 43 | RawQueue *queue = GetLoggingQueue(); |
| 44 | |
Brian Silverman | f5ca4d0 | 2015-03-01 16:52:24 -0500 | [diff] [blame] | 45 | ::std::unordered_set<uint32_t> written_type_ids; |
Brian Silverman | f5ca4d0 | 2015-03-01 16:52:24 -0500 | [diff] [blame] | 46 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 47 | while (true) { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 48 | const LogMessage *const msg = |
Brian Silverman | 18c2c36 | 2016-01-02 14:18:32 -0800 | [diff] [blame] | 49 | static_cast<const LogMessage *>(queue->ReadMessage(RawQueue::kNonBlock)); |
| 50 | if (msg == NULL) { |
| 51 | // If we've emptied the queue, then wait for a bit before starting to read |
| 52 | // again so the queue can buffer up some logs. This avoids lots of context |
| 53 | // switches and mutex contention which happens if we're constantly reading |
| 54 | // new messages as they come in. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 55 | ::std::this_thread::sleep_for(::std::chrono::milliseconds(100)); |
Brian Silverman | 18c2c36 | 2016-01-02 14:18:32 -0800 | [diff] [blame] | 56 | continue; |
| 57 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 58 | |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 59 | const size_t raw_output_length = |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 60 | sizeof(LogFileMessageHeader) + msg->name_length + msg->message_length; |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 61 | size_t output_length = raw_output_length; |
Brian Silverman | 9166063 | 2014-03-21 20:52:03 -0700 | [diff] [blame] | 62 | LogFileMessageHeader *const output = writer.GetWritePosition(output_length); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 63 | char *output_strings = reinterpret_cast<char *>(output) + sizeof(*output); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 64 | output->name_size = msg->name_length; |
| 65 | output->message_size = msg->message_length; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 66 | output->source = msg->source; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 67 | output->level = msg->level; |
| 68 | output->time_sec = msg->seconds; |
| 69 | output->time_nsec = msg->nseconds; |
| 70 | output->sequence = msg->sequence; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 71 | memcpy(output_strings, msg->name, msg->name_length); |
| 72 | |
| 73 | switch (msg->type) { |
| 74 | case LogMessage::Type::kString: |
| 75 | memcpy(output_strings + msg->name_length, msg->message, |
| 76 | msg->message_length); |
| 77 | output->type = LogFileMessageHeader::MessageType::kString; |
| 78 | break; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 81 | if (output->message_size - msg->message_length != |
| 82 | output_length - raw_output_length) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 83 | AOS_LOG(FATAL, "%zu != %zu\n", output->message_size - msg->message_length, |
| 84 | output_length - raw_output_length); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Brian Silverman | af221b8 | 2013-09-01 13:57:50 -0700 | [diff] [blame] | 87 | futex_set(&output->marker); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 88 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 89 | queue->FreeMessage(msg); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 92 | Cleanup(); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | } // namespace |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 97 | } // namespace linux_code |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 98 | } // namespace logging |
| 99 | } // namespace aos |
| 100 | |
| 101 | int main() { |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 102 | return ::aos::logging::linux_code::BinaryLogReaderMain(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 103 | } |