Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 1 | #ifndef AOS_FTRACE_H_ |
| 2 | #define AOS_FTRACE_H_ |
| 3 | |
| 4 | #include <fcntl.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include <string_view> |
| 10 | |
| 11 | #include "glog/logging.h" |
| 12 | |
| 13 | namespace aos { |
| 14 | |
| 15 | // Manages interacting with ftrace. Silently hides many errors, because they are |
| 16 | // expected to occur when ftrace is not enabled, and we want calling code to |
| 17 | // continue working in that case. |
| 18 | class Ftrace { |
| 19 | public: |
Austin Schuh | 2723736 | 2021-11-06 16:29:02 -0700 | [diff] [blame^] | 20 | Ftrace(); |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 21 | ~Ftrace(); |
| 22 | |
| 23 | // Writes a message with a printf-style format. |
| 24 | // |
| 25 | // Silently does nothing if tracing is disabled. |
| 26 | void FormatMessage(const char *format, ...) |
| 27 | __attribute__((format(__printf__, 2, 3))); |
| 28 | |
| 29 | // Writes a preformatted message. |
| 30 | // |
| 31 | // Silently does nothing if tracing is disabled. |
| 32 | void WriteMessage(std::string_view content); |
| 33 | |
| 34 | // Turns tracing off, or CHECK-fails if tracing is inaccessible. Does nothing |
| 35 | // if tracing is currently available but off. |
| 36 | void TurnOffOrDie() { |
| 37 | CHECK(on_fd_ != -1) |
| 38 | << ": Failed to open tracing_on earlier, cannot turn off tracing"; |
| 39 | char zero = '0'; |
| 40 | CHECK_EQ(write(on_fd_, &zero, 1), 1) << ": Failed to turn tracing off"; |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | int message_fd_, on_fd_; |
| 45 | }; |
| 46 | |
| 47 | } // namespace aos |
| 48 | |
| 49 | #endif // AOS_FTRACE_H_ |