Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,32 @@ public interface IFlowScopeLog {

private static final Format FORMAT = new DecimalFormat("#.###"); //$NON-NLS-1$

/**
* Offset between System.currentTimeMillis() and System.nanoTime() in nanoseconds.
* Used to convert between the two time bases while maintaining nanosecond precision.
*/
private static final long TIME_OFFSET;
static {
// Get current time in both bases
long currentMillis = System.currentTimeMillis();
long nanoTime = System.nanoTime();

// Convert millis to nanos and calculate offset, handling potential overflow
long millisToNanos = currentMillis * 1_000_000L;
TIME_OFFSET = millisToNanos - nanoTime;
}

/**
* Gets the current time in nanoseconds since the Unix epoch.
* This maintains nanosecond precision while being comparable to timestamps from
* System.currentTimeMillis().
*
* @return Current time in nanoseconds since Unix epoch
*/
private static long currentTimeNanos() {
return System.nanoTime() + TIME_OFFSET;
}

/*
* Field names
*/
Expand Down Expand Up @@ -250,7 +276,7 @@ public static class ScopeLog implements AutoCloseable {
* beginning of the scope
*/
public ScopeLog(Logger log, Level level, String label, Object... args) {
fTime = System.nanoTime();
fTime = currentTimeNanos();
fLogger = log;
fLevel = level;
fThreadId = Thread.currentThread().getId();
Expand Down Expand Up @@ -289,7 +315,7 @@ public void addData(String name, Object value) {

@Override
public void close() {
long time = System.nanoTime();
long time = currentTimeNanos();
char phase = 'E';
Supplier<String> msgSupplier = () -> {
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -487,7 +513,7 @@ public static class FlowScopeLog implements IFlowScopeLog, AutoCloseable {
* value2.... typically arguments
*/
private FlowScopeLog(Logger log, Level level, String label, String category, int id, boolean startFlow, Object... args) {
fTime = System.nanoTime();
fTime = currentTimeNanos();
fId = id;
fLogger = log;
fLevel = level;
Expand Down Expand Up @@ -531,7 +557,7 @@ private FlowScopeLog(Logger log, Level level, String label, String category, int
* the arguments to log
*/
public void step(String label, Object... args) {
long time = System.nanoTime();
long time = currentTimeNanos();
char phase = 't';
validateArgs(args);
Supplier<String> msgSupplier = () -> {
Expand Down Expand Up @@ -578,7 +604,7 @@ public int getId() {

@Override
public void close() {
long time = System.nanoTime();
long time = currentTimeNanos();
char phase = 'E';
Supplier<String> msgSupplier = () -> {
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -612,7 +638,7 @@ public void close() {
* @return The unique ID of this object (there may be collisions)
*/
public static int traceObjectCreation(Logger logger, Level level, Object item) {
long time = System.nanoTime();
long time = currentTimeNanos();
long threadId = Thread.currentThread().getId();
int identityHashCode = System.identityHashCode(item);
char phase = 'N';
Expand Down Expand Up @@ -643,7 +669,7 @@ public static int traceObjectCreation(Logger logger, Level level, Object item) {
* the Object to trace
*/
public static void traceObjectDestruction(Logger logger, Level level, Object item) {
long time = System.nanoTime();
long time = currentTimeNanos();
long threadId = Thread.currentThread().getId();
char phase = 'D';
Supplier<String> msgSupplier = () -> {
Expand Down Expand Up @@ -673,7 +699,7 @@ public static void traceObjectDestruction(Logger logger, Level level, Object ite
* The unique ID
*/
public static void traceObjectDestruction(Logger logger, Level level, Object item, int uniqueId) {
long time = System.nanoTime();
long time = currentTimeNanos();
long threadId = Thread.currentThread().getId();
char phase = 'D';
Supplier<String> msgSupplier = () -> {
Expand Down Expand Up @@ -706,7 +732,7 @@ public static void traceObjectDestruction(Logger logger, Level level, Object ite
* Additional arguments to log
*/
public static void traceAsyncStart(Logger logger, Level level, String name, String category, int id, Object... args) {
long time = System.nanoTime();
long time = currentTimeNanos();
long threadId = Thread.currentThread().getId();
char phase = 'b';
validateArgs(args);
Expand Down Expand Up @@ -741,7 +767,7 @@ public static void traceAsyncStart(Logger logger, Level level, String name, Stri
* Additional arguments to log
*/
public static void traceAsyncNested(Logger logger, Level level, String name, String category, int id, Object... args) {
long time = System.nanoTime();
long time = currentTimeNanos();
long threadId = Thread.currentThread().getId();
char phase = 'n';
validateArgs(args);
Expand Down Expand Up @@ -776,7 +802,7 @@ public static void traceAsyncNested(Logger logger, Level level, String name, Str
* Additional arguments to log
*/
public static void traceAsyncEnd(Logger logger, Level level, String name, String category, int id, Object... args) {
long time = System.nanoTime();
long time = currentTimeNanos();
long threadId = Thread.currentThread().getId();
char phase = 'e';
validateArgs(args);
Expand Down Expand Up @@ -810,7 +836,7 @@ public static void traceAsyncEnd(Logger logger, Level level, String name, String
* Additional arguments to log
*/
public static void traceInstant(Logger logger, Level level, String name, Object... args) {
long time = System.nanoTime();
long time = currentTimeNanos();
long threadId = Thread.currentThread().getId();
char phase = 'i';
validateArgs(args);
Expand Down Expand Up @@ -838,7 +864,7 @@ public static void traceInstant(Logger logger, Level level, String name, Object.
* The counters to log in the format : "title", value
*/
public static void traceCounter(Logger logger, Level level, String name, Object... args) {
long time = System.nanoTime();
long time = currentTimeNanos();
long threadId = Thread.currentThread().getId();
char phase = 'C';
validateArgs(args);
Expand Down Expand Up @@ -870,7 +896,7 @@ public static void traceCounter(Logger logger, Level level, String name, Object.
* "color" and an rbga will be used
*/
public static void traceMarker(Logger logger, Level level, String name, long duration, Object... args) {
long time = System.nanoTime();
long time = currentTimeNanos();
long threadId = Thread.currentThread().getId();
char phase = 'R';
validateArgs(args);
Expand Down