示例#1
0
文件: log.c 项目: jsjohnst/appweb
/*
 *  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;
}
示例#2
0
文件: log.c 项目: gamman/appweb-3
/*
 *  Start error and information logging. Note: this is not per-request access logging
 */
int maStartLogging(MprCtx ctx, cchar *logSpec)
{
    Mpr         *mpr;
    MprFile     *file;
    char        *levelSpec, *spec;
    int         level;

    level = 0;
    mpr = mprGetMpr(ctx);

    if (logSpec == 0) {
        logSpec = "stdout:0";
    }
    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->fileSystem->stdOutput;
        } else {
            if ((file = mprOpen(mpr, spec, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) {
                mprPrintfError(mpr, "Can't open log file %s\n", spec);
                return -1;
            }
        }
        mprSetLogLevel(mpr, level);
        mprSetLogHandler(mpr, logHandler, (void*) file);

        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_CPU);
        mprLog(mpr, MPR_CONFIG, "OS:                 %s", BLD_OS);
        if (strcmp(BLD_DIST, "Unknown") != 0) {
            mprLog(mpr, MPR_CONFIG, "Distribution:       %s %s", BLD_DIST, BLD_DIST_VER);
        }
        mprLog(mpr, MPR_CONFIG, "Version:            %s-%s", BLD_VERSION, BLD_NUMBER);
        mprLog(mpr, MPR_CONFIG, "BuildType:          %s", BLD_TYPE);
        mprLog(mpr, MPR_CONFIG, "---------------------------------------------");
    }
    return 0;
}
示例#3
0
/*
    function get ipaddr(): String
 */
static EjsString *system_ipaddr(Ejs *ejs, EjsObj *unused, int argc, EjsObj **argv)
{
    //  MOB - move this into MPR and call mprSetIpAddr
#if BIT_UNIX_LIKE || BIT_WIN_LIKE
    struct addrinfo *res, *reslist, hints;
    cchar           *ip;
    char            ipaddr[BIT_MAX_PATH], service[BIT_MAX_PATH];
    int             rc;

    if ((ip = mprGetIpAddr(ejs)) != 0) {
        return ejsCreateStringFromAsc(ejs, mprGetIpAddr(ejs));
    }
    memset((char*) &hints, 0, sizeof(hints));
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_family = AF_INET;
    hints.ai_flags = AI_PASSIVE;
    if ((rc = getaddrinfo(mprGetHostName(), NULL, &hints, &reslist)) == 0) {
        ip = 0;
        //  TODO - support IPv6
        for (res = reslist; res; res = res->ai_next) {
            if (getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen, ipaddr, (int) sizeof(ipaddr) - 1, service, 
                    (int) sizeof(service) - 1, NI_NUMERICHOST | NI_NUMERICSERV | NI_NOFQDN) == 0) {
                if (strncmp(ipaddr, "10.", 3) == 0 || strncmp(ipaddr, "127.", 4) == 0 ||
                     strncmp(ipaddr, "169.", 4) == 0 || strncmp(ipaddr, "172.", 4) == 0 ||
                     strncmp(ipaddr, "192.", 4) == 0) {
                    if (ip == 0) {
                        ip = sclone(ipaddr);
                    }
                } else {
                    ip = sclone(ipaddr);
                    break;
                }
            }
        }
        return ejsCreateStringFromAsc(ejs, ip ? ip : "127.0.0.1");
    } else {
        mprError("Cannot get IP address, check system hostname. Error %d.\n", rc);
    }
#endif
    return ESV(null);
}
示例#4
0
/*
    function get hostname(): String
 */
static EjsString *system_hostname(Ejs *ejs, EjsObj *unused, int argc, EjsObj **argv)
{
    return ejsCreateStringFromAsc(ejs, mprGetHostName());
}