/*
   <p>Test harness for <i>logsetup()</i>, <i>logmsg()</i>
   and <i>logdump()</i>.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include <stdio.h>

#ifdef OSK
#include <strings.h>
#else
#include <string.h>
#include <stdlib.h>
#endif

#include "environ.h"

main(int argc, char **argv)
{
   char  line[80];
   long  i, j;
   long  waitfact;
   int   mode;
   int   showtime;
   int   bsize;  

   setprog(argv[0]);
   if (argc < 3 || argc > 5 || strcmp(argv[1], "-?") == 0)
   {
      printf("testlogmsg mode[I|D] time[Y|N] buffsize wait\n");
      exit(1);
   }

   if (argc > 1)
   {
      if (strcmp(argv[1], "I") == 0)
         mode = INDIRECT_LOG;
      else
      if (strcmp(argv[1], "D") == 0)
         mode = DIRECT_LOG;
      else
      {
         printf("Mode must be I or D\n");
         exit(0);
      }
   }
   else
   {
      printf("Missing mode parameter\n");
   }
   
   printf("Mode = %s (%d)\n", argv[1], mode);

   if (argc > 2)
   {
      if (strcmp(argv[2], "Y") == 0)
         showtime = TRUE;
      else
      if (strcmp(argv[2], "N") == 0)
         showtime = FALSE;
      else
      {
         printf("time must be Y or N\n");
         exit(0);
      }
   }
   else
   {
      printf("Missing time parameter\n");
   }
   
   printf("Time = %s (%s)\n", argv[2], (showtime ? "TRUE" : "FALSE"));

   if (argc < 4)
      bsize = 20;
   else
      bsize = atol(argv[3]);
   
   printf("Bsize = %d\n", bsize);

   if (argc < 5)
      waitfact = 0;
   else
      waitfact = atol(argv[4]);

   printf("Wait factor = %ld\n", waitfact);
   logsetup(mode,STDOUT_LOG, bsize);
   logtime(showtime);
   while (fgets(line, 80, stdin))
   {
      line[79] = 0;
      if (line[strlen(line)-1] == '\n')
         line[strlen(line)-1] = 0;

      if (strcmp(line,"end")==0) break;
      if (strcmp(line,"quit")==0) break;
      if (strcmp(line,"dump")==0)
      {
         printf("Dump requested\n");
         logdump();
      }
      else
         logmsg("Input was: %s", line);
            
      /*
         Waste some time
      */
      if (waitfact)
         for (i=0; i<waitfact; i++)
             j = strlen(line);
   }
   printf("Final dump\n");
   logdump();
   return 0;
}
