Skip to content

Commit

Permalink
Cleaning the logging procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Feb 23, 2024
1 parent cb8a7a9 commit 53bc3ab
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 74 deletions.
2 changes: 1 addition & 1 deletion parent.pom
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<module>windows</module>
</modules>
<properties>
<inlandes.version>0.3.0</inlandes.version>
<inlandes.version>0.5.2</inlandes.version>
<bookdoc.version>0.5.0</bookdoc.version>
<jna.version>5.11.0</jna.version>
<junit.version>5.10.2</junit.version>
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/luwrain/core/Init.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2022 Michael Pozhidaev <[email protected]>
Copyright 2012-2024 Michael Pozhidaev <[email protected]>
This file is part of LUWRAIN.
Expand Down Expand Up @@ -42,8 +42,7 @@ static public void main(String[] args) throws IOException
final PrintStream log = new PrintStream(new BufferedOutputStream(new FileOutputStream(DEBUG_FILE)), true);
System.setOut(log);
System.setErr(log);
} else
Log.enableBriefMode();
}
final File userHomeDir = new File(System.getProperty("user.home"));
final List<URL> urls = new ArrayList<>();
addJarsToClassPath(new File("jar"), urls);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/luwrain/core/LaunchFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2022 Michael Pozhidaev <[email protected]>
Copyright 2012-2024 Michael Pozhidaev <[email protected]>
This file is part of LUWRAIN.
Expand Down
84 changes: 28 additions & 56 deletions src/main/java/org/luwrain/core/Log.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2022 Michael Pozhidaev <[email protected]>
Copyright 2012-2024 Michael Pozhidaev <[email protected]>
This file is part of LUWRAIN.
Expand All @@ -14,26 +14,26 @@
General Public License for more details.
*/

//LWR_API 1.0

package org.luwrain.core;

import java.util.*;

import static org.luwrain.core.NullCheck.*;

