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

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