Skip to content

Commit

Permalink
Add logging to file (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
krlvm committed May 13, 2020
1 parent bbe66c8 commit b15f9a0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/ru/krlvm/powertunnel/PowerTunnel.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public static void main(String[] args) {
" -port [Port] sets port\n" +
" -enable-journal enables PowerTunnel journal (when UI enabled)\n" +
" -enable-logs enables PowerTunnel logs (when UI enabled)\n" +
" -enable-log-to-file enables PowerTunnel logger and log file\n" +
" -with-web-ui [appendix] enables Web UI at http://" + String.format(PowerTunnelMonitor.FAKE_ADDRESS_TEMPLATE, "[appendix]") + "\n" +
" -disable-auto-proxy-setup disables auto proxy setup on Windows\n" +
" -auto-proxy-setup-win-ie auto proxy setup using IE instead of native API on Windows\n" +
Expand All @@ -151,6 +152,7 @@ public static void main(String[] args) {
}
case "full-output-mirroring": {
FULL_OUTPUT_MIRRORING = true;
Utility.print("[+] Enabled full output mirroring");
break;
}
case "debug": {
Expand Down Expand Up @@ -179,6 +181,10 @@ public static void main(String[] args) {
SETTINGS.setTemporaryValue(Settings.ENABLE_LOGS, "true");
break;
}
case "enable-log-to-file": {
Utility.initializeLogger();
break;
}
case "disable-auto-proxy-setup": {
SETTINGS.setTemporaryValue(Settings.AUTO_PROXY_SETUP_ENABLED, "false");
break;
Expand Down
2 changes: 1 addition & 1 deletion src/ru/krlvm/powertunnel/frames/LogFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static void print(String s) {
return;
}
instance.logArea.append(s);
if(!PowerTunnel.FULL_OUTPUT_MIRRORING) {
if(!s.endsWith("\n")) {
instance.logArea.append("\r\n");
}
instance.logArea.setCaretPosition(instance.logArea.getDocument().getLength());
Expand Down
6 changes: 6 additions & 0 deletions src/ru/krlvm/powertunnel/system/MirroredOutputStream.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.krlvm.powertunnel.system;

import ru.krlvm.powertunnel.frames.LogFrame;
import ru.krlvm.powertunnel.utilities.Utility;

import java.io.FilterOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -35,6 +36,11 @@ public void write(byte[] b, int off, int len) {
}

private void writeToLog(String s) {
if(Utility.LOGGER != null && !s.startsWith(MIRROR_TAG)) {
Utility.LOGGER.info(MIRROR_TAG + s);
}
LogFrame.print(s);
}

private static final String MIRROR_TAG = "[Mirrored] ";
}
31 changes: 29 additions & 2 deletions src/ru/krlvm/powertunnel/utilities/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ru.krlvm.powertunnel.PowerTunnel;
import ru.krlvm.powertunnel.frames.LogFrame;

import java.util.logging.*;

/**
* PowerTunnel Utilities
* Main utility class
Expand Down Expand Up @@ -35,8 +37,12 @@ public static void print(String message) {
print();
return;
}
System.out.println(message);
if(PowerTunnel.isUIEnabled() && !PowerTunnel.FULL_OUTPUT_MIRRORING) {
if(LOGGER == null) {
System.out.println(message);
} else {
LOGGER.info(message);
}
if(PowerTunnel.isUIEnabled() && (LOGGER != null || !PowerTunnel.FULL_OUTPUT_MIRRORING)) {
LogFrame.print(message);
}
}
Expand All @@ -47,4 +53,25 @@ public static void print(String message) {
public static void print() {
print("");
}

public static void initializeLogger() {
LOGGER = Logger.getLogger("PT");
try {
FileHandler handler = new FileHandler("powertunnel.log");
handler.setFormatter(new Formatter() {
@Override
public String format(LogRecord record) {
return String.valueOf(record.getLevel()) + ':' +
record.getMessage() + '\n';
}
});
LOGGER.addHandler(handler);
} catch (Exception ex) {
LOGGER = null;
print("[x] Failed to initialize logger: " + ex.getMessage());
Debugger.debug(ex);
}
}

public static Logger LOGGER;
}

0 comments on commit b15f9a0

Please sign in to comment.