Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
2
Indexable
Never
  /**
   * Reassigns the {@link System#out} and {@link System#err} streams to a file in the {@code logs}
   * directory.
   *
   * <p>Additonally, it filters out the {@code sessionid} parameter from the {@code Setting user: }
   * line that is printed to the console by one of the classes within the client executable.
   *
   * @param username the {@code username} parameter from MinecraftApplet
   */
  public static void reassignOutputStream(String username) {
    Path filePath = getFilePath(username);
    if (Objects.isNull(filePath)) {
      return;
    }

    try {
      PrintStream ps =
          new PrintStream(filePath.toFile()) {
            @Override
            public void println(String x) {
              String filter = addToFilter(x);
              super.println(filter);
            }

            private String addToFilter(String x) {
              String settingUser = "Setting user: ";
              if (x.startsWith(settingUser)) {
                String[] split = x.split(",");
                String[] tokenSplit = split[1].split(":");
                if (tokenSplit.length == 3) {
                  String accessToken = tokenSplit[1];
                  String profileId = tokenSplit[2];
                  return new StringBuilder()
                      .append(split[0])
                      .append(", token:")
                      .append(accessToken, 0, 0)
                      .append("<accessToken>:")
                      .append(profileId)
                      .toString();
                }
              }
              return x;
            }
          };
      System.setOut(ps);
      System.setErr(ps);
    } catch (FileNotFoundException fnfe) {
      LOGGER.error("Cannot create {}", filePath, fnfe);
    }
  }
}