| #include "parse_text_proto.h" | |
| #include <cstddef> | |
| #include <cstdio> | |
| #include <fstream> | |
| #include <string> | |
| #include <vector> | |
| #include "absl/log/log.h" | |
| #include "absl/strings/str_format.h" | |
| #include "absl/strings/str_split.h" | |
| #include "absl/strings/string_view.h" | |
| #include "google/protobuf/io/tokenizer.h" | |
| namespace safepower_agent { | |
| namespace internal { | |
| std::string ErrorCollectorString::ExtractLine(int line) const { | |
| std::vector<absl::string_view> lines = absl::StrSplit(text_proto_, '\n'); | |
| return (static_cast<std::size_t>(line) < lines.size()) | |
| ? std::string(lines[line]) | |
| : ""; | |
| } | |
| std::string ErrorCollectorFile::ExtractLine(int line) const { | |
| std::ifstream file(path_); | |
| std::string line_string; | |
| for (int i = 0; i <= line; ++i) { | |
| if (!file.good()) { | |
| return ""; | |
| } | |
| std::getline(file, line_string); | |
| LOG(INFO) << "line: " << line_string; | |
| } | |
| return line_string; | |
| } | |
| void ErrorCollector::RecordError(int line, google::protobuf::io::ColumnNumber column, | |
| absl::string_view message) { | |
| absl::StrAppendFormat(&message_, "%s [line %d, column %d]\n%s", message, | |
| line + 1, column + 1, ExtractLine(line)); | |
| } | |
| void ErrorCollector::RecordWarning(int line, google::protobuf::io::ColumnNumber column, | |
| absl::string_view message) { | |
| LOG(WARNING) << message << " [line " << line + 1 << ", column " << column + 1 | |
| << "]\n" | |
| << ExtractLine(line); | |
| } | |
| } // namespace internal | |
| } // namespace safepower_agent |