PageRenderTime 77ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ppt/poi/org/apache/poi/util/POILogFactory.java

https://github.com/minstrelsy/POI-Android
Java | 132 lines | 44 code | 19 blank | 69 comment | 7 complexity | fec4de1d768a33c0d041b1301f59c457 MD5 | raw file
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import java.util.*;
  17. /**
  18. * Provides logging without clients having to mess with
  19. * configuration/initialization.
  20. *
  21. * @author Andrew C. Oliver (acoliver at apache dot org)
  22. * @author Marc Johnson (mjohnson at apache dot org)
  23. * @author Nicola Ken Barozzi (nicolaken at apache.org)
  24. */
  25. public class POILogFactory
  26. {
  27. /**
  28. * Map of POILogger instances, with classes as keys
  29. */
  30. private static Map<String,POILogger> _loggers = new HashMap<String,POILogger>();;
  31. /**
  32. * A common instance of NullLogger, as it does nothing
  33. * we only need the one
  34. */
  35. private static POILogger _nullLogger = new NullLogger();
  36. /**
  37. * The name of the class to use. Initialised the
  38. * first time we need it
  39. */
  40. private static String _loggerClassName = null;
  41. /**
  42. * Construct a POILogFactory.
  43. */
  44. private POILogFactory()
  45. {
  46. }
  47. /**
  48. * Get a logger, based on a class name
  49. *
  50. * @param theclass the class whose name defines the log
  51. *
  52. * @return a POILogger for the specified class
  53. */
  54. public static POILogger getLogger(final Class theclass)
  55. {
  56. return getLogger(theclass.getName());
  57. }
  58. /**
  59. * Get a logger, based on a String
  60. *
  61. * @param cat the String that defines the log
  62. *
  63. * @return a POILogger for the specified class
  64. */
  65. public static POILogger getLogger(final String cat)
  66. {
  67. POILogger logger = null;
  68. // logger = new CommonsLogger();
  69. // logger.initialize(cat);
  70. //
  71. // If we haven't found out what logger to use yet,
  72. // then do so now
  73. // Don't look it up until we're first asked, so
  74. // that our users can set the system property
  75. // between class loading and first use
  76. if(_loggerClassName == null) {
  77. try {
  78. // _loggerClassName = System.getProperty("org.apache.poi.util.POILogger");
  79. _loggerClassName = CommonsLogger.class.getName();
  80. } catch(Exception e) {}
  81. // Use the default logger if none specified,
  82. // or none could be fetched
  83. if(_loggerClassName == null) {
  84. _loggerClassName = _nullLogger.getClass().getName();
  85. }
  86. }
  87. // Short circuit for the null logger, which
  88. // ignores all categories
  89. if(_loggerClassName.equals(_nullLogger.getClass().getName())) {
  90. return _nullLogger;
  91. }
  92. // Fetch the right logger for them, creating
  93. // it if that's required
  94. if (_loggers.containsKey(cat)) {
  95. logger = _loggers.get(cat);
  96. } else {
  97. try {
  98. Class<? extends POILogger> loggerClass =
  99. (Class<? extends POILogger>)Class.forName(_loggerClassName);
  100. logger = loggerClass.newInstance();
  101. logger.initialize(cat);
  102. } catch(Exception e) {
  103. // Give up and use the null logger
  104. logger = _nullLogger;
  105. }
  106. // Save for next time
  107. _loggers.put(cat, logger);
  108. }
  109. return logger;
  110. }
  111. } // end public class POILogFactory