Autopsy  4.6.0
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 package org.sleuthkit.autopsy.coreutils;
20 
21 import java.util.logging.Filter;
22 import java.util.logging.Handler;
23 import java.util.logging.Level;
24 import java.util.logging.LogRecord;
25 import java.util.logging.SimpleFormatter;
26 import javax.swing.JOptionPane;
27 import org.openide.util.lookup.ServiceProvider;
28 import org.netbeans.core.NbErrorManager;
29 
33 @ServiceProvider(service = Handler.class, supersedes = "org.netbeans.core.NbErrorManager")
34 public class AutopsyExceptionHandler extends Handler {
35 
36  static final int INFO_VALUE = Level.INFO.intValue();
37  static final int WARNING_VALUE = Level.WARNING.intValue();
38  static final int SEVERE_VALUE = Level.SEVERE.intValue();
39  static final Handler nbErrorManager = new NbErrorManager(); // Default NetBeans handler
40  static final Version.Type buildType = Version.getBuildType();
41  private final Logger logger = Logger.getLogger(AutopsyExceptionHandler.class.getName());
42 
44  super();
45 
46  this.setLevel(Level.SEVERE);
47  /*
48  * if (buildType == Version.Type.DEVELOPMENT) //for dev builds, show
49  * dialogs for WARNING and above this.setLevel(Level.WARNING); else
50  * //for production builds, show dialogs for SEVERE and above (TODO in
51  * future consider not show any, explicit dialogs should be in place)
52  * this.setLevel(Level.SEVERE);
53  */
54 
55  this.setFilter(new ExceptionFilter());
56  this.setFormatter(new SimpleFormatter());
57  }
58 
59  @Override
60  public void publish(LogRecord record) {
61 
62  if (isLoggable(record)) {
63  final String title = getTitleForLevelValue(record.getLevel().intValue());
64  final String message = formatExplanation(record);
65 
66  if (record.getMessage() != null) {
67  // Throwable was anticipated, caught and logged. Display log message and throwable message.
68  MessageNotifyUtil.Notify.error(title, message);
69  logger.log(Level.SEVERE, "Unexpected error: " + title + ", " + message); //NON-NLS
70  } else {
71  // Throwable (unanticipated) error. Use built-in exception handler to offer details, stacktrace.
72  nbErrorManager.publish(record);
73  }
74  }
75  }
76 
80  private static class ExceptionFilter implements Filter {
81 
82  @Override
83  public boolean isLoggable(LogRecord record) {
84  // True if there is an uncaught exception being thrown.
85  return record.getThrown() != null;
86  }
87  }
88 
97  private String formatExplanation(LogRecord record) {
98  final String logMessage = getFormatter().formatMessage(record);
99  String explanation = record.getThrown().getMessage();
100  String causeMessage = (explanation != null) ? "\nCaused by: " + explanation : ""; //NON-NLS
101 
102  return logMessage + causeMessage;
103  }
104 
105 // It's harder to do this cleanly than I thought, because Exceptions
106 // initialized with no message copy and prepend the cause's message
107 //
108 // private String recursiveExplanation(Throwable e) {
109 // String message = e.getMessage();
110 // String explanation = (message != null) ? "\nCaused by: " + message : "";
111 // Throwable cause = e.getCause();
112 // if (cause == null) {
113 // return explanation;
114 // } else {
115 // return explanation + recursiveExplanation(cause);
116 // }
117 // }
118  private static int getMessageTypeForLevelValue(int levelValue) {
119  if (levelValue >= SEVERE_VALUE) {
120  return JOptionPane.ERROR_MESSAGE;
121  } else if (levelValue >= WARNING_VALUE) {
122  return JOptionPane.WARNING_MESSAGE;
123  } else {
124  return JOptionPane.INFORMATION_MESSAGE;
125  }
126  }
127 
128  private static String getTitleForLevelValue(int levelValue) {
129  if (levelValue >= SEVERE_VALUE) {
130  return "Error"; //NON-NLS
131  } else if (levelValue >= WARNING_VALUE) {
132  return "Warning"; //NON-NLS
133  } else {
134  return "Message"; //NON-NLS
135  }
136  }
137 
138  @Override
139  public void flush() {
140  // no buffer to flush
141  }
142 
143  @Override
144  public void close() throws SecurityException {
145  // no resources to close
146  }
147 }
static Version.Type getBuildType()
Definition: Version.java:87
static void error(String title, String message)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.