1. /*
  2. * Copyright 2001-2002,2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.listener;
  18. import org.apache.log4j.Category;
  19. import org.apache.log4j.helpers.NullEnumeration;
  20. import org.apache.tools.ant.BuildEvent;
  21. import org.apache.tools.ant.BuildListener;
  22. import org.apache.tools.ant.Project;
  23. import org.apache.tools.ant.Target;
  24. import org.apache.tools.ant.Task;
  25. /**
  26. * Listener which sends events to Log4j logging system
  27. *
  28. */
  29. public class Log4jListener implements BuildListener {
  30. /** Indicates if the listener was initialized. */
  31. private boolean initialized = false;
  32. /**
  33. * Construct the listener and make sure there is a valid appender.
  34. */
  35. public Log4jListener() {
  36. initialized = false;
  37. Category cat = Category.getInstance("org.apache.tools.ant");
  38. Category rootCat = Category.getRoot();
  39. if (!(rootCat.getAllAppenders() instanceof NullEnumeration)) {
  40. initialized = true;
  41. } else {
  42. cat.error("No log4j.properties in build area");
  43. }
  44. }
  45. /**
  46. * @see BuildListener#buildStarted
  47. */
  48. public void buildStarted(BuildEvent event) {
  49. if (initialized) {
  50. Category cat = Category.getInstance(Project.class.getName());
  51. cat.info("Build started.");
  52. }
  53. }
  54. /**
  55. * @see BuildListener#buildFinished
  56. */
  57. public void buildFinished(BuildEvent event) {
  58. if (initialized) {
  59. Category cat = Category.getInstance(Project.class.getName());
  60. if (event.getException() == null) {
  61. cat.info("Build finished.");
  62. } else {
  63. cat.error("Build finished with error.", event.getException());
  64. }
  65. }
  66. }
  67. /**
  68. * @see BuildListener#targetStarted
  69. */
  70. public void targetStarted(BuildEvent event) {
  71. if (initialized) {
  72. Category cat = Category.getInstance(Target.class.getName());
  73. cat.info("Target \"" + event.getTarget().getName() + "\" started.");
  74. }
  75. }
  76. /**
  77. * @see BuildListener#targetFinished
  78. */
  79. public void targetFinished(BuildEvent event) {
  80. if (initialized) {
  81. String targetName = event.getTarget().getName();
  82. Category cat = Category.getInstance(Target.class.getName());
  83. if (event.getException() == null) {
  84. cat.info("Target \"" + targetName + "\" finished.");
  85. } else {
  86. cat.error("Target \"" + targetName
  87. + "\" finished with error.", event.getException());
  88. }
  89. }
  90. }
  91. /**
  92. * @see BuildListener#taskStarted
  93. */
  94. public void taskStarted(BuildEvent event) {
  95. if (initialized) {
  96. Task task = event.getTask();
  97. Category cat = Category.getInstance(task.getClass().getName());
  98. cat.info("Task \"" + task.getTaskName() + "\" started.");
  99. }
  100. }
  101. /**
  102. * @see BuildListener#taskFinished
  103. */
  104. public void taskFinished(BuildEvent event) {
  105. if (initialized) {
  106. Task task = event.getTask();
  107. Category cat = Category.getInstance(task.getClass().getName());
  108. if (event.getException() == null) {
  109. cat.info("Task \"" + task.getTaskName() + "\" finished.");
  110. } else {
  111. cat.error("Task \"" + task.getTaskName()
  112. + "\" finished with error.", event.getException());
  113. }
  114. }
  115. }
  116. /**
  117. * @see BuildListener#messageLogged
  118. */
  119. public void messageLogged(BuildEvent event) {
  120. if (initialized) {
  121. Object categoryObject = event.getTask();
  122. if (categoryObject == null) {
  123. categoryObject = event.getTarget();
  124. if (categoryObject == null) {
  125. categoryObject = event.getProject();
  126. }
  127. }
  128. Category cat
  129. = Category.getInstance(categoryObject.getClass().getName());
  130. switch (event.getPriority()) {
  131. case Project.MSG_ERR:
  132. cat.error(event.getMessage());
  133. break;
  134. case Project.MSG_WARN:
  135. cat.warn(event.getMessage());
  136. break;
  137. case Project.MSG_INFO:
  138. cat.info(event.getMessage());
  139. break;
  140. case Project.MSG_VERBOSE:
  141. cat.debug(event.getMessage());
  142. break;
  143. case Project.MSG_DEBUG:
  144. cat.debug(event.getMessage());
  145. break;
  146. default:
  147. cat.error(event.getMessage());
  148. break;
  149. }
  150. }
  151. }
  152. }