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.

DiskLoggingErrorManager.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.commandline.CommandLineOptionsModule.Directory;
  19. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  20. import com.dmdirc.config.binding.ConfigBinder;
  21. import com.dmdirc.config.binding.ConfigBinding;
  22. import com.dmdirc.events.ErrorEvent;
  23. import com.dmdirc.events.ProgramErrorEvent;
  24. import com.dmdirc.events.eventbus.EventBus;
  25. import com.dmdirc.config.provider.AggregateConfigProvider;
  26. import com.google.common.collect.Lists;
  27. import java.io.IOException;
  28. import java.nio.charset.Charset;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. import java.util.Arrays;
  32. import java.util.List;
  33. import javax.inject.Inject;
  34. import javax.inject.Singleton;
  35. import net.engio.mbassy.listener.Handler;
  36. /**
  37. * Listens for {@link ErrorEvent}s and writes them to disk.
  38. */
  39. @Singleton
  40. public class DiskLoggingErrorManager {
  41. /** The event bus to listen for errors on. */
  42. private final EventBus eventBus;
  43. /** The directory to log errors to. */
  44. private final Path errorsDirectory;
  45. /** Error creating directory, don't write to disk. */
  46. private boolean directoryError;
  47. /** Are we logging errors to disk? */
  48. private boolean logging;
  49. @Inject
  50. public DiskLoggingErrorManager(
  51. @Directory(DirectoryType.ERRORS) final Path errorsDirectory,
  52. final EventBus eventBus) {
  53. this.errorsDirectory = errorsDirectory;
  54. this.eventBus = eventBus;
  55. }
  56. /**
  57. * Initialises the error manager. Must be called before logging will start.
  58. */
  59. public void initialise(final AggregateConfigProvider config) {
  60. final ConfigBinder configBinder = config.getBinder();
  61. configBinder.bind(this, DiskLoggingErrorManager.class);
  62. eventBus.subscribe(this);
  63. if (!Files.exists(errorsDirectory)) {
  64. try {
  65. Files.createDirectories(errorsDirectory);
  66. } catch (IOException ex) {
  67. directoryError = true;
  68. }
  69. }
  70. }
  71. /**
  72. * This is true is there was an error creating the error directory. If this is true the logger
  73. * will not attempt to write to disk irrespective of the config setting.
  74. *
  75. * @return true if there was an error creating the error directory
  76. */
  77. public boolean isDirectoryError() {
  78. return directoryError;
  79. }
  80. @Handler
  81. void handleErrorEvent(final ProgramErrorEvent error) {
  82. if (directoryError || !logging) {
  83. return;
  84. }
  85. final String logName = error.getTimestamp() + "-" + error.getError().getLevel();
  86. final Path errorFile = errorsDirectory.resolve(logName + ".log");
  87. final List<String> data = Lists
  88. .newArrayList("Date: " + error.getTimestamp(),
  89. "Level: " + error.getError().getLevel(),
  90. "Description: " + error.getError().getMessage(),
  91. "Details: ");
  92. error.getError().getThrowableAsString()
  93. .ifPresent(s -> Arrays.stream(s.split("\n")).forEach(data::add));
  94. try {
  95. Files.write(errorFile, data, Charset.forName("UTF-8"));
  96. } catch (IOException ex) {
  97. //Not really anything we can do at this point, so don't try.
  98. }
  99. }
  100. @ConfigBinding(domain = "general", key = "logerrors")
  101. void handleLoggingSetting(final boolean value) {
  102. logging = value;
  103. }
  104. }