nginx 1.4.3 日志模块测试程序

1.源文件main.c
/**
 * 2014年1月7日16时37分 log test
 */


#include <stdio.h>
#include "ngx_config.h"
#include "ngx_conf_file.h"
#include "nginx.h"
#include "ngx_core.h"
#include "ngx_string.h"
#include "ngx_palloc.h"
#include "ngx_list.h"
#undef NGX_HAVE_VARIADIC_MACROS
#define NGX_HAVE_VARIADIC_MACROS  0

#include "ngx_log.h"



#define  ngx_prefix "/usr/local/nginx"
volatile ngx_cycle_t  *ngx_cycle;
ngx_pid_t     ngx_pid;
//volatile ngx_str_t       ngx_cached_err_log_time;

void ngx_cdecl
ngx_conf_log_error(ngx_uint_t level, ngx_conf_t *cf, ngx_err_t err,
                   const char *fmt, ...)
{
    u_char   errstr[NGX_MAX_CONF_ERRSTR], *p, *last;
    va_list  args;

    last = errstr + NGX_MAX_CONF_ERRSTR;

    va_start(args, fmt);
    p = ngx_vslprintf(errstr, last, fmt, args);
    va_end(args);

    if (err) {
        p = ngx_log_errno(p, last, err);
    }

    if (cf->conf_file == NULL) {
        ngx_log_error(level, cf->log, 0, "%*s", p - errstr, errstr);
        return;
    }

    if (cf->conf_file->file.fd == NGX_INVALID_FILE) {
        ngx_log_error(level, cf->log, 0, "%*s in command line",
                      p - errstr, errstr);
        return;
    }

    ngx_log_error(level, cf->log, 0, "%*s in %s:%ui",
                  p - errstr, errstr,
                  cf->conf_file->file.name.data, cf->conf_file->line);
}


ngx_open_file_t *
ngx_conf_open_file(ngx_cycle_t *cycle, ngx_str_t *name)
{
    ngx_str_t         full;
    ngx_uint_t        i;
    ngx_list_part_t  *part;
    ngx_open_file_t  *file;

#if (NGX_SUPPRESS_WARN)
    ngx_str_null(&full);
#endif

    if (name->len) {
        full = *name;

        if (ngx_conf_full_name(cycle, &full, 0) != NGX_OK) {
            return NULL;
        }

        part = &cycle->open_files.part;
        file = part->elts;

        for (i = 0; /* void */ ; i++) {

            if (i >= part->nelts) {
                if (part->next == NULL) {
                    break;
                }
                part = part->next;
                file = part->elts;
                i = 0;
            }

            if (full.len != file[i].name.len) {
                continue;
            }

            if (ngx_strcmp(full.data, file[i].name.data) == 0) {
                return &file[i];
            }
        }
    }

    file = ngx_list_push(&cycle->open_files);
    if (file == NULL) {
        return NULL;
    }

    if (name->len) {
        file->fd = NGX_INVALID_FILE;
        file->name = full;

    } else {
        file->fd = ngx_stderr;
        file->name = *name;
    }

    file->flush = NULL;
    file->data = NULL;

    return file;
}


static ngx_int_t
ngx_conf_test_full_name(ngx_str_t *name)
{
#if (NGX_WIN32)
    u_char  c0, c1;

    c0 = name->data[0];

    if (name->len < 2) {
        if (c0 == '/') {
            return 2;
        }

        return NGX_DECLINED;
    }

    c1 = name->data[1];

    if (c1 == ':') {
        c0 |= 0x20;

        if ((c0 >= 'a' && c0 <= 'z')) {
            return NGX_OK;
        }

        return NGX_DECLINED;
    }

    if (c1 == '/') {
        return NGX_OK;
    }

    if (c0 == '/') {
        return 2;
    }

    return NGX_DECLINED;

#else

    if (name->data[0] == '/') {
        return NGX_OK;
    }

    return NGX_DECLINED;

#endif
}


ngx_int_t
ngx_conf_full_name(ngx_cycle_t *cycle, ngx_str_t *name, ngx_uint_t conf_prefix)
{
    size_t      len;
    u_char     *p, *n, *prefix;
    ngx_int_t   rc;

    rc = ngx_conf_test_full_name(name);

    if (rc == NGX_OK) {
        return rc;
    }

    if (conf_prefix) {
        len = cycle->conf_prefix.len;
        prefix = cycle->conf_prefix.data;

    } else {
        len = cycle->prefix.len;
        prefix = cycle->prefix.data;
    }

#if (NGX_WIN32)

    if (rc == 2) {
        len = rc;
    }

#endif

    n = ngx_pnalloc(cycle->pool, len + name->len + 1);
    if (n == NULL) {
        return NGX_ERROR;
    }

    p = ngx_cpymem(n, prefix, len);
    ngx_cpystrn(p, name->data, name->len + 1);

    name->len += len;
    name->data = n;

    return NGX_OK;
}



int main()
{
    ngx_log_t        *log;
    ngx_pid = ngx_getpid();
    printf("%s\n",NGX_ERROR_LOG_PATH);

    log = ngx_log_init(ngx_prefix);
    if (log == NULL) {
        return 1;
    }
    ngx_time_init();
    ngx_time_update();
    ngx_log_stderr(ngx_errno,
                   "[alert] could not open error log file: "
                   ngx_open_file_n " \"%s\" failed", "MAO");

    ngx_log_error(NGX_LOG_NOTICE, log, 0, "TEST %d",2014);
    //system("cat /usr/local/nginx/logs/error.log");
    sleep(5);
    return 0;
}


2.makefile


CXX = gcc
CXXFLAGS += -g 
#-Wall -Wextra
   
NGX_ROOT =/root/work/nginx-1.4.3
   
TARGETS =main
TARGETS_C_FILE= $(TARGETS).c
   
CLEANUP = rm -f $(TARGETS) *.o
   
all:$(TARGETS)
   
clean:
	$(CLEANUP)
   
CORE_INCS =-I. \
-I$(NGX_ROOT)/src/core \
-I$(NGX_ROOT)/src/event \
-I$(NGX_ROOT)/src/event/modules \
-I$(NGX_ROOT)/src/os/unix \
-I$(NGX_ROOT)/objs


SRCS = $(NGX_ROOT)/src/core/ngx_log.c
SRCS += $(NGX_ROOT)/src/core/ngx_palloc.c
SRCS += $(NGX_ROOT)/src/core/ngx_string.c
SRCS += $(NGX_ROOT)/src/os/unix/ngx_alloc.c
SRCS += $(NGX_ROOT)/src/os/unix/ngx_errno.c
SRCS += $(NGX_ROOT)/src/os/unix/ngx_time.c
SRCS += $(NGX_ROOT)/src/core/ngx_list.c
SRCS += $(NGX_ROOT)/src/core/ngx_times.c



$(TARGETS):$(TARGETS_C_FILE)	
	$(CXX) $(CXXFLAGS) $(CORE_INCS)  $(SRCS) $^ -o $@



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值