您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DiskLoggingErrorManager.java 4.4KB

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