-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1500 from NASA-AMMOS/refactor/streamline-logging
Add streamline Logger
- Loading branch information
Showing
4 changed files
with
140 additions
and
48 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/debugging/Logger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package gov.nasa.jpl.aerie.contrib.streamline.debugging; | ||
|
||
import gov.nasa.jpl.aerie.merlin.framework.Registrar; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
import static java.util.stream.Collectors.toMap; | ||
|
||
public class Logger { | ||
private static final int LEVEL_INDICATOR_SIZE = Arrays.stream(LogLevel.values()) | ||
.map(v -> v.toString().length()) | ||
.max(Integer::compareTo) | ||
.orElseThrow(); | ||
private static final String LOG_MESSAGE_FORMAT = "[%-" + LEVEL_INDICATOR_SIZE + "s] %s"; | ||
|
||
private final Map<LogLevel, SimpleLogger> subLoggers; | ||
|
||
public Logger(Registrar registrar) { | ||
subLoggers = Arrays.stream(LogLevel.values()).collect(toMap( | ||
level -> level, | ||
level -> new SimpleLogger(level.name(), registrar))); | ||
} | ||
|
||
public void log(LogLevel level, String messageFormat, Object... args) { | ||
String message = messageFormat.formatted(args); | ||
subLoggers.get(level).log(LOG_MESSAGE_FORMAT.formatted(level, message)); | ||
} | ||
|
||
public void debug(String messageFormat, Object... args) { | ||
log(LogLevel.DEBUG, messageFormat, args); | ||
} | ||
|
||
public void info(String messageFormat, Object... args) { | ||
log(LogLevel.INFO, messageFormat, args); | ||
} | ||
|
||
public void warning(String messageFormat, Object... args) { | ||
log(LogLevel.WARNING, messageFormat, args); | ||
} | ||
|
||
public void error(String messageFormat, Object... args) { | ||
log(LogLevel.ERROR, messageFormat, args); | ||
} | ||
|
||
public enum LogLevel { | ||
DEBUG, | ||
INFO, | ||
WARNING, | ||
ERROR | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/debugging/Logging.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package gov.nasa.jpl.aerie.contrib.streamline.debugging; | ||
|
||
import gov.nasa.jpl.aerie.merlin.framework.Registrar; | ||
|
||
public final class Logging { | ||
private Logging() {} | ||
|
||
/** | ||
* The "main" logger. Unless you have a compelling reason to direct logging somewhere else, | ||
* this logger should be used by virtually all model components. | ||
* This logger will be initialized automatically when a registrar is constructed. | ||
*/ | ||
public static Logger LOGGER; | ||
|
||
/** | ||
* Initialize the primary logger. | ||
* This is called when constructing a {@link gov.nasa.jpl.aerie.contrib.streamline.modeling.Registrar}, | ||
* and does not need to be called directly by the model. | ||
*/ | ||
public static void init(final Registrar registrar) { | ||
if (LOGGER == null) { | ||
LOGGER = new Logger(registrar); | ||
} else { | ||
LOGGER.warning("Attempting to re-initialize primary logger. This attempt is being ignored."); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/debugging/SimpleLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package gov.nasa.jpl.aerie.contrib.streamline.debugging; | ||
|
||
import gov.nasa.jpl.aerie.merlin.framework.CellRef; | ||
import gov.nasa.jpl.aerie.merlin.framework.Registrar; | ||
import gov.nasa.jpl.aerie.merlin.protocol.model.CellType; | ||
import gov.nasa.jpl.aerie.merlin.protocol.model.EffectTrait; | ||
import gov.nasa.jpl.aerie.merlin.protocol.types.Unit; | ||
|
||
import static gov.nasa.jpl.aerie.contrib.serialization.rulesets.BasicValueMappers.string; | ||
import static gov.nasa.jpl.aerie.merlin.protocol.types.Unit.UNIT; | ||
|
||
public class SimpleLogger { | ||
private final CellRef<String, Unit> cellRef = CellRef.allocate(UNIT, new CellType<>() { | ||
@Override | ||
public EffectTrait<Unit> getEffectType() { | ||
return new EffectTrait<>() { | ||
@Override | ||
public Unit empty() { | ||
return UNIT; | ||
} | ||
|
||
@Override | ||
public Unit sequentially(Unit prefix, Unit suffix) { | ||
return UNIT; | ||
} | ||
|
||
@Override | ||
public Unit concurrently(Unit left, Unit right) { | ||
return UNIT; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Unit duplicate(Unit unit) { | ||
return unit; | ||
} | ||
|
||
@Override | ||
public void apply(Unit unit, Unit s) { | ||
} | ||
}, $ -> UNIT); | ||
|
||
public SimpleLogger(String name, Registrar registrar) { | ||
registrar.topic(name, cellRef, string()); | ||
} | ||
|
||
public void log(String message) { | ||
cellRef.emit(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters