Skip to content

Commit 0115290

Browse files
hansonrhansonr
authored andcommitted
java.logging.Logger support
1 parent 0be32ff commit 0115290

File tree

8 files changed

+2831
-1780
lines changed

8 files changed

+2831
-1780
lines changed

sources/net.sf.j2s.java.core/src/java/util/logging/LogManager.java

Lines changed: 1756 additions & 1758 deletions
Large diffs are not rendered by default.

sources/net.sf.j2s.java.core/src/java/util/logging/LogRecord.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -531,19 +531,21 @@ private void readObject(ObjectInputStream in)
531531
}
532532

533533
// Private method to infer the caller's class and method names
534-
private void inferCaller() {
534+
@SuppressWarnings("unused")
535+
private void inferCaller() {
535536
needToInferCaller = false;
536-
JavaLangAccess access = SharedSecrets.getJavaLangAccess();
537-
Throwable throwable = new Throwable();
538-
int depth = access.getStackTraceDepth(throwable);
537+
// JavaLangAccess access = SharedSecrets.getJavaLangAccess();
538+
// Throwable throwable = new Throwable();
539+
// int depth = access.getStackTraceDepth(throwable);
539540

541+
int depth = 20; // SwingJS arbitrary limit
540542
boolean lookingForLogger = true;
541-
for (int ix = 0; ix < depth; ix++) {
542-
// Calling getStackTraceElement directly prevents the VM
543-
// from paying the cost of building the entire stack frame.
544-
StackTraceElement frame =
545-
access.getStackTraceElement(throwable, ix);
546-
String cname = frame.getClassName();
543+
Object c = /** @j2sNative arguments.callee.caller || */ null;
544+
for (int ix = 0; ix < depth && c != null; ix++) {
545+
Object clazz = (/** @j2sNative c.exClazz || */ null);
546+
if (clazz == null)
547+
return;
548+
String cname = /** @j2sNative clazz.__CLASS_NAME__ || */ "?";
547549
boolean isLoggerImpl = isLoggerImplFrame(cname);
548550
if (lookingForLogger) {
549551
// Skip all frames until we have found the first logger frame.
@@ -556,11 +558,20 @@ private void inferCaller() {
556558
if (!cname.startsWith("java.lang.reflect.") && !cname.startsWith("sun.reflect.")) {
557559
// We've found the relevant frame.
558560
setSourceClassName(cname);
559-
setSourceMethodName(frame.getMethodName());
561+
String mname = /** @j2sNative c.exName || */ null;
562+
setSourceMethodName(mname);
560563
return;
561564
}
562565
}
563566
}
567+
/**
568+
*
569+
* @j2sNative
570+
*
571+
* c = c.caller;
572+
*
573+
*/
574+
564575
}
565576
// We haven't found a suitable frame, so just punt. This is
566577
// OK as we are only committed to making a "best effort" here.

sources/net.sf.j2s.java.core/src/java/util/logging/Logger.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ static LoggerBundle get(String name, ResourceBundle bundle) {
276276
private ArrayList<LogManager.LoggerWeakRef> kids; // WeakReferences to loggers that have us as parent
277277
private volatile Level levelObject;
278278
private volatile int levelValue; // current effective level value
279-
private WeakReference<ClassLoader> callersClassLoaderRef;
279+
private //WeakReference<
280+
ClassLoader callersClassLoaderRef;
280281
private final boolean isSystemLogger;
281282

282283
/**
@@ -387,13 +388,14 @@ private void setCallersClassLoaderRef(Class<?> caller) {
387388
? caller.getClassLoader()
388389
: null);
389390
if (callersClassLoader != null) {
390-
this.callersClassLoaderRef = new WeakReference<>(callersClassLoader);
391+
this.callersClassLoaderRef = //new WeakReference<>(
392+
callersClassLoader;
391393
}
392394
}
393395

394396
private ClassLoader getCallersClassLoader() {
395397
return (callersClassLoaderRef != null)
396-
? callersClassLoaderRef.get()
398+
? callersClassLoaderRef//.get()
397399
: null;
398400
}
399401

@@ -446,12 +448,12 @@ public String run() {
446448

447449
private static Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
448450
LogManager manager = LogManager.getLogManager();
449-
SecurityManager sm = System.getSecurityManager();
450-
if (sm != null && !SystemLoggerHelper.disableCallerCheck) {
451-
if (caller.getClassLoader() == null) {
452-
return manager.demandSystemLogger(name, resourceBundleName);
453-
}
454-
}
451+
// SecurityManager sm = System.getSecurityManager();
452+
// if (sm != null && !SystemLoggerHelper.disableCallerCheck) {
453+
// if (caller.getClassLoader() == null) {
454+
// return manager.demandSystemLogger(name, resourceBundleName);
455+
// }
456+
// }
455457
return manager.demandLogger(name, resourceBundleName, caller);
456458
// ends up calling new Logger(name, resourceBundleName, caller)
457459
// iff the logger doesn't exist already
@@ -636,7 +638,7 @@ public static Logger getAnonymousLogger() {
636638
public static Logger getAnonymousLogger(String resourceBundleName) {
637639
LogManager manager = LogManager.getLogManager();
638640
// cleanup some Loggers that have been GC'ed
639-
manager.drainLoggerRefQueueBounded();
641+
// manager.drainLoggerRefQueueBounded();
640642
Logger result = new Logger(null, resourceBundleName,
641643
Reflection.getCallerClass(), manager, false);
642644
result.anonymous = true;
@@ -2073,7 +2075,7 @@ private void doSetParent(Logger newParent) {
20732075
// we didn't have a previous parent
20742076
ref = manager.new LoggerWeakRef(this);
20752077
}
2076-
ref.setParentRef(new WeakReference<>(parent));
2078+
ref.setParentRef(parent);
20772079
parent.kids.add(ref);
20782080

20792081
// As a result of the reparenting, the effective level
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
27+
package sun.util.logging;
28+
29+
/**
30+
* A proxy interface for the java.util.logging support.
31+
*
32+
* @see sun.util.logging.LoggingSupport
33+
*/
34+
public interface LoggingProxy {
35+
// Methods to bridge java.util.logging.Logger methods
36+
public Object getLogger(String name);
37+
38+
public Object getLevel(Object logger);
39+
40+
public void setLevel(Object logger, Object newLevel);
41+
42+
public boolean isLoggable(Object logger, Object level);
43+
44+
public void log(Object logger, Object level, String msg);
45+
46+
public void log(Object logger, Object level, String msg, Throwable t);
47+
48+
public void log(Object logger, Object level, String msg, Object... params);
49+
50+
// Methods to bridge java.util.logging.LoggingMXBean methods
51+
public java.util.List<String> getLoggerNames();
52+
53+
public String getLoggerLevel(String loggerName);
54+
55+
public void setLoggerLevel(String loggerName, String levelName);
56+
57+
public String getParentLoggerName(String loggerName);
58+
59+
// Methods to bridge Level.parse() and Level.getName() method
60+
public Object parseLevel(String levelName);
61+
62+
public String getLevelName(Object level);
63+
64+
public int getLevelValue(Object level);
65+
66+
// return the logging property
67+
public String getProperty(String key);
68+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
27+
package sun.util.logging;
28+
29+
import java.lang.reflect.Field;
30+
import java.security.AccessController;
31+
import java.security.PrivilegedAction;
32+
import java.util.Date;
33+
34+
/**
35+
* Internal API to support JRE implementation to detect if the java.util.logging
36+
* support is available but with no dependency on the java.util.logging
37+
* classes. This LoggingSupport class provides several static methods to
38+
* access the java.util.logging functionality that requires the caller
39+
* to ensure that the logging support is {@linkplain #isAvailable available}
40+
* before invoking it.
41+
*
42+
* @see sun.util.logging.PlatformLogger if you want to log messages even
43+
* if the logging support is not available
44+
*/
45+
public class LoggingSupport {
46+
private LoggingSupport() { }
47+
48+
private static final LoggingProxy proxy =
49+
AccessController.doPrivileged(new PrivilegedAction<LoggingProxy>() {
50+
public LoggingProxy run() {
51+
try {
52+
// create a LoggingProxyImpl instance when
53+
// java.util.logging classes exist
54+
Class<?> c = Class.forName("java.util.logging.LoggingProxyImpl", true, null);
55+
Field f = c.getDeclaredField("INSTANCE");
56+
f.setAccessible(true);
57+
return (LoggingProxy) f.get(null);
58+
} catch (ClassNotFoundException cnf) {
59+
return null;
60+
} catch (NoSuchFieldException e) {
61+
throw new AssertionError(e);
62+
} catch (IllegalAccessException e) {
63+
throw new AssertionError(e);
64+
}
65+
}});
66+
67+
/**
68+
* Returns true if java.util.logging support is available.
69+
*/
70+
public static boolean isAvailable() {
71+
return proxy != null;
72+
}
73+
74+
private static void ensureAvailable() {
75+
if (proxy == null)
76+
throw new AssertionError("Should not here");
77+
}
78+
79+
public static java.util.List<String> getLoggerNames() {
80+
ensureAvailable();
81+
return proxy.getLoggerNames();
82+
}
83+
public static String getLoggerLevel(String loggerName) {
84+
ensureAvailable();
85+
return proxy.getLoggerLevel(loggerName);
86+
}
87+
88+
public static void setLoggerLevel(String loggerName, String levelName) {
89+
ensureAvailable();
90+
proxy.setLoggerLevel(loggerName, levelName);
91+
}
92+
93+
public static String getParentLoggerName(String loggerName) {
94+
ensureAvailable();
95+
return proxy.getParentLoggerName(loggerName);
96+
}
97+
98+
public static Object getLogger(String name) {
99+
ensureAvailable();
100+
return proxy.getLogger(name);
101+
}
102+
103+
public static Object getLevel(Object logger) {
104+
ensureAvailable();
105+
return proxy.getLevel(logger);
106+
}
107+
108+
public static void setLevel(Object logger, Object newLevel) {
109+
ensureAvailable();
110+
proxy.setLevel(logger, newLevel);
111+
}
112+
113+
public static boolean isLoggable(Object logger, Object level) {
114+
ensureAvailable();
115+
return proxy.isLoggable(logger,level);
116+
}
117+
118+
public static void log(Object logger, Object level, String msg) {
119+
ensureAvailable();
120+
proxy.log(logger, level, msg);
121+
}
122+
123+
public static void log(Object logger, Object level, String msg, Throwable t) {
124+
ensureAvailable();
125+
proxy.log(logger, level, msg, t);
126+
}
127+
128+
public static void log(Object logger, Object level, String msg, Object... params) {
129+
ensureAvailable();
130+
proxy.log(logger, level, msg, params);
131+
}
132+
133+
public static Object parseLevel(String levelName) {
134+
ensureAvailable();
135+
return proxy.parseLevel(levelName);
136+
}
137+
138+
public static String getLevelName(Object level) {
139+
ensureAvailable();
140+
return proxy.getLevelName(level);
141+
}
142+
143+
public static int getLevelValue(Object level) {
144+
ensureAvailable();
145+
return proxy.getLevelValue(level);
146+
}
147+
148+
private static final String DEFAULT_FORMAT =
149+
"%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n";
150+
151+
private static final String FORMAT_PROP_KEY = "java.util.logging.SimpleFormatter.format";
152+
public static String getSimpleFormat() {
153+
return getSimpleFormat(true);
154+
}
155+
156+
// useProxy if true will cause initialization of
157+
// java.util.logging and read its configuration
158+
static String getSimpleFormat(boolean useProxy) {
159+
String format =
160+
AccessController.doPrivileged(
161+
new PrivilegedAction<String>() {
162+
public String run() {
163+
return System.getProperty(FORMAT_PROP_KEY);
164+
}
165+
});
166+
167+
if (useProxy && proxy != null && format == null) {
168+
format = proxy.getProperty(FORMAT_PROP_KEY);
169+
}
170+
171+
if (format != null) {
172+
try {
173+
// validate the user-defined format string
174+
String.format(format, new Date(), "", "", "", "", "");
175+
} catch (IllegalArgumentException e) {
176+
// illegal syntax; fall back to the default format
177+
format = DEFAULT_FORMAT;
178+
}
179+
} else {
180+
format = DEFAULT_FORMAT;
181+
}
182+
return format;
183+
}
184+
185+
}

0 commit comments

Comments
 (0)