Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 1 | #include "aos/ftrace.h" |
| 2 | |
| 3 | #include <stdarg.h> |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | namespace aos { |
| 7 | |
| 8 | Ftrace::~Ftrace() { |
| 9 | if (message_fd_ != -1) { |
| 10 | PCHECK(close(message_fd_) == 0); |
| 11 | } |
| 12 | if (message_fd_ != -1) { |
| 13 | PCHECK(close(on_fd_) == 0); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | void Ftrace::FormatMessage(const char *format, ...) { |
| 18 | if (message_fd_ == -1) { |
| 19 | return; |
| 20 | } |
| 21 | char buffer[512]; |
| 22 | va_list ap; |
| 23 | va_start(ap, format); |
| 24 | const int result = vsnprintf(buffer, sizeof(buffer), format, ap); |
| 25 | va_end(ap); |
| 26 | CHECK_LE(static_cast<size_t>(result), sizeof(buffer)) |
| 27 | << ": Format string ended up too long: " << format; |
| 28 | WriteMessage(std::string_view(buffer, result)); |
| 29 | } |
| 30 | |
| 31 | void Ftrace::WriteMessage(std::string_view content) { |
| 32 | if (message_fd_ == -1) { |
| 33 | return; |
| 34 | } |
| 35 | const int result = write(message_fd_, content.data(), content.size()); |
| 36 | if (result == -1 && errno == EBADF) { |
| 37 | // This just means tracing is turned off. Ignore it. |
| 38 | return; |
| 39 | } |
| 40 | PCHECK(result >= 0) << ": Failed to write ftrace message: " << content; |
| 41 | CHECK_EQ(static_cast<size_t>(result), content.size()) |
| 42 | << ": Failed to write complete ftrace message: " << content; |
| 43 | } |
| 44 | |
| 45 | } // namespace aos |