PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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