Skip to content

Commit b5b8435

Browse files
committed
Fixed logging.
1 parent 6285062 commit b5b8435

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

build/DnsMadeEasy.jsmooth

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</skeletonProperties>
2323
<skeletonProperties>
2424
<key>URL</key>
25-
<value></value>
25+
<value>http://java.com</value>
2626
</skeletonProperties>
2727
<skeletonProperties>
2828
<key>SingleProcess</key>

src/com/esotericsoftware/dnsmadeeasy/DnsMadeEasy.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.InputStreamReader;
1111
import java.io.OutputStream;
1212
import java.io.PrintStream;
13+
import java.io.PrintWriter;
1314
import java.io.StringWriter;
1415
import java.lang.Thread.UncaughtExceptionHandler;
1516
import java.net.URL;
@@ -19,51 +20,47 @@
1920
import java.util.TimerTask;
2021

2122
public class DnsMadeEasy {
23+
static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
24+
2225
String user = "", pass = "", id = "", lastIP = "";
2326
int minutes = 30;
2427
final File configFile = new File(System.getProperty("user.home"), ".dnsmadeeasy/config.txt");
25-
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
2628

2729
public DnsMadeEasy () throws IOException {
2830
loadConfig();
2931

32+
log("Started.", null);
3033
new Timer("Timer").schedule(new TimerTask() {
3134
public void run () {
32-
System.out.print(dateFormat.format(new Date()) + ", Started.");
3335
try {
3436
update(user, pass, id);
3537
} catch (IOException ex) {
3638
ex.printStackTrace();
3739
}
3840
}
3941
}, 0, minutes * 60 * 1000);
40-
41-
// Don't return so service isn't terminated.
42-
while (true) {
43-
synchronized (this) {
44-
try {
45-
wait();
46-
} catch (InterruptedException ignored) {
47-
}
48-
}
49-
}
5042
}
5143

5244
void update (String user, String pass, String id) throws IOException {
53-
String newIP = read("http://www.dnsmadeeasy.com/myip.jsp");
45+
String newIP;
46+
try {
47+
newIP = http("http://www.dnsmadeeasy.com/myip.jsp");
48+
} catch (IOException ex) {
49+
log("Error obtaining IP.", ex);
50+
return;
51+
}
5452
if (newIP.equals(lastIP)) return;
5553

56-
System.out.print(dateFormat.format(new Date()) + ", " + newIP + ", ");
57-
String result = read("http://www.dnsmadeeasy.com/servlet/updateip?username=" + user + "&password=" + pass + "&id=" + id
54+
String result = http("http://www.dnsmadeeasy.com/servlet/updateip?username=" + user + "&password=" + pass + "&id=" + id
5855
+ "&ip=" + newIP);
59-
System.out.println(result);
56+
log(newIP + ", " + result, null);
6057
if (result.equals("success")) {
6158
lastIP = newIP;
6259
saveConfig();
6360
}
6461
}
6562

66-
String read (String url) throws IOException {
63+
String http (String url) throws IOException {
6764
InputStreamReader reader = new InputStreamReader(new URL(url).openStream());
6865
StringWriter writer = new StringWriter(128);
6966
char[] buffer = new char[1024];
@@ -134,22 +131,29 @@ public void write (byte[] b, int off, int len) throws IOException {
134131
}
135132
}
136133

137-
static public void main (final String[] args) throws Exception {
134+
static void log (String message, Throwable ex) {
135+
if (ex != null) {
136+
StringWriter buffer = new StringWriter(1024);
137+
ex.printStackTrace(new PrintWriter(buffer));
138+
message += "\n" + buffer.toString();
139+
}
140+
System.out.println(dateFormat.format(new Date()) + " " + message);
141+
}
142+
143+
static public void main (String[] args) throws Exception {
138144
try {
139145
File dir = new File(System.getProperty("user.home"), ".dnsmadeeasy");
140146
dir.mkdirs();
141147
FileOutputStream logFile = new FileOutputStream(new File(dir, "dnsmadeeasy.log"));
142148
System.setOut(new PrintStream(new MultiplexOutputStream(System.out, logFile), true));
143149
System.setErr(new PrintStream(new MultiplexOutputStream(System.err, logFile), true));
144150
} catch (Throwable ex) {
145-
System.out.println("Unable to write log file.");
146-
ex.printStackTrace();
151+
log("Unable to write log file.", ex);
147152
}
148153

149154
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
150155
public void uncaughtException (Thread thread, Throwable ex) {
151-
ex.printStackTrace();
152-
System.out.println("Uncaught exception, exiting.");
156+
log("Uncaught exception, exiting.", ex);
153157
System.exit(0);
154158
}
155159
});

0 commit comments

Comments
 (0)