Skip to content

Commit 9c74993

Browse files
committed
Use consistent time unit format in logs
1 parent b943657 commit 9c74993

File tree

5 files changed

+51
-44
lines changed

5 files changed

+51
-44
lines changed

Core/src/main/java/mezz/jei/core/util/LoggedTimer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public void start(String message) {
1818

1919
public void stop() {
2020
stopWatch.stop();
21-
LOGGER.info("{} took {}", message, stopWatch);
21+
LOGGER.info("{} took {}", message, TimeUtil.toHumanString(stopWatch.elapsed()));
2222
}
2323
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package mezz.jei.core.util;
2+
3+
import java.time.Duration;
4+
import java.util.Locale;
5+
import java.util.concurrent.TimeUnit;
6+
7+
public class TimeUtil {
8+
public static String toHumanString(Duration duration) {
9+
TimeUnit unit = getSmallestUnit(duration);
10+
long nanos = duration.toNanos();
11+
double value = (double) nanos / TimeUnit.NANOSECONDS.convert(1, unit);
12+
return String.format(Locale.ROOT, "%.4g %s", value, unitToString(unit));
13+
}
14+
15+
private static TimeUnit getSmallestUnit(Duration duration) {
16+
if (duration.toDays() > 0) {
17+
return TimeUnit.DAYS;
18+
}
19+
if (duration.toHours() > 0) {
20+
return TimeUnit.HOURS;
21+
}
22+
if (duration.toMinutes() > 0) {
23+
return TimeUnit.MINUTES;
24+
}
25+
if (duration.toSeconds() > 0) {
26+
return TimeUnit.SECONDS;
27+
}
28+
if (duration.toMillis() > 0) {
29+
return TimeUnit.MILLISECONDS;
30+
}
31+
if (duration.toNanos() > 1000) {
32+
return TimeUnit.MICROSECONDS;
33+
}
34+
return TimeUnit.NANOSECONDS;
35+
}
36+
37+
private static String unitToString(TimeUnit unit) {
38+
return unit.name().toLowerCase(Locale.ROOT);
39+
}
40+
}

Library/src/main/java/mezz/jei/library/load/PluginCaller.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.base.Stopwatch;
44
import mezz.jei.api.IModPlugin;
5+
import mezz.jei.core.util.TimeUtil;
56
import mezz.jei.library.plugins.vanilla.VanillaPlugin;
67
import net.minecraft.resources.ResourceLocation;
78
import org.apache.logging.log4j.LogManager;
@@ -35,6 +36,6 @@ public static void callOnPlugins(String title, List<IModPlugin> plugins, Consume
3536
}
3637
}
3738

38-
LOGGER.info("{} took {}", title, stopwatch);
39+
LOGGER.info("{} took {}", title, TimeUtil.toHumanString(stopwatch.elapsed()));
3940
}
4041
}
Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package mezz.jei.library.load;
22

3+
import mezz.jei.core.util.TimeUtil;
34
import net.minecraft.resources.ResourceLocation;
45
import org.apache.logging.log4j.LogManager;
56
import org.apache.logging.log4j.Logger;
67

78
import java.time.Duration;
8-
import java.util.Locale;
9-
import java.util.concurrent.TimeUnit;
10-
11-
import static java.util.concurrent.TimeUnit.*;
129

1310
public class PluginCallerTimerRunnable {
1411
private static final Logger LOGGER = LogManager.getLogger();
@@ -33,48 +30,16 @@ public void check() {
3330
Duration elapsed = Duration.ofNanos(System.nanoTime() - this.startTime);
3431
long elapsedMs = elapsed.toMillis();
3532
if (elapsedMs > nextLongReportDurationMs) {
36-
LOGGER.error("{}: {} is running and has taken {} so far", title, pluginUid, toHumanString(elapsed));
33+
LOGGER.error("{}: {} is running and has taken {} so far", title, pluginUid, TimeUtil.toHumanString(elapsed));
3734
nextLongReportDurationMs += longReportDurationInterval;
3835
}
3936
}
4037

4138
public void stop() {
4239
Duration elapsed = Duration.ofNanos(System.nanoTime() - this.startTime);
4340
if (elapsed.toMillis() > startReportDurationMs) {
44-
LOGGER.info("{}: {} took {}", title, pluginUid, toHumanString(elapsed));
45-
}
46-
}
47-
48-
private static String toHumanString(Duration duration) {
49-
TimeUnit unit = getSmallestUnit(duration);
50-
long nanos = duration.toNanos();
51-
double value = (double) nanos / NANOSECONDS.convert(1, unit);
52-
return String.format(Locale.ROOT, "%.4g %s", value, unitToString(unit));
53-
}
54-
55-
private static TimeUnit getSmallestUnit(Duration duration) {
56-
if (duration.toDays() > 0) {
57-
return DAYS;
41+
LOGGER.info("{}: {} took {}", title, pluginUid, TimeUtil.toHumanString(elapsed));
5842
}
59-
if (duration.toHours() > 0) {
60-
return HOURS;
61-
}
62-
if (duration.toMinutes() > 0) {
63-
return MINUTES;
64-
}
65-
if (duration.toSeconds() > 0) {
66-
return SECONDS;
67-
}
68-
if (duration.toMillis() > 0) {
69-
return MILLISECONDS;
70-
}
71-
if (duration.toNanos() > 1000) {
72-
return MICROSECONDS;
73-
}
74-
return NANOSECONDS;
7543
}
7644

77-
private static String unitToString(TimeUnit unit) {
78-
return unit.name().toLowerCase(Locale.ROOT);
79-
}
8045
}

Library/src/main/java/mezz/jei/library/recipes/PluginManager.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import mezz.jei.api.recipe.RecipeType;
77
import mezz.jei.api.recipe.advanced.IRecipeManagerPlugin;
88
import mezz.jei.api.recipe.category.IRecipeCategory;
9+
import mezz.jei.core.util.TimeUtil;
910
import mezz.jei.library.recipes.collect.RecipeTypeData;
1011
import org.apache.logging.log4j.LogManager;
1112
import org.apache.logging.log4j.Logger;
@@ -89,12 +90,12 @@ private <T> Stream<T> getRecipes(IRecipeManagerPlugin plugin, IRecipeCategory<T>
8990
}
9091

9192
private <T> T safeCallPlugin(IRecipeManagerPlugin plugin, Supplier<T> supplier, T defaultValue) {
92-
Stopwatch stopWatch = Stopwatch.createStarted();
93+
Stopwatch stopwatch = Stopwatch.createStarted();
9394
try {
9495
T result = supplier.get();
95-
stopWatch.stop();
96-
if (stopWatch.elapsed(TimeUnit.MILLISECONDS) > 10) {
97-
LOGGER.warn("Recipe registry plugin is slow, took {}. {}", stopWatch, plugin.getClass());
96+
stopwatch.stop();
97+
if (stopwatch.elapsed(TimeUnit.MILLISECONDS) > 10) {
98+
LOGGER.warn("Recipe registry plugin is slow, took {}. {}", TimeUtil.toHumanString(stopwatch.elapsed()), plugin.getClass());
9899
}
99100
return result;
100101
} catch (RuntimeException | LinkageError e) {

0 commit comments

Comments
 (0)