[BACK]Return to log.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Diff for /src/usr.bin/mg/log.c between version 1.11 and 1.12

version 1.11, 2019/07/18 10:50:24 version 1.12, 2021/03/02 13:06:50
Line 46 
Line 46 
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
 #include <unistd.h>  #include <unistd.h>
   #include <stdarg.h>
   
 #include "def.h"  #include "def.h"
 #include "key.h"  #include "key.h"
Line 55 
Line 56 
   
 #include "log.h"  #include "log.h"
   
 static char     *mglogfiles_create(char *);  static char     *mglogfiles_create(FILE **, char *);
 static int       mglog_lines(PF);  static int       mglog_lines(PF);
 static int       mglog_undo(void);  static int       mglog_undo(void);
 static int       mglog_window(void);  static int       mglog_window(void);
 static int       mglog_key(KEYMAP *map);  static int       mglog_key(KEYMAP *map);
   
 char            *mglogdir;  const char      *mglogdir;
 extern char     *mglogpath_lines;  const char      *mglogpath_lines;
 extern char     *mglogpath_undo;  const char      *mglogpath_undo;
 extern char     *mglogpath_window;  const char      *mglogpath_window;
 extern char     *mglogpath_key;  const char      *mglogpath_key;
 extern char     *mglogpath_interpreter;  const char      *mglogpath_interpreter;
   const char      *mglogpath_misc;
 int              mgloglevel;  int              mgloglevel;
   
   FILE            *fd_lines;
   FILE            *fd_undo;
   FILE            *fd_window;
   FILE            *fd_key;
   FILE            *fd_interpreter;
   FILE            *fd_misc;
   
 int  int
 mglog(PF funct, KEYMAP *map)  mglog(PF funct, void *map)
 {  {
         if(!mglog_lines(funct))          if(!mglog_lines(funct))
                 ewprintf("Problem logging lines");                  ewprintf("Problem logging lines");
Line 88 
Line 97 
 static int  static int
 mglog_key(KEYMAP *map)  mglog_key(KEYMAP *map)
 {  {
         struct stat      sb;  
         FILE            *fd;  
         PF              *pfp;          PF              *pfp;
   
         if(stat(mglogpath_key, &sb))  
                  return (FALSE);  
         fd = fopen(mglogpath_key, "a");  
   
         if (ISWORD(*key.k_chars)) {          if (ISWORD(*key.k_chars)) {
                 if (fprintf(fd, "k_count:%d k_chars:%hd\tchr:%c\t", key.k_count,                  fprintf(fd_key, "k_count:%d k_chars:%hd\tchr:%c\t", key.k_count,
                     *key.k_chars, CHARMASK(*key.k_chars)) == -1) {                      *key.k_chars, CHARMASK(*key.k_chars));
                         fclose(fd);  
                         return (FALSE);  
                 }  
         } else {          } else {
                 if (fprintf(fd, "k_count:%d k_chars:%hd\t\t", key.k_count,                  fprintf(fd_key, "k_count:%d k_chars:%hd\t\t", key.k_count,
                     *key.k_chars) == -1) {                      *key.k_chars);
                         fclose(fd);  
                         return (FALSE);  
                 }  
         }          }
         if (fprintf(fd, "map:%p %d %d %p %hd %hd\n",          fprintf(fd_key, "map:%p %d %d %p %hd %hd\n",
             map,              map,
             map->map_num,              map->map_num,
             map->map_max,              map->map_max,
             map->map_default,              map->map_default,
             map->map_element->k_base,              map->map_element->k_base,
             map->map_element->k_num              map->map_element->k_num
             ) == -1) {              );
                 fclose(fd);          for (pfp = map->map_element->k_funcp; *pfp != NULL; pfp++)
                 return (FALSE);                  fprintf(fd_key, "%s ", function_name(*pfp));
         }  
         for (pfp = map->map_element->k_funcp; *pfp != '\0'; pfp++)  
                 fprintf(fd, "%s ", function_name(*pfp));  
   
         fprintf(fd, "\n\n");          fprintf(fd_key, "\n\n");
         fclose(fd);          fflush(fd_key);
         return (TRUE);          return (TRUE);
 }  }
   
Line 132 
Line 126 
 mglog_window(void)  mglog_window(void)
 {  {
         struct mgwin    *wp;          struct mgwin    *wp;
         struct stat      sb;  
         FILE            *fd;  
         int              i;          int              i;
   
         if(stat(mglogpath_window, &sb))  
                 return (FALSE);  
         fd = fopen(mglogpath_window, "a");  
   
         for (wp = wheadp, i = 0; wp != NULL; wp = wp->w_wndp, ++i) {          for (wp = wheadp, i = 0; wp != NULL; wp = wp->w_wndp, ++i) {
                 if (fprintf(fd,                  fprintf(fd_window,
                     "%d wh%p wlst%p wbfp%p wlp%p wdtp%p wmkp%p wdto%d wmko%d" \                      "%d wh%p wlst%p wbfp%p wlp%p wdtp%p wmkp%p wdto%d wmko%d" \
                     " wtpr%d wntr%d wfrm%d wrfl%c wflg%c wwrl%p wdtl%d" \                      " wtpr%d wntr%d wfrm%d wrfl%c wflg%c wwrl%p wdtl%d" \
                     " wmkl%d\n",                      " wmkl%d\n",
Line 161 
Line 149 
                     wp->w_flag,                      wp->w_flag,
                     wp->w_wrapline,                      wp->w_wrapline,
                     wp->w_dotline,                      wp->w_dotline,
                     wp->w_markline) == -1) {                      wp->w_markline
                         fclose(fd);                      );
                         return (FALSE);  
                 }  
         }          }
         fclose(fd);          fflush(fd_window);
         return (TRUE);          return (TRUE);
 }  }
   
Line 174 
Line 160 
 mglog_undo(void)  mglog_undo(void)
 {  {
         struct undo_rec *rec;          struct undo_rec *rec;
         struct stat      sb;  
         FILE            *fd;  
         char             buf[4096], tmp[1024];          char             buf[4096], tmp[1024];
         int              num;          int              num;
         char            *jptr;          char            *jptr;
   
         jptr = "^J"; /* :) */          jptr = "^J"; /* :) */
   
         if(stat(mglogpath_undo, &sb))  
                 return (FALSE);  
         fd = fopen(mglogpath_undo, "a");  
   
         /*          /*
          * From undo_dump()           * From undo_dump()
          */           */
         num = 0;          num = 0;
         TAILQ_FOREACH(rec, &curbp->b_undo, next) {          TAILQ_FOREACH(rec, &curbp->b_undo, next) {
                 num++;                  num++;
                 if (fprintf(fd, "%d:\t %s at %d ", num,                  fprintf(fd_undo, "%d:\t %s at %d ", num,
                     (rec->type == DELETE) ? "DELETE":                      (rec->type == DELETE) ? "DELETE":
                     (rec->type == DELREG) ? "DELREGION":                      (rec->type == DELREG) ? "DELREGION":
                     (rec->type == INSERT) ? "INSERT":                      (rec->type == INSERT) ? "INSERT":
                     (rec->type == BOUNDARY) ? "----" :                      (rec->type == BOUNDARY) ? "----" :
                     (rec->type == MODIFIED) ? "MODIFIED": "UNKNOWN",                      (rec->type == MODIFIED) ? "MODIFIED": "UNKNOWN",
                     rec->pos) == -1) {                      rec->pos
                         fclose(fd);                      );
                         return (FALSE);  
                 }  
                 if (rec->content) {                  if (rec->content) {
                         (void)strlcat(buf, "\"", sizeof(buf));                          (void)strlcat(buf, "\"", sizeof(buf));
                         snprintf(tmp, sizeof(tmp), "%.*s",                          snprintf(tmp, sizeof(tmp), "%.*s",
Line 216 
Line 193 
                         ewprintf("Undo record too large. Aborted.");                          ewprintf("Undo record too large. Aborted.");
                         return (FALSE);                          return (FALSE);
                 }                  }
                 if (fprintf(fd, "%s\n", buf) == -1) {                  fprintf(fd_undo, "%s\n", buf);
                         fclose(fd);  
                         return (FALSE);  
                 }  
                 tmp[0] = buf[0] = '\0';                  tmp[0] = buf[0] = '\0';
         }          }
         if (fprintf(fd, "\t [end-of-undo]\n\n") == -1) {          fprintf(fd_undo, "\t [end-of-undo]\n\n");
                 fclose(fd);          fflush(fd_undo);
                 return (FALSE);  
         }  
         fclose(fd);  
   
         return (TRUE);          return (TRUE);
 }  }
Line 235 
Line 206 
 mglog_lines(PF funct)  mglog_lines(PF funct)
 {  {
         struct line     *lp;          struct line     *lp;
         struct stat      sb;  
         char            *curline, *tmp, o;          char            *curline, *tmp, o;
         FILE            *fd;  
         int              i;          int              i;
   
         i = 0;          i = 0;
   
         if(stat(mglogpath_lines, &sb))          fprintf(fd_lines, "%s\n", function_name(funct));
                 return (FALSE);  
   
         fd = fopen(mglogpath_lines, "a");  
         if (fprintf(fd, "%s\n", function_name(funct)) == -1) {  
                 fclose(fd);  
                 return (FALSE);  
         }  
         lp = bfirstlp(curbp);          lp = bfirstlp(curbp);
   
         for(;;) {          for(;;) {
Line 269 
Line 231 
                         tmp = lp->l_text;                          tmp = lp->l_text;
   
                 /* segv on fprintf below with long lines */                  /* segv on fprintf below with long lines */
                 if (fprintf(fd, "%s%p b^%p f.%p %d %d\t%c|%s\n", curline,                  fprintf(fd_lines, "%s%p b^%p f.%p %d %d\t%c|%s\n", curline,
                     lp, lp->l_bp, lp->l_fp,                      lp, lp->l_bp, lp->l_fp,
                     lp->l_size, lp->l_used, o, tmp) == -1) {                      lp->l_size, lp->l_used, o, tmp);
                         fclose(fd);  
                         return (FALSE);  
                 }  
                 lp = lforw(lp);                  lp = lforw(lp);
                 if (lp == curbp->b_headp) {                  if (lp == curbp->b_headp) {
                         if (fprintf(fd, " %p b^%p f.%p [bhead]\n(EOB)\n",                          fprintf(fd_lines, " %p b^%p f.%p [bhead]\n(EOB)\n",
                             lp, lp->l_bp, lp->l_fp) == -1) {                              lp, lp->l_bp, lp->l_fp);
                                 fclose(fd);  
                                 return (FALSE);                          fprintf(fd_lines, "lines:raw:%d buf:%d wdot:%d\n\n",
                         }                              i, curbp->b_lines, curwp->w_dotline);
                         if (fprintf(fd, "lines:raw:%d buf:%d wdot:%d\n\n",  
                             i, curbp->b_lines, curwp->w_dotline) == -1) {  
                                 fclose(fd);  
                                 return (FALSE);  
                         }  
                         break;                          break;
                 }                  }
         }          }
         fclose(fd);          fflush(fd_lines);
   
         return (TRUE);          return (TRUE);
 }  }
Line 305 
Line 261 
         const int         sizof          const int         sizof
 )  )
 {  {
         FILE            *fd;  
   
         fd = fopen(mglogpath_interpreter, "a");          fprintf(fd_interpreter, " argbuf:%s,argp:%s,sizof:%d<\n",
   
         if (fprintf(fd, " argbuf:%s,argp:%s,sizof:%d<\n",  
             argbuf,              argbuf,
             argp,              argp,
             sizof              sizof);
             ) == -1) {  
                 fclose(fd);          fflush(fd_interpreter);
                 return (FALSE);  
         }  
         fclose(fd);  
         return (TRUE);          return (TRUE);
 }  }
   
Line 337 
Line 287 
         const char* const contbuf          const char* const contbuf
 )  )
 {  {
         FILE            *fd;          fprintf(fd_interpreter, "%sexcbuf:%s,argbuf:%s,argp:%s,last:%d,inlist:%d,"\
   
         fd = fopen(mglogpath_interpreter, "a");  
   
         if (fprintf(fd, "%sexcbuf:%s,argbuf:%s,argp:%s,last:%d,inlist:%d,"\  
             "cmdp:%s,p:%s,contbuf:%s<\n",              "cmdp:%s,p:%s,contbuf:%s<\n",
             pre,              pre,
             excbuf,              excbuf,
Line 352 
Line 298 
             cmdp,              cmdp,
             p,              p,
             contbuf              contbuf
             ) == -1) {              );
                 fclose(fd);          fflush(fd_interpreter);
           return (TRUE);
   }
   
   /*
    * Misc. logging for various subsystems
    */
   int
   mglog_misc(
           const char *fmt,
           ...
   )
   {
           va_list         ap;
           int             rc;
   
           va_start(ap, fmt);
           rc = vfprintf(fd_misc, fmt, ap);
           va_end(ap);
           fflush(fd_misc);
   
           if (rc < 0)
                 return (FALSE);                  return (FALSE);
         }  
         fclose(fd);  
         return (TRUE);          return (TRUE);
 }  }
   
   
   
 /*  /*
  * Make sure logging to log files can happen.   * Make sure logging to log files can happen.
  */   */
