1 | #pragma once |
---|---|
2 | |
3 | #include <string> |
4 | |
5 | #include "spdlog/spdlog.h" |
6 | #include "taichi/common/core.h" |
7 | |
8 | namespace taichi { |
9 | |
10 | class LineAppender { |
11 | public: |
12 | explicit LineAppender(int indent_size = 2) |
13 | : single_indent_(indent_size, ' ') { |
14 | } |
15 | |
16 | LineAppender(const LineAppender &) = default; |
17 | LineAppender &operator=(const LineAppender &) = default; |
18 | LineAppender(LineAppender &&) = default; |
19 | LineAppender &operator=(LineAppender &&) = default; |
20 | |
21 | inline const std::string &lines() const { |
22 | return lines_; |
23 | } |
24 | |
25 | template <typename... Args> |
26 | void append(std::string f, Args &&...args) { |
27 | lines_ += indent_ + fmt::format(f, std::forward<Args>(args)...) + '\n'; |
28 | } |
29 | |
30 | inline void append_raw(const std::string &s) { |
31 | lines_ += s + '\n'; |
32 | } |
33 | |
34 | inline void dump(std::string *output) { |
35 | *output = std::move(lines_); |
36 | } |
37 | |
38 | void clear_lines() { |
39 | // Free up the memory as well |
40 | std::string s; |
41 | dump(&s); |
42 | } |
43 | |
44 | void clear_all() { |
45 | clear_lines(); |
46 | indent_.clear(); |
47 | } |
48 | |
49 | inline void push_indent() { |
50 | indent_ += single_indent_; |
51 | } |
52 | |
53 | inline void pop_indent() { |
54 | indent_.erase(indent_.size() - single_indent_.size()); |
55 | } |
56 | |
57 | private: |
58 | std::string single_indent_; |
59 | std::string indent_; |
60 | std::string lines_; |
61 | }; |
62 | |
63 | class ScopedIndent { |
64 | public: |
65 | explicit ScopedIndent(LineAppender &la) : la_(la) { |
66 | la_.push_indent(); |
67 | } |
68 | |
69 | ~ScopedIndent() { |
70 | la_.pop_indent(); |
71 | } |
72 | |
73 | private: |
74 | LineAppender &la_; |
75 | }; |
76 | |
77 | } // namespace taichi |
78 |