From 3f131ea803feb926805c9c54575ffd0d69f292a4 Mon Sep 17 00:00:00 2001 From: Christoph Rueger Date: Thu, 8 May 2025 20:06:19 +0200 Subject: [PATCH] LogView: Date column should be more fine grained - and show yyyy-MM-dd HH:mm:ss.SSS instead of just minute precision - this is useful where you want to see the exact time of log events in cases where seconds or even milliseconds matter add and use getFormattedDate() reuse LogEntry DateTimeFormatters fix potential NPE in case somebody calls getFormattedDate before setDate() was called --- .../ui/internal/views/log/LogEntry.java | 4 +-- .../ui/internal/views/log/LogSession.java | 33 ++++++++++++++++--- .../views/log/LogViewLabelProvider.java | 7 ++-- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogEntry.java b/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogEntry.java index c73e2e22afb..8b5b255169a 100644 --- a/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogEntry.java +++ b/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogEntry.java @@ -31,9 +31,9 @@ public class LogEntry extends AbstractEntry { public static final String SPACE = " "; //$NON-NLS-1$ public static final String F_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$ - private static final DateTimeFormatter GREGORIAN_SDF = DateTimeFormatter.ofPattern(F_DATE_FORMAT, Locale.ENGLISH) + static final DateTimeFormatter GREGORIAN_SDF = DateTimeFormatter.ofPattern(F_DATE_FORMAT, Locale.ENGLISH) .withZone(ZoneId.systemDefault()); - private static final DateTimeFormatter LOCAL_SDF = DateTimeFormatter.ofPattern(F_DATE_FORMAT) + static final DateTimeFormatter LOCAL_SDF = DateTimeFormatter.ofPattern(F_DATE_FORMAT) .withZone(ZoneId.systemDefault()); private String pluginId; diff --git a/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogSession.java b/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogSession.java index 6399d77bae8..127122874f3 100644 --- a/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogSession.java +++ b/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogSession.java @@ -15,9 +15,11 @@ *******************************************************************************/ package org.eclipse.ui.internal.views.log; +import static org.eclipse.ui.internal.views.log.LogEntry.GREGORIAN_SDF; +import static org.eclipse.ui.internal.views.log.LogEntry.LOCAL_SDF; + import java.io.PrintWriter; -import java.text.ParseException; -import java.text.SimpleDateFormat; +import java.time.Instant; import java.util.Date; /** @@ -31,8 +33,10 @@ public class LogSession extends Group { * @since 3.5 */ public static final String SESSION = "!SESSION"; //$NON-NLS-1$ + private String sessionData; private Date date; + private String fDateString; public LogSession() { super(Messages.LogViewLabelProvider_Session); @@ -42,11 +46,30 @@ public Date getDate() { return date; } + /** + * Returns a pretty-print formatting for the date for this entry + * + * @return the formatted date for this entry + */ + public String getFormattedDate() { + if (fDateString == null) { + Date tmpdate = getDate(); + if (tmpdate != null) { + fDateString = LOCAL_SDF.format(tmpdate.toInstant()); + } + } + return fDateString; + } + public void setDate(String dateString) { - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //$NON-NLS-1$ try { - date = formatter.parse(dateString); - } catch (ParseException e) { // do nothing + Date parsed = Date.from(Instant.from(GREGORIAN_SDF.parse(dateString))); + if (parsed != null) { + this.date = parsed; + fDateString = LOCAL_SDF.format(parsed.toInstant()); + } + } catch (Exception e) { + // do nothing } } diff --git a/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogViewLabelProvider.java b/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogViewLabelProvider.java index bc687e2f852..b31a69cbad1 100644 --- a/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogViewLabelProvider.java +++ b/bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogViewLabelProvider.java @@ -15,7 +15,6 @@ *******************************************************************************/ package org.eclipse.ui.internal.views.log; -import java.text.DateFormat; import java.util.ArrayList; import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.resource.JFaceResources; @@ -34,8 +33,6 @@ public class LogViewLabelProvider extends LabelProvider implements ITableLabelPr private Image errorWithStackImage; private Image hierarchicalImage; ArrayList consumers = new ArrayList<>(); - private DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); - private LogView logView; public LogViewLabelProvider(LogView logView) { @@ -81,7 +78,7 @@ public String getColumnText(Object element, int columnIndex) { if (session.getDate() == null) return ""; //$NON-NLS-1$ - return dateFormat.format(session.getDate()); + return session.getFormattedDate(); } if ((element instanceof Group) && (columnIndex == 0)) { @@ -106,7 +103,7 @@ public String getColumnText(Object element, int columnIndex) { if (entry.getPluginId() != null) return entry.getPluginId(); case 2 : - return dateFormat.format(entry.getDate()); + return entry.getFormattedDate(); } }