-
Notifications
You must be signed in to change notification settings - Fork 1
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
cb8a7a9
commit 53bc3ab
Showing
5 changed files
with
43 additions
and
74 deletions.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
Copyright 2012-2022 Michael Pozhidaev <[email protected]> | ||
Copyright 2012-2024 Michael Pozhidaev <[email protected]> | ||
This file is part of LUWRAIN. | ||
|
@@ -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); | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
Copyright 2012-2022 Michael Pozhidaev <[email protected]> | ||
Copyright 2012-2024 Michael Pozhidaev <[email protected]> | ||
This file is part of LUWRAIN. | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
Copyright 2012-2022 Michael Pozhidaev <[email protected]> | ||
Copyright 2012-2024 Michael Pozhidaev <[email protected]> | ||
This file is part of LUWRAIN. | ||
|
@@ -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; | ||
|
@@ -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()); | ||
|
@@ -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); | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
Copyright 2012-2022 Michael Pozhidaev <[email protected]> | ||
Copyright 2012-2024 Michael Pozhidaev <[email protected]> | ||
This file is part of LUWRAIN. | ||
|
@@ -14,8 +14,6 @@ | |
General Public License for more details. | ||
*/ | ||
|
||
//LWR_API 1.0 | ||
|
||
package org.luwrain.core; | ||
|
||
/** | ||
|
@@ -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. | ||
|
@@ -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"); | ||
} | ||
|
||
/** | ||
|
@@ -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"); | ||
} | ||
|
||
/** | ||
|
@@ -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"); | ||
} | ||
|
||
/** | ||
|
@@ -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"); | ||
} | ||
} | ||
|
||
|
@@ -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"); | ||
} | ||
} |