PageRenderTime 268ms CodeModel.GetById 141ms app.highlight 17ms RepoModel.GetById 103ms app.codeStats 1ms

/src/mpv5/logging/YConsole.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 227 lines | 150 code | 25 blank | 52 comment | 19 complexity | 3d00a596907cc2015a697c90bbc71c25 MD5 | raw file
  1/*
  2 * Logger.java
  3 *
  4 * Created on 18. Februar 2008, 22:14
  5 */
  6package mpv5.logging;
  7
  8import java.io.File;
  9import mpv5.utils.files.*;
 10import java.io.IOException;
 11import javax.swing.JComponent;
 12import javax.swing.text.Document;
 13import javax.swing.text.Element;
 14
 15/**
 16 *
 17 *  
 18 */
 19public class YConsole extends javax.swing.JFrame implements LogConsole {
 20
 21    private static File logfile = null;
 22    public static boolean FILE_LOG_ENABLED = false;
 23    public static boolean CONSOLE_LOG_ENABLED = false;
 24    public static boolean WINDOW_LOG_ENABLED = false;
 25    private static FileReaderWriter logwriter;
 26    private static final long serialVersionUID = 1L;
 27
 28    /**
 29     * Enable/disable log stream targets
 30     * @param fileLog
 31     * @param consoleLog
 32     * @param windowLog
 33     */
 34    public static void setLogStreams(boolean fileLog, boolean consoleLog, boolean windowLog) {
 35        FILE_LOG_ENABLED = fileLog;
 36        CONSOLE_LOG_ENABLED = consoleLog;
 37        WINDOW_LOG_ENABLED = windowLog;
 38    }
 39
 40    /**
 41     * Set a file to log to. A null value for file stops file logging
 42     * @param file The log file
 43     * @throws java.lang.Exception
 44     */
 45    public static void setLogFile(String file) throws Exception {
 46        if (file != null) {
 47            YConsole.logfile = new File(file);
 48//            getLogfile().delete();
 49            if (!getLogfile().exists()&& !logfile.createNewFile()) {
 50                throw new Exception("Error creating " + getLogfile().getCanonicalPath());
 51            } else {
 52                FILE_LOG_ENABLED = true;
 53                logwriter = new FileReaderWriter(getLogfile());
 54                Log.Debug(YConsole.class, "Logging to File: " + getLogfile().getPath());
 55            }
 56        } else {
 57            FILE_LOG_ENABLED = false;
 58        }
 59    }
 60    private static int line = 0;
 61
 62    /**
 63     * @return the logfile
 64     */
 65    public static File getLogfile() {
 66        return logfile;
 67    }
 68
 69    /** Creates new form Logger */
 70    public YConsole() {
 71        initComponents();
 72    }
 73
 74    /**
 75     * Append a new line to the logging object
 76     */
 77    @Override
 78    public void log(){
 79        log("\n");
 80    }
 81
 82    /**
 83     * Log the String value of the given Object
 84     * @param object Null objects will lead to the String "NULL"
 85     */
 86    @Override
 87    public synchronized void log(final Object object){
 88
 89        Runnable runnable = new Runnable() {
 90
 91            @Override
 92            public void run() {
 93                line++;
 94                if (FILE_LOG_ENABLED) {
 95                    if (logwriter != null) {
 96                        logwriter.write(line + ": " + object.toString());
 97                    } else {
 98                        Log.Print(object);
 99                    }
100                }
101                if (CONSOLE_LOG_ENABLED) {
102                    System.out.println(object);
103                }
104                if (WINDOW_LOG_ENABLED) {
105                    if (object != null) {
106                        jTextArea1.append("\n" + line + ": " + object.toString());
107                    } else {
108                        jTextArea1.append("\n" + line + ": " + "NULL");
109                    }
110                }
111
112                Document document = jTextArea1.getDocument();
113                Element rootElem = document.getDefaultRootElement();
114                int numLines = rootElem.getElementCount();
115                Element lineElem = rootElem.getElement(numLines - 1);
116                int lineStart = lineElem.getStartOffset();
117                int lineEnd = lineElem.getEndOffset();
118                jTextArea1.setCaretPosition(lineStart);
119            }
120        };
121        if (Log.getLoglevel() != Log.LOGLEVEL_DEBUG) {
122            Thread t = new Thread(runnable);
123            t.start();
124        } else {
125            //On Event Thread
126            runnable.run();
127        }
128    }
129
130    /**
131     * Show the log konsole (will change the log level to DEBUG and enable Window logging)
132     * @return 
133     */
134    public JComponent open() {
135        Log.setLogLevel(Log.LOGLEVEL_DEBUG);
136        WINDOW_LOG_ENABLED = true;
137        return getRootPane();
138    }
139
140    /** This method is called from within the constructor to
141     * initialize the form.
142     * WARNING: Do NOT modify this code. The content of this method is
143     * always regenerated by the Form Editor.
144     */
145    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
146    private void initComponents() {
147
148        jScrollPane1 = new javax.swing.JScrollPane();
149        jTextArea1 = new javax.swing.JTextArea();
150        jButton1 = new javax.swing.JButton();
151
152        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
153        setTitle("MP Logger");
154
155        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
156        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
157        jScrollPane1.setAutoscrolls(true);
158
159        jTextArea1.setColumns(20);
160        jTextArea1.setFont(new java.awt.Font("Dialog", 0, 10));
161        jTextArea1.setForeground(new java.awt.Color(0, 51, 51));
162        jTextArea1.setRows(5);
163        jScrollPane1.setViewportView(jTextArea1);
164
165        jButton1.setText("Flush");
166        jButton1.addActionListener(new java.awt.event.ActionListener() {
167            public void actionPerformed(java.awt.event.ActionEvent evt) {
168                jButton1ActionPerformed(evt);
169            }
170        });
171
172        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
173        getContentPane().setLayout(layout);
174        layout.setHorizontalGroup(
175            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
176            .addGroup(layout.createSequentialGroup()
177                .addContainerGap()
178                .addComponent(jButton1)
179                .addContainerGap(338, Short.MAX_VALUE))
180            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
181        );
182        layout.setVerticalGroup(
183            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
184            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
185                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
186                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
187                .addComponent(jButton1)
188                .addContainerGap())
189        );
190
191        pack();
192    }// </editor-fold>//GEN-END:initComponents
193
194private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
195    flush();
196}//GEN-LAST:event_jButton1ActionPerformed
197
198    /**
199     * @param args the command line arguments
200     */
201    public static void main(String args[]) {
202        java.awt.EventQueue.invokeLater(new Runnable() {
203
204            public void run() {
205                new YConsole().setVisible(true);
206            }
207        });
208    }
209
210    /**
211     * Flush the current logging object
212     */
213    public void flush() {
214        if (FILE_LOG_ENABLED) {
215            logwriter.flush();
216        }
217        if (WINDOW_LOG_ENABLED) {
218            jTextArea1.setText(null);
219        }
220    }
221    // Variables declaration - do not modify//GEN-BEGIN:variables
222    private javax.swing.JButton jButton1;
223    private javax.swing.JScrollPane jScrollPane1;
224    private javax.swing.JTextArea jTextArea1;
225    // End of variables declaration//GEN-END:variables
226    // End of variables declaration
227}