SOURCECODE

How to... display only certain entries of a log file


Description:
In a logfile you can log any event of your framework-application, wether it is a commited process or a rollback, a User logging on, a wrong password entered, just anything.
Displaying only certain entries of a logfile in a FormSheet, is almost the same thing like displaying the content of a log file on a FormSheet. Here you, too, use the LogTableForm, which is a subclass of FormSheet and contains a JLogTable. And you also have to remember that the information in that table is being read at creation time and won't be updated when the logfile changes.
In this example we take a look at the same LogTableForm in the Action() of a MenuItem in the Office, but add a LogEntryFilter to the LogInputStream that only accepts instances of OpenLogEntry, which we created in "How to define a new LogEntry". This can be achieved by subclassing the LogEntryFilter, define the accept() method properly and add this filter to the LogInputStream.
For more information on LogEntryFilters please refer to "How to define a LogEntryFilter".

ToDo's:
  1. Create a FileInputStream to process the logfile
  2. Implement the LogEntryFilter and add it with the FileInputStream to the LogInputStream, which will only process entries accepted by the filter
  3. Initialize a new LogTableForm that is made for displaying LogInputStreams
  4. Set the LogTableForm to the FormSheet of the Salespoint by calling setFormSheet(SaleProcess sp, FormSheet fs)
  5. Catch the FileNotFoundEception
  6. Catch the IOException
  7. And finally catch the InterruptedException




SourceCode


        //create a new MenuSheetItem to be displayed in the submenu
        MenuSheetItem msi2 = new MenuSheetItem (

                                    "See log file", //the caption

                                    new sale.Action() {
                //the action to be performed when the MenuSheetItem is being chosen
                //as you can see, such an action can also be quite complex and
                //even contain FormSheets and dialogs

                public void doAction (SaleProcess p, SalesPoint sp) {
                    try {
    
                         1
                        //create a FileInputStream
                        FileInputStream fis = new FileInputStream ("machine.log");
                        
    
                         2
                        //implement the LogEntryFilter
                        LogEntryFilter lef = new LogEntryFilter() {
                            //by defining the accept(Loggable l) method
                            public boolean accept(LogEntry le) {
                                //to only accept instances of OpenLogEntry
                                return (le instanceof OpenLogEntry);
                            }
                        };

                        //and put the FileInputStream and the filter
                        //into the LogInputStream
                        LogInputStream lis2 = new LogInputStream (fis, lef);
    
                         3
                        //and finally have the stream being displayed on the LogTableForm
                        LogTableForm ltf2 = new LogTableForm ("View log file", lis2);
                        
                        //no logfile specific action. just a removal of the cancelbutton
                        ltf2.removeButton (FormSheet.BTNID_CANCEL);
                        
                         4
                        //setup the window with the LogTableForm
                        setFormSheet (null, ltf2);

                    //looks like lots of exceptions to be caught,
                    //but only the three of them refer to the InputStreams,
                    //the InterruptedExceptions (except number 8) belong to the setFormSheet(...) method
    
                     5
                    //this
                    } catch (FileNotFoundException fnfexc) {
                        //this
                        try {
                            setFormSheet(null, new MsgForm("Error", "Log file not found."));
                        } catch (InterruptedException iexc) {
                            System.out.println(iexc);
                        }

                     6
                    //this
                    } catch (IOException ioexc) {
                        try {
                            setFormSheet( null,
                                    new MsgForm("Error",
                                    "Log file corrupt. It might be empty."));
                        } catch (InterruptedException iexc) {
                            System.out.println(iexc);
                        }
                
                 7
                    //and this
                    } catch (InterruptedException iexc) {
                        try {
                            setFormSheet (null, new MsgForm ("Error", iexc.toString()));
                        } catch (InterruptedException iexc2) {
                            System.out.println(iexc2);
                        }
                    }
                }
        });

        //add the MenuSheetItem to the msSubMenu
        msSubMenu.add(msi2);