Skip to content

Commit

Permalink
[Debug] Show elapsed Time in Console & Properties
Browse files Browse the repository at this point in the history
Elapsed duration formated as H:MM:SS.mmm

While process is running console header is updated once per second.
  • Loading branch information
EcljpseB0T authored and jukzi committed Sep 5, 2024
1 parent 9d9bf70 commit 7e1f60c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public class DebugPreferencesMessages extends NLS {

public static String ProcessPropertyPage_11;

public static String ProcessPropertyPage_12;

public static String ProcessPropertyPage_1;

public static String ProcessPropertyPage_2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ ProcessPropertyPage_Command_Line__1=Co&mmand Line:
ProcessPropertyPage_0=Run-&at time:
ProcessPropertyPage_10=&Terminated-at time:
ProcessPropertyPage_11=PID:
ProcessPropertyPage_12=Elapsed:
ProcessPropertyPage_1=&Path:
ProcessPropertyPage_2=Process properties
ProcessPropertyPage_3=No path information available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.text.DateFormat;
import java.text.ParseException;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Date;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -75,6 +77,30 @@ protected Control createContents(Composite ancestor) {
text.setBackground(parent.getBackground());
SWTFactory.createVerticalSpacer(parent, 2);

// Elapsed Duration:
SWTFactory.createLabel(parent, DebugPreferencesMessages.ProcessPropertyPage_12, fHeadingFont, 1);
text = SWTFactory.createText(parent, SWT.READ_ONLY, 1);
((GridData) text.getLayoutData()).horizontalIndent = 10;
String elapsedString = "?"; //$NON-NLS-1$
try {
if (proc != null) {
String launch = proc.getAttribute(DebugPlugin.ATTR_LAUNCH_TIMESTAMP);
String terminate = proc.getAttribute(DebugPlugin.ATTR_TERMINATE_TIMESTAMP);
Date launchTime = launch == null ? null : new Date(Long.parseLong(launch));
Date terminateTime = terminate == null ? null : new Date(Long.parseLong(terminate));
Duration elapsedTime = Duration.between(launchTime != null ? launchTime.toInstant() : Instant.now(),
terminateTime != null ? terminateTime.toInstant() : Instant.now());
elapsedString = String.format("%d:%02d:%02d.%03d", elapsedTime.toHours(), //$NON-NLS-1$
elapsedTime.toMinutesPart(), elapsedTime.toSecondsPart(), elapsedTime.toMillisPart());
}
} catch (Exception e) {
// ignore wrong time format
}
PlatformUI.getWorkbench().getHelpSystem().setHelp(text, IDebugHelpContextIds.PROCESS_PAGE_TERMINATE_AT);
text.setText(elapsedString);
text.setBackground(parent.getBackground());
SWTFactory.createVerticalSpacer(parent, 2);

// create the process terminate time section
SWTFactory.createLabel(parent, DebugPreferencesMessages.ProcessPropertyPage_10, fHeadingFont, 1);
text = SWTFactory.createText(parent, SWT.READ_ONLY, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ ProcessConsole_0=<terminated> {0}
ProcessConsole_1=[Console output redirected to file:{0}]\n
ProcessConsole_2=[Invalid file specified for console output: {0}]\n
ProcessConsole_3=[Invalid file specified for stdin file: {0}]\n
ProcessConsole_commandLabel_withStart={0} ({1})
ProcessConsole_commandLabel_withStart={0} ({1} elapsed: {2})
ProcessConsole_commandLabel_withEnd={0} (Terminated {1})
ProcessConsole_commandLabel_withStartEnd={0} ({1} \u2013 {2})
ProcessConsole_commandLabel_withStartEnd={0} ({1} \u2013 {2} elapsed: {3})
ShowStandardErrorAction_0=Show Console When Standard Error Changes
ShowStandardOutAction_0=Show Console When Standard Out Changes
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.ParseException;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -333,6 +335,14 @@ protected String computeName() {
}

DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
Duration elapsedTime = Duration.between(launchTime != null ? launchTime.toInstant() : Instant.now(),
terminateTime != null ? terminateTime.toInstant() : Instant.now());
String elapsedString = String.format("%d:%02d:%02d.%03d", elapsedTime.toHours(), //$NON-NLS-1$
elapsedTime.toMinutesPart(), elapsedTime.toSecondsPart(), elapsedTime.toMillisPart());
if (terminateTime == null) {
DebugUIPlugin.getStandardDisplay().asyncExec(
() -> DebugUIPlugin.getStandardDisplay().timerExec(1000, () -> resetName(false)));
}
if (launchTime != null && terminateTime != null) {
String launchTimeStr = dateTimeFormat.format(launchTime);
// Check if process started and terminated at same day. If so only print the
Expand All @@ -351,10 +361,10 @@ protected String computeName() {
}

buffer.append(MessageFormat.format(ConsoleMessages.ProcessConsole_commandLabel_withStartEnd,
procLabel, launchTimeStr, terminateTimeStr));
procLabel, launchTimeStr, terminateTimeStr, elapsedString));
} else if (launchTime != null) {
buffer.append(MessageFormat.format(ConsoleMessages.ProcessConsole_commandLabel_withStart,
procLabel, dateTimeFormat.format(launchTime)));
procLabel, dateTimeFormat.format(launchTime), elapsedString));
} else if (terminateTime != null) {
buffer.append(MessageFormat.format(ConsoleMessages.ProcessConsole_commandLabel_withEnd,
procLabel, dateTimeFormat.format(terminateTime)));
Expand Down Expand Up @@ -559,7 +569,7 @@ protected void init() {
setName(computeName());
if (fProcess.isTerminated()) {
closeStreams();
resetName();
resetName(true);
DebugPlugin.getDefault().removeDebugEventListener(this);
}
IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore();
Expand Down Expand Up @@ -600,24 +610,26 @@ public void handleDebugEvents(DebugEvent[] events) {
DebugPlugin.getDefault().removeDebugEventListener(this);
}

resetName();
resetName(true);
}
}
}

/**
* resets the name of this console to the original computed name
*/
private synchronized void resetName() {
private synchronized void resetName(boolean changed) {
final String newName = computeName();
String name = getName();
if (!name.equals(newName)) {
UIJob job = new UIJob("Update console title") { //$NON-NLS-1$
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
ProcessConsole.this.setName(newName);
warnOfContentChange();
return Status.OK_STATUS;
ProcessConsole.this.setName(newName);
if (changed) {
warnOfContentChange();
}
return Status.OK_STATUS;
}
};
job.setSystem(true);
Expand Down

0 comments on commit 7e1f60c

Please sign in to comment.