Skip to content

Commit

Permalink
after all, it was used...
Browse files Browse the repository at this point in the history
  • Loading branch information
brice-morin committed Aug 13, 2019
1 parent f9de7cd commit 7ea6778
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pom.xml
Expand Up @@ -205,7 +205,9 @@
</repositories>

<modules>
<module>language/thingml.target</module>
<module>utilities</module>

<module>language/thingml.target</module>
<module>language/thingml</module>

<module>compilers</module>
Expand Down
27 changes: 27 additions & 0 deletions utilities/.classpath
@@ -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>
23 changes: 23 additions & 0 deletions utilities/.project
@@ -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>
34 changes: 34 additions & 0 deletions utilities/pom.xml
@@ -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>
@@ -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 utilities/src/main/java/org/thingml/utilities/logging/Logger.java
@@ -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(); };
}
@@ -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) {}
}
@@ -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);
}

}

0 comments on commit 7ea6778

Please sign in to comment.