Skip to content

Commit cd179b7

Browse files
committed
refactor logging in DependencyLoader
1 parent 11c94f4 commit cd179b7

File tree

1 file changed

+57
-65
lines changed

1 file changed

+57
-65
lines changed

src/main/java/com/falsepattern/lib/dependencies/DependencyLoader.java

Lines changed: 57 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import com.falsepattern.lib.util.FileUtil;
2121
import lombok.*;
22+
import org.apache.logging.log4j.LogManager;
23+
import org.apache.logging.log4j.Logger;
24+
2225
import net.minecraft.launchwrapper.LaunchClassLoader;
2326

2427

@@ -33,6 +36,8 @@ public class DependencyLoader {
3336
private static final Map<String, Version> loadedLibraries = new HashMap<>();
3437
private static final Map<String, String> loadedLibraryMods = new HashMap<>();
3538
private static final Set<String> mavenRepositories = new HashSet<>();
39+
40+
private static final Logger log = LogManager.getLogger(Tags.MODNAME + " Dependency Downloader");
3641

3742
public static void addMavenRepo(String url) {
3843
mavenRepositories.add(url);
@@ -49,9 +54,7 @@ public static void loadLibrary(@NonNull String loadingModId,
4954
String devSuffix) {
5055
val suffix = FalsePatternLib.isDeveloperEnvironment() ? devSuffix : regularSuffix;
5156
val artifactLogName = String.format("%s:%s:%s%s", groupId, artifactId, preferredVersion, suffix != null ? "-" + suffix : "");
52-
FalsePatternLib.getLog()
53-
.info("Adding library {}, requested by mod {}", artifactLogName,
54-
loadingModId);
57+
log.info("Adding library {}, requested by mod {}", artifactLogName, loadingModId);
5558
var artifact = groupId + ":" + artifactId + ":" + suffix;
5659
if (loadedLibraries.containsKey(artifact)) {
5760
val currentVer = loadedLibraries.get(artifact);
@@ -61,35 +64,33 @@ public static void loadLibrary(@NonNull String loadingModId,
6164
val rangeString = "(minimum: " + minVersion + (maxVersion == null ? "" : ", maximum: " + maxVersion) + ")";
6265
if (minVersion.compareTo(currentVer) > 0 || (maxVersion != null && maxVersion.compareTo(currentVer) < 0)) {
6366
for (int i = 0; i < 16; i++) {
64-
FalsePatternLib.getLog().fatal("ALERT VVVVVVVVVVVV ALERT");
67+
log.fatal("ALERT VVVVVVVVVVVV ALERT");
6568
}
66-
FalsePatternLib.getLog()
67-
.fatal("Library {}:{}{} already loaded with version {}, " +
68-
"but a version in the range {} was requested! Thing may go horribly wrong! " +
69-
"Requested by mod: {}, previously loaded by mod: {}",
70-
groupId,
71-
artifactId,
72-
suffix != null ? ":" + suffix : "",
73-
currentVer,
74-
rangeString,
75-
loadingModId,
76-
loadedLibraryMods.get(artifact));
69+
log.fatal("Library {}:{}{} already loaded with version {}, " +
70+
"but a version in the range {} was requested! Thing may go horribly wrong! " +
71+
"Requested by mod: {}, previously loaded by mod: {}",
72+
groupId,
73+
artifactId,
74+
suffix != null ? ":" + suffix : "",
75+
currentVer,
76+
rangeString,
77+
loadingModId,
78+
loadedLibraryMods.get(artifact));
7779
for (int i = 0; i < 16; i++) {
78-
FalsePatternLib.getLog().fatal("ALERT ^^^^^^^^^^^^ ALERT");
80+
log.fatal("ALERT ^^^^^^^^^^^^ ALERT");
7981
}
8082
} else {
81-
FalsePatternLib.getLog()
82-
.info("Attempted loading of library {}:{}{} with preferred version {}, " +
83-
"but version {} was already loaded, which matched the range {}. This is not an " +
84-
"error. " + "Requested by mod: {}, previously loaded by mod: {}",
85-
groupId,
86-
artifactId,
87-
suffix != null ? ":" + suffix : "",
88-
preferredVersion,
89-
currentVer,
90-
rangeString,
91-
loadingModId,
92-
loadedLibraryMods.get(artifact));
83+
log.info("Attempted loading of library {}:{}{} with preferred version {}, " +
84+
"but version {} was already loaded, which matched the range {}. This is not an " +
85+
"error. " + "Requested by mod: {}, previously loaded by mod: {}",
86+
groupId,
87+
artifactId,
88+
suffix != null ? ":" + suffix : "",
89+
preferredVersion,
90+
currentVer,
91+
rangeString,
92+
loadingModId,
93+
loadedLibraryMods.get(artifact));
9394
}
9495
return;
9596
}
@@ -107,7 +108,7 @@ public static void loadLibrary(@NonNull String loadingModId,
107108
val libDir = new File(modsDir, "falsepattern");
108109
if (!libDir.exists()) {
109110
if (!libDir.mkdirs()) {
110-
FalsePatternLib.getLog().fatal("Failed to create directory {}", libDir);
111+
log.fatal("Failed to create directory {}", libDir);
111112
throw new RuntimeException("Failed to create directory " + libDir);
112113
}
113114
}
@@ -116,14 +117,12 @@ public static void loadLibrary(@NonNull String loadingModId,
116117
try {
117118
addToClasspath(file);
118119
loadedLibraries.put(artifact, preferredVersion);
119-
FalsePatternLib.getLog()
120-
.info("Library {} successfully loaded from disk!", artifactLogName);
120+
log.info("Library {} successfully loaded from disk!", artifactLogName);
121121
return;
122122
} catch (RuntimeException e) {
123-
FalsePatternLib.getLog()
124-
.warn("Failed to load library {} from file! Redownloading...", artifactLogName);
123+
log.warn("Failed to load library {} from file! Re-downloading...", artifactLogName);
125124
if (!file.delete()) {
126-
FalsePatternLib.getLog().fatal("Failed to delete file {}", file);
125+
log.fatal("Failed to delete file {}", file);
127126
throw new RuntimeException("Failed to delete file " + file);
128127
}
129128
}
@@ -133,7 +132,7 @@ public static void loadLibrary(@NonNull String loadingModId,
133132
((suffix != null) ? ":" + suffix : "") + ": " + Tags.MODNAME +
134133
" library downloading has been disabled in the config, and the library is not present " +
135134
"on disk! Requested by mod: " + loadingModId;
136-
FalsePatternLib.getLog().fatal(errorMessage);
135+
log.fatal(errorMessage);
137136
throw new IllegalStateException(errorMessage);
138137
}
139138
for (var repo : mavenRepositories) {
@@ -154,59 +153,52 @@ public static void loadLibrary(@NonNull String loadingModId,
154153
break;
155154
}
156155
val success = new AtomicBoolean(false);
157-
Internet.connect(new URL(url), (ex) -> {
158-
FalsePatternLib.getLog()
159-
.info("Artifact {} could not be downloaded from repo {}: {}", artifactLogName,
160-
finalRepo, ex.getMessage());
161-
}, (input) -> {
162-
FalsePatternLib.getLog().info("Downloading {} from {}", artifactLogName, finalRepo);
156+
Internet.connect(new URL(url),
157+
(ex) -> log.info("Artifact {} could not be downloaded from repo {}: {}", artifactLogName,
158+
finalRepo, ex.getMessage()),
159+
(input) -> {
160+
log.info("Downloading {} from {}", artifactLogName, finalRepo);
163161
download(input, file);
164-
FalsePatternLib.getLog().info("Downloaded {}", artifactLogName);
162+
log.info("Downloaded {}", artifactLogName);
165163
success.set(true);
166164
});
167165
if (success.get()) {
168-
FalsePatternLib.getLog().info("Validating checksum for {}", artifactLogName);
166+
log.info("Validating checksum for {}", artifactLogName);
169167
boolean hadChecksum = false;
170168
for (val checksumType : CHECKSUM_TYPES) {
171169
val checksumURL = url + "." + checksumType;
172170
val checksumFile = new File(libDir, jarName + "." + checksumType);
173-
FalsePatternLib.getLog().info("Attempting to get {} checksum...", checksumType);
171+
log.info("Attempting to get {} checksum...", checksumType);
174172
success.set(false);
175-
Internet.connect(new URL(checksumURL), (ex) -> {
176-
FalsePatternLib.getLog()
177-
.info("Could not get {} checksum for {}: {}", checksumType,
178-
artifactLogName, ex.getMessage());
179-
}, (input) -> {
180-
FalsePatternLib.getLog()
181-
.info("Downloading {} checksum for {}", checksumType, artifactLogName);
173+
Internet.connect(new URL(checksumURL),
174+
(ex) -> log.info("Could not get {} checksum for {}: {}", checksumType,
175+
artifactLogName, ex.getMessage()),
176+
(input) -> {
177+
log.info("Downloading {} checksum for {}", checksumType, artifactLogName);
182178
download(input, checksumFile);
183-
FalsePatternLib.getLog()
184-
.info("Downloaded {} checksum for {}", checksumType, artifactLogName);
179+
log.info("Downloaded {} checksum for {}", checksumType, artifactLogName);
185180
success.set(true);
186181
});
187182
if (success.get()) {
188183
val fileHash = hash(checksumType, file);
189184
val referenceHash = new String(Files.readAllBytes(checksumFile.toPath()));
190185
if (!fileHash.equals(referenceHash)) {
191-
FalsePatternLib.getLog()
192-
.error("Failed {} checksum validation for {}. Retrying download...",
193-
checksumType, artifactLogName);
186+
log.error("Failed {} checksum validation for {}. Retrying download...",
187+
checksumType, artifactLogName);
194188
file.delete();
195189
retry = true;
196190
continue redownload;
197191
}
198-
FalsePatternLib.getLog()
199-
.info("Successfully validated {} checksum for {}", checksumType,
200-
artifactLogName);
192+
log.info("Successfully validated {} checksum for {}", checksumType,
193+
artifactLogName);
201194
hadChecksum = true;
202195
break;
203196
}
204197
}
205198
if (!hadChecksum) {
206-
FalsePatternLib.getLog()
207-
.warn("The library {} had no checksum available on the repository.\n" +
208-
"There's a chance it might have gotten corrupted during download,\n" +
209-
"but we're loading it anyways.", artifactLogName);
199+
log.warn("The library {} had no checksum available on the repository.\n" +
200+
"There's a chance it might have gotten corrupted during download,\n" +
201+
"but we're loading it anyways.", artifactLogName);
210202
}
211203
loadedLibraries.put(artifact, preferredVersion);
212204
loadedLibraryMods.put(artifact, loadingModId);
@@ -219,7 +211,7 @@ public static void loadLibrary(@NonNull String loadingModId,
219211
}
220212
val errorMessage = "Failed to download library " + groupId + ":" + artifactId + ":" + preferredVersion + ((suffix != null) ? ":" + suffix : "") + " from any repository! Requested by mod: " +
221213
loadingModId;
222-
FalsePatternLib.getLog().fatal(errorMessage);
214+
log.fatal(errorMessage);
223215
throw new IllegalStateException(errorMessage);
224216
}
225217

@@ -264,7 +256,7 @@ private static void addToClasspath(File file) {
264256
try {
265257
val cl = (LaunchClassLoader) DependencyLoader.class.getClassLoader();
266258
cl.addURL(file.toURI().toURL());
267-
FalsePatternLib.getLog().info("Injected file {} into classpath!", file.getPath());
259+
log.info("Injected file {} into classpath!", file.getPath());
268260
} catch (Exception e) {
269261
throw new RuntimeException("Failed to add library to classpath: " + file.getAbsolutePath(), e);
270262
}

0 commit comments

Comments
 (0)