Line 369 
Line 337 
         struct stat      sb;          struct stat      sb;
         mode_t           dir_mode, f_mode, oumask;          mode_t           dir_mode, f_mode, oumask;
         char            *mglogfile_lines, *mglogfile_undo, *mglogfile_window;          char            *mglogfile_lines, *mglogfile_undo, *mglogfile_window;
         char            *mglogfile_key, *mglogfile_interpreter;          char            *mglogfile_key, *mglogfile_interpreter, *mglogfile_misc;
   
         mglogdir = "./log/";          mglogdir = "./log/";
         mglogfile_lines = "line.log";          mglogfile_lines = "line.log";
Line 377 
Line 345 
         mglogfile_window = "window.log";          mglogfile_window = "window.log";
         mglogfile_key = "key.log";          mglogfile_key = "key.log";
         mglogfile_interpreter = "interpreter.log";          mglogfile_interpreter = "interpreter.log";
           mglogfile_misc = "misc.log";
   
         /*          /*
          * Change mgloglevel for desired level of logging.           * Change mgloglevel for desired level of logging.
Line 394 
Line 363 
                 if (chmod(mglogdir, f_mode) == -1)                  if (chmod(mglogdir, f_mode) == -1)
                         return (FALSE);                          return (FALSE);
         }          }
         mglogpath_lines = mglogfiles_create(mglogfile_lines);          mglogpath_lines = mglogfiles_create(&fd_lines, mglogfile_lines);
         if (mglogpath_lines == NULL)          if (mglogpath_lines == NULL)
                 return (FALSE);                  return (FALSE);
         mglogpath_undo = mglogfiles_create(mglogfile_undo);          mglogpath_undo = mglogfiles_create(&fd_undo, mglogfile_undo);
         if (mglogpath_undo == NULL)          if (mglogpath_undo == NULL)
                 return (FALSE);                  return (FALSE);
         mglogpath_window = mglogfiles_create(mglogfile_window);          mglogpath_window = mglogfiles_create(&fd_window, mglogfile_window);
         if (mglogpath_window == NULL)          if (mglogpath_window == NULL)
                 return (FALSE);                  return (FALSE);
         mglogpath_key = mglogfiles_create(mglogfile_key);          mglogpath_key = mglogfiles_create(&fd_key, mglogfile_key);
         if (mglogpath_key == NULL)          if (mglogpath_key == NULL)
                 return (FALSE);                  return (FALSE);
         mglogpath_interpreter = mglogfiles_create(mglogfile_interpreter);          mglogpath_interpreter = mglogfiles_create(&fd_interpreter,
               mglogfile_interpreter);
         if (mglogpath_interpreter == NULL)          if (mglogpath_interpreter == NULL)
                 return (FALSE);                  return (FALSE);
           mglogpath_misc = mglogfiles_create(&fd_misc, mglogfile_misc);
           if (mglogpath_misc == NULL)
                   return (FALSE);
   
         return (TRUE);          return (TRUE);
 }  }
   
   
 static char *  static char *
 mglogfiles_create(char *mglogfile)  mglogfiles_create(FILE ** fd, char *mglogfile)
 {  {
         struct stat      sb;  
         char             tmp[NFILEN], *tmp2;          char             tmp[NFILEN], *tmp2;
         int              fd;  
   
         if (strlcpy(tmp, mglogdir, sizeof(tmp)) >          if (strlcpy(tmp, mglogdir, sizeof(tmp)) >
             sizeof(tmp))              sizeof(tmp))
Line 430 
Line 401 
         if ((tmp2 = strndup(tmp, NFILEN)) == NULL)          if ((tmp2 = strndup(tmp, NFILEN)) == NULL)
                 return (NULL);                  return (NULL);
   
         if(stat(tmp2, &sb))          if ((*fd = fopen(tmp2, "w")) == NULL)
                 fd = open(tmp2, O_RDWR | O_CREAT | O_TRUNC, 0644);  
         else  
                 fd = open(tmp2, O_RDWR | O_TRUNC, 0644);  
   
         if (fd == -1)  
                 return (NULL);                  return (NULL);
   
         close(fd);  
   
         return (tmp2);          return (tmp2);
 }  }
   
 /*  
  * Template log function.  
  */  
 /*  
 int  
 mglog_?(void)  
 {  
         struct stat      sb;  
         FILE            *fd;  
   
         if(stat(mglogpath_?, &sb))  
         fd = fopen(mglogpath_?, "a");  
   
         if (fprintf(fd, "%?", ??) == -1) {  
                 fclose(fd);  
                 return (FALSE);  
         }  
         fclose(fd);  
         return (TRUE);  
 }  
 */  

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12