Skip to content

Commit

Permalink
Improved logging/docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
luckydonald committed May 10, 2016
1 parent b039b6c commit 17d2c29
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 15 deletions.
3 changes: 3 additions & 0 deletions logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# de.luckydonald.pipboyserver.PipBoyServer.input.Level = FINEST
de.luckydonald.pipboyserver.PipBoyServer.input..Level = FINEST

Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,33 @@ public static Database newWithDefault() {
Database db = new Database();
return db.fillWithDefault();
}

public Database fillWithDefault() {
return fillWithDefault(this);
}

/**
* Creates a new database and adds some very basic values into the database.
* @return The created database
*/
public static Database newWithBasicDefault() {
Database db = new Database();
return db.fillWithBasicDefault();
}

/**
* Inserts some very basic values into the database.
* @return The created database
*/
public Database fillWithBasicDefault() {
return fillWithBasicDefault(this);
}

/**
* Inserts some very basic values into the database.
* @param db The database
* @return The same database
*/
public static Database fillWithBasicDefault(Database db) {
try {
DBDict rootDict = new DBDict(null);
Expand Down Expand Up @@ -500,6 +517,13 @@ public static Database fillWithBasicDefault(Database db) {
return db;

}

/**
* Inserts values into the database.
*
* @param db The database
* @return The same database
*/
public static Database fillWithDefault(Database db) {
ObjectMapper mapper = new ObjectMapper();
//mapper.registerModule(new Jdk8Module());
Expand All @@ -526,8 +550,8 @@ public static Database fillWithDefault(Database db) {
}
}
}
// else
return fillWithBasicDefault(db);
// src can be a File, URL, InputStream etc
}

