Skip to content

Commit a89455a

Browse files
committed
Address review comments: Add Handler support and clean tests
1 parent bfe17fb commit a89455a

10 files changed

Lines changed: 199 additions & 155 deletions

File tree

src/classes/modules/java.logging/java/util/logging/FileHandler.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,27 @@
66
* The Java Pathfinder core (jpf-core) platform is licensed under the
77
* Apache License, Version 2.0 (the "License"); you may not use this file except
88
* in compliance with the License. You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0.
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0.
1111
*
1212
* Unless required by applicable law or agreed to in writing, software
1313
* distributed under the License is distributed on an "AS IS" BASIS,
1414
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
* See the License for the specific language governing permissions and
15+
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
1918
package java.util.logging;
20-
2119
import java.io.FileOutputStream;
2220
import java.io.IOException;
23-
2421
/**
2522
* simple stub to avoid execptions when using basic logging
2623
*/
2724
public class FileHandler extends StreamHandler {
28-
public FileHandler() throws IOException {
29-
this("log.log");
30-
}
31-
32-
public FileHandler(String pattern) throws IOException {
33-
super();
34-
setOutputStream(new FileOutputStream(pattern));
35-
}
36-
}
25+
public FileHandler() throws IOException {
26+
this("log.log");
27+
}
28+
public FileHandler(String pattern) throws IOException {
29+
super();
30+
setOutputStream(new FileOutputStream(pattern));
31+
}
32+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package java.util.logging;
2+
public abstract class Formatter {
3+
public abstract String format(LogRecord record);
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package java.util.logging;
2+
/**
3+
* MJI model class for java.util.logging.Handler.
4+
*/
5+
public abstract class Handler {
6+
public abstract void publish(LogRecord record);
7+
public abstract void flush();
8+
public abstract void close();
9+
}
Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
1-
/*
2-
* Copyright (C) 2014, United States Government, as represented by the
3-
* Administrator of the National Aeronautics and Space Administration.
4-
* All rights reserved.
5-
*/
61
package java.util.logging;
7-
8-
/**
9-
* MJI model class for java.util.logging.Level
10-
*/
112
public class Level {
12-
private final String name;
13-
private final int value;
14-
15-
protected Level(String name, int value) {
16-
this.name = name;
17-
this.value = value;
18-
}
19-
20-
public String getName() {
21-
return name;
22-
}
23-
24-
public final int intValue() {
25-
return value;
26-
}
27-
28-
public static final Level OFF = new Level("OFF", Integer.MAX_VALUE);
29-
public static final Level SEVERE = new Level("SEVERE", 1000);
30-
public static final Level WARNING = new Level("WARNING", 900);
31-
public static final Level INFO = new Level("INFO", 800);
32-
public static final Level CONFIG = new Level("CONFIG", 700);
33-
public static final Level FINE = new Level("FINE", 500);
34-
public static final Level FINER = new Level("FINER", 400);
35-
public static final Level FINEST = new Level("FINEST", 300);
36-
public static final Level ALL = new Level("ALL", Integer.MIN_VALUE);
37-
38-
public String toString() {
39-
return name;
40-
}
41-
}
3+
private final String name;
4+
private final int value;
5+
protected Level(String name, int value) {
6+
this.name = name;
7+
this.value = value;
8+
}
9+
public String getName() {
10+
return name;
11+
}
12+
public final int intValue() {
13+
return value;
14+
}
15+
public static final Level OFF = new Level("OFF", Integer.MAX_VALUE);
16+
public static final Level SEVERE = new Level("SEVERE", 1000);
17+
public static final Level WARNING = new Level("WARNING", 900);
18+
public static final Level INFO = new Level("INFO", 800);
19+
public static final Level CONFIG = new Level("CONFIG", 700);
20+
public static final Level FINE = new Level("FINE", 500);
21+
public static final Level FINER = new Level("FINER", 400);
22+
public static final Level FINEST = new Level("FINEST", 300);
23+
public static final Level ALL = new Level("ALL", Integer.MIN_VALUE);
24+
public String toString() {
25+
return name;
26+
}
27+
}
Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,32 @@
1-
/*
2-
* Copyright (C) 2014, United States Government, as represented by the
3-
* Administrator of the National Aeronautics and Space Administration.
4-
* All rights reserved.
5-
*/
61
package java.util.logging;
7-
82
import java.util.HashMap;
93
import java.util.Map;
10-
11-
/**
12-
* MJI model class for java.util.logging.LogManager
13-
*/
144
public class LogManager {
15-
16-
private static final LogManager manager = new LogManager();
17-
private final Map<String, Logger> loggers = new HashMap<>();
18-
19-
protected LogManager() {
20-
}
21-
22-
public static LogManager getLogManager() {
23-
return manager;
24-
}
25-
26-
public synchronized Logger getLogger(String name) {
27-
return loggers.get(name);
28-
}
29-
30-
public synchronized boolean addLogger(Logger logger) {
31-
String name = logger.getName();
32-
if (loggers.containsKey(name)) {
33-
return false;
5+
private static final LogManager manager = new LogManager();
6+
private final Map<String, Logger> loggers = new HashMap<>();
7+
protected LogManager() {
348
}
35-
loggers.put(name, logger);
36-
return true;
37-
}
38-
39-
Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
40-
Logger result = getLogger(name);
41-
if (result == null) {
42-
Logger newLogger = new Logger(name, resourceBundleName);
43-
addLogger(newLogger);
44-
result = newLogger;
9+
public static LogManager getLogManager() {
10+
return manager;
4511
}
46-
return result;
47-
}
48-
}
12+
public synchronized Logger getLogger(String name) {
13+
return loggers.get(name);
14+
}
15+
public synchronized boolean addLogger(Logger logger) {
16+
String name = logger.getName();
17+
if (loggers.containsKey(name)) {
18+
return false;
19+
}
20+
loggers.put(name, logger);
21+
return true;
22+
}
23+
Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
24+
Logger result = getLogger(name);
25+
if (result == null) {
26+
Logger newLogger = new Logger(name, resourceBundleName);
27+
addLogger(newLogger);
28+
result = newLogger;
29+
}
30+
return result;
31+
}
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package java.util.logging;
2+
/**
3+
* MJI model class for java.util.logging.LogRecord.
4+
*/
5+
public class LogRecord {
6+
private Level level;
7+
private String message;
8+
public LogRecord(Level level, String msg) {
9+
this.level = level;
10+
this.message = msg;
11+
}
12+
public Level getLevel() {
13+
return level;
14+
}
15+
public String getMessage() {
16+
return message;
17+
}
18+
}

