Skip to content

Commit

Permalink
Compatibility fix for IU-182.5262.2, IU-183.6156.11
Browse files Browse the repository at this point in the history
Signed-off-by: Cybrosis <[email protected]>
  • Loading branch information
Cybr0sis committed Jun 18, 2019
1 parent aa6fc12 commit 8e75717
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.cybrosis.catdea.highlighting.syntax.CatdeaSyntaxHighlighter;
import com.cybrosis.catdea.lang.CatdeaLanguage;
import com.cybrosis.catdea.lang.psi.PsiCatdeaEntry;
import com.cybrosis.catdea.utils.CompatibilityUtil;
import com.intellij.execution.impl.ConsoleViewUtil;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.Disposable;
Expand Down Expand Up @@ -56,12 +57,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class CatdeaLogcatView implements Disposable {
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
private static final AnActionEvent FAKE_ACTION_EVENT = new TestActionEvent();

private final Project project;
Expand Down Expand Up @@ -236,7 +232,7 @@ private static String convert(@NotNull LogCatMessage message) {
final LogCatHeader header = message.getHeader();
return String.format(
"%s %d-%d/%s %s/%s: %s\n",
LocalDateTime.ofInstant(header.getTimestampInstant(), ZoneId.systemDefault()).format(TIME_FORMATTER),
CompatibilityUtil.LogcatHeader.getTimestamp(header),
header.getPid(),
header.getTid(),
header.getAppName(),
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/cybrosis/catdea/utils/CompatibilityUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019 Cybrosis
*
* 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 com.cybrosis.catdea.utils;

import com.android.ddmlib.logcat.LogCatHeader;
import com.android.ddmlib.logcat.LogCatTimestamp;

import java.lang.reflect.Method;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class CompatibilityUtil {
public static class LogcatHeader {
private static final ZoneId ZONE = ZoneId.systemDefault();
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");

public static String getTimestamp(LogCatHeader header) {
Instant instant;

try {
final Method method = header.getClass().getDeclaredMethod("getTimestampInstant");
instant = (Instant) method.invoke(header);
} catch (ReflectiveOperationException ignore) {
// Compatibility issue with IU-182.5262.2, IU-183.6156.11
try {
final Method method = header.getClass().getDeclaredMethod("getTimestamp");
final LogCatTimestamp timestamp = (LogCatTimestamp) method.invoke(header);

return timestamp.toString();
} catch (ReflectiveOperationException | NullPointerException e) {
instant = Instant.now();
}
}

return LocalDateTime.ofInstant(instant, ZONE).format(FORMATTER);
}
}
}

0 comments on commit 8e75717

Please sign in to comment.