001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic;
015
016import ch.qos.logback.classic.Level;
017
018/**
019 * @author ceki
020 */
021public class HLoggerContext {
022
023    private HLogger root;
024    private int size;
025
026    public HLoggerContext() {
027        this.root = new HLogger("root", null);
028        this.root.setLevel(Level.DEBUG);
029        size = 1;
030    }
031
032    /**
033     * Return this contexts root logger
034     *
035     * @return
036     */
037    public HLogger getRootLogger() {
038        return root;
039    }
040
041    public HLogger getLogger(final String name) {
042
043        int i = 0;
044        HLogger HLogger = root;
045        HLogger childHLogger = null;
046        String childName;
047
048        while (true) {
049            int h = name.indexOf('.', i);
050            if (h == -1) {
051                childName = name.substring(i);
052            } else {
053                childName = name.substring(i, h);
054            }
055            // move i left of the last point
056            i = h + 1;
057
058            synchronized (HLogger) {
059                childHLogger = HLogger.getChildBySuffix(childName);
060                if (childHLogger == null) {
061                    childHLogger = HLogger.createChildByLastNamePart(childName);
062                    incSize();
063                }
064            }
065            HLogger = childHLogger;
066            if (h == -1) {
067                return childHLogger;
068            }
069        }
070    }
071
072    private synchronized void incSize() {
073        size++;
074    }
075
076    int size() {
077        return size;
078    }
079
080    /**
081     * Check if the named logger exists in the hierarchy. If so return
082     * its reference, otherwise returns <code>null</code>.
083     *
084     * @param name the name of the logger to search for.
085     */
086    HLogger exists(String name) {
087        int i = 0;
088        HLogger HLogger = root;
089        HLogger childHLogger = null;
090        String childName;
091        while (true) {
092            int h = name.indexOf('.', i);
093            if (h == -1) {
094                childName = name.substring(i);
095            } else {
096                childName = name.substring(i, h);
097            }
098            // move i left of the last point
099            i = h + 1;
100
101            synchronized (HLogger) {
102                childHLogger = HLogger.getChildBySuffix(childName);
103                if (childHLogger == null) {
104                    return null;
105                }
106            }
107            HLogger = childHLogger;
108            if (h == -1) {
109                if (childHLogger.getName().equals(name)) {
110                    return childHLogger;
111                } else {
112                    return null;
113                }
114            }
115        }
116    }
117}