Skip to content

Commit 4b40f2e

Browse files
authored
add IFlowScopeLog interface in LogUtils (#34)
This allows a wrapped object to be used as parent scope for the builder. Signed-off-by: Patrick Tasse <[email protected]>
1 parent f42d4d8 commit 4b40f2e

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

src/main/java/org/eclipse/tracecompass/trace_event_logger/LogUtils.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@
118118
*/
119119
public final class LogUtils {
120120

121+
/**
122+
* Interface for flow scope logger
123+
*/
124+
public interface IFlowScopeLog {
125+
126+
/**
127+
* Get the category for this scope. The category can be injected to
128+
* other components that can use it for the scope loggers
129+
*
130+
* @return The category of this scope
131+
*/
132+
String getCategory();
133+
134+
/**
135+
* Get the ID for this scope. The ID can be injected to other components
136+
* that can use it for the scope loggers
137+
*
138+
* @return The ID of this scope
139+
*/
140+
int getId();
141+
}
142+
121143
private static final Format FORMAT = new DecimalFormat("#.###"); //$NON-NLS-1$
122144

123145
/*
@@ -292,7 +314,7 @@ public static class FlowScopeLogBuilder {
292314
private final Object[] fArgs;
293315
private int fId = Integer.MIN_VALUE;
294316
private String fCategory = null;
295-
private FlowScopeLog fParent = null;
317+
private IFlowScopeLog fParent = null;
296318
private boolean fHasParent = false;
297319

298320
/**
@@ -321,7 +343,7 @@ public FlowScopeLogBuilder(Logger logger, Level level, String label, Object... a
321343
* will be automatically generated.
322344
*
323345
* This method is mutually exclusive with
324-
* {@link #setParentScope(FlowScopeLog)}. Calling both will throw an
346+
* {@link #setParentScope(IFlowScopeLog)}. Calling both will throw an
325347
* exception.
326348
*
327349
* @param category
@@ -340,7 +362,7 @@ public FlowScopeLogBuilder setCategory(String category) {
340362
* Set a category and ID for the flow scope.
341363
*
342364
* This method is mutually exclusive with
343-
* {@link #setParentScope(FlowScopeLog)}. Calling both will throw an
365+
* {@link #setParentScope(IFlowScopeLog)}. Calling both will throw an
344366
* exception.
345367
*
346368
* @param category
@@ -373,7 +395,7 @@ public FlowScopeLogBuilder setCategoryAndId(String category, int id) {
373395
* The parent scope
374396
* @return This builder
375397
*/
376-
public FlowScopeLogBuilder setParentScope(FlowScopeLog parent) {
398+
public FlowScopeLogBuilder setParentScope(IFlowScopeLog parent) {
377399
if (fCategory != null) {
378400
throw new IllegalStateException("FlowScopeLogBuilder: Cannot set a parent scope if a category has already been set"); //$NON-NLS-1$
379401
}
@@ -387,10 +409,10 @@ public FlowScopeLogBuilder setParentScope(FlowScopeLog parent) {
387409
* @return The flow scope log
388410
*/
389411
public FlowScopeLog build() {
390-
FlowScopeLog parent = fParent;
412+
IFlowScopeLog parent = fParent;
391413
if (parent != null) {
392414
// Has a parent scope, so step in flow
393-
return new FlowScopeLog(fLogger, fLevel, fLabel, parent.fCategory, parent.fId, false, fArgs);
415+
return new FlowScopeLog(fLogger, fLevel, fLabel, parent.getCategory(), parent.getId(), false, fArgs);
394416
}
395417
return new FlowScopeLog(fLogger, fLevel, fLabel, String.valueOf(fCategory), (fId == Integer.MIN_VALUE ? ID_GENERATOR.incrementAndGet() : fId), !fHasParent, fArgs);
396418
}
@@ -430,7 +452,7 @@ public FlowScopeLog build() {
430452
* INFO: {"ts":"12420,"ph":"f","tid":0,"cat":"category", "id":256}
431453
* }</pre>
432454
*/
433-
public static class FlowScopeLog implements AutoCloseable {
455+
public static class FlowScopeLog implements IFlowScopeLog, AutoCloseable {
434456

435457
private final long fThreadId;
436458
private final Logger fLogger;
@@ -541,12 +563,12 @@ public void addData(String name, Object value) {
541563
fData.put(name, value);
542564
}
543565

544-
/**
545-
* Get the ID for this scope. The ID can be injected to other components
546-
* that can use it for the scope loggers
547-
*
548-
* @return The ID of this scope
549-
*/
566+
@Override
567+
public String getCategory() {
568+
return fCategory;
569+
}
570+
571+
@Override
550572
public int getId() {
551573
return fId;
552574
}

src/test/java/org/eclipse/tracecompass/trace_event_logger/LoggerTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2024 Ericsson
2+
* Copyright (c) 2024, 2025 Ericsson
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy
55
* of this software and associated documentation files (the “Software”), to deal
@@ -39,6 +39,7 @@
3939

4040
import org.eclipse.tracecompass.trace_event_logger.LogUtils.FlowScopeLog;
4141
import org.eclipse.tracecompass.trace_event_logger.LogUtils.FlowScopeLogBuilder;
42+
import org.eclipse.tracecompass.trace_event_logger.LogUtils.IFlowScopeLog;
4243
import org.junit.Before;
4344
import org.junit.Test;
4445

@@ -349,7 +350,7 @@ public void testFlowBuilderNoExtra() {
349350

350351
/**
351352
* Test the flow scope builder calling
352-
* {@link FlowScopeLogBuilder#setParentScope(FlowScopeLog)}, then
353+
* {@link FlowScopeLogBuilder#setParentScope(IFlowScopeLog)}, then
353354
* {@link FlowScopeLogBuilder#setCategory(String)}.
354355
*/
355356
@Test(expected = IllegalStateException.class)
@@ -366,7 +367,7 @@ public void testFlowBuilderCatThenParent() {
366367

367368
/**
368369
* Test the flow scope builder calling
369-
* {@link FlowScopeLogBuilder#setParentScope(FlowScopeLog)}, then
370+
* {@link FlowScopeLogBuilder#setParentScope(IFlowScopeLog)}, then
370371
* {@link FlowScopeLogBuilder#setCategory(String)}.
371372
*/
372373
@Test(expected = IllegalStateException.class)
@@ -384,7 +385,7 @@ public void testFlowBuilderParentThenCat() {
384385

385386
/**
386387
* Test the flow scope builder calling
387-
* {@link FlowScopeLogBuilder#setParentScope(FlowScopeLog)}, then
388+
* {@link FlowScopeLogBuilder#setParentScope(IFlowScopeLog)}, then
388389
* {@link FlowScopeLogBuilder#setCategory(String)}.
389390
*/
390391
@Test(expected = IllegalStateException.class)
@@ -401,7 +402,7 @@ public void testFlowBuilderParentThenCatId() {
401402

402403
/**
403404
* Test the flow scope builder calling
404-
* {@link FlowScopeLogBuilder#setParentScope(FlowScopeLog)}, then
405+
* {@link FlowScopeLogBuilder#setParentScope(IFlowScopeLog)}, then
405406
* {@link FlowScopeLogBuilder#setCategory(String)}.
406407
*/
407408
@Test(expected = IllegalStateException.class)

0 commit comments

Comments
 (0)