13.7. Common: Starting and Stopping the Cascade TextLogger - log_toggle

[Note]

This function is located in the lib/common.g file.

The Log to button calls the log_toggle function to provide some additional functionality when it starts and stops the Cascade TextLogger. Timers might have to be run or stopped, and output files and formats have to be specified, according to the configuration file and some of the settings in the program.

   /*--------------------------------------------------------------------
    * Function:    log_toggle
    * Returns:     t or nil
    * Description: Handles some extra work that must be done when the Cascade
    *              TextLogger starts up or stops.  It sets up timers for
    *              prepare_times() and get_recent_data().  It also enables
    *              or disables the output files, according to the respective
    *              check boxes.  When the Cascade TextLogger stops, this
    *              function stops the prepare_times() and get_recent_data()
    *              timers, and it puts the log data from output file tloutput.dat
    *              into the text display widget.
    *------------------------------------------------------------------*/
   function log_toggle(button, txt_wgt, filebut, stdoutbut, anybut, fillbut, allbut)
   {
     local tsk, font, infile, line, line_ret;
   
     if (button.switched_on())
       {
         if (find_on_path("gnuplot"))
           {
             PT_TIMER = every (.1, #prepare_times());
             RCNT_TIMER = every (30.1, #get_recent_data());
           }
   
         /* Give time for the TextLogger to start on a slow processor. */
         usleep(500000);
         

Using the find_on_path function we check to see if gnuplot is installed (it's only available for Linux). If so, the Log program can create two plots to show logged data, as described in previous sections. We use timers to prepare data for these plots, using the global variables PT_TIMER and RCNT_TIMER to refer to the timers. These variables are initially defined in the gtklog.g program. The timers call prepare_times every 1/10 second and get_recent_data every 30 seconds. These functions are described in the Preparing Plots section of this chapter.

Before logging starts, we send commands to the Cascade TextLogger (its task name is tlog), based on the settings of Collect (Any, All and Fill) or Log to (file and/or stdout), using the Gamma send function. With this function, the Cascade TextLogger commands are passed like Gamma functions. For more on sending commands, please refer to Section 2.6, “Sending Commands” and the Controlling the Cascade TextLogger section of this chapter.

         if ((tsk = locate_task("tlog", nil)) != nil)
           {
             anygui_show_text(txt_wgt,
                              string("To stop logging and see the results,\n", 
                                     "press the Log to: button again.\n\n",
                                     read_msg("5.11")), 1);
   
             if (filebut.switched_on())
               send(tsk, #enable(tldemoboth));
             else
               send(tsk, #disable(tldemoboth));
             
             if (stdoutbut.switched_on())
               send(tsk, #enable(tldemostdout));
             else
               send(tsk, #disable(tldemostdout));
             
             if (anybut.switched_on())
               {
                 send(tsk, #collect(any, tldemoboth));
                 send(tsk, #collect(any, tldemostdout));
               }
             else if (fillbut.switched_on())
               {
                 send(tsk, #collect(fill, tldemoboth));
                 send(tsk, #collect(fill, tldemostdout));
               }
             else if (allbut.switched_on())
               {
                 send(tsk, #collect(all, tldemoboth));
                 send(tsk, #collect(all, tldemostdout));
               }
             close_task(tsk);
           }
       }
     else
       {
         if (find_on_path("gnuplot"))
           {
             cancel(PT_TIMER);
             cancel(RCNT_TIMER);
           }

When the button is untoggled the logging is finished, so we can cancel both timers using the Gamma cancel function, and then insert the text of the output file (tloutput.dat) into the text display widget of the program. Using a Gamma while statement, we construct a loop that reads each line of the file and constructs one long string that gets inserted into the widget using the anygui_show_text function.

   
         infile = open("/tmp/cogentdemo/tloutput.dat", "r");
         if (infile)
           {
             line = read_line(infile);
             line_ret = "";
             while(line != _eof_)
               {
                 line_ret = string(line_ret, line, "\n");
                 line = read_line(infile);
               }
             anygui_show_text(txt_wgt, line_ret, 3);
             close(infile);
           }
       }
   }