1 23 package com.lutris.logging; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 import java.util.Hashtable ; 31 32 import com.lutris.util.Config; 33 import com.lutris.util.ConfigException; 34 import com.lutris.util.ConfigFile; 35 import com.lutris.util.KeywordValueException; 36 37 51 public class StandardLogger extends Logger { 52 53 private static final String LOG_SECTION = "Server"; 54 private static final String LOG_FILE = "LogFile"; 55 private static final String LOG_TO_FILE = "LogToFile"; 56 private static final String LOG_TO_STDERR = "LogToStderr"; 57 58 63 private Hashtable levelNumbers = new Hashtable (); 64 65 69 protected String [] levelNames = new String [MAX_STD_LEVEL * 2]; 70 protected int numLevels = 0; 71 72 76 protected boolean[] enabledLevelFlags = null; 77 78 82 protected boolean[] logFileLevelFlags = null; 83 84 90 protected boolean[] stderrLevelFlags = null; 91 92 95 File activeLogFile; 96 97 100 PrintWriter logFileStream; 101 102 105 PrintWriter stderrStream; 106 107 111 private Hashtable logChannels = new Hashtable (); 112 113 119 public StandardLogger(boolean makeCentral) { 120 int level; 121 122 for (level = 0; level <= MAX_STD_LEVEL; level++) { 123 String name = standardLevelNames[level]; 124 125 levelNumbers.put(name, new Integer (level)); 126 levelNames[level] = name; 127 } 128 numLevels = level; 129 if (makeCentral) { 130 centralLogger = this; 131 } 132 } 133 134 140 private int getMaxLevel(String [] levels) { 141 int levelNum; 142 int maxLevelNum = 0; 143 144 for (int idx = 0; idx < levels.length; idx++) { 145 levelNum = getLevel(levels[idx]); 146 if (levelNum > maxLevelNum) { 147 maxLevelNum = levelNum; 148 } 149 } 150 return maxLevelNum; 151 } 152 153 160 private boolean[] getLevelStateArray(String [] levels, int maxLevelNum) { 161 int levelNum; 162 boolean[] levelNums = new boolean[maxLevelNum + 1]; 164 165 for (int idx = 0; idx < levels.length; idx++) { 166 levelNums[getLevel(levels[idx])] = true; 167 } 168 return levelNums; 169 } 170 171 180 public synchronized File switchLogFile(File logFile) 181 throws java.io.IOException { 182 PrintWriter oldLogFileStream = logFileStream; 183 File oldActiveLogFile = activeLogFile; 184 FileOutputStream out = new FileOutputStream (logFile.getPath(), 186 true); 187 188 logFileStream = new PrintWriter (out, false); 189 activeLogFile = logFile; 190 if (oldLogFileStream != null) { 192 synchronized (oldLogFileStream) { 193 oldLogFileStream.close(); 194 } 195 } 196 return oldActiveLogFile; 197 } 198 199 211 public synchronized void configure(File logFile, 212 String [] fileLevels, 213 String [] stderrLevels) 214 throws java.io.IOException { 215 if (logFile.getParent() != null) { 217 new File (logFile.getParent()).mkdirs(); 218 } 219 switchLogFile(logFile); 221 stderrStream = new PrintWriter (System.err, false); 222 223 231 int maxLevelNum; 232 int levelNum; 233 234 if (enabledLevelFlags != null) { 235 maxLevelNum = enabledLevelFlags.length - 1; 236 } else { 237 maxLevelNum = MAX_STD_LEVEL; 238 } 239 levelNum = getMaxLevel(fileLevels); 240 if (levelNum > maxLevelNum) { 241 maxLevelNum = levelNum; 242 } 243 levelNum = getMaxLevel(stderrLevels); 244 if (levelNum > maxLevelNum) { 245 maxLevelNum = levelNum; 246 } 247 logFileLevelFlags = getLevelStateArray(fileLevels, maxLevelNum); 249 stderrLevelFlags = getLevelStateArray(stderrLevels, maxLevelNum); 250 enabledLevelFlags = new boolean[maxLevelNum + 1]; 251 for (int idx = 0; idx < logFileLevelFlags.length; idx++) { 252 if (logFileLevelFlags[idx]) { 253 enabledLevelFlags[idx] = true; 254 } 255 } 256 for (int idx = 0; idx < stderrLevelFlags.length; idx++) { 257 if (stderrLevelFlags[idx]) { 258 enabledLevelFlags[idx] = true; 259 } 260 } 261 } 262 263 266 private synchronized StandardLogChannel createChannel(String facility) { 267 StandardLogChannel channel 268 = (StandardLogChannel) logChannels.get(facility); 269 270 if (channel == null) { 271 channel = new StandardLogChannel(facility, this); 272 logChannels.put(facility, channel); 273 } 274 return channel; 275 } 276 277 283 public LogChannel getChannel(String facility) { 284 StandardLogChannel channel 285 = (StandardLogChannel) logChannels.get(facility); 286 287 if (channel == null) { 288 channel = createChannel(facility); 290 } 291 return channel; 292 } 293 294 297 private synchronized Integer createLevel(String level) { 298 Integer intLevel = (Integer ) levelNumbers.get(level); 299 300 if (intLevel == null) { 301 intLevel = new Integer (numLevels); 302 levelNames[numLevels] = level; 303 levelNumbers.put(level, intLevel); 304 numLevels++; 305 } 306 return intLevel; 307 } 308 309 316 public synchronized int getLevel(String level) { 317 Integer intLevel = (Integer ) levelNumbers.get(level); 318 319 if (intLevel == null) { 320 intLevel = createLevel(level); 322 } 323 return intLevel.intValue(); 324 } 325 326 332 public String getLevelName(int level) { 333 if ((level >= 0) && (level < numLevels)) { 334 return levelNames[level]; 335 } else { 336 return null; 337 } 338 } 339 340 346 public void configure(String confFilePath) throws ConfigException { 347 try { 348 FileInputStream configFIS = new FileInputStream (confFilePath); 349 ConfigFile cFile = new ConfigFile(configFIS); 350 Config config = cFile.getConfig(); 351 352 configFIS.close(); 353 Config logConfig = (Config) config.getSection(LOG_SECTION); 354 355 configure(logConfig); 356 } catch (KeywordValueException kve) { 357 throw new ConfigException("Error parsing configuration for logger.", 358 kve); 359 } catch (IOException ioe) { 360 throw new ConfigException("Error configuring logger.", ioe); 361 } 362 } 363 364 369 public void configure(Config logConfig) throws ConfigException { 370 if (logConfig == null) { 371 throw new ConfigException("Cannot configure logger. Config is null."); 372 } else { 373 String logFile = null; 374 375 logFile = logConfig.getString(LOG_FILE); 376 String [] toFile = null; 377 378 toFile = logConfig.getStrings(LOG_TO_FILE); 379 String [] toStderr = null; 380 381 toStderr = logConfig.getStrings(LOG_TO_STDERR); 382 File theLogFile = new File (logFile); 383 384 try { 385 configure(theLogFile, toFile, toStderr); 386 } catch (IOException ioe) { 387 throw new ConfigException("Error configuring logger.", 388 ioe); 389 } 390 } 391 } 392 } 393 | Popular Tags |