1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/chromebox_for_meetings/artemis/log_file.h"
#include <unistd.h>
#include <fstream>
#include "base/logging.h"
#include "base/threading/scoped_blocking_call.h"
namespace ash::cfm {
LogFile::LogFile(const std::string& filepath) : filepath_(filepath) {}
LogFile::~LogFile() {
CloseStream();
}
const std::string& LogFile::GetFilePath() const {
return filepath_.value();
}
bool LogFile::OpenAtOffset(std::streampos offset) {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
if (access(GetFilePath().c_str(), F_OK) != 0) {
LOG(ERROR) << "File " << filepath_ << " doesn't exist";
return false;
}
if (access(GetFilePath().c_str(), R_OK) != 0) {
LOG(ERROR) << "Not enough permissions on file " << filepath_;
return false;
}
file_stream_ = std::ifstream(GetFilePath(), std::ios::in);
file_stream_.seekg(offset);
if (IsInFailState()) {
LOG(ERROR) << "Unable to seek to offset " << offset << " in file "
<< filepath_;
return false;
}
is_open_ = true;
return true;
}
void LogFile::CloseStream() {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
file_stream_.close();
is_open_ = false;
}
bool LogFile::IsInFailState() const {
return file_stream_.bad();
}
bool LogFile::IsAtEOF() const {
return file_stream_.eof() && !IsInFailState();
}
bool LogFile::IsOpen() const {
return is_open_;
}
std::streampos LogFile::GetCurrentOffset() {
std::streampos offset;
// tellg() will report -1 if the last read resulted in an EOF,
// but we want the true offset value. Use the last-known read
// offset as it will be the offset right before we hit the EOF.
if (IsAtEOF()) {
return last_read_offset_;
}
return file_stream_.tellg();
}
bool LogFile::Refresh() {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
std::streampos curr_pos = GetCurrentOffset();
CloseStream();
return OpenAtOffset(curr_pos);
}
std::vector<std::string> LogFile::RetrieveNextLogs(size_t line_count,
size_t max_byte_limit) {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
std::vector<std::string> logs;
size_t num_read_lines = 0;
size_t num_read_bytes = 0;
std::string line;
while (!IsAtEOF() && !IsInFailState() && num_read_lines < line_count &&
num_read_bytes < max_byte_limit && std::getline(file_stream_, line)) {
num_read_bytes += line.size();
num_read_lines++;
logs.push_back(std::move(line));
last_read_offset_ = file_stream_.tellg();
}
if (IsInFailState()) {
LOG(ERROR) << "Error reading file " << filepath_ << " after "
<< num_read_lines << " lines";
} else if (num_read_lines < line_count && num_read_bytes > max_byte_limit &&
!IsAtEOF()) {
LOG(WARNING) << "Requested " << line_count << " lines for " << GetFilePath()
<< ", but only read " << num_read_lines
<< " due to byte cap. Limit exceeded by "
<< num_read_bytes - max_byte_limit << " bytes.";
// Drop logs until we're within our limit. This is a highly unlikely
// scenario, so this shouldn't impact our data analysis too much.
size_t orig_size = logs.size();
while (num_read_bytes > max_byte_limit && !logs.empty()) {
num_read_bytes -= logs.back().size();
logs.pop_back();
}
LOG(WARNING) << "Dropped " << orig_size - logs.size() << " logs.";
} else {
VLOG(3) << "Read " << num_read_bytes << " bytes from " << GetFilePath();
}
return logs;
}
} // namespace ash::cfm
|