void MprLogToFile::writeTimeStamp() { time_t now; char timeText[80]; char *cp; now = time(0); mprCtime(&now, timeText, sizeof(timeText)); if ((cp = strchr(timeText, '\n')) != 0) { *cp = '\0'; } mprLog(MPR_CONFIG, "[%s]\n", timeText); }
/* * Start error and information logging. Note: this is not per-request access logging */ int maStartLogging(MprCtx ctx, cchar *logSpec) { Mpr *mpr; MprFile *file; MprTime now; char *levelSpec, *spec, timeText[80]; int level; level = 0; mpr = mprGetMpr(ctx); if (logSpec == 0) { logSpec = "stdout:2"; } if (*logSpec && strcmp(logSpec, "none") != 0) { spec = mprStrdup(mpr, logSpec); if ((levelSpec = strrchr(spec, ':')) != 0 && isdigit((int) levelSpec[1])) { *levelSpec++ = '\0'; level = atoi(levelSpec); } if (strcmp(spec, "stdout") == 0) { file = mpr->fileService->console; } else { if ((file = mprOpen(mpr, spec, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) { mprErrorPrintf(mpr, "Can't open log file %s\n", spec); return -1; } } mprSetLogLevel(mpr, level); mprSetLogHandler(mpr, logHandler, (void*) file); now = mprGetTime(mpr); mprCtime(mpr, timeText, sizeof(timeText), now); mprLog(mpr, MPR_CONFIG, "Configuration for %s", mprGetAppTitle(mpr)); mprLog(mpr, MPR_CONFIG, "--------------------------------------------"); mprLog(mpr, MPR_CONFIG, "Host: %s", mprGetHostName(mpr)); mprLog(mpr, MPR_CONFIG, "CPU: %s", BLD_HOST_CPU); mprLog(mpr, MPR_CONFIG, "OS: %s", BLD_HOST_OS); mprLog(mpr, MPR_CONFIG, "Distribution: %s %s", BLD_HOST_DIST, BLD_HOST_DIST_VER); mprLog(mpr, MPR_CONFIG, "OS: %s", BLD_HOST_OS); mprLog(mpr, MPR_CONFIG, "Version: %s.%d", BLD_VERSION, BLD_NUMBER); mprLog(mpr, MPR_CONFIG, "BuildType: %s", BLD_TYPE); mprLog(mpr, MPR_CONFIG, "Started at: %s", timeText); mprLog(mpr, MPR_CONFIG, "--------------------------------------------"); } return 0; }
void MprLogToFile::logConfig() { Mpr *mpr; time_t now; char timeText[80]; mpr = mprGetMpr(); if (mpr == 0) { return; } now = time(0); mprCtime(&now, timeText, sizeof(timeText)); mprLog(MPR_CONFIG, "Configuration for %s\n", mpr->getAppTitle()); mprLog(MPR_CONFIG, "--------------------------------------------\n"); mprLog(MPR_CONFIG, "Host: %s\n", mpr->getHostName()); mprLog(MPR_CONFIG, "CPU: %s\n", mpr->getCpu()); mprLog(MPR_CONFIG, "OS: %s\n", mpr->getOs()); mprLog(MPR_CONFIG, "Distribution: %s %s\n", BLD_HOST_DIST, BLD_HOST_DIST_VER); mprLog(MPR_CONFIG, "OS: %s\n", mpr->getOs()); mprLog(MPR_CONFIG, "Version: %s.%d\n", mpr->getVersion(), mpr->getBuildNumber()); mprLog(MPR_CONFIG, "BuildType: %s\n", mpr->getBuildType()); mprLog(MPR_CONFIG, "Started at: %s", timeText); mprLog(MPR_CONFIG, "Log rotation count: %d\n", rotationCount); mprLog(MPR_CONFIG, "--------------------------------------------\n"); #if BLD_DEBUG #if BLD_FEATURE_HIRES_TIME int64 elapsed = mprGetElapsedTime(); mprSleep(250); elapsed = mprGetElapsedTime(); mprLog(4, "1 second is ~%,16Ld cycles\n", elapsed * 4); #else int elapsed = mprGetTime(0); mprSleep(250); elapsed = mprGetTime(0) - elapsed; mprLog(4, "1 second is ~%,8d cycles\n", elapsed * 4); #endif #endif }
void maLogRequest(MaConn *conn) { MaHost *logHost, *host; MaResponse *resp; MaRequest *req; MprBuf *buf; MprTime now; char keyBuf[80], timeBuf[64], *fmt, *cp, *qualifier, *value, c; int len; resp = conn->response; req = conn->request; host = req->host; logHost = host->logHost; if (logHost == 0) { return; } fmt = logHost->logFormat; if (fmt == 0) { return; } len = MA_MAX_URL + 256; buf = mprCreateBuf(resp, len, len); while ((c = *fmt++) != '\0') { if (c != '%' || (c = *fmt++) == '%') { mprPutCharToBuf(buf, c); continue; } switch (c) { case 'a': /* Remote IP */ mprPutStringToBuf(buf, conn->remoteIpAddr); break; case 'A': /* Local IP */ mprPutStringToBuf(buf, conn->sock->listenSock->ipAddr); break; case 'b': if (resp->bytesWritten == 0) { mprPutCharToBuf(buf, '-'); } else { mprPutIntToBuf(buf, resp->bytesWritten); } break; case 'B': /* Bytes written (minus headers) */ mprPutIntToBuf(buf, resp->bytesWritten - resp->headerSize); break; case 'h': /* Remote host */ // TODO - Should this trigger a reverse DNS? mprPutStringToBuf(buf, conn->remoteIpAddr); break; case 'n': /* Local host */ mprPutStringToBuf(buf, req->parsedUri->host); break; case 'l': /* Supplied in authorization */ mprPutStringToBuf(buf, req->user ? req->user : "******"); break; case 'O': /* Bytes written (including headers) */ mprPutIntToBuf(buf, resp->bytesWritten); break; case 'r': /* First line of request */ mprPutFmtToBuf(buf, "%s %s %s", req->methodName, req->parsedUri->originalUri, req->httpProtocol); break; case 's': /* Response code */ mprPutIntToBuf(buf, resp->code); break; case 't': /* Time */ now = mprGetTime(conn); mprCtime(conn, timeBuf, sizeof(timeBuf), now); if ((cp = strchr(timeBuf, '\n')) != 0) { *cp = '\0'; } mprPutCharToBuf(buf, '['); mprPutStringToBuf(buf, timeBuf); mprPutCharToBuf(buf, ']'); break; case 'u': /* Remote username */ mprPutStringToBuf(buf, req->user ? req->user : "******"); break; case '{': /* Header line */ qualifier = fmt; if ((cp = strchr(qualifier, '}')) != 0) { fmt = &cp[1]; *cp = '\0'; c = *fmt++; mprStrcpy(keyBuf, sizeof(keyBuf), "HTTP_"); mprStrcpy(&keyBuf[5], sizeof(keyBuf) - 5, qualifier); mprStrUpper(keyBuf); switch (c) { case 'i': value = (char*) mprLookupHash(req->headers, keyBuf); mprPutStringToBuf(buf, value ? value : "-"); break; default: mprPutStringToBuf(buf, qualifier); } *cp = '}'; } else { mprPutCharToBuf(buf, c); } break; case '>': if (*fmt == 's') { fmt++; mprPutIntToBuf(buf, resp->code); } break; default: mprPutCharToBuf(buf, c); break; } } mprPutCharToBuf(buf, '\n'); mprAddNullToBuf(buf); mprWrite(logHost->accessLog, mprGetBufStart(buf), mprGetBufLength(buf)); }