log handling now in own thread

This commit is contained in:
bixilon 2020-06-06 00:21:42 +02:00
parent faae40e0dc
commit eb05a86704
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 39 additions and 8 deletions

View File

@ -16,6 +16,9 @@ public class Minosoft {
static Configuration config; static Configuration config;
public static void main(String[] args) { public static void main(String[] args) {
// int log thread
Log.initThread();
Log.info("Starting..."); Log.info("Starting...");
setConfigFolder(); setConfigFolder();
Log.info("Reading config file..."); Log.info("Reading config file...");
@ -39,20 +42,25 @@ public class Minosoft {
* Sets Config.homeDir to the correct folder per OS * Sets Config.homeDir to the correct folder per OS
*/ */
public static void setConfigFolder() { public static void setConfigFolder() {
String folder = System.getProperty("user.home"); String path = System.getProperty("user.home");
if (!folder.endsWith(File.separator)) { if (!path.endsWith(File.separator)) {
folder += "/"; path += "/";
} }
switch (OSUtil.getOS()) { switch (OSUtil.getOS()) {
case LINUX: case LINUX:
folder += ".local/share/minosoft/"; path += ".local/share/minosoft/";
break; break;
case WINDOWS: case WINDOWS:
Config.homeDir = "AppData/Roaming/Minosoft/"; path = "AppData/Roaming/Minosoft/";
break; break;
//ToDo: Mac, Other //ToDo: Mac, Other
} }
Config.homeDir = folder; File folder = new File(path);
if (!folder.exists() && !folder.mkdirs()) {
// failed creating folder
throw new RuntimeException(String.format("Could not create home folder (%s)!", path));
}
Config.homeDir = path;
} }
public static Configuration getConfig() { public static Configuration getConfig() {

View File

@ -28,7 +28,6 @@ public class World {
} }
public Block getBlock(BlockPosition pos) { public Block getBlock(BlockPosition pos) {
//ToDo
ChunkLocation loc = pos.getChunkLocation(); ChunkLocation loc = pos.getChunkLocation();
if (getChunk(loc) != null) { if (getChunk(loc) != null) {
return getChunk(loc).getBlock(pos.getX() % 16, pos.getX(), pos.getZ() % 16); return getChunk(loc).getBlock(pos.getX() % 16, pos.getX(), pos.getZ() % 16);

View File

@ -1,17 +1,41 @@
package de.bixilon.minosoft.logging; package de.bixilon.minosoft.logging;
import de.bixilon.minosoft.util.Util;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class Log { public class Log {
static LogLevel level = LogLevel.PROTOCOL; static LogLevel level = LogLevel.PROTOCOL;
final static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); final static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
final static List<String> queue = new ArrayList<>();
public static void log(LogLevel l, String message) { public static void log(LogLevel l, String message) {
if (l.getId() > level.getId()) { if (l.getId() > level.getId()) {
// log level too low // log level too low
return; return;
} }
System.out.println(String.format("[%s] [%s] %s", timeFormat.format(System.currentTimeMillis()), l.name(), message)); queue.add(String.format("[%s] [%s] %s", timeFormat.format(System.currentTimeMillis()), l.name(), message));
}
public static void initThread() {
Thread logThread = new Thread(() -> {
while (true) {
while (queue.size() > 0) {
// something to print
System.out.println(queue.get(0));
//ToDo: log to file
queue.remove(0);
}
Util.sleep(1);
}
});
logThread.start();
} }
/** /**