|
@@ -22,12 +22,12 @@
|
22
|
22
|
|
23
|
23
|
package com.dmdirc.logger;
|
24
|
24
|
|
25
|
|
-import java.io.File;
|
26
|
|
-import java.io.FileOutputStream;
|
27
|
25
|
import java.io.IOException;
|
28
|
26
|
import java.io.OutputStream;
|
29
|
27
|
import java.io.PrintWriter;
|
30
|
28
|
import java.io.Serializable;
|
|
29
|
+import java.nio.file.Files;
|
|
30
|
+import java.nio.file.Path;
|
31
|
31
|
import java.text.DateFormat;
|
32
|
32
|
import java.text.SimpleDateFormat;
|
33
|
33
|
import java.util.Date;
|
|
@@ -45,7 +45,7 @@ public final class ProgramError implements Serializable {
|
45
|
45
|
/** A version number for this class. */
|
46
|
46
|
private static final long serialVersionUID = 3;
|
47
|
47
|
/** Directory used to store errors. */
|
48
|
|
- private static File errorDir;
|
|
48
|
+ private static Path errorDir;
|
49
|
49
|
/** Semaphore used to serialise write access. */
|
50
|
50
|
private static final Semaphore WRITING_SEM = new Semaphore(1);
|
51
|
51
|
/** The reporter to use to send this error. */
|
|
@@ -208,7 +208,7 @@ public final class ProgramError implements Serializable {
|
208
|
208
|
*
|
209
|
209
|
* @param directory The directory to save the error in.
|
210
|
210
|
*/
|
211
|
|
- public void save(final String directory) {
|
|
211
|
+ public void save(final Path directory) {
|
212
|
212
|
try (PrintWriter out = new PrintWriter(getErrorFile(directory), true)) {
|
213
|
213
|
out.println("Date:" + getDate());
|
214
|
214
|
out.println("Level: " + getLevel());
|
|
@@ -229,30 +229,39 @@ public final class ProgramError implements Serializable {
|
229
|
229
|
* @return BufferedOutputStream to write to the error file
|
230
|
230
|
*/
|
231
|
231
|
@SuppressWarnings("PMD.SystemPrintln")
|
232
|
|
- private OutputStream getErrorFile(final String directory) {
|
|
232
|
+ private OutputStream getErrorFile(final Path directory) {
|
233
|
233
|
WRITING_SEM.acquireUninterruptibly();
|
234
|
234
|
|
235
|
|
- if (errorDir == null || !errorDir.exists()) {
|
236
|
|
- errorDir = new File(directory);
|
237
|
|
- if (!errorDir.exists()) {
|
238
|
|
- errorDir.mkdirs();
|
|
235
|
+ try {
|
|
236
|
+ if (errorDir == null || !Files.exists(errorDir)) {
|
|
237
|
+ errorDir = directory;
|
|
238
|
+ if (!Files.exists(errorDir)) {
|
|
239
|
+ Files.createDirectories(errorDir);
|
|
240
|
+ }
|
239
|
241
|
}
|
240
|
|
- }
|
241
|
|
-
|
242
|
|
- final String logName = getDate().getTime() + "-" + getLevel();
|
243
|
242
|
|
244
|
|
- final File errorFile = new File(errorDir, logName + ".log");
|
245
|
|
-
|
246
|
|
- if (errorFile.exists()) {
|
247
|
|
- boolean rename = false;
|
248
|
|
- for (int i = 0; !rename; i++) {
|
249
|
|
- rename = errorFile.renameTo(new File(errorDir, logName + '-' + i + ".log"));
|
|
243
|
+ final String logName = getDate().getTime() + "-" + getLevel();
|
|
244
|
+
|
|
245
|
+ final Path errorFile = errorDir.resolve(logName + ".log");
|
|
246
|
+
|
|
247
|
+ if (Files.exists(errorFile)) {
|
|
248
|
+ boolean rename = false;
|
|
249
|
+ for (int i = 0; !rename; i++) {
|
|
250
|
+ try {
|
|
251
|
+ Files.move(errorFile, errorDir.resolve(logName + '-' + i + ".log"));
|
|
252
|
+ rename = true;
|
|
253
|
+ } catch (IOException ex) {
|
|
254
|
+ rename = false;
|
|
255
|
+ if (i > 20) {
|
|
256
|
+ // Something's probably catestrophically wrong. Give up.
|
|
257
|
+ throw ex;
|
|
258
|
+ }
|
|
259
|
+ }
|
|
260
|
+ }
|
250
|
261
|
}
|
251
|
|
- }
|
252
|
262
|
|
253
|
|
- try {
|
254
|
|
- errorFile.createNewFile();
|
255
|
|
- return new FileOutputStream(errorFile);
|
|
263
|
+ Files.createFile(errorFile);
|
|
264
|
+ return Files.newOutputStream(errorFile);
|
256
|
265
|
} catch (IOException ex) {
|
257
|
266
|
System.err.println("Error creating new file: ");
|
258
|
267
|
ex.printStackTrace();
|