Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
AutopsyExceptionHandler.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 package org.sleuthkit.autopsy.coreutils;
21 
22 import java.awt.Component;
23 import java.util.logging.Filter;
24 import java.util.logging.Handler;
25 import java.util.logging.Level;
26 import java.util.logging.LogRecord;
27 import java.util.logging.SimpleFormatter;
28 import javax.swing.JOptionPane;
29 import javax.swing.SwingUtilities;
30 import org.openide.util.lookup.ServiceProvider;
31 import org.netbeans.core.NbErrorManager;
32 
36 @ServiceProvider(service = Handler.class, supersedes = "org.netbeans.core.NbErrorManager")
37 public class AutopsyExceptionHandler extends Handler {
38 
39  static final int INFO_VALUE = Level.INFO.intValue();
40  static final int WARNING_VALUE = Level.WARNING.intValue();
41  static final int SEVERE_VALUE = Level.SEVERE.intValue();
42  static final Handler nbErrorManager = new NbErrorManager(); // Default NetBeans handler
43  static final Version.Type buildType = Version.getBuildType();
44  private final Logger logger = Logger.getLogger(AutopsyExceptionHandler.class.getName());
45 
47  super();
48 
49  this.setLevel(Level.SEVERE);
50  /*
51  if (buildType == Version.Type.DEVELOPMENT)
52  //for dev builds, show dialogs for WARNING and above
53  this.setLevel(Level.WARNING);
54  else
55  //for production builds, show dialogs for SEVERE and above (TODO in future consider not show any, explicit dialogs should be in place)
56  this.setLevel(Level.SEVERE);
57  */
58 
59  this.setFilter(new ExceptionFilter());
60  this.setFormatter(new SimpleFormatter());
61  }
62 
63  @Override
64  public void publish(LogRecord record) {
65 
66  if (isLoggable(record)) {
67 
68  if (record.getMessage() != null) {
69  // Throwable was anticipated, caught and logged. Display log message and throwable message.
70 
71  final int levelValue = record.getLevel().intValue();
72 
73  final Component parentComponent = null; // Use default window frame.
74  final String message = formatExplanation(record);
75  final String title = getTitleForLevelValue(levelValue);
76  final int messageType = getMessageTypeForLevelValue(levelValue);
77 
78  // publish() was probably not called from the EDT, so run the message box there instead of here.
79  //only show the dialog in dev builds
80  if (buildType == Version.Type.DEVELOPMENT) {
81  SwingUtilities.invokeLater(new Runnable() {
82 
83  @Override
84  public void run() {
85  JOptionPane.showMessageDialog(
86  parentComponent,
87  message,
88  title,
89  messageType);
90  }
91  });
92  }
93  logger.log(Level.SEVERE, "Unexpected error: " + title + ", " + message ); //NON-NLS
94  } else {
95  // Throwable (unanticipated) error. Use built-in exception handler to offer details, stacktrace.
96  nbErrorManager.publish(record);
97  }
98  }
99  }
100 
101 
105  private static class ExceptionFilter implements Filter {
106  @Override
107  public boolean isLoggable(LogRecord record) {
108  // True if there is an uncaught exception being thrown.
109  return record.getThrown() != null;
110  }
111  }
112 
118  private String formatExplanation(LogRecord record) {
119  final String logMessage = getFormatter().formatMessage(record);
120  String explanation = record.getThrown().getMessage();
121  String causeMessage = (explanation != null) ? "\nCaused by: " + explanation : ""; //NON-NLS
122 
123  return logMessage + causeMessage;
124  }
125 
126 // It's harder to do this cleanly than I thought, because Exceptions
127 // initialized with no message copy and prepend the cause's message
128 //
129 // private String recursiveExplanation(Throwable e) {
130 // String message = e.getMessage();
131 // String explanation = (message != null) ? "\nCaused by: " + message : "";
132 // Throwable cause = e.getCause();
133 // if (cause == null) {
134 // return explanation;
135 // } else {
136 // return explanation + recursiveExplanation(cause);
137 // }
138 // }
139 
140  private static int getMessageTypeForLevelValue(int levelValue) {
141  if (levelValue >= SEVERE_VALUE) {
142  return JOptionPane.ERROR_MESSAGE;
143  } else if (levelValue >= WARNING_VALUE) {
144  return JOptionPane.WARNING_MESSAGE;
145  } else {
146  return JOptionPane.INFORMATION_MESSAGE;
147  }
148  }
149 
150  private static String getTitleForLevelValue(int levelValue) {
151  if (levelValue >= SEVERE_VALUE) {
152  return "Error"; //NON-NLS
153  } else if (levelValue >= WARNING_VALUE) {
154  return "Warning"; //NON-NLS
155  } else {
156  return "Message"; //NON-NLS
157  }
158  }
159 
160  @Override
161  public void flush() {
162  // no buffer to flush
163  }
164 
165  @Override
166  public void close() throws SecurityException {
167  // no resources to close
168  }
169 }
static Version.Type getBuildType()
Definition: Version.java:88
static Logger getLogger(String name)
Definition: Logger.java:131

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.