public final class Log
{
public enum Level {DEBUG, INFO, WARNING, ERROR, FATAL};

static public class Message
static public final class Message
{
public final Level level;
public final String component;
public final String message;
Message(Level level, String component, String message)
{
NullCheck.notNull(level, "level");
NullCheck.notNull(component, "component");
NullCheck.notNull(message, "message");
notNull(level, "level");
notNull(component, "component");
notNull(message, "message");
this.level = level;
this.component = component;
this.message = message;
Expand All @@ -45,69 +45,38 @@ public interface Listener
void onLogMessage(Message message);
}

static private final List<Listener> listeners = new ArrayList<Listener>();
static private boolean briefMode = false;
static private final Object syncObj = new Object();
static private final List<Listener> listeners = new ArrayList<>();

static public void addListener(Listener listener)
{
NullCheck.notNull(listener, "listener");
synchronized (syncObj) {
for(int i = 0;i < listeners.size();++i)
if (listeners.get(i) == listener)
return;
listeners.add(listener);
}
notNull(listener, "listener");
for(int i = 0;i < listeners.size();++i)
if (listeners.get(i) == listener)
return;
listeners.add(listener);
}

static public void removeListener(Listener listener)
{
NullCheck.notNull(listener, "listener");
synchronized (syncObj) {
for(int i = 0;i < listeners.size();++i)
if (listeners.get(i) == listener)
{
listeners.remove(i);
return;
}
}
}

static void enableBriefMode()
{
briefMode = true;
notNull(listener, "listener");
for(int i = 0;i < listeners.size();++i)
if (listeners.get(i) == listener)
{
listeners.remove(i);
return;
}
}

static private void message(Level level, String component, String message)
{
if (level == null || component == null || message == null)
return;
synchronized (syncObj) {
final Message msg = new Message(level, component, message);
for(Listener l: listeners)
l.onLogMessage(msg);
switch(level)
{
case DEBUG:
if (!briefMode)
{
if (component.equals("init") || component.equals("core"))
System.out.println(cap(message)); else
System.out.println(component .toUpperCase()+ ": " + cap(message));
}
break;
case INFO:
if (component.equals("init") || component.equals("core"))
System.out.println(cap(message)); else
System.out.println(component .toUpperCase()+ ": " + cap(message));
break;
default:
System.out.println(level.toString() + ": " + component.toUpperCase() + ": " + message);
}
}
final Message msg = new Message(level, component, message);
for(Listener l: listeners)
l.onLogMessage(msg);
}

public static void debug(String component, String message)
static public void debug(String component, String message)
{
if (message != null)
message(Level.DEBUG, (component != null && !component.isEmpty())?component.trim():"-", message.trim());
Expand All @@ -134,13 +103,16 @@ static public void error(String component, String message)
static public void fatal(String component, String message)
{
if (message != null)
{
System.err.println("FATAL: " + cap(message));
message(Level.FATAL, (component != null && !component.isEmpty())?component.trim():"-", message.trim());
}
}

static private String cap(String str)
{
if (str.isEmpty())
return str;
return "";
final char ch = str.charAt(0);
if (Character.isLetter(ch) && Character.toUpperCase(ch) != ch)
return Character.toUpperCase(ch) + str.substring(1);
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/org/luwrain/core/NullCheck.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2022 Michael Pozhidaev <[email protected]>
Copyright 2012-2024 Michael Pozhidaev <[email protected]>
This file is part of LUWRAIN.
Expand All @@ -14,8 +14,6 @@
General Public License for more details.
*/

//LWR_API 1.0

package org.luwrain.core;

/**
Expand All @@ -24,7 +22,7 @@
* corresponding exceptions. All method are rather short and purposed
* only for making code more readable and self-documented.
*/
public class NullCheck
public final class NullCheck
{
/**
* Checks that provided reference isn't null.
Expand All @@ -36,7 +34,7 @@ public class NullCheck
static public void notNull(Object obj, String name)
{
if (obj == null)
throw new NullPointerException(name + " may not be null");
throw new NullPointerException(name + " can't be null");
}

/**
Expand All @@ -49,9 +47,9 @@ static public void notNull(Object obj, String name)
static public void notEmpty(Object obj, String name)
{
if (obj == null)
throw new NullPointerException(name + " may not be null");
throw new NullPointerException(name + " can't be null");
if (obj.toString().isEmpty())
throw new IllegalArgumentException(name + " may not be empty");
throw new IllegalArgumentException(name + " can't be empty");
}

/**
Expand All @@ -67,7 +65,7 @@ static public void notNullItems(Object[] items, String name)
notNull(items, name);
for(int i = 0;i < items.length;++i)
if (items[i] == null)
throw new NullPointerException(name + "[" + i + "] may not be null");
throw new NullPointerException(name + "[" + i + "] can't be null");
}

/**
Expand All @@ -81,13 +79,13 @@ static public void notNullItems(Object[] items, String name)
static public void notEmptyItems(Object[] items, String name)
{
if (items == null)
throw new NullPointerException(name + " may not be null");
throw new NullPointerException(name + " can't be null");
for(int i = 0;i < items.length;++i)
{
if (items[i] == null)
throw new NullPointerException(name + "[" + i + "] may not be null");
throw new NullPointerException(name + "[" + i + "] can't be null");
if (items[i].toString().isEmpty())
throw new IllegalArgumentException(name + "[" + i + "] may not be empty");
throw new IllegalArgumentException(name + "[" + i + "] can't be empty");
}
}

Expand All @@ -101,8 +99,8 @@ static public void notEmptyItems(Object[] items, String name)
static public void notEmptyArray(Object[] items, String name)
{
if (items == null)
throw new NullPointerException(name + " may not be null");
throw new NullPointerException(name + " can't be null");
if (items.length < 1)
throw new IllegalArgumentException(name + " may not be empty");
throw new IllegalArgumentException(name + " can't be empty");
}
}

0 comments on commit 53bc3ab

Please sign in to comment.