src/classes/modules/java.logging/java/util/logging/Logger.java

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,57 @@
44
* All rights reserved.
55
*/
66
package java.util.logging;
7-
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import java.util.ArrayList;
10+
import java.util.List;
811
/**
912
* MJI model class for java.util.logging.Logger
1013
*/
1114
public class Logger {
12-
15+
private static final Map<String, Logger> loggers = new HashMap<>();
1316
private String name;
14-
private String resourceBundleName;
17+
private final List<Handler> handlers = new ArrayList<>();
1518

1619
protected Logger(String name, String resourceBundleName) {
1720
this.name = name;
18-
this.resourceBundleName = resourceBundleName;
1921
}
20-
21-
public static Logger getLogger(String name) {
22-
return getLogger(name, null);
22+
public static synchronized Logger getLogger(String name) {
23+
Logger logger = loggers.get(name);
24+
if (logger == null) {
25+
logger = new Logger(name, null);
26+
loggers.put(name, logger);
27+
}
28+
return logger;
2329
}
24-
25-
public static Logger getLogger(String name, String resourceBundleName) {
26-
LogManager manager = LogManager.getLogManager();
27-
return manager.demandLogger(name, resourceBundleName, null);
28-
}
29-
3030
public static Logger getAnonymousLogger() {
31-
return new Logger("", null);
31+
return new Logger("", null);
3232
}
33-
34-
public static Logger getAnonymousLogger(String resourceBundleName) {
35-
return new Logger("", resourceBundleName);
36-
}
37-
3833
public String getName() {
3934
return name;
4035
}
41-
42-
public String getResourceBundleName() {
43-
return resourceBundleName;
36+
public void addHandler(Handler handler) {
37+
handlers.add(handler);
38+
}
39+
public void removeHandler(Handler handler) {
40+
handlers.remove(handler);
41+
}
42+
public Handler[] getHandlers() {
43+
return handlers.toArray(new Handler[0]);
44+
}
45+
public void log(Level level, String msg) {
46+
LogRecord record = new LogRecord(level, msg);
47+
for (Handler h : handlers) {
48+
h.publish(record);
49+
}
4450
}
45-
4651
public void info(String msg) {
4752
log(Level.INFO, msg);
4853
}
49-
5054
public void warning(String msg) {
5155
log(Level.WARNING, msg);
5256
}
53-
5457
public void severe(String msg) {
5558
log(Level.SEVERE, msg);
5659
}
57-
58-
public void config(String msg) {
59-
log(Level.CONFIG, msg);
60-
}
61-
62-
public void fine(String msg) {
63-
log(Level.FINE, msg);
64-
}
65-
66-
public void finer(String msg) {
67-
log(Level.FINER, msg);
68-
}
69-
70-
public void finest(String msg) {
71-
log(Level.FINEST, msg);
72-
}
73-
74-
public void log(Level level, String msg) {
75-
System.out.println("[" + level.getName() + "] " + name + ": " + msg);
76-
}
7760
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package java.util.logging;
2+
import java.io.OutputStream;
3+
import java.io.IOException;
4+
/**
5+
* MJI model class for java.util.logging.StreamHandler.
6+
*/
7+
public class StreamHandler extends Handler {
8+
private OutputStream out;
9+
public StreamHandler() {
10+
this.out = System.out;
11+
}
12+
public StreamHandler(OutputStream out, Formatter formatter) {
13+
this.out = out;
14+
}
15+
protected void setOutputStream(OutputStream out) {
16+
this.out = out;
17+
}
18+
@Override
19+
public void publish(LogRecord record) {
20+
if (out == null) return;
21+
try {
22+
String msg;
23+
if (record.getLevel() != null) {
24+
msg = "[" + record.getLevel().getName() + "] " + record.getMessage() + "\n";
25+
} else {
26+
msg = "[UNKNOWN] " + record.getMessage() + "\n";
27+
}
28+
out.write(msg.getBytes());
29+
flush();
30+
} catch (IOException e) {
31+
e.printStackTrace();
32+
}
33+
}
34+
@Override
35+
public void flush() {
36+
if (out != null) {
37+
try {
38+
out.flush();
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
}
44+
@Override
45+
public void close() {
46+
flush();
47+
out = null;
48+
}
49+
}

0 commit comments

Comments
 (0)