Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9de7cd
commit 7ea6778
Showing
8 changed files
with
342 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
<attribute name="test" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>utilities</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
See the NOTICE file distributed with this work for additional | ||
information regarding copyright ownership. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>utilities</artifactId> | ||
<packaging>jar</packaging> | ||
<name>ThingML :: Utilities</name> | ||
|
||
<parent> | ||
<groupId>org.thingml</groupId> | ||
<artifactId>org.thingml.root</artifactId> | ||
<version>2.0.0-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
</project> |
145 changes: 145 additions & 0 deletions
145
utilities/src/main/java/org/thingml/utilities/logging/BufferedLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
*/ | ||
package org.thingml.utilities.logging; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ListIterator; | ||
|
||
public class BufferedLogger extends Logger { | ||
private static class LogMsg { | ||
public enum Type { DEBUG, INFO, WARNING, ERROR }; | ||
|
||
public Type type; | ||
public String message; | ||
|
||
public LogMsg(Type type, String message) { | ||
this.type = type; | ||
this.message = message; | ||
} | ||
} | ||
|
||
List<LogMsg> messages; | ||
|
||
public BufferedLogger() { | ||
messages = new ArrayList<LogMsg>(); | ||
} | ||
|
||
/* --- Logger interface --- */ | ||
@Override | ||
public void debug(String message) { | ||
synchronized (messages) { | ||
messages.add(new LogMsg(LogMsg.Type.DEBUG, message)); | ||
} | ||
} | ||
|
||
@Override | ||
public void info(String message) { | ||
synchronized (messages) { | ||
messages.add(new LogMsg(LogMsg.Type.INFO, message)); | ||
} | ||
} | ||
|
||
@Override | ||
public void warning(String message) { | ||
synchronized (messages) { | ||
messages.add(new LogMsg(LogMsg.Type.WARNING, message)); | ||
} | ||
} | ||
|
||
@Override | ||
public void error(String message) { | ||
synchronized (messages) { | ||
messages.add(new LogMsg(LogMsg.Type.ERROR, message)); | ||
} | ||
} | ||
|
||
/* --- Access to buffered messages --- */ | ||
private boolean hasAny(LogMsg.Type type) { | ||
synchronized (messages) { | ||
if (messages.isEmpty()) return false; | ||
for (LogMsg message : messages) { | ||
if (message.type.equals(type)) return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
public boolean hasDebug() { return hasAny(LogMsg.Type.DEBUG); } | ||
public boolean hasInfo() { return hasAny(LogMsg.Type.INFO); } | ||
public boolean hasWarning() { return hasAny(LogMsg.Type.WARNING); } | ||
public boolean hasError() { return hasAny(LogMsg.Type.ERROR); } | ||
|
||
|
||
private String getAll(LogMsg.Type type, boolean remove) { | ||
synchronized (messages) { | ||
StringBuilder result = new StringBuilder(); | ||
ListIterator<LogMsg> it = messages.listIterator(); | ||
while (it.hasNext()) { | ||
LogMsg msg = it.next(); | ||
if (msg.type.equals(type)) | ||
result.append(msg.message+System.lineSeparator()); | ||
if (remove) | ||
it.remove(); | ||
} | ||
return result.toString(); | ||
} | ||
} | ||
|
||
public String getDebug(boolean remove) { return getAll(LogMsg.Type.DEBUG, remove); } | ||
public String getDebug() { return getDebug(true); } | ||
|
||
public String getInfo(boolean remove) { return getAll(LogMsg.Type.INFO, remove); } | ||
public String getInfo() { return getInfo(true); } | ||
|
||
public String getWarning(boolean remove) { return getAll(LogMsg.Type.WARNING, remove); } | ||
public String getWarning() { return getWarning(true); } | ||
|
||
public String getError(boolean remove) { return getAll(LogMsg.Type.ERROR, remove); } | ||
public String getError() { return getError(true); } | ||
|
||
|
||
/* --- Print the buffered messages to another logger --- */ | ||
public void print(Logger logger, boolean remove) { | ||
synchronized (messages) { | ||
ListIterator<LogMsg> it = messages.listIterator(); | ||
while (it.hasNext()) { | ||
LogMsg msg = it.next(); | ||
switch (msg.type) { | ||
case DEBUG: | ||
logger.debug(msg.message); | ||
break; | ||
case INFO: | ||
logger.info(msg.message); | ||
break; | ||
case WARNING: | ||
logger.warning(msg.message); | ||
break; | ||
case ERROR: | ||
logger.error(msg.message);; | ||
break; | ||
} | ||
if (remove) | ||
it.remove(); | ||
} | ||
} | ||
} | ||
|
||
public void print(Logger logger) { print(logger, true); } | ||
public void print(boolean remove) { print(Logger.SYSTEM, remove); } | ||
public void print() { print(Logger.SYSTEM, true); } | ||
} |
39 changes: 39 additions & 0 deletions
39
utilities/src/main/java/org/thingml/utilities/logging/Logger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
*/ | ||
package org.thingml.utilities.logging; | ||
|
||
public abstract class Logger { | ||
public abstract void debug(String message); | ||
|
||
public abstract void info(String message); | ||
|
||
public abstract void warning(String message); | ||
|
||
public abstract void error(String message); | ||
|
||
public final void error(Throwable t) { | ||
error(t.toString()); | ||
} | ||
public final void error(String message, Throwable t) { | ||
error(new Error(message, t)); | ||
} | ||
|
||
/* --- Implementations --- */ | ||
public static final Logger SYSTEM = new SystemLogger(); | ||
public static final Logger NULL = new NullLogger(); | ||
public static final BufferedLogger newBuffered() { return new BufferedLogger(); }; | ||
} |
31 changes: 31 additions & 0 deletions
31
utilities/src/main/java/org/thingml/utilities/logging/NullLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
*/ | ||
package org.thingml.utilities.logging; | ||
|
||
public class NullLogger extends Logger { | ||
|
||
@Override | ||
public void debug(String message) {} | ||
@Override | ||
public void info(String message) {} | ||
|
||
@Override | ||
public void warning(String message) {} | ||
|
||
@Override | ||
public void error(String message) {} | ||
} |
40 changes: 40 additions & 0 deletions
40
utilities/src/main/java/org/thingml/utilities/logging/SystemLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
*/ | ||
package org.thingml.utilities.logging; | ||
|
||
public class SystemLogger extends Logger { | ||
@Override | ||
public void debug(String message) { | ||
System.out.println(message); | ||
} | ||
|
||
@Override | ||
public void info(String message) { | ||
System.out.println(message); | ||
} | ||
|
||
@Override | ||
public void warning(String message) { | ||
System.out.println(message); | ||
} | ||
|
||
@Override | ||
public void error(String message) { | ||
System.err.println(message); | ||
} | ||
|
||
} |