Index: ossp-pkg/fsl/fsl.c RCS File: /v/ossp/cvs/ossp-pkg/fsl/fsl.c,v rcsdiff -q -kk '-r1.9' '-r1.10' -u '/v/ossp/cvs/ossp-pkg/fsl/fsl.c,v' 2>/dev/null --- fsl.c 2002/07/17 14:28:00 1.9 +++ fsl.c 2002/07/17 15:26:36 1.10 @@ -139,36 +139,13 @@ /* internal context structure */ static struct { - FILE *log; - char *logfile; - const char *ident; - int logopt; - int facility; - int maskpri; + l2_env_t *l2_env; + l2_channel_t *l2_nch; } ctx = { - NULL, LOGFILE, "unknown", 0, LOG_USER, 0xff + NULL, + NULL, }; -void FIXMEopenlog(const char *ident, int logopt, int facility) -{ - /* remember parameters */ - ctx.ident = ident; - ctx.logopt = logopt; - ctx.facility = facility; - - /* close perhaps still open logfile */ - if (ctx.log != NULL) { - fclose(ctx.log); - ctx.log = NULL; - } - - /* (re-)open new logfile */ - if (!(ctx.logopt & LOG_NDELAY) && ctx.log == NULL) - if ((ctx.log = fopen(ctx.logfile, "a")) == NULL) - return; - return; -} - void traverse(cfg_t *cfg, cfg_node_t *cfgnode) { int rc; @@ -279,8 +256,6 @@ #define OVECSIZE 30 //FIXME find a good place for this int ovec[OVECSIZE]; const char **acpMatch; - l2_env_t *l2_env; - l2_channel_t *l2_nch; /* noop channel */ l2_channel_t *ch; /* scratch variable */ l2_result_t l2rv; @@ -290,28 +265,42 @@ buf.size = 0; cpIdent = NULL; cpISF = NULL; - l2_env = NULL; - l2_nch = NULL; -fprintf(stderr, "DEBUG: BEGIN ident=\"%s\", logopt=0x%.8lx, facility=0x%.8lx\n", ident, (unsigned long)logopt, (unsigned long)facility); + /*FIXME this should be available to the user to help him finding out what the application passes along + * fprintf(stderr, "DEBUG: BEGIN ident=\"%s\", logopt=0x%.8lx, facility=0x%.8lx\n", ident, (unsigned long)logopt, (unsigned long)facility); + */ + + /* check for repeated execution */ + if (ctx.l2_nch != NULL) { + l2_channel_destroy(ctx.l2_nch); + ctx.l2_nch = NULL; + } + if (ctx.l2_env != NULL) { + l2_env_destroy(ctx.l2_env); + ctx.l2_env = NULL; + } + + /*FIXME this should be available to the user to help him finding out what the application passes along + * currently we completely ignore logopt LOG_CONS, LOG_NDELAY, LOG_PERROR, LOG_PID + */ /* create L2 environment */ - if ((l2rv = l2_env_create(&l2_env)) != L2_OK) { - cp = l2_env_strerror(l2_env, l2rv); fprintf(stderr, "Error: failed to create L2 environment (%d)\n", l2rv); CU(1); } - if ((l2rv = l2_env_levels(l2_env, L2_LEVEL_ALL, L2_LEVEL_NONE)) != L2_OK) { - cp = l2_env_strerror(l2_env, l2rv); fprintf(stderr, "Error: logging failed to set global logging level defaults %s(%d)\n", cp, l2rv); CU(1); } - if ((l2rv = l2_env_formatter(l2_env, 'D', l2_util_fmt_dump, NULL)) != L2_OK) { - cp = l2_env_strerror(l2_env, l2rv); fprintf(stderr, "Error: logging failed to register dump formatter %s(%d)\n", cp, l2rv); CU(1); } - if ((l2rv = l2_env_formatter(l2_env, 'S', l2_util_fmt_string, NULL)) != L2_OK) { - cp = l2_env_strerror(l2_env, l2rv); fprintf(stderr, "Error: logging failed to register string formatter %s(%d)\n", cp, l2rv); CU(1); } - if ((l2rv = l2_env_formatter(l2_env, 'm', l2_util_fmt_errno, NULL)) != L2_OK) { - cp = l2_env_strerror(l2_env, l2rv); fprintf(stderr, "Error: logging failed to register errno formatter %s(%d)\n", cp, l2rv); CU(1); } - if ((l2rv = l2_env_settimer(l2_env, 20)) != L2_OK) { //FIXME do we need this? - cp = l2_env_strerror(l2_env, l2rv); fprintf(stderr, "Error: failed to configure timer %s(%d)\n", cp, l2rv); CU(1); } + if ((l2rv = l2_env_create(&ctx.l2_env)) != L2_OK) { + cp = l2_env_strerror(ctx.l2_env, l2rv); fprintf(stderr, "Error: failed to create L2 environment (%d)\n", l2rv); CU(1); } + if ((l2rv = l2_env_levels(ctx.l2_env, L2_LEVEL_ALL, L2_LEVEL_NONE)) != L2_OK) { + cp = l2_env_strerror(ctx.l2_env, l2rv); fprintf(stderr, "Error: logging failed to set global logging level defaults %s(%d)\n", cp, l2rv); CU(1); } + if ((l2rv = l2_env_formatter(ctx.l2_env, 'D', l2_util_fmt_dump, NULL)) != L2_OK) { + cp = l2_env_strerror(ctx.l2_env, l2rv); fprintf(stderr, "Error: logging failed to register dump formatter %s(%d)\n", cp, l2rv); CU(1); } + if ((l2rv = l2_env_formatter(ctx.l2_env, 'S', l2_util_fmt_string, NULL)) != L2_OK) { + cp = l2_env_strerror(ctx.l2_env, l2rv); fprintf(stderr, "Error: logging failed to register string formatter %s(%d)\n", cp, l2rv); CU(1); } + if ((l2rv = l2_env_formatter(ctx.l2_env, 'm', l2_util_fmt_errno, NULL)) != L2_OK) { + cp = l2_env_strerror(ctx.l2_env, l2rv); fprintf(stderr, "Error: logging failed to register errno formatter %s(%d)\n", cp, l2rv); CU(1); } + if ((l2rv = l2_env_settimer(ctx.l2_env, 20)) != L2_OK) { //FIXME do we need this? + cp = l2_env_strerror(ctx.l2_env, l2rv); fprintf(stderr, "Error: failed to configure timer %s(%d)\n", cp, l2rv); CU(1); } /* create L2 noop channel */ - if ((l2rv = l2_channel_create(&l2_nch, l2_env, "noop")) != L2_OK) { - cp = l2_env_strerror(l2_env, l2rv); fprintf(stderr, "Error: logging failed to create noop channel; %s(%d)\n", cp, l2rv); CU(1); } + if ((l2rv = l2_channel_create(&ctx.l2_nch, ctx.l2_env, "noop")) != L2_OK) { + cp = l2_env_strerror(ctx.l2_env, l2rv); fprintf(stderr, "Error: logging failed to create noop channel; %s(%d)\n", cp, l2rv); CU(1); } /* create IdentSlashFacility */ if ((cpIdent = strdup((ident != NULL) ? ident : "unknown")) == NULL) { @@ -443,10 +432,10 @@ fprintf(stderr, "DEBUG: argident=%s, argmatch=%s, argl2spec=%s\n", argident, argmatch, argl2spec); /* create L2 channel throuh spec and link into root channel */ - if ((l2rv = l2_spec(&ch, l2_env, argl2spec)) != L2_OK) { - cp = l2_env_strerror(l2_env, l2rv); fprintf(stderr, "Error: logging failed to create stream from spec %s(%d)\n", cp, l2rv); CU(1); } - if ((l2rv = l2_channel_link(l2_nch, L2_LINK_CHILD, ch, NULL)) != L2_OK) { - cp = l2_env_strerror(l2_env, l2rv); fprintf(stderr, "Error: logging failed to link child channel %s(%d)\n", cp, l2rv); CU(1); } + if ((l2rv = l2_spec(&ch, ctx.l2_env, argl2spec)) != L2_OK) { + cp = l2_env_strerror(ctx.l2_env, l2rv); fprintf(stderr, "Error: logging failed to create stream from spec %s(%d)\n", cp, l2rv); CU(1); } + if ((l2rv = l2_channel_link(ctx.l2_nch, L2_LINK_CHILD, ch, NULL)) != L2_OK) { + cp = l2_env_strerror(ctx.l2_env, l2rv); fprintf(stderr, "Error: logging failed to link child channel %s(%d)\n", cp, l2rv); CU(1); } free(argl2spec); } @@ -456,42 +445,41 @@ if ((cfgrv = cfg_node_get(cfg, cfgdir, CFG_NODE_ATTR_RBROTH, &cfgdir)) != CFG_OK) { (void)cfg_error(cfg, cfgrv, &cp); fprintf(stderr, "Error: cfg_node_get(CFG_NODE_ATTR_RBROTH) failed with error %s (%d)\n", cp, cfgrv); CU(1); } } - if ((l2rv = l2_channel_open(l2_nch)) != L2_OK) { - cp = l2_env_strerror(l2_env, l2rv); fprintf(stderr, "Error: logging failed to open channel stream %s(%d)\n", cp, l2rv); CU(1); } - l2_channel_log(l2_nch, L2_LEVEL_DEBUG, "Hello, World!"); //FIXME DEBUG + if ((l2rv = l2_channel_open(ctx.l2_nch)) != L2_OK) { + cp = l2_env_strerror(ctx.l2_env, l2rv); fprintf(stderr, "Error: logging failed to open channel stream %s(%d)\n", cp, l2rv); CU(1); } -fprintf(stderr, "DEBUG: *END*, buf.base=0x%.8lx, buf.used=%d, buf.size=%d\n", (unsigned long)buf.base, (int)buf.used, (int)buf.size); CU(0); - FIXMEopenlog(ident, logopt, facility); CUS: if (cpISF != NULL) free(cpISF); if (cpIdent != NULL) free(cpIdent); - if (l2_nch != NULL) - l2_channel_destroy(l2_nch); - if (l2_env != NULL) - l2_env_destroy(l2_env); #if 0 if (cfg != NULL) if ((cfgrv = cfg_destroy(cfg)) != CFG_OK) { (void)cfg_error(cfg, cfgrv, &cp); fprintf(stderr, "Error: cfg_destroy() failed with error %s (%d)\n", cp, cfgrv); CU(1); } #endif + if (buf.base != NULL) + free(buf.base); return; } void closelog(void) { - /* close open logfile*/ - if (ctx.log != NULL) { - fclose(ctx.log); - ctx.log = NULL; + if (ctx.l2_nch != NULL) { + l2_channel_destroy(ctx.l2_nch); + ctx.l2_nch = NULL; + } + if (ctx.l2_env != NULL) { + l2_env_destroy(ctx.l2_env); + ctx.l2_env = NULL; } return; } int setlogmask(int maskpri) { +#if 0 int omask; /* remember the logging mask */ @@ -499,6 +487,8 @@ if (maskpri != 0) ctx.maskpri = maskpri; return omask; +#endif + return maskpri; //FIXME } void syslog(int priority, const char *message, ...) @@ -518,103 +508,10 @@ void vsyslog(int priority, const char *fmt, va_list args) #endif { - time_t now; - int saved_errno; - char buf[2048]; - char *cpBuf; - int nBuf; - char caMsg[2048]; - char *cpMsg; - char *cpFmt; - int i; - char *cpTime; - char *cpLevel; - - /* Check for invalid bits */ - if (priority & ~(LOG_PRIMASK|LOG_FACMASK)) - priority &= (LOG_PRIMASK|LOG_FACMASK); - - /* stop processing if mask disabled this call */ - if (!(LOG_MASK(LOG_PRI(priority)) & ctx.maskpri)) + if (ctx.l2_nch == NULL) return; - /* make sure we have a reasonable default for facility */ - if ((priority & LOG_FACMASK) == 0) - priority |= ctx.facility; - - /* remember errno for optional %m processing below */ - saved_errno = errno; - - /* open logfile now (if it was delayed) */ - if (ctx.log == NULL) - if ((ctx.log = fopen(ctx.logfile, "a")) == NULL) - return; - - /* determine current time */ - time(&now); - cpTime = ctime(&now) + 4; - - /* determine logging level name */ - cpLevel = "unknown"; - for (i = 0; level2string[i].string != NULL; i++) { - if (LOG_PRI(priority) == level2string[i].level) { - cpLevel = level2string[i].string; - break; - } - } - - /* start output generation */ - cpBuf = buf; - sprintf(cpBuf, "%.15s <%s> %s", cpTime, cpLevel, ctx.ident); - nBuf = strlen(cpBuf); - - /* optionally add process id */ - if (ctx.logopt & LOG_PID) { - sprintf(cpBuf+nBuf, "[%d]", getpid()); - nBuf += strlen(cpBuf+nBuf); - } - - /* end prefix */ - strcpy(cpBuf+nBuf, ": "); - nBuf += 2; - - /* optionally expand %m in format string */ - cpFmt = (char *)fmt; - if (strstr(cpFmt, "%m")) { - cpMsg = caMsg; - while (cpFmt[0] != '\0') { - if (cpFmt[0] == '%' && cpFmt[1] == 'm') { - strcpy(cpMsg, strerror(saved_errno)); - cpMsg += strlen(cpMsg); - cpFmt += 2; - } else { - *cpMsg++ = *cpFmt++; - *cpMsg = '\0'; - } - } - cpFmt = caMsg; - } - - /* append message to output buffer */ - vsprintf(cpBuf+nBuf, cpFmt, args); - nBuf += strlen(cpBuf+nBuf); - - /* append newline if still not present */ - if (cpBuf[nBuf-1] != '\n') { - cpBuf[nBuf++] = '\n'; - cpBuf[nBuf] = '\0'; - } - - /* now finally write output buffer to logfile */ - fwrite(cpBuf, nBuf, 1, ctx.log); - fflush(ctx.log); - -#ifdef LOG_PERROR - /* optionally also write output buffer to stderr */ - if (ctx.logopt & LOG_PERROR) - fwrite(cpBuf, nBuf, 1, stderr); -#endif - + l2_channel_vlog(ctx.l2_nch, L2_LEVEL_DEBUG, fmt, args); return; }