1#include "taichi/util/action_recorder.h"
2#include "taichi/util/str.h"
3
4namespace taichi {
5
6void ActionArg::serialize(std::ostream &ss) const {
7 ss << key << ": ";
8 if (type == argument_type::str) {
9 ss << lang::c_quoted(val_str);
10 } else if (type == argument_type::int64) {
11 ss << std::to_string(val_int64);
12 } else {
13 ss << std::to_string(val_float64);
14 }
15}
16
17ActionRecorder &ActionRecorder::get_instance() {
18 static ActionRecorder rec;
19 return rec;
20}
21
22ActionRecorder::ActionRecorder() {
23}
24
25void ActionRecorder::start_recording(const std::string &fn) {
26 TI_INFO("ActionRecorder: start recording to [{}]", fn);
27 TI_ASSERT(!running_);
28 running_ = true;
29 ofs_.open(fn);
30}
31
32void ActionRecorder::stop_recording() {
33 TI_INFO("ActionRecorder: stop recording");
34 TI_ASSERT(running_);
35 running_ = false;
36 ofs_.close();
37}
38
39bool ActionRecorder::is_recording() {
40 return running_;
41}
42
43void ActionRecorder::record(const std::string &content,
44 const std::vector<ActionArg> &arguments) {
45 if (!running_)
46 return;
47 ofs_ << "- action: \"" << content << "\"" << std::endl;
48 for (auto &arg : arguments) {
49 ofs_ << " ";
50 arg.serialize(ofs_);
51 ofs_ << std::endl;
52 }
53 ofs_.flush();
54}
55
56} // namespace taichi
57