1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-08-13 17:03:45 -04:00

Simplify logging output

This commit is contained in:
Reinhard Pointner 2017-04-02 01:46:25 +08:00
parent eb7e393ddb
commit 83c1796cf9
4 changed files with 23 additions and 22 deletions

View File

@ -1,6 +1,7 @@
package net.filebot;
import static java.nio.channels.Channels.*;
import static java.util.Arrays.*;
import static java.util.stream.Collectors.*;
import java.io.File;
@ -22,7 +23,6 @@ import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.codehaus.groovy.runtime.StackTraceUtils;
@ -107,6 +107,10 @@ public final class Logging {
return () -> getMessage(null, t);
}
public static Supplier<String> cause(Object... elements) {
return () -> getMessage(elements);
}
private static String getMessage(String m, Throwable t) {
// try to unravel stacked exceptions
if (t instanceof RuntimeException && t.getCause() != null) {
@ -114,7 +118,11 @@ public final class Logging {
}
// e.g. Failed to create file: AccessDeniedException: /path/to/file
return Stream.of(m, t.getClass().getSimpleName(), t.getMessage()).filter(Objects::nonNull).map(Objects::toString).collect(joining(": "));
return getMessage(m, t.getClass().getSimpleName(), t.getMessage());
}
private static String getMessage(Object... elements) {
return stream(elements).filter(Objects::nonNull).map(Objects::toString).collect(joining(": "));
}
public static class ConsoleFormatter extends Formatter {

View File

@ -7,6 +7,7 @@ import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.AbstractMap;
@ -48,15 +49,15 @@ public class MetaAttributeView extends AbstractMap<String, String> {
try {
if (xattr instanceof UserDefinedFileAttributeView) {
UserDefinedFileAttributeView attributeView = (UserDefinedFileAttributeView) xattr;
int size = attributeView.size(key);
if (size > 0) {
ByteBuffer buffer = ByteBuffer.allocate(size);
try {
ByteBuffer buffer = ByteBuffer.allocate(attributeView.size(key));
attributeView.read(key, buffer);
buffer.flip();
return UTF_8.decode(buffer).toString();
} else {
return null; // attribute does not exist
} catch (NoSuchFileException e) {
// attribute does not exist
return null;
}
}

View File

@ -45,7 +45,7 @@ public class MetaAttributes {
public long getCreationDate(long time) {
try {
return fileAttributeView.readAttributes().creationTime().toMillis();
} catch (Exception e) {
} catch (IOException e) {
return 0;
}
}
@ -59,21 +59,13 @@ public class MetaAttributes {
}
public void setObject(Object object) {
try {
metaAttributeView.put(METADATA_KEY, toJson(object));
} catch (Exception e) {
throw new RuntimeException(e);
}
metaAttributeView.put(METADATA_KEY, toJson(object));
}
public Object getObject() {
try {
String json = metaAttributeView.get(METADATA_KEY);
if (json != null && json.length() > 0) {
return toObject(json);
}
} catch (Exception e) {
throw new RuntimeException(e);
String json = metaAttributeView.get(METADATA_KEY);
if (json != null && json.length() > 0) {
return toObject(json);
}
return null;
}

View File

@ -78,9 +78,9 @@ public class XattrMetaInfo {
// make file writable if necessary
if (!f.canWrite()) {
if (f.setWritable(true)) {
debug.finest("Grant write permissions: " + f);
debug.fine(cause("Grant write permissions", f));
} else {
debug.warning("Failed to grant write permissions: " + f);
debug.warning(cause("Failed to grant write permissions", f));
}
}
return f;