Skip to content

Commit

Permalink
make another thread for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
awidesky committed May 25, 2021
1 parent a200249 commit 2ab0345
Showing 1 changed file with 67 additions and 11 deletions.
78 changes: 67 additions & 11 deletions src/com/awidesky/YoutubeClipboardAutoDownloader/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import javax.swing.SwingUtilities;

Expand All @@ -39,8 +40,12 @@ public class Main {
private static boolean isSecondtime = false;
private static ClipBoardCheckerThread clipChecker;
private static PrintWriter logTo;

private static GUI gui = new GUI();

private static LoggerThread logger = new LoggerThread();

private static LinkedBlockingQueue<Runnable> loggerQueue = new LinkedBlockingQueue<>();

private static volatile int taskNum = 0;

public static final String version = "v1.5.7";
Expand Down Expand Up @@ -244,6 +249,8 @@ private static void prepareLogFile() {
logTo = new PrintWriter(System.out, true);
GUI.error("Error when creating log flie", "%e%", e);

} finally {
logger.start();
}

}
Expand All @@ -260,13 +267,20 @@ private static void readProperties() {
try (BufferedReader br = new BufferedReader(new FileReader(new File(
YoutubeAudioDownloader.getProjectpath() + File.separator + "YoutubeAudioAutoDownloader-resources" + File.separator + "config.txt")))) {

p = Optional.of(br.readLine()).orElse("SavePath=" + p) .split("=")[1];
f = Optional.of(br.readLine()).orElse("Format=" + f) .split("=")[1];
q = Optional.of(br.readLine()).orElse("Quality=" + q) .split("=")[1];
l = Optional.of(br.readLine()).orElse("Playlist=" + l) .split("=")[1];
n = Optional.of(br.readLine()).orElse("FileNameFormat=" + n) .split("=")[1];
c = Optional.of(br.readLine()).orElse("ClipboardListenOption=" + c) .split("=")[1];

String p1 = Optional.of(br.readLine()).orElse("SavePath=" + p) .split("=")[1];
String f1 = Optional.of(br.readLine()).orElse("Format=" + f) .split("=")[1];
String q1 = Optional.of(br.readLine()).orElse("Quality=" + q) .split("=")[1];
String l1 = Optional.of(br.readLine()).orElse("Playlist=" + l) .split("=")[1];
String n1 = Optional.of(br.readLine()).orElse("FileNameFormat=" + n) .split("=")[1];
String c1 = Optional.of(br.readLine()).orElse("ClipboardListenOption=" + c) .split("=")[1];

p = p1.equals("null") ? p : p1;
f = f1.equals("null") ? f : f1;
q = q1.equals("null") ? q : q1;
l = l1.equals("null") ? l : l1;
n = n1.equals("null") ? n : n1;
c = c1.equals("null") ? c : c1;

} catch (FileNotFoundException e1) {

GUI.warning("config.txt not exists!","%e%\nDon't worry! I'll make one later...", e1);
Expand Down Expand Up @@ -337,13 +351,17 @@ public static void logProperties(String status) {

public static void log(String data) {

logTo.println(data);

loggerQueue.offer(() -> {
logTo.println(data);
});

}

public static void log(Exception e) {

e.printStackTrace(logTo);
loggerQueue.offer(() -> {
e.printStackTrace(logTo);
});

}

Expand All @@ -364,6 +382,15 @@ public static void kill(int exitcode) {
if (executorService != null && !executorService.isShutdown()) executorService.shutdownNow();

Main.writeProperties();

LoggerThread.isStop = true;

try {
logger.join();
} catch (InterruptedException e) {
e.printStackTrace(logTo);
}

logTo.close();
System.exit(exitcode);

Expand All @@ -390,5 +417,34 @@ public static void webBrowse(String link) {
}

}

private static class LoggerThread extends Thread {

public static volatile boolean isStop = false;

public LoggerThread() {

super(() -> {

while(true) {

if(loggerQueue.isEmpty() && isStop) {
return;
}

try {
loggerQueue.take().run();
} catch (InterruptedException e) {
logTo.println("LoggerThread Interrupted! : " + e.getMessage());
}
}

});

}

}

}


0 comments on commit 2ab0345

Please sign in to comment.