1 /*
2 * Copyright (c) 2000, 2007, 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
461 if (level != null) {
462 doSetLevel(logger, level);
463 }
464
465 // Do we have a per logger handler too?
466 // Note: this will add a 200ms penalty
467 loadLoggerHandlers(logger, name, name+".handlers");
468 processParentHandlers(logger, name);
469
470 // Find the new node and its parent.
471 LogNode node = findNode(name);
472 node.loggerRef = new WeakReference<Logger>(logger);
473 Logger parent = null;
474 LogNode nodep = node.parent;
475 while (nodep != null) {
476 WeakReference<Logger> nodeRef = nodep.loggerRef;
477 if (nodeRef != null) {
478 parent = nodeRef.get();
479 if (parent != null) {
480 break;
481 }
482 }
483 nodep = nodep.parent;
484 }
485
486 if (parent != null) {
487 doSetParent(logger, parent);
488 }
489 // Walk over the children and tell them we are their new parent.
490 node.walkAndSetParent(logger);
491
492 return true;
493 }
494
495
496 // Private method to set a level on a logger.
497 // If necessary, we raise privilege before doing the call.
498 private static void doSetLevel(final Logger logger, final Level level) {
499 SecurityManager sm = System.getSecurityManager();
500 if (sm == null) {
501 // There is no security manager, so things are easy.
502 logger.setLevel(level);
503 return;
504 }
505 // There is a security manager. Raise privilege before
506 // calling setLevel.
507 AccessController.doPrivileged(new PrivilegedAction<Object>() {
508 public Object run() {
509 logger.setLevel(level);
510 return null;
511 }});
944 HashMap<String,LogNode> children;
945 WeakReference<Logger> loggerRef;
946 LogNode parent;
947
948 LogNode(LogNode parent) {
949 this.parent = parent;
950 }
951
952 // Recursive method to walk the tree below a node and set
953 // a new parent logger.
954 void walkAndSetParent(Logger parent) {
955 if (children == null) {
956 return;
957 }
958 Iterator<LogNode> values = children.values().iterator();
959 while (values.hasNext()) {
960 LogNode node = values.next();
961 WeakReference<Logger> ref = node.loggerRef;
962 Logger logger = (ref == null) ? null : ref.get();
963 if (logger == null) {
964 node.walkAndSetParent(parent);
965 } else {
966 doSetParent(logger, parent);
967 }
968 }
969 }
970 }
971
972 // We use a subclass of Logger for the root logger, so
973 // that we only instantiate the global handlers when they
974 // are first needed.
975 private class RootLogger extends Logger {
976
977 private RootLogger() {
978 super("", null);
979 setLevel(defaultLevel);
980 }
981
982 public void log(LogRecord record) {
983 // Make sure that the global handlers have been instantiated.
984 initializeGlobalHandlers();
985 super.log(record);
986 }
987
988 public void addHandler(Handler h) {
989 initializeGlobalHandlers();
|
1 /*
2 * Copyright (c) 2000, 2010, 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
461 if (level != null) {
462 doSetLevel(logger, level);
463 }
464
465 // Do we have a per logger handler too?
466 // Note: this will add a 200ms penalty
467 loadLoggerHandlers(logger, name, name+".handlers");
468 processParentHandlers(logger, name);
469
470 // Find the new node and its parent.
471 LogNode node = findNode(name);
472 node.loggerRef = new WeakReference<Logger>(logger);
473 Logger parent = null;
474 LogNode nodep = node.parent;
475 while (nodep != null) {
476 WeakReference<Logger> nodeRef = nodep.loggerRef;
477 if (nodeRef != null) {
478 parent = nodeRef.get();
479 if (parent != null) {
480 break;
481 } else {
482 // nodep holds a stale weak reference to a Logger
483 // which has been GC-ed. Note this will only cleanup
484 // stale weak refs that we encounter before we find
485 // our parent LogNode.
486 nodep.loggerRef = null;
487 }
488 }
489 nodep = nodep.parent;
490 }
491
492 if (parent != null) {
493 doSetParent(logger, parent);
494 }
495 // Walk over the children and tell them we are their new parent.
496 node.walkAndSetParent(logger);
497
498 if (node.parent != null) {
499 // Look for possible weak reference cleanup from the new
500 // parent LogNode down. The walkAndSetParent() call above
501 // might have already done some or none of this work so
502 // this call is the only way to be absolutely sure we have
503 // checked for stale weak refs in every LogNode in the
504 // parent LogNode's hierarchy.
505 node.parent.deleteStaleWeakRefs();
506 }
507
508 return true;
509 }
510
511
512 // Private method to set a level on a logger.
513 // If necessary, we raise privilege before doing the call.
514 private static void doSetLevel(final Logger logger, final Level level) {
515 SecurityManager sm = System.getSecurityManager();
516 if (sm == null) {
517 // There is no security manager, so things are easy.
518 logger.setLevel(level);
519 return;
520 }
521 // There is a security manager. Raise privilege before
522 // calling setLevel.
523 AccessController.doPrivileged(new PrivilegedAction<Object>() {
524 public Object run() {
525 logger.setLevel(level);
526 return null;
527 }});
960 HashMap<String,LogNode> children;
961 WeakReference<Logger> loggerRef;
962 LogNode parent;
963
964 LogNode(LogNode parent) {
965 this.parent = parent;
966 }
967
968 // Recursive method to walk the tree below a node and set
969 // a new parent logger.
970 void walkAndSetParent(Logger parent) {
971 if (children == null) {
972 return;
973 }
974 Iterator<LogNode> values = children.values().iterator();
975 while (values.hasNext()) {
976 LogNode node = values.next();
977 WeakReference<Logger> ref = node.loggerRef;
978 Logger logger = (ref == null) ? null : ref.get();
979 if (logger == null) {
980 // node holds a stale weak reference to a Logger
981 // which has been GC-ed. Note this will only cleanup
982 // stale weak refs that we encounter during our walk
983 // from the original node.
984 node.loggerRef = null;
985 node.walkAndSetParent(parent);
986 } else {
987 doSetParent(logger, parent);
988 }
989 }
990 }
991
992 // Recursively delete stale WeakReferences on each of our children.
993 void deleteStaleWeakRefs() {
994 if (children == null) {
995 return;
996 }
997 Iterator<LogNode> values = children.values().iterator();
998 while (values.hasNext()) {
999 LogNode node = values.next();
1000 WeakReference<Logger> ref = node.loggerRef;
1001 if (ref != null) {
1002 Logger logger = ref.get();
1003 if (logger == null) {
1004 // node holds a stale weak reference to a Logger
1005 // which has been GC-ed.
1006 node.loggerRef = null;
1007 }
1008 }
1009 node.deleteStaleWeakRefs();
1010 }
1011 }
1012 }
1013
1014 // We use a subclass of Logger for the root logger, so
1015 // that we only instantiate the global handlers when they
1016 // are first needed.
1017 private class RootLogger extends Logger {
1018
1019 private RootLogger() {
1020 super("", null);
1021 setLevel(defaultLevel);
1022 }
1023
1024 public void log(LogRecord record) {
1025 // Make sure that the global handlers have been instantiated.
1026 initializeGlobalHandlers();
1027 super.log(record);
1028 }
1029
1030 public void addHandler(Handler h) {
1031 initializeGlobalHandlers();
|