Changeset 2297


Ignore:
Timestamp:
Mar 1, 2009, 3:04:38 AM (16 years ago)
Author:
bird
Message:

kash: removed the stdio based logging.

Location:
trunk/src/kash
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/shfile.c

    r2296 r2297  
    4141# include <fcntl.h>
    4242# include <dirent.h>
    43 #endif
    44 
    45 #if defined(DEBUG) && defined(TRACE_VIA_STDIO)
    46 extern FILE *tracefile;
    4743#endif
    4844
     
    611607#endif
    612608
    613 #if defined(DEBUG) && defined(TRACE_VIA_STDIO)
    614     if (tracefile)
    615 #endif
    616         TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
     609    TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
    617610    return fd;
    618611}
     
    999992#endif
    1000993
    1001 #ifdef DEBUG
    1002 # ifdef TRACE_VIA_STDIO
    1003     if (tracefile)
    1004 # endif
    1005         switch (cmd)
    1006         {
    1007             case F_GETFL:
    1008                 TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno));
    1009                 break;
    1010             case F_SETFL:
    1011                 TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno));
    1012                 break;
    1013             case F_DUPFD:
    1014                 TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno));
    1015                 break;
    1016             default:
    1017                 TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno));
    1018                 break;
    1019         }
    1020 #endif
     994    switch (cmd)
     995    {
     996        case F_GETFL: TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno));  break;
     997        case F_SETFL: TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno)); break;
     998        case F_DUPFD: TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
     999        default:  TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno)); break;
     1000    }
    10211001    return rc;
    10221002}
  • trunk/src/kash/show.c

    r2296 r2297  
    263263
    264264
     265#ifdef DEBUG
    265266/*
    266267 * Debugging stuff.
    267268 */
    268 
    269 #ifdef DEBUG
    270 #ifdef TRACE_VIA_STDIO
    271 FILE *tracefile;
    272 #endif
    273269
    274270/** @def TRY_GET_PSH_OR_RETURN
     
    284280/** @def RETURN_IF_NOT_TRACING
    285281 * Return if we're not tracing. */
    286 # ifdef TRACE_VIA_STDIO
    287 #  define RETURN_IF_NOT_TRACING(psh) \
    288         if (debug(psh) != 1 || !tracefile)
    289                 return; \
    290         else do {} while (0)
    291 # else
    292 #  define RETURN_IF_NOT_TRACING(psh) \
    293         if (debug(psh) != 1 || psh->tracefd == -1) \
    294                 return; \
    295         else do {} while (0)
    296 # endif
    297 
    298 /** @def TRACE_PUTC
    299  * putc/trace_char wrapper. The @a psh / @a tracefile
    300  * is taken from the context and not as a paramenter. */
    301 # ifdef TRACE_VIA_STDIO
    302 #  define TRACE_PUTC(c)         fputc(c, tracefile)
    303 # else
    304 #  define TRACE_PUTC(c)         trace_char(psh, c)
    305 # endif
    306 
    307 
    308 # ifndef TRACE_VIA_STDIO
     282# define RETURN_IF_NOT_TRACING(psh) \
     283   if (debug(psh) != 1 || psh->tracefd == -1) \
     284        return; \
     285   else do      {} while (0)
     286
    309287/* Flushes the tracebuf. */
    310288static void
     
    321299
    322300        if (pos) {
     301        int     s = errno;
    323302                char    prefix[40];
    324303                size_t  len;
     
    330309                psh->tracepos = 0;
    331310                psh->tracebuf[0] = '\0';
     311
     312        errno = s;
    332313        }
    333314}
     
    378359                } else {
    379360                        /* it's too big for some reason... */
     361            int s = errno;
    380362                        trace_flush(psh);
    381363                        shfile_write(&psh->fdtab, psh->tracefd, str, len);
    382364                        if (!flush_it)
    383365                                shfile_write(&psh->fdtab, psh->tracefd, "[too long]\n", sizeof( "[too long]\n") - 1);
     366            errno = s;
    384367                }
    385368
     
    388371        }
    389372}
    390 # endif
    391373
    392374void
     
    396378        RETURN_IF_NOT_TRACING(psh);
    397379
    398 # ifdef TRACE_VIA_STDIO
    399     putc(c, tracefile);
    400 # else
    401380        trace_char(psh, c);
    402 # endif
    403 }
    404 #endif
     381}
    405382
    406383void
    407384trace(shinstance *psh, const char *fmt, ...)
    408385{
    409 #ifdef DEBUG
    410         int savederrno = errno;
    411386        va_list va;
    412 # ifndef TRACE_VIA_STDIO
    413387        char buf[2048];
    414 # endif
    415388
    416389        TRY_GET_PSH_OR_RETURN(psh);
    417390        RETURN_IF_NOT_TRACING(psh);
    418391
    419 # ifdef TRACE_VIA_STDIO
    420         fprintf(tracefile, "[%d] ", sh_getpid(psh));
    421         va_start(va, fmt);
    422         (void) vfprintf(tracefile, fmt, va);
    423         va_end(va);
    424 # else
    425392        va_start(va, fmt);
    426393#  ifdef _MSC_VER
     
    431398        va_end(va);
    432399        trace_string(psh, buf);
    433 # endif
    434 
    435         errno = savederrno;
    436 #endif
    437400}
    438401
     
    440403tracev(shinstance *psh, const char *fmt, va_list va)
    441404{
    442 #ifdef DEBUG
    443         int savederrno = errno;
    444 # ifndef TRACE_VIA_STDIO
    445405        char buf[2048];
    446 # endif
    447406
    448407        TRY_GET_PSH_OR_RETURN(psh);
    449408        RETURN_IF_NOT_TRACING(psh);
    450409
    451 # ifdef TRACE_VIA_STDIO
    452         fprintf(tracefile, "[%d] ", sh_getpid(psh));
    453         (void) vfprintf(tracefile, fmt, va);
    454 # else
    455410#  ifdef _MSC_VER
    456411        _vsnprintf(buf, sizeof(buf), fmt, va);
     
    459414#  endif
    460415        trace_string(psh, buf);
    461 # endif
    462 
    463         errno = savederrno;
    464 #endif
    465 }
    466 
    467 
    468 #ifdef DEBUG
     416}
     417
    469418void
    470419trputs(shinstance *psh, const char *s)
    471420{
    472         int savederrno = errno;
    473 
    474421        TRY_GET_PSH_OR_RETURN(psh);
    475422        RETURN_IF_NOT_TRACING(psh);
    476423
    477 # ifdef TRACE_VIA_STDIO
    478         fputs(s, tracefile);
    479 # else
    480424        trace_string(psh, s);
    481 # endif
    482 
    483         errno = savederrno;
     425    trace_char(psh, '\n');
    484426}
    485427
     
    488430trstring(shinstance *psh, char *s)
    489431{
    490         int savederrno = errno;
    491432        char *p;
    492433        char c;
     
    495436        RETURN_IF_NOT_TRACING(psh);
    496437
    497         TRACE_PUTC('"');
     438        trace_char(psh, '"');
    498439        for (p = s ; *p ; p++) {
    499440                switch (*p) {
     
    508449                case CTLBACKQ:  c = 'q';  goto backslash;
    509450                case CTLBACKQ+CTLQUOTE:  c = 'Q';  goto backslash;
    510 backslash:        TRACE_PUTC('\\');
    511                         TRACE_PUTC(c);
     451backslash:        trace_char(psh, '\\');
     452                        trace_char(psh, c);
    512453                        break;
    513454                default:
    514455                        if (*p >= ' ' && *p <= '~')
    515                                 TRACE_PUTC(*p);
     456                                trace_char(psh, *p);
    516457                        else {
    517                                 TRACE_PUTC('\\');
    518                                 TRACE_PUTC(*p >> 6 & 03);
    519                                 TRACE_PUTC(*p >> 3 & 07);
    520                                 TRACE_PUTC(*p & 07);
     458                                trace_char(psh, '\\');
     459                                trace_char(psh, *p >> 6 & 03);
     460                                trace_char(psh, *p >> 3 & 07);
     461                                trace_char(psh, *p & 07);
    521462                        }
    522463                        break;
    523464                }
    524465        }
    525         TRACE_PUTC('"');
    526 
    527         errno = savederrno;
    528 }
    529 #endif
    530 
     466        trace_char(psh, '"');
     467}
    531468
    532469void
    533470trargs(shinstance *psh, char **ap)
    534471{
    535 #ifdef DEBUG
    536         int savederrno = errno;
    537 
    538472        TRY_GET_PSH_OR_RETURN(psh);
    539473        RETURN_IF_NOT_TRACING(psh);
     
    542476                trstring(psh, *ap++);
    543477                if (*ap)
    544                         TRACE_PUTC(' ');
     478                        trace_char(psh, ' ');
    545479                else
    546                         TRACE_PUTC('\n');
    547         }
    548 
    549         errno = savederrno;
    550 #endif
    551 }
    552 
    553 
    554 #ifdef DEBUG
     480                        trace_char(psh, '\n');
     481        }
     482}
     483
    555484void
    556485opentrace(shinstance *psh)
    557486{
    558487    static const char s[] = "./trace";
    559 # ifdef TRACE_VIA_STDIO
    560         int fd;
    561 # endif
    562488
    563489        TRY_GET_PSH_OR_RETURN(psh);
    564490        if (debug(psh) != 1) {
    565 # ifdef TRACE_VIA_STDIO
    566                 if (tracefile)
    567                         fflush(tracefile);
    568                 /* leave open because libedit might be using it */
    569 # else
     491        /* disabled */
    570492                if (psh->tracefd != -1) {
    571493                        trace_flush(psh);
     
    573495                        psh->tracefd = -1;
    574496                }
    575 # endif
    576497                return;
    577498        }
    578 
    579 # ifdef TRACE_VIA_STDIO
    580         if (tracefile) {
    581                 if (!freopen(s, "a", tracefile)) {
    582                         fprintf(stderr, "Can't re-open %s\n", s);
    583                         debug(psh) = 0;
    584                         return;
    585                 }
    586         } else {
    587                 fd = open(s, O_APPEND | O_RDWR | O_CREAT, 0600);
    588                 if (fd != -1) {
    589 #  if K_OS == K_OS_WINDOWS
    590             int fds[50];
    591             int i = 0;
    592             while (i < 50) {
    593                 fds[i] = _dup(fd);
    594                 if (fds[i] == -1 || fds[i] > 199)
    595                     break;
    596                 i++;
    597             }
    598             if (i > 0) {
    599                 close(fd);
    600                 fd = fds[--i];
    601             }
    602             while (i-- > 0)
    603                 close(fds[i]);
    604 #  else
    605             int fdTarget = 199;
    606             while (fdTarget > 10)
    607             {
    608                 int fd2 = shfile_fcntl(&psh->fdtab, fd, F_DUPFD, fdTarget);
    609                                 if (fd2 != -1) {
    610                                         close(fd);
    611                                         fd = fd2;
    612                                         break;
    613                                 }
    614                 fdTarget = (fdTarget + 1 / 2) - 1;
    615             }
    616 #  endif
    617                 }
    618                 if (fd == -1 || (tracefile = fdopen(fd, "a")) == NULL) {
    619                         fprintf(stderr, "Can't open %s\n", s);
    620                         debug(psh) = 0;
    621                         return;
    622                 }
    623         }
    624         setvbuf(tracefile, (char *)NULL, _IOLBF, 1024);
    625         fputs("\nTracing started.\n", tracefile);
    626 
    627 # else  /* !TRACE_VIA_STDIO */
    628         if (psh->tracefd != -1) {
    629                 return;
    630         }
     499    /* else: (re-)enabled */
     500
     501        if (psh->tracefd != -1)
     502        return;
     503
    631504        psh->tracefd = shfile_open(&psh->fdtab, s, O_APPEND | O_RDWR | O_CREAT, 0600);
    632505        if (psh->tracefd != -1) {
    633506                /* relocate it */
    634                 int fdTarget = 199;
    635                 while (fdTarget > 10)
     507                int want_fd = 199;
     508                while (want_fd > 10)
    636509                {
    637                         int fd2 = shfile_fcntl(&psh->fdtab, psh->tracefd, F_DUPFD, fdTarget);
     510                        int fd2 = shfile_fcntl(&psh->fdtab, psh->tracefd, F_DUPFD, want_fd);
    638511                        if (fd2 != -1) {
    639512                                shfile_close(&psh->fdtab, psh->tracefd);
     
    641514                                break;
    642515                        }
    643                         fdTarget = (fdTarget + 1 / 2) - 1;
     516                        want_fd = ((want_fd + 1) / 2) - 1;
    644517                }
    645518        }
     
    650523        }
    651524        trace_string(psh, "Tracing started.\n");
    652 
    653 # endif /* !TRACE_VIA_STDIO */
    654 }
     525}
     526
    655527#endif /* DEBUG */
     528
  • trunk/src/kash/show.h

    r1233 r2297  
    3939union node;
    4040void showtree(struct shinstance *, union node *);
     41#ifdef DEBUG
    4142void trace(struct shinstance *, const char *, ...);
    4243void tracev(struct shinstance *, const char *, va_list);
    4344void trargs(struct shinstance *, char **);
    44 #ifdef DEBUG
    4545void trputc(struct shinstance *, int);
    4646void trputs(struct shinstance *, const char *);
Note: See TracChangeset for help on using the changeset viewer.