Annotation of 43BSDReno/contrib/isode-beta/compat/logger.c, revision 1.1.1.1

1.1       root        1: /* logger.c - system logging routines */
                      2: 
                      3: #ifndef        lint
                      4: static char *rcsid = "$Header: /f/osi/compat/RCS/logger.c,v 7.2 90/04/23 00:08:12 mrose Exp $";
                      5: #endif
                      6: 
                      7: /* 
                      8:  * $Header: /f/osi/compat/RCS/logger.c,v 7.2 90/04/23 00:08:12 mrose Exp $
                      9:  *
                     10:  *
                     11:  * $Log:       logger.c,v $
                     12:  * Revision 7.2  90/04/23  00:08:12  mrose
                     13:  * touch-up
                     14:  * 
                     15:  * Revision 7.1  90/02/19  13:07:17  mrose
                     16:  * update
                     17:  * 
                     18:  * Revision 7.0  89/11/23  21:23:17  mrose
                     19:  * Release 6.0
                     20:  * 
                     21:  */
                     22: 
                     23: /*
                     24:  *                               NOTICE
                     25:  *
                     26:  *    Acquisition, use, and distribution of this module and related
                     27:  *    materials are subject to the restrictions of a license agreement.
                     28:  *    Consult the Preface in the User's Manual for the full terms of
                     29:  *    this agreement.
                     30:  *
                     31:  */
                     32: 
                     33: 
                     34: /* LINTLIBRARY */
                     35: 
                     36: #include <stdio.h>
                     37: #include <varargs.h>
                     38: #include "general.h"
                     39: #include "manifest.h"
                     40: #include "logger.h"
                     41: #include "tailor.h"
                     42: 
                     43: #ifdef NULL
                     44: #undef NULL
                     45: #endif
                     46: #include <sys/param.h>
                     47: #ifndef        NULL
                     48: #define        NULL    0
                     49: #endif
                     50: #ifdef BSD42
                     51: #include <sys/file.h>
                     52: #endif
                     53: #ifdef SYS5
                     54: #include <fcntl.h>
                     55: #endif
                     56: #include <sys/stat.h>
                     57: 
                     58: #ifndef        BSD42
                     59: #include <time.h>
                     60: #else
                     61: #ifndef        timerisset
                     62: #include <sys/time.h>
                     63: #endif
                     64: #endif
                     65: 
                     66: #ifndef        SYS5
                     67: #include <syslog.h>
                     68: #endif
                     69: 
                     70: /*  */
                     71: 
                     72: long   lseek (), time ();
                     73: 
                     74: /*  */
                     75: 
                     76: int    ll_open (lp)
                     77: register LLog *lp;
                     78: {
                     79:     int            mask,
                     80:            mode;
                     81:     char   *bp,
                     82:            buffer[BUFSIZ];
                     83:     struct stat st;
                     84: 
                     85:     if (lp -> ll_file == NULLCP
                     86:            || *lp -> ll_file == NULL) {
                     87:        (void) ll_close (lp);
                     88:        lp -> ll_stat |= LLOGERR;
                     89:        return NOTOK;
                     90:     }
                     91: 
                     92:     lp -> ll_stat &= ~LLOGERR;
                     93: 
                     94:     if (lp -> ll_fd != NOTOK)
                     95:        return OK;
                     96: 
                     97:     if (strcmp (lp -> ll_file, "-") == 0) {
                     98:        lp -> ll_stat |= LLOGTTY;
                     99:        return OK;
                    100:     }
                    101: 
                    102:     (void) sprintf (bp = buffer, _isodefile (isodelogpath, lp -> ll_file),
                    103:                    getpid ());
                    104: 
                    105:     mode = O_WRONLY | O_APPEND;
                    106:     if (stat (bp, &st) == NOTOK && (lp -> ll_stat & LLOGCRT))
                    107:        mode |= O_CREAT;
                    108: 
                    109:     mask = umask (~0666);
                    110:     lp -> ll_fd = open (bp, mode, 0666);
                    111:     (void) umask (mask);
                    112: 
                    113:     if (ll_check (lp) == NOTOK)
                    114:        return (NOTOK);
                    115: 
                    116:     return (lp -> ll_fd != NOTOK ? OK : NOTOK);
                    117: }
                    118: 
                    119: /*  */
                    120: 
                    121: int    ll_close (lp)
                    122: register LLog *lp;
                    123: {
                    124:     int            status;
                    125:     
                    126:     if (lp -> ll_fd == NOTOK)
                    127:        return OK;
                    128: 
                    129:     status = close (lp -> ll_fd);
                    130:     lp -> ll_fd = NOTOK;
                    131: 
                    132:     return status;
                    133: }
                    134: 
                    135: /*  */
                    136: 
                    137: #ifndef        lint
                    138: int    ll_log (va_alist)
                    139: va_dcl
                    140: {
                    141:     int            event,
                    142:            result;
                    143:     LLog   *lp;
                    144:     va_list ap;
                    145: 
                    146:     va_start (ap);
                    147: 
                    148:     lp = va_arg (ap, LLog *);
                    149:     event = va_arg (ap, int);
                    150: 
                    151:     result = _ll_log (lp, event, ap);
                    152: 
                    153:     va_end (ap);
                    154: 
                    155:     return result;
                    156: }
                    157: #else
                    158: /* VARARGS4 */
                    159: 
                    160: int    ll_log (lp, event, what, fmt)
                    161: LLog   *lp;
                    162: int    event;
                    163: char   *what,
                    164:        *fmt;
                    165: {
                    166:     return ll_log (lp, event, what, fmt);
                    167: }
                    168: #endif
                    169: 
                    170: /*  */
                    171: 
                    172: int    _ll_log (lp, event, ap) /* what, fmt, args ... */
                    173: register LLog *lp;
                    174: int    event;
                    175: va_list        ap;
                    176: {
                    177:     int            cc,
                    178:            status;
                    179:     long    clock;
                    180:     register char *bp;
                    181:     char   *what,
                    182:            buffer[BUFSIZ];
                    183:     register struct tm *tm;
                    184: 
                    185:     if (!(lp -> ll_events & event))
                    186:        return OK;
                    187: 
                    188:     bp = buffer;
                    189: 
                    190:     (void) time (&clock);
                    191:     tm = localtime (&clock);
                    192:     (void) sprintf (bp, "%2d/%2d %2d:%02d:%02d %s %s ",
                    193:                    tm -> tm_mon + 1, tm -> tm_mday,
                    194:                    tm -> tm_hour, tm -> tm_min, tm -> tm_sec,
                    195:                    lp -> ll_hdr ? lp -> ll_hdr : "",
                    196:                    lp -> ll_dhdr ? lp -> ll_dhdr : "");
                    197:     bp += strlen (bp);
                    198: 
                    199:     what = va_arg (ap, char *);
                    200: 
                    201:     _asprintf (bp, what, ap);
                    202: 
                    203: #ifndef        SYS5
                    204:     if (lp -> ll_syslog & event) {
                    205:        int     priority;
                    206: 
                    207:        switch (event) {
                    208:            case LLOG_FATAL:
                    209:                priority = LOG_ERR;
                    210:                break;
                    211: 
                    212:            case LLOG_EXCEPTIONS:
                    213:                priority = LOG_WARNING;
                    214:                break;
                    215: 
                    216:            case LLOG_NOTICE:
                    217:                priority = LOG_INFO;
                    218:                break;
                    219: 
                    220:            case LLOG_PDUS:
                    221:            case LLOG_TRACE:
                    222:            case LLOG_DEBUG:
                    223:                priority = LOG_DEBUG;
                    224:                break;
                    225: 
                    226:            default:
                    227:                priority = LOG_NOTICE;
                    228:                break;
                    229:        }
                    230: 
                    231:        (void) syslog (priority, "%s", buffer + 13);
                    232: 
                    233:        if (lp -> ll_stat & LLOGCLS)
                    234:            (void) closelog ();
                    235:     }
                    236: #endif
                    237: 
                    238:     if (!(lp -> ll_stat & LLOGTTY)
                    239:            && lp -> ll_fd == NOTOK
                    240:            && strcmp (lp -> ll_file, "-") == 0)
                    241:        lp -> ll_stat |= LLOGTTY;
                    242: 
                    243:     if (lp -> ll_stat & LLOGTTY) {
                    244:        (void) fflush (stdout);
                    245: 
                    246:        if (lp -> ll_fd != NOTOK)
                    247:            (void) fprintf (stderr, "LOGGING: ");
                    248:        (void) fputs (bp, stderr);
                    249:        (void) fputc ('\n', stderr);
                    250:        (void) fflush (stderr);
                    251:     }
                    252:     bp += strlen (bp);
                    253: 
                    254:     if (lp -> ll_fd == NOTOK) {
                    255:        if ((lp -> ll_stat & (LLOGERR | LLOGTTY)) == (LLOGERR | LLOGTTY))
                    256:            return OK;
                    257:        if (ll_open (lp) == NOTOK)
                    258:            return NOTOK;
                    259:     }
                    260:     else
                    261:        if (ll_check (lp) == NOTOK)
                    262:            return NOTOK;
                    263: 
                    264:     *bp++ = '\n', *bp = NULL;
                    265:     cc = bp - buffer;
                    266: 
                    267:     if ((status = write (lp -> ll_fd, buffer, cc)) != cc) {
                    268:        if (status == NOTOK) {
                    269:            (void) ll_close (lp);
                    270: error: ;
                    271:            lp -> ll_stat |= LLOGERR;
                    272:            return NOTOK;
                    273:        }
                    274: 
                    275:        status = NOTOK;
                    276:     }
                    277:     else
                    278:        status = OK;
                    279: 
                    280:     if ((lp -> ll_stat & LLOGCLS) && ll_close (lp) == NOTOK)
                    281:        goto error;
                    282: 
                    283:     return status;
                    284: }
                    285: 
                    286: /*  */
                    287: 
                    288: void   ll_hdinit (lp, prefix)
                    289: register LLog *lp;
                    290: char   *prefix;
                    291: {
                    292:     register char  *cp,
                    293:                   *up;
                    294:     char    buffer[BUFSIZ],
                    295:            user[10];
                    296: 
                    297:     if (prefix == NULLCP) {
                    298:        if ((lp -> ll_stat & LLOGHDR) && strlen (lp -> ll_hdr) == 25)
                    299:            (cp = lp -> ll_hdr)[8] = NULL;
                    300:        else
                    301:            cp = "unknown";
                    302:     }
                    303:     else {
                    304:        if ((cp = rindex (prefix, '/')))
                    305:            cp++;
                    306:        if (cp == NULL || *cp == NULL)
                    307:            cp = prefix;
                    308:     }
                    309: 
                    310:     if ((up = getenv ("USER")) == NULLCP
                    311:            && (up = getenv ("LOGNAME")) == NULLCP) {
                    312:        (void) sprintf (user, "#%d", getuid ());
                    313:        up = user;
                    314:     }
                    315:     (void) sprintf (buffer, "%-8.8s %05d (%-8.8s)",
                    316:                    cp, getpid () % 100000, up);
                    317: 
                    318:     if (lp -> ll_stat & LLOGHDR)
                    319:        free (lp -> ll_hdr);
                    320:     lp -> ll_stat &= ~LLOGHDR;
                    321: 
                    322:     if ((lp -> ll_hdr = malloc ((unsigned) (strlen (buffer) + 1))) == NULLCP)
                    323:        return;
                    324: 
                    325:     (void) strcpy (lp -> ll_hdr, buffer);
                    326:     lp -> ll_stat |= LLOGHDR;
                    327: }
                    328: 
                    329: /*  */
                    330: 
                    331: void   ll_dbinit (lp, prefix)
                    332: register LLog *lp;
                    333: char   *prefix;
                    334: {
                    335:     register char  *cp;
                    336:     char    buffer[BUFSIZ];
                    337: 
                    338:     ll_hdinit (lp, prefix);
                    339: 
                    340:     if (prefix) {
                    341:        if ((cp = rindex (prefix, '/')))
                    342:            cp++;
                    343:        if (cp == NULL || *cp == NULL)
                    344:            cp = prefix;
                    345: 
                    346:        (void) sprintf (buffer, "./%s.log", cp);
                    347: 
                    348:        if ((lp -> ll_file = malloc ((unsigned) (strlen (buffer) + 1)))
                    349:                == NULLCP)
                    350:            return;
                    351: 
                    352:        (void) strcpy (lp -> ll_file, buffer);
                    353:     }
                    354: 
                    355:     lp -> ll_events |= LLOG_ALL;
                    356:     lp -> ll_stat |= LLOGTTY;
                    357: }
                    358: 
                    359: /*  */
                    360: 
                    361: #ifndef        lint
                    362: int    ll_printf (va_alist)
                    363: va_dcl
                    364: {
                    365:     int            result;
                    366:     LLog    *lp;
                    367:     va_list ap;
                    368: 
                    369:     va_start (ap);
                    370: 
                    371:     lp = va_arg (ap, LLog *);
                    372: 
                    373:     result = _ll_printf (lp, ap);
                    374: 
                    375:     va_end (ap);
                    376: 
                    377:     return result;
                    378: }
                    379: #else
                    380: /* VARARGS2 */
                    381: 
                    382: int    ll_printf (lp, fmt)
                    383: LLog   *lp;
                    384: char   *fmt;
                    385: {
                    386:     return ll_printf (lp, fmt);
                    387: }
                    388: #endif
                    389: 
                    390: /*  */
                    391: 
                    392: #ifndef        lint
                    393: static
                    394: #endif
                    395: int  _ll_printf (lp, ap)               /* fmt, args ... */
                    396: register LLog *lp;
                    397: va_list        ap;
                    398: {
                    399:     int            cc,
                    400:            status;
                    401:     register char   *bp;
                    402:     char     buffer[BUFSIZ];
                    403:     char    *fmt;
                    404:     va_list fp = ap;
                    405: 
                    406:     fmt = va_arg (fp, char *);
                    407:     if (strcmp (fmt, "%s") != 0) {
                    408:        bp = buffer;
                    409:        _asprintf (bp, NULLCP, ap);
                    410:     }
                    411:     else {
                    412:        bp = NULL;
                    413:        fmt = va_arg (fp, char *);
                    414:     }
                    415: 
                    416:     if (!(lp -> ll_stat & LLOGTTY)
                    417:            && lp -> ll_fd == NOTOK
                    418:            && strcmp (lp -> ll_file, "-") == 0)
                    419:        lp -> ll_stat |= LLOGTTY;
                    420: 
                    421:     if (lp -> ll_stat & LLOGTTY) {
                    422:        (void) fflush (stdout);
                    423: 
                    424:        if (bp)
                    425:            (void) fputs (bp, stderr);
                    426:        else
                    427:            (void) fputs (fmt, stderr);
                    428:        (void) fflush (stderr);
                    429:     }
                    430:     if (bp)
                    431:        bp += strlen (bp);
                    432: 
                    433:     if (lp -> ll_fd == NOTOK) {
                    434:        if ((lp -> ll_stat & (LLOGERR | LLOGTTY)) == (LLOGERR | LLOGTTY))
                    435:            return OK;
                    436:        if (ll_open (lp) == NOTOK)
                    437:            return NOTOK;
                    438:     }
                    439:     else
                    440:        if (ll_check (lp) == NOTOK)
                    441:            return NOTOK;
                    442: 
                    443:     if (bp)
                    444:        cc = bp - buffer;
                    445:     else
                    446:        cc = strlen (fmt);
                    447: 
                    448:     if ((status = write (lp -> ll_fd, bp ? buffer : fmt, cc)) != cc) {
                    449:        if (status == NOTOK) {
                    450:            (void) ll_close (lp);
                    451:            lp -> ll_stat |= LLOGERR;
                    452:            return NOTOK;
                    453:        }
                    454: 
                    455:        status = NOTOK;
                    456:     }
                    457:     else
                    458:        status = OK;
                    459: 
                    460:     return status;
                    461: }
                    462: 
                    463: /*  */
                    464: 
                    465: int    ll_sync (lp)
                    466: register LLog *lp;
                    467: {
                    468:     if (lp -> ll_stat & LLOGCLS)
                    469:        return ll_close (lp);
                    470: 
                    471:     return OK;
                    472: }
                    473: 
                    474: /*  */
                    475: 
                    476: #ifndef        lint
                    477: char   *ll_preset (va_alist)
                    478: va_dcl
                    479: {
                    480:     va_list ap;
                    481:     static char buffer[BUFSIZ];
                    482: 
                    483:     va_start (ap);
                    484: 
                    485:     _asprintf (buffer, NULLCP, ap);
                    486: 
                    487:     va_end (ap);
                    488: 
                    489:     return buffer;
                    490: }
                    491: #else
                    492: /* VARARGS1 */
                    493: 
                    494: char   *ll_preset (fmt)
                    495: char   *fmt;
                    496: {
                    497:     return ll_preset (fmt);
                    498: }
                    499: #endif
                    500: 
                    501: /*  */
                    502: 
                    503: int    ll_check (lp)
                    504: register LLog *lp;
                    505: {
                    506: #ifndef        BSD42
                    507:     int            fd;
                    508: #endif
                    509:     long    size;
                    510:     struct stat st;
                    511: 
                    512:     if ((size = lp -> ll_msize) <= 0)
                    513:        return OK;
                    514: 
                    515:     if (lp -> ll_fd == NOTOK
                    516:            || (fstat (lp -> ll_fd, &st) != NOTOK
                    517:                    && st.st_size < (size <<= 10)))
                    518:        return OK;
                    519: 
                    520:     if (!(lp -> ll_stat & LLOGZER)) {
                    521:        (void) ll_close (lp);
                    522: 
                    523: #ifndef        BSD42
                    524: error: ;
                    525: #endif
                    526:        lp -> ll_stat |= LLOGERR;
                    527:        return NOTOK;
                    528:     }
                    529: 
                    530: #ifdef BSD42
                    531: #ifdef SUNOS4
                    532:     (void) ftruncate (lp -> ll_fd, (off_t) 0);
                    533: #else
                    534:     (void) ftruncate (lp -> ll_fd, 0);
                    535: #endif
                    536:     (void) lseek (lp -> ll_fd, 0L, 0);
                    537:     return OK;
                    538: #else
                    539:     if ((fd = open (lp -> ll_file, O_WRONLY | O_APPEND | O_TRUNC)) == NOTOK)
                    540:        goto error;
                    541:     (void) close (fd);
                    542:     return OK;
                    543: #endif
                    544: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.