| 1 | package de.uka.ipd.sdq.reliability.solver.helper; |
| 2 | |
| 3 | import org.apache.log4j.BasicConfigurator; |
| 4 | import org.apache.log4j.Level; |
| 5 | import org.apache.log4j.Logger; |
| 6 | import org.apache.log4j.PatternLayout; |
| 7 | import org.apache.log4j.WriterAppender; |
| 8 | import org.eclipse.ui.console.ConsolePlugin; |
| 9 | import org.eclipse.ui.console.IConsole; |
| 10 | import org.eclipse.ui.console.MessageConsole; |
| 11 | import org.eclipse.ui.console.MessageConsoleStream; |
| 12 | |
| 13 | public class LoggingHelper { |
| 14 | |
| 15 | /** |
| 16 | * The singleton instance. |
| 17 | */ |
| 18 | private static LoggingHelper singletonInstance = null; |
| 19 | |
| 20 | /** |
| 21 | * Retrieves the singleton instance. |
| 22 | * |
| 23 | * @return the singleton instance |
| 24 | */ |
| 25 | public static LoggingHelper getSingletonInstance() { |
| 26 | if (singletonInstance == null) { |
| 27 | singletonInstance = new LoggingHelper(); |
| 28 | } |
| 29 | return singletonInstance; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Private constructor. |
| 34 | * |
| 35 | * Prohibits external object creation. |
| 36 | */ |
| 37 | private LoggingHelper() { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Creates a new message console for the SimuService, or references an |
| 42 | * existing one. |
| 43 | * |
| 44 | * @return the message console for the SimuService |
| 45 | */ |
| 46 | private MessageConsole createOrReferenceMessageConsole() { |
| 47 | |
| 48 | // The name of the console: |
| 49 | String SimuServiceConsoleName = "PCM Reliability Solver: Markov Comparison"; |
| 50 | |
| 51 | // If the console already exists, return a reference to the existing |
| 52 | // console: |
| 53 | for (IConsole console : ConsolePlugin.getDefault().getConsoleManager() |
| 54 | .getConsoles()) { |
| 55 | if (console.getName().equals(SimuServiceConsoleName)) { |
| 56 | return (MessageConsole) console; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Create a new console: |
| 61 | MessageConsole console = new MessageConsole(SimuServiceConsoleName, |
| 62 | null); |
| 63 | ConsolePlugin.getDefault().getConsoleManager().addConsoles( |
| 64 | new IConsole[] { console }); |
| 65 | return console; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Initializes the logging functionality. |
| 70 | * |
| 71 | */ |
| 72 | public void initializeLogging() { |
| 73 | |
| 74 | // Retrieve a message console for the logging: |
| 75 | MessageConsole console = createOrReferenceMessageConsole(); |
| 76 | console.activate(); |
| 77 | console.clearConsole(); |
| 78 | MessageConsoleStream stream = console.newMessageStream(); |
| 79 | |
| 80 | // Configure log4j: |
| 81 | PatternLayout myLayout = new PatternLayout("[%-10t] %-5p: %m%n"); |
| 82 | WriterAppender writerAppender = new WriterAppender(myLayout, stream); |
| 83 | BasicConfigurator.resetConfiguration(); |
| 84 | BasicConfigurator.configure(writerAppender); |
| 85 | Logger.getRootLogger().setLevel(Level.INFO); |
| 86 | } |
| 87 | } |