1 package net.suberic.pooka; 2 3 import net.suberic.util.*; 4 5 import java.util.logging.*; 6 import java.util.*; 7 8 12 13 public class PookaLogManager implements ValueChangeListener { 14 15 Set mMonitoredLogStrings = null; 17 18 String [] mDefaultLogSettings = new String [] { 20 "Pooka.debug", 21 "Pooka.debug.session", 22 "editors.debug", 23 "Pooka.debug.gui", 24 "Pooka.debug.gui.focus", 25 "Pooka.debug.gui.filechooser", 26 "Pooka.debug.folderTracker", 27 "Pooka.debug.logManager", 28 "Pooka.debug.startupManager", 29 "Pooka.debug.messaging", 30 "Pooka.debug.sslFactory" 31 }; 32 33 34 38 public PookaLogManager() { 39 VariableBundle globalBundle = Pooka.getResources(); 40 mMonitoredLogStrings = new HashSet(Arrays.asList(mDefaultLogSettings)); 41 42 Iterator it = mMonitoredLogStrings.iterator(); 43 while (it.hasNext()) { 44 globalBundle.addValueChangeListener(this, (String ) it.next() + ".logLevel"); 45 } 46 47 refresh(); 48 49 configureListeners(); 50 51 Logger global = Logger.getLogger(""); 53 Handler[] globalHandlers = global.getHandlers(); 54 for (int i = 0; i < globalHandlers.length; i++) { 55 globalHandlers[i].setLevel(Level.ALL); 56 } 57 } 58 59 60 63 public void refresh() { 64 Iterator it = mMonitoredLogStrings.iterator(); 65 while (it.hasNext()) { 66 String current = (String ) it.next(); 67 refresh(current); 68 } 69 } 70 71 74 public void refresh(String pKey) { 75 String levelKey = Pooka.getProperty(pKey + ".logLevel", "DEFAULT"); 76 Level newLevel = null; 77 if (levelKey == null || levelKey.equals("") || levelKey.equalsIgnoreCase("DEFAULT")) { 78 newLevel = null; 79 } else { 80 newLevel = Level.parse(levelKey); 81 } 82 getLogger().log(Level.FINE, "set log level for " + pKey + " to " + newLevel); 83 setLogLevel(pKey, newLevel); 84 85 } 86 87 90 public void configureListeners() { 91 java.awt.KeyboardFocusManager mgr = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager(); 93 mgr.addPropertyChangeListener(new java.beans.PropertyChangeListener () { 94 public void propertyChange(java.beans.PropertyChangeEvent evt) { 95 Logger logger = Logger.getLogger("Pooka.debug.gui.focus"); 96 Level logLevel = Level.FINEST; 97 if (evt.getPropertyName().equalsIgnoreCase("permanentFocusOwner")) { 98 logLevel = Level.FINE; 99 } else if (evt.getPropertyName().equalsIgnoreCase("focusOwner") || evt.getPropertyName().equalsIgnoreCase("focusOwner")) { 100 logLevel = Level.FINER; 101 } 102 String oldValue = "null"; 103 String newValue = "null"; 104 if (evt.getOldValue() != null) { 105 oldValue = evt.getOldValue().getClass().getName(); 106 } 107 if (evt.getNewValue() != null) { 108 newValue = evt.getNewValue().getClass().getName(); 109 } 110 logger.log(logLevel, evt.getPropertyName() + ": oldValue=" + oldValue + "; newValue=" + newValue); 111 112 } 113 }); 114 115 } 116 117 120 public void setLogLevel(String pName, Level pLogLevel) { 121 Logger current = Logger.getLogger(pName); 122 if (current.getLevel() != pLogLevel) { 123 current.setLevel(pLogLevel); 124 125 } 126 127 if (pName == "Pooka.debug") { 128 setLogLevel("", pLogLevel); 129 } 130 131 } 132 133 137 public void valueChanged(String pChangedValue) { 138 String key = pChangedValue; 139 if (pChangedValue.endsWith(".logLevel")) { 141 key = pChangedValue.substring(0, pChangedValue.length() - 9); 142 } 143 144 refresh(key); 145 } 146 147 150 public void addLogger(String pKey) { 151 if (! mMonitoredLogStrings.contains(pKey)) { 152 mMonitoredLogStrings.add(pKey); 153 Pooka.getResources().addValueChangeListener(this, pKey + ".logLevel"); 154 String levelKey = Pooka.getProperty(pKey + ".logLevel", "DEFAULT"); 155 Level newLevel = null; 156 if (levelKey == null || levelKey.equals("") || levelKey.equalsIgnoreCase("DEFAULT")) { 157 newLevel = null; 158 } else { 159 newLevel = Level.parse(levelKey); 160 } 161 setLogLevel(pKey, newLevel); 162 getLogger().log(Level.FINE, "added key " + pKey + "; set value to " + newLevel); 163 } 164 } 165 166 169 public void removeLogger(String pKey) { 170 if (mMonitoredLogStrings.contains(pKey)) { 171 mMonitoredLogStrings.remove(pKey); 172 Pooka.getResources().removeValueChangeListener(this, pKey + ".logLevel"); 173 getLogger().log(Level.FINE, "removed key " + pKey); 174 } 175 } 176 177 180 public Logger getLogger() { 181 return Logger.getLogger("Pooka.debug.logManager"); 182 } 183 } 184 | Popular Tags |