Skip to content

Commit

Permalink
Do not require name for @log, instead infer based on var name
Browse files Browse the repository at this point in the history
  • Loading branch information
superpenguin612 committed Dec 21, 2023
1 parent 1add9da commit ee16e93
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis
for (Method method : loggedObject.getClass().getDeclaredMethods()) {
Log subLogAnnotation = method.getAnnotation(Log.class);
if (subLogAnnotation != null) {
String varName = subLogAnnotation.name() != "" ? subLogAnnotation.name() : method.getName();
method.setAccessible(true);

Supplier<Object> valueSupplier = () -> {
Expand All @@ -82,7 +83,7 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis
}
};

Optional<Logger> optLogger = getLoggerForValue(valueSupplier, subLogAnnotation);
Optional<Logger> optLogger = getLoggerForValue(valueSupplier, subLogAnnotation, varName);
if (optLogger.isPresent()) {
loggers.add(optLogger.get());
}
Expand All @@ -93,6 +94,7 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis
field.setAccessible(true);
Log subLogAnnotation = field.getAnnotation(Log.class);
if (subLogAnnotation != null) {
String varName = subLogAnnotation.name().equals("") ? field.getName() : subLogAnnotation.name();
LoggedObject loggedObjectAnnotation = field.getType().getAnnotation(LoggedObject.class);
if (loggedObjectAnnotation != null) {
try {
Expand All @@ -102,7 +104,7 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis
updatedSubkeys.add(name);
updatedSubkeys.addAll(Arrays.asList(logGroups));

handleLoggedObject(field.get(loggedObject), subLogAnnotation.name(), updatedSubkeys);
handleLoggedObject(field.get(loggedObject), varName, updatedSubkeys);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -128,7 +130,7 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis
}
};

TunableDouble logger = new TunableDouble(getName(subLogAnnotation),
TunableDouble logger = new TunableDouble(getName(subLogAnnotation, varName),
(double) valueSupplier.get(), consumer);
loggers.add(logger);
} else if (value.getClass() == Boolean.class) {
Expand All @@ -140,13 +142,13 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis
}
};

TunableBoolean logger = new TunableBoolean(getName(subLogAnnotation),
TunableBoolean logger = new TunableBoolean(getName(subLogAnnotation, varName),
(boolean) valueSupplier.get(), consumer);
loggers.add(logger);
}

} else {
Optional<Logger> optLogger = getLoggerForValue(valueSupplier, subLogAnnotation);
Optional<Logger> optLogger = getLoggerForValue(valueSupplier, subLogAnnotation, varName);
if (optLogger.isPresent()) {
loggers.add(optLogger.get());
}
Expand All @@ -158,7 +160,8 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis
if (subSendableLogAnnotation != null) {
ArrayList<String> nameComponents = new ArrayList<String>();
nameComponents.addAll(Arrays.asList(subSendableLogAnnotation.groups()));
nameComponents.add(subSendableLogAnnotation.name());
String varName = subSendableLogAnnotation.name().equals("") ? field.getName() : subLogAnnotation.name();
nameComponents.add(varName);
String formattedName = String.join("/", nameComponents);

try {
Expand All @@ -176,10 +179,10 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis
new LogGroup(loggers.toArray(new Logger[loggers.size()])));
}

public static String getName(Log logAnnotation) {
public static String getName(Log logAnnotation, String varName) {
ArrayList<String> nameComponents = new ArrayList<String>();
nameComponents.addAll(Arrays.asList(logAnnotation.groups()));
nameComponents.add(logAnnotation.name());
nameComponents.add(varName);
return String.join("/", nameComponents);
}

Expand All @@ -189,10 +192,11 @@ public static String getName(Log logAnnotation) {
* @param object the object to get the value from
* @return
*/
public static Optional<Logger> getLoggerForValue(Supplier<Object> valueSupplier, Log logAnnotation) {
public static Optional<Logger> getLoggerForValue(Supplier<Object> valueSupplier, Log logAnnotation,
String varName) {
try {
Object value = valueSupplier.get();
String name = getName(logAnnotation);
String name = getName(logAnnotation, varName);

if (valueSupplier.get() instanceof Supplier) {
Map<Class<?>, Supplier<Logger>> supplierClassToLoggerMap = Map.ofEntries(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD })
public @interface Log {
public String name();
public String name() default "";

public String[] groups() default {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface SendableLog {
public String name();
public String name() default "";

public String[] groups() default {};

Expand Down

0 comments on commit ee16e93

Please sign in to comment.