13.3. GTK: Preparing Plots - prepare_times, get_recent_data

These two functions create the necessary data files for gnuplot, as described in the previous section.

[Note]

All the data files in the demo are written to the /tmp/cogentdemo/ temporary directory, and deleted when the Controller exits.

   /*--------------------------------------------------------------------
    * Function:    prepare_times
    * Returns:     t or nil
    * Description: Writes time data to the gnuplot load file pllogtimes.dat.
    *              The data is used to create x-axis labels for time, and
    *              to update the top and bottom range, to give a scrolling
    *              "trend" effect to the plot.
    *------------------------------------------------------------------*/
   function prepare_times ()
   {
     local line, time, top, bottom;
     local fp = open("/tmp/cogentdemo/pllogtimes.dat", "w", nil);
   
     cuttime = (floor(clock() / 10000) * 10000);
     line = string("cuttime = ", cuttime, "\n");
     writec(fp, line);
   
     top = nanoclock() - cuttime;
     line = string("top = ", top, "\n");
     writec(fp, line);
   
     bottom = top - 15;
     line = string("bottom = ", bottom, "\n");
     writec(fp, line);
     close(fp);
   }
   

The prepare_times function uses the following Gamma functions: open to open the file, clock to get the system time in seconds, nanoclock to get the system time in seconds and nanoseconds, string to construct strings, writec to write formatted output to the file, and close to close the open file.

The get_recent_data function uses the Gamma function rename to change the name of a file. It also uses the Gamma send function to send commands to the Cascade TextLogger. Please refer to the explanation of the send_command function for details on using this function for this purpose.

   /*--------------------------------------------------------------------
    * Function:    get_recent_data
    * Returns:     t or nil
    * Description: disables the tldemorecent log, renames the tlrecent.dat
    *              file, and restarts logging to a new tlrecent.dat file.
    *------------------------------------------------------------------*/
   function get_recent_data()
   {
     local tsk, fr, fw, line = "";
   
     if ((tsk = locate_task("tlog", nil)) != nil)
       {
         send(tsk, `disable (tldemorecent));
         rename("/tmp/cogentdemo/tlrecent.dat", "/tmp/cogentdemo/tlold.dat");
         
         /* Ensure that tlrecent.dat file gets closed and deleted. */
         send(tsk, `file("/tmp/cogentdemo/null.dat", tldemorecent));
         
         send(tsk, `file("/tmp/cogentdemo/tlrecent.dat", tldemorecent));
         send(tsk, `enable(tldemorecent));
       }
   }