diff options
-rw-r--r-- | .idea/artifacts/Logging_jar.xml | 8 | ||||
-rw-r--r-- | .idea/misc.xml | 6 | ||||
-rw-r--r-- | .idea/modules.xml | 8 | ||||
-rw-r--r-- | .idea/uiDesigner.xml | 124 | ||||
-rw-r--r-- | Logging.iml | 11 | ||||
-rw-r--r-- | src/de/romjaki/logger/EventHandler.java | 7 | ||||
-rw-r--r-- | src/de/romjaki/logger/Level.java | 18 | ||||
-rw-r--r-- | src/de/romjaki/logger/LogEvent.java | 26 | ||||
-rw-r--r-- | src/de/romjaki/logger/LogHandler.java | 7 | ||||
-rw-r--r-- | src/de/romjaki/logger/LoggedEvent.java | 23 | ||||
-rw-r--r-- | src/de/romjaki/logger/Logger.java | 179 |
11 files changed, 417 insertions, 0 deletions
diff --git a/.idea/artifacts/Logging_jar.xml b/.idea/artifacts/Logging_jar.xml new file mode 100644 index 0000000..e273d94 --- /dev/null +++ b/.idea/artifacts/Logging_jar.xml @@ -0,0 +1,8 @@ +<component name="ArtifactManager"> + <artifact type="jar" name="Logging:jar"> + <output-path>$PROJECT_DIR$/out/artifacts/Logging_jar</output-path> + <root id="archive" name="Logging.jar"> + <element id="module-output" name="Logging" /> + </root> + </artifact> +</component>
\ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> + <output url="file://$PROJECT_DIR$/out" /> + </component> +</project>
\ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4e63086 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/Logging.iml" filepath="$PROJECT_DIR$/Logging.iml" /> + </modules> + </component> +</project>
\ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="Palette2"> + <group name="Swing"> + <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> + </item> + <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> + </item> + <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> + <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> + <initial-values> + <property name="text" value="Button" /> + </initial-values> + </item> + <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="RadioButton" /> + </initial-values> + </item> + <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="CheckBox" /> + </initial-values> + </item> + <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="Label" /> + </initial-values> + </item> + <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> + <preferred-size width="200" height="200" /> + </default-constraints> + </item> + <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> + <preferred-size width="200" height="200" /> + </default-constraints> + </item> + <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> + </item> + <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> + <preferred-size width="-1" height="20" /> + </default-constraints> + </item> + <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> + </item> + <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> + </item> + </group> + </component> +</project>
\ No newline at end of file diff --git a/Logging.iml b/Logging.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Logging.iml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module>
\ No newline at end of file diff --git a/src/de/romjaki/logger/EventHandler.java b/src/de/romjaki/logger/EventHandler.java new file mode 100644 index 0000000..6e07466 --- /dev/null +++ b/src/de/romjaki/logger/EventHandler.java @@ -0,0 +1,7 @@ +package de.romjaki.logger; + +/** + * Created by RGR on 17.08.2017. + */ +public @interface EventHandler { +} diff --git a/src/de/romjaki/logger/Level.java b/src/de/romjaki/logger/Level.java new file mode 100644 index 0000000..38310ab --- /dev/null +++ b/src/de/romjaki/logger/Level.java @@ -0,0 +1,18 @@ +package de.romjaki.logger; + +/** + * Created by RGR on 17.08.2017. + */ +public enum Level { + ALL, + FAILURE, + ERROR, + WARNING, + INFO, + DEBUG, + OFF; + + public boolean logs(Level level) { + return this.ordinal() <= level.ordinal(); + } +} diff --git a/src/de/romjaki/logger/LogEvent.java b/src/de/romjaki/logger/LogEvent.java new file mode 100644 index 0000000..e62742c --- /dev/null +++ b/src/de/romjaki/logger/LogEvent.java @@ -0,0 +1,26 @@ +package de.romjaki.logger; + +/** + * Created by RGR on 17.08.2017. + */ +public abstract class LogEvent { + + boolean interrupted = false; + Class<?> actor; + + public LogEvent(Class<?> actor) { + this.actor = actor; + } + + public void interrupt() { + interrupted = true; + } + + public Class<?> getActor() { + return actor; + } + + public boolean isInterrupted() { + return interrupted; + } +} diff --git a/src/de/romjaki/logger/LogHandler.java b/src/de/romjaki/logger/LogHandler.java new file mode 100644 index 0000000..d5c01f6 --- /dev/null +++ b/src/de/romjaki/logger/LogHandler.java @@ -0,0 +1,7 @@ +package de.romjaki.logger; + +/** + * Created by RGR on 17.08.2017. + */ +public interface LogHandler { +} diff --git a/src/de/romjaki/logger/LoggedEvent.java b/src/de/romjaki/logger/LoggedEvent.java new file mode 100644 index 0000000..143ab0c --- /dev/null +++ b/src/de/romjaki/logger/LoggedEvent.java @@ -0,0 +1,23 @@ +package de.romjaki.logger; + +/** + * Created by RGR on 17.08.2017. + */ +public class LoggedEvent extends LogEvent { + private final Level level; + private final String text; + + public LoggedEvent(Class<?> clazz, Level level, String text) { + super(clazz); + this.level = level; + this.text = text; + } + + public Level getLevel() { + return level; + } + + public String getText() { + return text; + } +} diff --git a/src/de/romjaki/logger/Logger.java b/src/de/romjaki/logger/Logger.java new file mode 100644 index 0000000..f860d9d --- /dev/null +++ b/src/de/romjaki/logger/Logger.java @@ -0,0 +1,179 @@ +package de.romjaki.logger; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import static de.romjaki.logger.Level.*; + +/** + * Created by RGR on 17.08.2017. + */ +public class Logger { + static String format = "[{LEVEL}][{DATE}]{NAME}: {TEXT}"; + static DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); + String name; + Level logLevel = WARNING; + List<LogHandler> handlers = new ArrayList<>(); + + public Logger() { + this(""); + } + + public Logger(String name) { + this.name = name; + } + + public Level getLogLevel() { + return logLevel; + } + + public void setLogLevel(Level logLevel) { + this.logLevel = logLevel; + } + + public String getName() { + return name; + } + + public void log(Level level, String text) { + if (logLevel.logs(level)) { + doLog(level, text); + } + } + + public void logf(Level level, String format, Object... fillIn) { + log(level, String.format(format, fillIn)); + } + + public void info(String text) { + log(INFO, text); + } + + public void infof(String format, Object... fillIn) { + logf(INFO, format, fillIn); + } + + public void warn(String text) { + log(WARNING, text); + } + + public void warnf(String format, Object... fillIn) { + logf(WARNING, format, fillIn); + } + + public void debug(String text) { + log(DEBUG, text); + } + + public void debugf(String format, Object... fillIn) { + logf(DEBUG, format, fillIn); + } + + public void error(String text) { + log(ERROR, text); + } + + public void errorf(String format, Object... fillIn) { + logf(ERROR, format, fillIn); + } + + public void failure(String text) { + log(FAILURE, text); + } + + public void failuref(String format, Object... fillIn) { + logf(Level.FAILURE, format, fillIn); + } + + public PrintStream getAsPrintStream() { + return getAsPrintStream(ALL); + } + + public PrintStream getAsPrintStream(Level level) { + return getAsPrintStream(level, System.lineSeparator()); + } + + public PrintStream getAsPrintStream(Level level, String sep) { + return new PrintStream(new OutputStream() { + StringBuffer buffer = new StringBuffer(); + + Object locker = new Object(); + + @Override + public void write(int b) throws IOException { + char c = (char) b; + synchronized (locker) { + buffer.append(c); + if (buffer.indexOf(sep) != -1) { + String[] splits = buffer.toString().split(sep, 2); + log(level, splits[0]); + if (splits.length > 1) { + buffer = new StringBuffer(splits[1].replace(sep, "")); + } else { + buffer = new StringBuffer(); + } + } + } + } + }, true); + } + + public void exception(Throwable t) { + t.printStackTrace(getAsPrintStream(ERROR)); + } + + void doLog(Level level, String text) { + LogEvent event = new LoggedEvent(getCallingClass(), level, text); + dispatchEvent(event); + if (!event.isInterrupted()) { + System.out.println(format + .replace("{LEVEL}", level.name()) + .replace("{DATE}", dateFormat.format(new Date())) + .replace("{NAME}", name != null ? name : "") + .replace("{TEXT}", text)); + } + } + + public void dispatchEvent(LogEvent event) { + handlers.forEach(handler -> dispatchEvent(event, handler)); + } + + void dispatchEvent(LogEvent event, LogHandler handler) { + for (Method method : handler.getClass().getMethods()) { + invokeSingle(method, event, handler); + } + } + + void invokeSingle(Method method, LogEvent event, LogHandler handler) { + if (method.isAnnotationPresent(EventHandler.class)) { + Class<?>[] args = method.getParameterTypes(); + if (args.length == 1 && args[0].isInstance(event)) { + try { + method.invoke(handler, event); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + + Class<?> getCallingClass() { + StackTraceElement[] trace = Thread.currentThread().getStackTrace(); + for (int i = 1; i < trace.length; i++) { + StackTraceElement ste = trace[i]; + if (!ste.getClass().equals(Logger.class) && ste.getClassName().indexOf("java.lang.Thread") != 0) { + return ste.getClass(); + } + } + return null; + } + +} |