Skip to content

Commit

Permalink
Add type to carry parse warnings alongside DumpstateDataSource
Browse files Browse the repository at this point in the history
Issue: #277
  • Loading branch information
mlopatkin committed Oct 10, 2022
1 parent 3199568 commit 87d8037
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/name/mlopatkin/andlogview/FileTransferHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public boolean importData(TransferSupport support) {
logger.debug("Start importing " + file);
DataSource source;
try {
source = FileDataSourceFactory.createDataSource(file);
source = FileDataSourceFactory.createDataSource(file).getDataSource();
frame.setSource(source);
} catch (UnrecognizedFormatException e) {
ErrorDialogsHelper.showError(frame, "Unrecognized file format for " + file);
Expand Down
2 changes: 1 addition & 1 deletion src/name/mlopatkin/andlogview/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private void start(Try<?> configurationLoadingState) {
File baseDir = fileToOpen.getAbsoluteFile().getParentFile();
window.setRecentDir(baseDir);
try {
window.setSourceAsync(FileDataSourceFactory.createDataSource(fileToOpen));
window.setSourceAsync(FileDataSourceFactory.createDataSource(fileToOpen).getDataSource());
} catch (UnrecognizedFormatException e) {
ErrorDialogsHelper.showError(window, "Unrecognized file format for " + fileToOpen);
logger.error("Exception while reading the file", e);
Expand Down
2 changes: 1 addition & 1 deletion src/name/mlopatkin/andlogview/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ private void setupMainMenu(MainFilterController mainFilterController) {
public void actionPerformed(ActionEvent event) {
dependencies.getFileDialog().selectFileToOpen().ifPresent(file -> {
try {
DataSource source = FileDataSourceFactory.createDataSource(file);
DataSource source = FileDataSourceFactory.createDataSource(file).getDataSource();
setSource(source);
} catch (UnrecognizedFormatException e) {
logger.error("Unrecognized source file " + file, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public ParserControl unparseableLogcatSection() {
return this;
}

public DumpstateFileDataSource readFrom(BufferedReader in) throws IOException, UnrecognizedFormatException {
public ImportResult readFrom(BufferedReader in) throws IOException, UnrecognizedFormatException {
ParserUtils.readInto(Objects.requireNonNull(pushParser), in::readLine);

if (isUnparseable) {
Expand All @@ -182,8 +182,9 @@ public DumpstateFileDataSource readFrom(BufferedReader in) throws IOException, U
OfflineSorter sorter = new OfflineSorter();
records.values().forEach(list -> list.forEach(sorter::add));

return new DumpstateFileDataSource(fileName, sorter.build(), EnumSet.allOf(Field.class), buffers,
pidToProcessConverter);
return new ImportResult(
new DumpstateFileDataSource(fileName, sorter.build(), EnumSet.allOf(Field.class), buffers,
pidToProcessConverter));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package name.mlopatkin.andlogview.liblogcat.file;

import name.mlopatkin.andlogview.logmodel.DataSource;
import name.mlopatkin.andlogview.parsers.FormatSniffer;
import name.mlopatkin.andlogview.parsers.MultiplexParser;
import name.mlopatkin.andlogview.parsers.ReplayParser;
Expand All @@ -40,11 +39,11 @@ public class FileDataSourceFactory {

private FileDataSourceFactory() {}

public static DataSource createDataSource(File file) throws UnrecognizedFormatException, IOException {
public static ImportResult createDataSource(File file) throws UnrecognizedFormatException, IOException {
return createDataSource(file.getName(), Files.asCharSource(file, StandardCharsets.UTF_8));
}

public static DataSource createDataSource(String fileName, CharSource file)
public static ImportResult createDataSource(String fileName, CharSource file)
throws UnrecognizedFormatException, IOException {
try (BufferedReader in = file.openBufferedStream()) {
DumpstateFormatSniffer dumpstateSniffer = DumpstateParsers.detectFormat();
Expand All @@ -71,15 +70,15 @@ public static DataSource createDataSource(String fileName, CharSource file)
}
}

private static DataSource createLogFileSource(String fileName, LogcatFormatSniffer formatSniffer,
private static ImportResult createLogFileSource(String fileName, LogcatFormatSniffer formatSniffer,
ReplayParser<?> replayParser, BufferedReader in)
throws IOException {
return new LogfileDataSource.Builder(fileName).setParserFactory(
handler -> FormatSniffer.createAndReplay(replayParser, formatSniffer::createParser, handler))
.readFrom(in);
}

private static DataSource createDumpstateFileSource(String fileName, DumpstateFormatSniffer formatSniffer,
private static ImportResult createDumpstateFileSource(String fileName, DumpstateFormatSniffer formatSniffer,
ReplayParser<?> replayParser, BufferedReader in) throws IOException, UnrecognizedFormatException {
return new DumpstateFileDataSource.Builder(fileName).setParserFactory(
h -> FormatSniffer.createAndReplay(replayParser, formatSniffer::createParser, h)).readFrom(in);
Expand Down
32 changes: 32 additions & 0 deletions src/name/mlopatkin/andlogview/liblogcat/file/ImportProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package name.mlopatkin.andlogview.liblogcat.file;

/**
* Represents a problem/warning encountered when importing a file.
*/
public class ImportProblem {
private final String message;

public ImportProblem(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
47 changes: 47 additions & 0 deletions src/name/mlopatkin/andlogview/liblogcat/file/ImportResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package name.mlopatkin.andlogview.liblogcat.file;

import name.mlopatkin.andlogview.logmodel.DataSource;

import java.util.Collection;
import java.util.Collections;

/**
* Represents a result of loading the data source. May carry information about problems along the actual source.
*/
public class ImportResult {
private final DataSource dataSource;
private final Collection<ImportProblem> problems;

public ImportResult(DataSource dataSource) {
this(dataSource, Collections.emptySet());
}

public ImportResult(DataSource dataSource, Collection<ImportProblem> problems) {
this.dataSource = dataSource;
this.problems = problems;
}

public DataSource getDataSource() {
return dataSource;
}

public Collection<ImportProblem> getProblems() {
return problems;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ protected ParserControl logRecord(LogRecord record) {
return this;
}

public LogfileDataSource readFrom(BufferedReader in) throws IOException {
public ImportResult readFrom(BufferedReader in) throws IOException {
assert pushParser != null;
ParserUtils.readInto(pushParser, in::readLine);
return new LogfileDataSource(fileName, pushParser.getAvailableFields(), records);
return new ImportResult(new LogfileDataSource(fileName, pushParser.getAvailableFields(), records));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void openBlankFile() throws Exception {
public void openDumpstate() throws Exception {
CharSource dumpstateFile = openTestData("galaxy_nexus_jbmr2.minimized.dump");

DataSource dumpstate = FileDataSourceFactory.createDataSource("dumpstate.log", dumpstateFile);
DataSource dumpstate = FileDataSourceFactory.createDataSource("dumpstate.log", dumpstateFile).getDataSource();
assertThat(dumpstate.getAvailableBuffers(),
Matchers.containsInAnyOrder(Buffer.EVENTS, Buffer.RADIO, Buffer.MAIN));
}
Expand All @@ -62,7 +62,7 @@ public void openDumpstate() throws Exception {
public void openBrief() throws Exception {
CharSource briefLog = openTestData("galaxy_nexus_jbmr2_brief.log");

DataSource brief = FileDataSourceFactory.createDataSource("brief.log", briefLog);
DataSource brief = FileDataSourceFactory.createDataSource("brief.log", briefLog).getDataSource();
assertThat(brief.getAvailableFields(),
Matchers.containsInAnyOrder(Field.PRIORITY, Field.TAG, Field.PID, Field.MESSAGE));
}
Expand All @@ -71,7 +71,7 @@ public void openBrief() throws Exception {
public void openTime() throws Exception {
CharSource log = openTestData("galaxy_nexus_jbmr2_time.log");

DataSource source = FileDataSourceFactory.createDataSource("time.log", log);
DataSource source = FileDataSourceFactory.createDataSource("time.log", log).getDataSource();
assertThat(source.getAvailableFields(),
Matchers.containsInAnyOrder(Field.TIME, Field.PRIORITY, Field.TAG, Field.PID, Field.MESSAGE));
}
Expand All @@ -80,7 +80,7 @@ public void openTime() throws Exception {
public void openThreadtime() throws Exception {
CharSource log = openTestData("galaxy_nexus_jbmr2_threadtime.log");

DataSource source = FileDataSourceFactory.createDataSource("threadtime.log", log);
DataSource source = FileDataSourceFactory.createDataSource("threadtime.log", log).getDataSource();
assertThat(source.getAvailableFields(),
Matchers.containsInAnyOrder(
Field.TIME, Field.PID, Field.TID, Field.PRIORITY, Field.TAG, Field.MESSAGE));
Expand All @@ -90,15 +90,15 @@ public void openThreadtime() throws Exception {
public void openLogFileWithExtraStuffInTheBeginning() throws Exception {
CharSource extraStuffLog = openTestData("huawei_p10_log_snippet.log");

DataSource source = FileDataSourceFactory.createDataSource("huawei.log", extraStuffLog);
DataSource source = FileDataSourceFactory.createDataSource("huawei.log", extraStuffLog).getDataSource();
source.close();
}

@Test
public void openAndroidStudioLog() throws Exception {
CharSource log = openTestData("emulator_cupcake_android_studio.log");

DataSource source = FileDataSourceFactory.createDataSource("androidstudio.log", log);
DataSource source = FileDataSourceFactory.createDataSource("androidstudio.log", log).getDataSource();
assertThat(source.getAvailableFields(),
Matchers.containsInAnyOrder(
Field.TIME, Field.PID, Field.TID, Field.PRIORITY, Field.TAG, Field.MESSAGE, Field.APP_NAME));
Expand All @@ -108,7 +108,7 @@ public void openAndroidStudioLog() throws Exception {
public void openDumpstateWithTimeTravel() throws Exception {
CharSource log = openTestData("emulator_nougat.minimized.with-time-travel.dump");

DataSource source = FileDataSourceFactory.createDataSource("time-travel.dump", log);
DataSource source = FileDataSourceFactory.createDataSource("time-travel.dump", log).getDataSource();

ArrayList<LogRecord> records = new ArrayList<>();
source.setLogRecordListener(new RecordListener<>() {
Expand Down

0 comments on commit 87d8037

Please sign in to comment.