You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProgramErrorManager.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.logger;
  23. import com.dmdirc.events.ErrorEvent;
  24. import com.dmdirc.events.FatalProgramErrorEvent;
  25. import com.dmdirc.events.NonFatalProgramErrorEvent;
  26. import com.dmdirc.events.ProgramErrorDeletedEvent;
  27. import com.dmdirc.events.ProgramErrorEvent;
  28. import com.dmdirc.interfaces.EventBus;
  29. import com.dmdirc.util.EventUtils;
  30. import com.dmdirc.util.LogUtils;
  31. import ch.qos.logback.classic.spi.ILoggingEvent;
  32. import java.time.LocalDateTime;
  33. import java.util.Collection;
  34. import java.util.Collections;
  35. import java.util.HashSet;
  36. import java.util.Set;
  37. import java.util.concurrent.CopyOnWriteArraySet;
  38. import javax.annotation.Nullable;
  39. import javax.inject.Inject;
  40. import javax.inject.Singleton;
  41. import org.slf4j.Marker;
  42. import net.engio.mbassy.listener.Handler;
  43. /**
  44. * Listens for {@link ErrorEvent}s, creates {@link ProgramError}s and raises {@link
  45. * ProgramErrorEvent}s.
  46. */
  47. @Singleton
  48. public class ProgramErrorManager {
  49. /** The event bus to listen for errors on. */
  50. private final EventBus eventBus;
  51. /** The current list of errors. */
  52. private final Set<ProgramError> errors;
  53. /** Factory to create {@link ProgramError}s. */
  54. private final ProgramErrorFactory programErrorFactory;
  55. @Inject
  56. public ProgramErrorManager(final EventBus eventBus,
  57. final ProgramErrorFactory programErrorFactory) {
  58. this.eventBus = eventBus;
  59. this.programErrorFactory = programErrorFactory;
  60. errors = new CopyOnWriteArraySet<>();
  61. }
  62. /**
  63. * Initialises the error manager. Must be called before logging will start.
  64. */
  65. public void initialise() {
  66. eventBus.subscribe(this);
  67. }
  68. void handle(final ILoggingEvent event) {
  69. final ProgramError error = addError(LogUtils.getErrorLevel(event.getLevel()),
  70. event.getFormattedMessage(), LogUtils.getThrowable(event),
  71. isAppError(event.getMarker()));
  72. handleErrorEvent(error);
  73. }
  74. private boolean isAppError(@Nullable final Marker marker) {
  75. return marker != null
  76. && (marker == LogUtils.APP_ERROR || marker == LogUtils.FATAL_APP_ERROR);
  77. }
  78. void handleErrorEvent(final ProgramError error) {
  79. if (error.getLevel() == ErrorLevel.FATAL) {
  80. eventBus.publish(new FatalProgramErrorEvent(error));
  81. } else {
  82. eventBus.publish(new NonFatalProgramErrorEvent(error));
  83. }
  84. }
  85. @Handler(priority = EventUtils.PRIORITY_LOWEST)
  86. @SuppressWarnings({"UseOfSystemOutOrSystemErr", "ThrowableResultOfMethodCallIgnored"})
  87. void handleProgramError(final NonFatalProgramErrorEvent event) {
  88. if (!event.isHandled()) {
  89. System.err.println(event.getError().getMessage());
  90. event.getError().getThrowableAsString().ifPresent(System.err::println);
  91. }
  92. }
  93. /**
  94. * Adds a new error to the manager with the specified details.
  95. *
  96. * @param level The severity of the error
  97. * @param message The error message
  98. * @param throwable The exception that caused the error, if any.
  99. * @param appError Whether or not this is an application error
  100. *
  101. * @since 0.6.3m1
  102. */
  103. private ProgramError addError(final ErrorLevel level, final String message,
  104. final Throwable throwable, final boolean appError) {
  105. final ProgramError error = programErrorFactory.create(level, message, throwable,
  106. LocalDateTime.now(), appError);
  107. errors.add(error);
  108. return error;
  109. }
  110. /**
  111. * Called when an error needs to be deleted from the list.
  112. *
  113. * @param error ProgramError that changed
  114. */
  115. public void deleteError(final ProgramError error) {
  116. errors.remove(error);
  117. eventBus.publishAsync(new ProgramErrorDeletedEvent(error));
  118. }
  119. /**
  120. * Deletes all errors from the manager.
  121. *
  122. * @since 0.6.3m1
  123. */
  124. public void deleteAll() {
  125. final Collection<ProgramError> errorsCopy = new HashSet<>(errors);
  126. errors.clear();
  127. errorsCopy.stream().map(ProgramErrorDeletedEvent::new).forEach(eventBus::publish);
  128. }
  129. /**
  130. * Returns the list of program errors.
  131. *
  132. * @return Program error list
  133. */
  134. public Set<ProgramError> getErrors() {
  135. return Collections.unmodifiableSet(errors);
  136. }
  137. }