blob: 3b5326fefb4882206d92c84f7b6693300c56bd94 [file] [log] [blame]
Brian Silverman79ec7fc2020-06-08 20:11:22 -05001#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
13namespace 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.
18class Ftrace {
19 public:
Austin Schuh27237362021-11-06 16:29:02 -070020 Ftrace();
Brian Silverman79ec7fc2020-06-08 20:11:22 -050021 ~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_