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

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.