Init log.c

From PeformIQ Upgrade
Jump to navigation Jump to search
//----------------------------------------------------------------------

char datestamp[64];
char logdir[128];

int log_init(char *group, int id)
{
   double now;
   time_t t;
   char *tag = "a";
   TM *ptm;
   long tag_fp;
   char *tagfile = "C:\\temp\\tag.txt";
   char *loadgen_host = lr_get_host_name();
   int  count = 0;
   char buf[1000];
   char today[16];
   char date[16];
   char ctag;
   char *sid;

   ref_time(1);

   time(&t);

   ptm = (TM*)localtime(&t);

   sid = (char*)lr_get_scenario_id();  // XXXXX

   sprintf(today, "%04d%02d%02d", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);

   if ((tag_fp = fopen(tagfile, "r")) == NULL ) {
      // lr_error_message("Cannot open %s", tagfile);
      // lr_exit(LR_EXIT_VUSER, LR_STOP);
      tag = "a";
   } else {
     if (!feof(tag_fp)) {
        count = fread(buf, sizeof(char), 9, tag_fp);
        lr_output_message("%3d read", count);

        if (ferror(tag_fp)) { /* Check for file I/O errors */
           lr_output_message("Error reading file %s", tagfile);
        }

        if (count == 9) {
           buf[count] = '\0';
           lr_output_message("TAG \"%s\"", buf);
           ctag = buf[8];
           buf[8] = '\0';
           strncpy(date, buf, 8);
           date[8] = '\0';
           lr_output_message("date \"%s\"", date);
           lr_output_message("ctag \"%c\"", ctag);
           if (!strcmp(date, today)) {
              ctag += 1;
           } else {
              ctag = 'a';
           }
        } else {
           lr_output_message("TAG count %d", count);
        }
      }

      close(tag_fp);
   }

   sprintf(datestamp, "%s%c", today, ctag);

   if ((tag_fp = fopen(tagfile, "w")) == NULL ) {
      lr_error_message("Cannot open %s", tagfile);
      lr_exit(LR_EXIT_VUSER, LR_STOP);
   } else {
      fprintf(tag_fp, datestamp);
      close(tag_fp);
   }

   lr_output_message("DATESTAMP %s", datestamp);

   sprintf(logdir, "C:\\temp\\%s.%s", loadgen_host, datestamp);

   lr_output_message("LOGDIR %s", logdir);

/*
   if ( !fstat(logdir) ) {   
      mkdir(logdir);
   }
*/


   if (mkdir(logdir)) {
      lr_output_message("Create directory \"%s\" failed", logdir);
      // lr_exit(LR_EXIT_VUSER, LR_STOP);
   } else
      lr_output_message("Created new log directory \"%s\"", logdir);

   if (chdir(logdir)) {
      lr_output_message("Unable to change to dir \"%s\"", logdir);
      // lr_exit(LR_EXIT_VUSER, LR_STOP);
   } else
      lr_output_message("Changed to new log dir \"%s\"", logdir);

   // sprintf(logdir, "C:\\temp");
   sprintf(logfile, "%s\\%s_%04d.log", logdir, group, id);

   if ((lfp = fopen(logfile, "w")) == NULL ) {
      lr_error_message("Cannot open %s", logfile);
   }

   LOG("Timestamp (%d) - %s", t, ctime(&t) );
   fprintf(lfp, "******  %s", msgbuf);
   fflush(lfp);

   lr_output_message("=====>  %s", msgbuf);

   // lr_exit(LR_EXIT_VUSER, LR_STOP);
}  // log_init