diff options
author | Andreas Gampe <agampe@google.com> | 2015-02-25 18:12:27 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-02-25 18:12:27 +0000 |
commit | 01b7c431ed85e0eae4554f6fc0f79a78bc82f31f (patch) | |
tree | 3d203eca0ee9c82c7e5d1fd82c9cb3e5d7375987 /runtime/utils.cc | |
parent | ec27dc12124d57283d8f86e5386c95896b01eb23 (diff) | |
parent | a6dfdae1cffc78f9791348b2e1dc8f4c6c3f7128 (diff) | |
download | art-01b7c431ed85e0eae4554f6fc0f79a78bc82f31f.tar.gz art-01b7c431ed85e0eae4554f6fc0f79a78bc82f31f.tar.bz2 art-01b7c431ed85e0eae4554f6fc0f79a78bc82f31f.zip |
Merge "ART: Print maps directly to log"
Diffstat (limited to 'runtime/utils.cc')
-rw-r--r-- | runtime/utils.cc | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/runtime/utils.cc b/runtime/utils.cc index dea8f7939..6afc373f9 100644 --- a/runtime/utils.cc +++ b/runtime/utils.cc @@ -133,14 +133,14 @@ void GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size, siz } bool ReadFileToString(const std::string& file_name, std::string* result) { - std::unique_ptr<File> file(new File); - if (!file->Open(file_name, O_RDONLY)) { + File file; + if (!file.Open(file_name, O_RDONLY)) { return false; } std::vector<char> buf(8 * KB); while (true) { - int64_t n = TEMP_FAILURE_RETRY(read(file->Fd(), &buf[0], buf.size())); + int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[0], buf.size())); if (n == -1) { return false; } @@ -151,6 +151,59 @@ bool ReadFileToString(const std::string& file_name, std::string* result) { } } +bool PrintFileToLog(const std::string& file_name, LogSeverity level) { + File file; + if (!file.Open(file_name, O_RDONLY)) { + return false; + } + + constexpr size_t kBufSize = 256; // Small buffer. Avoid stack overflow and stack size warnings. + char buf[kBufSize + 1]; // +1 for terminator. + size_t filled_to = 0; + while (true) { + DCHECK_LT(filled_to, kBufSize); + int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[filled_to], kBufSize - filled_to)); + if (n <= 0) { + // Print the rest of the buffer, if it exists. + if (filled_to > 0) { + buf[filled_to] = 0; + LOG(level) << buf; + } + return n == 0; + } + // Scan for '\n'. + size_t i = filled_to; + bool found_newline = false; + for (; i < filled_to + n; ++i) { + if (buf[i] == '\n') { + // Found a line break, that's something to print now. + buf[i] = 0; + LOG(level) << buf; + // Copy the rest to the front. + if (i + 1 < filled_to + n) { + memmove(&buf[0], &buf[i + 1], filled_to + n - i - 1); + filled_to = filled_to + n - i - 1; + } else { + filled_to = 0; + } + found_newline = true; + break; + } + } + if (found_newline) { + continue; + } else { + filled_to += n; + // Check if we must flush now. + if (filled_to == kBufSize) { + buf[kBufSize] = 0; + LOG(level) << buf; + filled_to = 0; + } + } + } +} + std::string GetIsoDate() { time_t now = time(NULL); tm tmbuf; |