Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/crash/MinidumpLogcatPrepender.java

Issue 2748913002: [Merge to M58] [Android Crash Reporting] Simplify crash report upload code. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.crash;
6
7 import org.chromium.base.Log;
8 import org.chromium.base.VisibleForTesting;
9 import org.chromium.components.minidump_uploader.CrashFileManager;
10
11 import java.io.BufferedInputStream;
12 import java.io.BufferedOutputStream;
13 import java.io.BufferedReader;
14 import java.io.BufferedWriter;
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.FileOutputStream;
18 import java.io.FileReader;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.util.List;
22
23 /**
24 * Prepends a logcat file to a minidump file for upload.
25 */
26 public class MinidumpLogcatPrepender {
27 private static final String TAG = "LogcatPrepender";
28
29 @VisibleForTesting
30 static final String LOGCAT_CONTENT_DISPOSITION =
31 "Content-Disposition: form-data; name=\"logcat\"; filename=\"logcat\ "";
32
33 @VisibleForTesting
34 static final String LOGCAT_CONTENT_TYPE = "Content-Type: text/plain";
35
36 private final CrashFileManager mFileManager;
37 private final File mMinidumpFile;
38 private final List<String> mLogcat;
39
40 public MinidumpLogcatPrepender(
41 CrashFileManager fileManager, File minidumpFile, List<String> logcat ) {
42 mFileManager = fileManager;
43 mMinidumpFile = minidumpFile;
44 mLogcat = logcat;
45 }
46
47 /**
48 * Read the boundary from the first line of the file.
49 */
50 private static String getBoundary(File minidumpFile) throws IOException {
51 BufferedReader reader = null;
52 try {
53 reader = new BufferedReader(new FileReader(minidumpFile));
54 return reader.readLine();
55 } finally {
56 if (reader != null) {
57 reader.close();
58 }
59 }
60 }
61
62 /**
63 * Write the logcat data to the specified target {@link File}.
64 *
65 * Target file is overwritten, not appended to the end.
66 *
67 * @param targetFile File to which logcat data should be written.
68 * @param logcat The lines of the logcat output.
69 * @param boundary String MIME boundary to prepend.
70 * @throws IOException if something goes wrong.
71 */
72 private static void writeLogcat(File targetFile, List<String> logcat, String boundary)
73 throws IOException {
74 BufferedWriter writer = null;
75 try {
76 writer = new BufferedWriter(new FileWriter(targetFile, false));
77 writer.write(boundary);
78 writer.newLine();
79 // Next we write the logcat data in a MIME block.
80 writer.write(LOGCAT_CONTENT_DISPOSITION);
81 writer.newLine();
82 writer.write(LOGCAT_CONTENT_TYPE);
83 writer.newLine();
84 writer.newLine();
85 // Emits the contents of the buffer into the output file.
86 for (String ln : logcat) {
87 writer.write(ln);
88 writer.newLine();
89 }
90 } finally {
91 if (writer != null) {
92 writer.close();
93 }
94 }
95 }
96
97 /**
98 * Append the minidump file data to the specified target {@link File}.
99 *
100 * @param minidumpFile File containing data to append.
101 * @param targetFile File to which data should be appended.
102 * @throws IOException when standard IO errors occur.
103 */
104 private static void appendMinidump(File minidumpFile, File targetFile) throw s IOException {
105 BufferedInputStream in = null;
106 BufferedOutputStream out = null;
107 try {
108 byte[] buf = new byte[256];
109 in = new BufferedInputStream(new FileInputStream(minidumpFile));
110 out = new BufferedOutputStream(new FileOutputStream(targetFile, true ));
111 int count;
112 while ((count = in.read(buf)) != -1) {
113 out.write(buf, 0, count);
114 }
115 } finally {
116 if (in != null) in.close();
117 if (out != null) out.close();
118 }
119 }
120
121 /**
122 * Prepends the logcat output to the minidump file.
123 * @return On success, returns the file containing the combined logcat and m inidump output.
124 * On failure, returns the original file containing just the minidump.
125 */
126 public File run() {
127 if (mLogcat.isEmpty()) return mMinidumpFile;
128
129 String targetFileName = mMinidumpFile.getName() + ".try0";
130 File targetFile = null;
131 boolean success = false;
132 try {
133 String boundary = getBoundary(mMinidumpFile);
134 if (boundary == null) {
135 return mMinidumpFile;
136 }
137
138 targetFile = mFileManager.createNewTempFile(targetFileName);
139 writeLogcat(targetFile, mLogcat, boundary);
140
141 // Finally reopen and append the original minidump MIME sections, in cluding the leading
142 // boundary.
143 appendMinidump(mMinidumpFile, targetFile);
144 success = true;
145 } catch (IOException e) {
146 Log.w(TAG, "Error while trying to annotate minidump file %s with log cat data",
147 mMinidumpFile.getAbsoluteFile(), e);
148 if (targetFile != null) {
149 CrashFileManager.deleteFile(targetFile);
150 }
151 }
152
153 if (!success) return mMinidumpFile;
154
155 // Try to clean up the previous file. Note that this step is best-effort , and failing to
156 // perform the cleanup does not count as an overall failure to prepend t he logcat output.
157 if (!mMinidumpFile.delete()) {
158 Log.w(TAG, "Failed to delete minidump file: " + mMinidumpFile.getNam e());
159 }
160
161 assert targetFile != null;
162 assert targetFile.exists();
163 return targetFile;
164 }
165 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698