public void loadJsonRoot(ObjectNode node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private static BufferedInputStream from(URL url) throws IOException {
}

public UnsignedLong uint64_t() throws IOException {
long posBefore = this.pos;
long i1 = readByte(),
i2 = readByte(),
i3 = readByte(),
Expand All @@ -58,9 +59,12 @@ public UnsignedLong uint64_t() throws IOException {
i7 = readByte(),
i8 = readByte();
long integer = (i8 << 56) + (i7 << 48) + (i6 << 40) + (i5 << 32) + (i4 << 24) + (i3 << 16) + (i2 << 8) + (i1);
return new UnsignedLong(integer);
UnsignedLong result = new UnsignedLong(integer);
getLogger().fine("uint64_t: " + result + " (" + posBefore + "-" + pos + ")");
return result;
}
public SignedLong int64_t() throws IOException {
long posBefore = this.pos;
long i1 = readByte(),
i2 = readByte(),
i3 = readByte(),
Expand All @@ -70,43 +74,61 @@ public SignedLong int64_t() throws IOException {
i7 = readByte(),
i8 = readByte();
long integer = (i8 << 56) + (i7 << 48) + (i6 << 40) + (i5 << 32) + (i4 << 24) + (i3 << 16) + (i2 << 8) + (i1);
return new SignedLong(integer);
SignedLong result = new SignedLong(integer);
getLogger().fine("int64_t: " + result + " (" + posBefore + "-" + pos + ")");
return result;
}

public UnsignedInteger uint32_t() throws IOException {
long posBefore = this.pos;
int i1 = readByte(),
i2 = readByte(),
i3 = readByte(),
i4 = readByte();
int integer = (i4 << 24) + (i3 << 16) + (i2 << 8) + (i1);
return new UnsignedInteger(integer);
UnsignedInteger result = new UnsignedInteger(integer);
getLogger().fine("uint32_t: " + result + " (" + posBefore + "-" + pos + ")");
return result;
}
public SignedInteger int32_t() throws IOException {
long posBefore = this.pos;
int i1 = readByte(),
i2 = readByte(),
i3 = readByte(),
i4 = readByte();
int integer = (i4 << 24) + (i3 << 16) + (i2 << 8) + (i1);
return new SignedInteger(integer);
SignedInteger result = new SignedInteger(integer);
getLogger().fine("int32_t: " + result + " (" + posBefore + "-" + pos + ")");
return result;
}

public UnsignedByte uint8_t() throws IOException {
long posBefore = this.pos;
Integer i1 = readByte();
return new UnsignedByte(i1.byteValue());
UnsignedByte result = new UnsignedByte(i1.byteValue());
getLogger().fine("uint8_t: " + result + " (" + posBefore + "-" + pos + ")");
return result;
}
public SignedByte int8_t() throws IOException {
long posBefore = this.pos;
Integer i1 = readByte();
return new SignedByte(i1.byteValue());
SignedByte result = new SignedByte(i1.byteValue());
getLogger().fine("int8_t: " + result + " (" + posBefore + "-" + pos + ")");
return result;
}
public Float float32_t() throws IOException {
long posBefore = this.pos;
int i1 = readByte(),
i2 = readByte(),
i3 = readByte(),
i4 = readByte();
int integer = (i4 << 24) + (i3 << 16) + (i2 << 8) + (i1);
return Float.intBitsToFloat(integer);
Float result = Float.intBitsToFloat(integer);
getLogger().fine("float32_t: " + result + " (" + posBefore + "-" + pos + ")");
return result;
}
public Double float64_t() throws IOException {
long posBefore = this.pos;
long i1 = readByte(),
i2 = readByte(),
i3 = readByte(),
Expand All @@ -116,22 +138,27 @@ public Double float64_t() throws IOException {
i7 = readByte(),
i8 = readByte();
long integer = (i8 << 56) + (i7 << 48) + (i6 << 40) + (i5 << 32) + (i4 << 24) + (i3 << 16) + (i2 << 8) + (i1);
return Double.longBitsToDouble(integer);
Double result = Double.longBitsToDouble(integer);
getLogger().fine("float64_t: " + result + " (" + posBefore + "-" + pos + ")");
return result;
}
public String string_t() throws IOException {
/*struct String {
uint32_t length;
char str[length];
};
*/
long posBefore = this.pos;
ByteArrayOutputStream b = new ByteArrayOutputStream();
UnsignedInteger length = uint32_t();
for (long i = 0; i < length.asLong(); i++) {
int c;
c = readByte();
b.write(c);
}
return new String(b.toByteArray(), "UTF-8");
String result = new String(b.toByteArray(), "UTF-8");
getLogger().fine("string_t: " + result + " (" + posBefore + "-" + pos + ")");
return result;
}

public synchronized int readByte() throws IOException {
Expand Down Expand Up @@ -163,16 +190,21 @@ public DBEntry readNextEntry(Database db) throws IOException {
DBEntry d;
if (value_type == 0) {
// Primitive
getLogger().fine("Primitive " + value_id + " start: " + (pos-5));
d = this.readPrimitive(db, value_id);
getLogger().fine("Primitive " + value_id + " ended: " + pos + " > " + d.toSimpleString());
} else if (value_type == 1) {
// array/list
getLogger().fine("List " + value_id + " start: " + (pos-5));
d = this.readList(db, value_id);
getLogger().fine("List " + value_id + " ended: " + pos + " > " + d.toSimpleString());
} else if (value_type == 2) {
// object/dict
getLogger().fine("Dict " + value_id + " start: " + (pos-5));
d = this.readDict(db, value_id);
getLogger().fine("Dict " + value_id + " ended: " + pos + " > " + d.toSimpleString());
} else {
getLogger().info("Position: " + pos);
throw new InvalidObjectException("unknown type: " + value_type);
throw new InvalidObjectException("unknown type: " + value_type + ", position: " + pos);
}
jumpOut(logger);
return d;
Expand Down Expand Up @@ -203,6 +235,7 @@ private DBEntry readDict(Database db, UnsignedInteger value_id) throws IOExcepti
private DBList readList(Database db, UnsignedInteger value_id) throws IOException {
BinFileReadLogger logger = new BinFileReadLogger(pos);
getLoggerz().add(logger);
long posBefore = this.pos;
UnsignedInteger list_count = uint32_t();
DBList list = (DBList) db.add(value_id.getSignedValue(), new DBList());
for ( long i = 0; i < list_count.asLong(); i++) {
Expand Down
28 changes: 25 additions & 3 deletions src/main/java/de/luckydonald/utils/ObjectWithLogger.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package de.luckydonald.utils;

import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Created by luckydonald on 12.02.16.
*/
public class ObjectWithLogger {
static
private Logger logger = null;
public Logger getLogger() {
if (this.logger == null) {
this.logger = Logger.getLogger(this.getClass().getCanonicalName());
if (logger == null) {
logger = Logger.getLogger(this.getClass().getCanonicalName());
}
return this.logger;
logger.setLevel(Level.FINE);
return logger;
}
private static Logger staticLogger;
public static Logger getStaticLogger() {
Expand All @@ -20,4 +25,21 @@ public static Logger getStaticLogger() {
}
return staticLogger;
}

public void addLogConsoleHandler() {
addLogConsoleHandler(null);
}
public void addLogConsoleHandler(Level l) {
if (l == null) {
l = Level.ALL;
}
// Create and set handler
Handler systemOut = new ConsoleHandler();
systemOut.setLevel( l );
getLogger().addHandler( systemOut );
getLogger().setLevel( l );

// Prevent logs from processed by default Console handler.
getLogger().setUseParentHandlers( false ); // Solution 1
}
}

0 comments on commit 17d2c29

Please sign in to comment.