1   /**
2    * LOGBack: the reliable, fast and flexible logging library for Java.
3    *
4    * Copyright (C) 1999-2005, QOS.ch, LOGBack.com
5    *
6    * This library is free software, you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation.
9    */
10  
11  package ch.qos.logback.classic;
12  
13  import ch.qos.logback.classic.Level;
14  
15  
16  
17  /**
18   * @author ceki
19   */
20  public class HLoggerContext {
21  
22    private HLogger root;
23    private int size;
24  
25    public HLoggerContext() {
26      this.root = new HLogger("root", null);
27      this.root.setLevel(Level.DEBUG);
28      size = 1;
29    }
30  
31    /**
32     * Return this contexts root logger
33     *
34     * @return
35     */
36    public HLogger getRootLogger() {
37      return root;
38    }
39  
40    public HLogger getLogger(final String name) {
41  
42      int i = 0;
43      HLogger HLogger = root;
44      HLogger childHLogger = null;
45      String childName;
46  
47      while (true) {
48        int h = name.indexOf('.', i);
49        if (h == -1) {
50          childName = name.substring(i);
51        } else {
52          childName = name.substring(i, h);
53        }
54        // move i left of the last point
55        i = h + 1;
56  
57        synchronized (HLogger) {
58          childHLogger = HLogger.getChildBySuffix(childName);
59          if (childHLogger == null) {
60            childHLogger = HLogger.createChildByLastNamePart(childName);
61            incSize();
62          }
63        }
64        HLogger = childHLogger;
65        if (h == -1) {
66          return childHLogger;
67        }
68      }
69    }
70  
71    private synchronized  void incSize() {
72      size++;
73    }
74  
75    int size() {
76      return size;
77    }
78    /**
79     * Check if the named logger exists in the hierarchy. If so return
80     * its reference, otherwise returns <code>null</code>.
81     *
82     * @param name the name of the logger to search for.
83     */
84    HLogger exists(String name) {
85      int i = 0;
86      HLogger HLogger = root;
87      HLogger childHLogger = null;
88      String childName;
89      while (true) {
90        int h = name.indexOf('.', i);
91        if (h == -1) {
92          childName = name.substring(i);
93        } else {
94          childName = name.substring(i, h);
95        }
96        // move i left of the last point
97        i = h + 1;
98  
99        synchronized (HLogger) {
100         childHLogger = HLogger.getChildBySuffix(childName);
101         if (childHLogger == null) {
102           return null;
103         }
104       }
105       HLogger = childHLogger;
106       if (h == -1) {
107         if (childHLogger.getName().equals(name)) {
108           return childHLogger;
109         } else {
110           return null;
111         }
112       }
113     }
114   }
115 }