1 | #pragma once |
2 | |
3 | #include <fstream> |
4 | |
5 | #include "taichi/common/core.h" |
6 | |
7 | namespace taichi { |
8 | |
9 | struct ActionArg { |
10 | ActionArg(const std::string &key, const std::string &val) |
11 | : key(key), val_str(val), type(argument_type::str) { |
12 | } |
13 | |
14 | ActionArg(const std::string &key, int64 val) |
15 | : key(key), val_int64(val), type(argument_type::int64) { |
16 | } |
17 | |
18 | ActionArg(const std::string &key, float64 val) |
19 | : key(key), val_float64(val), type(argument_type::float64) { |
20 | } |
21 | |
22 | ActionArg(const std::string &key, int32 val) |
23 | : key(key), val_int64(val), type(argument_type::int64) { |
24 | } |
25 | |
26 | ActionArg(const std::string &key, float32 val) |
27 | : key(key), val_float64(val), type(argument_type::float64) { |
28 | } |
29 | |
30 | void serialize(std::ostream &ss) const; |
31 | |
32 | std::string key; |
33 | |
34 | std::string val_str; |
35 | int64 val_int64; |
36 | float64 val_float64; |
37 | |
38 | enum class argument_type { str, int64, float64 }; |
39 | argument_type type; |
40 | }; |
41 | |
42 | // TODO: Make this thread safe when switching to async mode. |
43 | class ActionRecorder { |
44 | public: |
45 | static ActionRecorder &get_instance(); |
46 | |
47 | void record(const std::string &content, |
48 | const std::vector<ActionArg> &arguments = {}); |
49 | |
50 | void start_recording(const std::string &fn); |
51 | |
52 | void stop_recording(); |
53 | |
54 | bool is_recording(); |
55 | |
56 | private: |
57 | ActionRecorder(); |
58 | |
59 | std::ofstream ofs_; |
60 | |
61 | bool running_{false}; |
62 | }; |
63 | |
64 | } // namespace taichi |
65 | |