| 63 | } |
| 64 | |
| 65 | void FileReader::ReadUntilEof() |
| 66 | { |
| 67 | std::string line; |
| 68 | while (std::getline(m_ifstream, line)) |
| 69 | { |
| 70 | if (m_ifstream.eof()) |
| 71 | { |
| 72 | // the line ended without a newline character, store the line particle |
| 73 | m_line += line; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | SafeAddLine(m_line + line); |
| 78 | m_line.clear(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (m_ifstream.eof()) |
| 83 | { |
| 84 | m_ifstream.clear(); // clear EOF condition |
| 85 | |
| 86 | // resync to end of file, even if the file shrunk |
| 87 | auto lastReadPosition = m_ifstream.tellg(); |
| 88 | m_ifstream.seekg(0, m_ifstream.end); |
| 89 | auto length = m_ifstream.tellg(); |
| 90 | if (length > lastReadPosition) |
| 91 | m_ifstream.seekg(lastReadPosition); |
| 92 | else if (length != lastReadPosition) |
| 93 | { |
| 94 | m_line.clear(); |
| 95 | Add(stringbuilder() << "file shrank, resynced at offset " << length); |
| 96 | } |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | // Some error other then EOF occured |
| 101 | Add("Stopped tailing " + m_filename); |
| 102 | m_end = true; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void FileReader::SafeAddLine(const std::string& line) |
| 107 | { |