void ngx_cdecl ngx_log_stderr(ngx_err_t err, const char *fmt, ...) { u_char *p, *last; va_list args; u_char errstr[NGX_MAX_ERROR_STR]; last = errstr + NGX_MAX_ERROR_STR; va_start(args, fmt); p = ngx_vslprintf(errstr, last, fmt, args); va_end(args); if (err) { p = ngx_log_errno(p, last, err); } if (p > last - NGX_LINEFEED_SIZE) { p = last - NGX_LINEFEED_SIZE; } ngx_linefeed(p); (void) ngx_write_console(ngx_stderr, errstr, p - errstr); }
/* * 记录配置信息错误日志 */ 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); }
inline void ngx_http_xray_line( ngx_http_request_t *r, const ngx_int_t level, const char *format, ...) { va_list args; ngx_http_xray_ctx_t *xctx = ngx_http_get_module_ctx(r, ngx_http_xray_module); if (!xctx || !xctx->xray) { return; } if (xctx->xray_level < level) { return; } va_start(args, format); xctx->xray->last = ngx_vslprintf(xctx->xray->last, xctx->xray->end, format, args); va_end(args); if (xctx->xray->last < xctx->xray->end) { *xctx->xray->last++ = '\n'; } }
void NPrintBig(NLogCb LogCb,const char* LEVEL, const char* funcName, const char* fileName, int line, const char* format, ...){ #define NLOG_BIGBUF_LEN (1024*32) #define big_buf_rest(buf, p) (buf+NLOG_BIGBUF_LEN-p-1) u_char logbuf[NLOG_BIGBUF_LEN]; memset(logbuf, 0, NLOG_BIGBUF_LEN); u_char* p = logbuf; //不显示日志时间。 time_t timep; struct tm *ptm, mytm; timep = ngx_time(); ptm = localtime_r(&timep, &mytm); p = ngx_snprintf(p, big_buf_rest(logbuf, p), NLOG_TF "%s:%s[%s:%d] ", 1+ptm->tm_mon, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, LEVEL, funcName, fileName, line); va_list args; va_start(args, format); p = ngx_vslprintf(p , logbuf+NLOG_BIGBUF_LEN-2, format , args); va_end(args); if(big_buf_rest(logbuf, p) > 0){ p = ngx_snprintf(p, big_buf_rest(logbuf, p), "\n"); } logbuf[NLOG_BIGBUF_LEN-1] = 0; if(LogCb == NULL){ fprintf(stderr, "%.*s", (int)(p-logbuf), logbuf); }else{ LogCb((const char*)logbuf, p-logbuf); } }
ngx_int_t nchan_respond_sprintf(ngx_http_request_t *r, ngx_int_t status_code, const ngx_str_t *content_type, const ngx_int_t finalize, char *fmt, ...) { u_char *p; ngx_str_t str; va_list args; str.len = 1024; str.data = ngx_palloc(r->pool, 1024); if(str.data == NULL) { return nchan_respond_status(r, status_code, NULL, NULL, finalize); return NGX_ERROR; } va_start(args, fmt); p = ngx_vslprintf(str.data, str.data + str.len, fmt, args); va_end(args); str.len = p - str.data; return nchan_respond_string(r, status_code, content_type, &str, finalize); }
static void ngx_cdecl ngx_mbedtls_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, int sslerr, char *fmt, ...) { va_list args; u_char *p, *last; u_char errstr[NGX_MAX_CONF_ERRSTR]; last = errstr + NGX_MAX_CONF_ERRSTR; va_start(args, fmt); p = ngx_vslprintf(errstr, last - 1, fmt, args); va_end(args); p = ngx_cpystrn(p, (u_char *) " (SSL:", last - p); error_strerror(sslerr, (char *) p, last - p); ngx_log_error(level, log, err, "%s", errstr); }
void ngx_cdecl ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, char *fmt, ...) { va_list args; u_char *p, *last; u_char errstr[NGX_MAX_CONF_ERRSTR]; last = errstr + NGX_MAX_CONF_ERRSTR; va_start(args, fmt); p = ngx_vslprintf(errstr, last - 1, fmt, args); va_end(args); /* * PolarSSL does not have an error queue so it's not possible to access the * last error. This doesn't really matter since this routine is not used * in PolarSSL builds. */ ngx_log_error(level, log, err, "%s", errstr); }
/* * Helper function to send a formatted string as response. */ static ngx_int_t ngx_http_rrd_outprintf(ngx_http_request_t *r, ngx_uint_t http_status, const char* fmt, ...) { ngx_log_t *log = r->connection->log; ngx_buf_t *buf = ngx_create_temp_buf(r->pool, 2048); if (NULL == buf) { /* nothing else I can do... */ ngx_log_error(NGX_LOG_ALERT, log, 0, "buf alloc pb @ngx_http_rrd_outprintf"); return NGX_HTTP_INTERNAL_SERVER_ERROR; } ngx_chain_t *out_chain = ngx_alloc_chain_link(r->pool); if (NULL == out_chain) { /* nothing else I can do... */ ngx_log_error(NGX_LOG_ALERT, log, 0, "chain alloc pb @ngx_http_rrd_outprintf"); return NGX_HTTP_INTERNAL_SERVER_ERROR; } out_chain->buf = buf; out_chain->next = NULL; va_list args; va_start(args, fmt); buf->last = ngx_vslprintf(buf->start, buf->end, fmt, args); va_end(args); buf->last_buf = 1; buf->last_in_chain = 1; ngx_int_t rc; r->headers_out.status = http_status; r->headers_out.content_length_n = buf->last - buf->start; rc = ngx_http_send_header(r); if (rc != NGX_OK) { ngx_log_error(NGX_LOG_ALERT, log, 0, "pb sending header @ngx_http_rrd_outprintf"); return NGX_HTTP_INTERNAL_SERVER_ERROR; } return ngx_http_output_filter(r, out_chain); }
void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, const char *fmt, va_list args) #endif { #if (NGX_HAVE_VARIADIC_MACROS) va_list args; #endif u_char *p, *last, *msg; u_char errstr[NGX_MAX_ERROR_STR]; if (log->file->fd == NGX_INVALID_FILE) { return; } last = errstr + NGX_MAX_ERROR_STR; p = errstr ; p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]); /* pid#tid */ p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ", ngx_log_pid, ngx_log_tid); msg = p; #if (NGX_HAVE_VARIADIC_MACROS) va_start(args, fmt); p = ngx_vslprintf(p, last, fmt, args); va_end(args); #else p = ngx_vslprintf(p, last, fmt, args); #endif if (err) { p = ngx_log_errno(p, last, err); } if (level != NGX_LOG_DEBUG && log->handler) { p = log->handler(log, p, last - p); } if (p > last - NGX_LINEFEED_SIZE) { p = last - NGX_LINEFEED_SIZE; } ngx_linefeed(p); (void) ngx_write_fd(log->file->fd, errstr, p - errstr); if (!ngx_use_stderr || level > NGX_LOG_WARN || log->file->fd == ngx_stderr) { return; } msg -= (err_levels[level].len + 4); (void) ngx_sprintf(msg, "[%V]: ", &err_levels[level]); (void) ngx_write_console(ngx_stderr, msg, p - msg); }
void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, const char *fmt, va_list args) #endif { #if (NGX_HAVE_VARIADIC_MACROS) va_list args; #endif u_char *p, *last, *msg; u_char errstr[NGX_MAX_ERROR_STR]; ngx_uint_t wrote_stderr, debug_connection; last = errstr + NGX_MAX_ERROR_STR; ngx_memcpy(errstr, ngx_cached_err_log_time.data, ngx_cached_err_log_time.len); p = errstr + ngx_cached_err_log_time.len; p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]); /* pid#tid */ p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ", ngx_log_pid, ngx_log_tid); if (log->connection) { p = ngx_slprintf(p, last, "*%uA ", log->connection); } msg = p; #if (NGX_HAVE_VARIADIC_MACROS) va_start(args, fmt); p = ngx_vslprintf(p, last, fmt, args); va_end(args); #else p = ngx_vslprintf(p, last, fmt, args); #endif if (err) { p = ngx_log_errno(p, last, err); } if (level != NGX_LOG_DEBUG && log->handler) { p = log->handler(log, p, last - p); } if (p > last - NGX_LINEFEED_SIZE) { p = last - NGX_LINEFEED_SIZE; } ngx_linefeed(p); wrote_stderr = 0; debug_connection = (log->log_level & NGX_LOG_DEBUG_CONNECTION) != 0; while (log) { if (log->log_level < level && !debug_connection) { break; } (void) ngx_write_fd(log->file->fd, errstr, p - errstr); if (log->file->fd == ngx_stderr) { wrote_stderr = 1; } log = log->next; } if (!ngx_use_stderr || level > NGX_LOG_WARN || wrote_stderr) { return; } msg -= (7 + err_levels[level].len + 3); (void) ngx_sprintf(msg, "nginx: [%V] ", &err_levels[level]); (void) ngx_write_console(ngx_stderr, msg, p - msg); }
void ngx_log_error_coreall(ngx_uint_t level, ngx_log_t *log, const char* filename, int lineno, ngx_err_t err, const char *fmt, va_list args) #endif { #if (NGX_HAVE_VARIADIC_MACROS) va_list args; #endif u_char *p, *last, *msg; ssize_t n; ngx_uint_t wrote_stderr, debug_connection; u_char errstr[NGX_MAX_ERROR_STR]; char filebuf[52]; last = errstr + NGX_MAX_ERROR_STR; p = ngx_cpymem(errstr, ngx_cached_err_log_time.data, ngx_cached_err_log_time.len); snprintf(filebuf, sizeof(filebuf), "[%35s, %5d][yangyazhou @@@ test]", filename, lineno); p = ngx_slprintf(p, last, "%s ", filebuf); p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]); /* pid#tid */ p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ", ngx_log_pid, ngx_log_tid); //进程ID和线程ID(在开启线程池的时候线程ID和进程ID不同) if (log->connection) { p = ngx_slprintf(p, last, "*%uA ", log->connection); } msg = p; #if (NGX_HAVE_VARIADIC_MACROS) va_start(args, fmt); p = ngx_vslprintf(p, last, fmt, args); va_end(args); #else p = ngx_vslprintf(p, last, fmt, args); #endif if (err) { p = ngx_log_errno(p, last, err); } if (level != NGX_LOG_DEBUG && log->handler) { p = log->handler(log, p, last - p); } if (p > last - NGX_LINEFEED_SIZE) { p = last - NGX_LINEFEED_SIZE; } ngx_linefeed(p); wrote_stderr = 0; debug_connection = (log->log_level & NGX_LOG_DEBUG_CONNECTION) != 0; while (log) { if (log->writer) { log->writer(log, level, errstr, p - errstr); goto next; } if (ngx_time() == log->disk_full_time) { /* * on FreeBSD writing to a full filesystem with enabled softupdates * may block process for much longer time than writing to non-full * filesystem, so we skip writing to a log for one second */ goto next; } n = ngx_write_fd(log->file->fd, errstr, p - errstr); //写到log文件中 if (n == -1 && ngx_errno == NGX_ENOSPC) { log->disk_full_time = ngx_time(); } if (log->file->fd == ngx_stderr) { wrote_stderr = 1; } next: log = log->next; } if (!ngx_use_stderr || level > NGX_LOG_WARN || wrote_stderr) /* 如果满足这些条件,则不会输出打印到前台,只会写到errlog文件中 */ { return; } msg -= (7 + err_levels[level].len + 3); (void) ngx_sprintf(msg, "nginx: [%V] ", &err_levels[level]); (void) ngx_write_console(ngx_stderr, msg, p - msg); }
void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, const char *fmt, va_list args) #endif { #if (NGX_HAVE_VARIADIC_MACROS) va_list args; #endif u_char *p, *last, *msg; ssize_t n; ngx_uint_t wrote_stderr, debug_connection; u_char errstr[NGX_MAX_ERROR_STR]; // 错误消息的最大长度,2k字节 last = errstr + NGX_MAX_ERROR_STR; // 先拷贝当前的时间 // 格式是"1970/09/28 12:00:00" p = ngx_cpymem(errstr, ngx_cached_err_log_time.data, ngx_cached_err_log_time.len); // 打印错误等级的字符串描述信息,使用关联数组err_levels p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]); // 打印pid和tid // #define ngx_log_pid ngx_pid // #define ngx_log_tid ngx_thread_tid() // in os/unix /* pid#tid */ p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ", ngx_log_pid, ngx_log_tid); // 如果有连接计数则打印 if (log->connection) { p = ngx_slprintf(p, last, "*%uA ", log->connection); } // 前面输出的是基本的信息:当前时间+[错误级别]+pid#tid: msg = p; // 打印可变参数 #if (NGX_HAVE_VARIADIC_MACROS) va_start(args, fmt); p = ngx_vslprintf(p, last, fmt, args); va_end(args); #else p = ngx_vslprintf(p, last, fmt, args); #endif // 如果有系统错误码,那么记录(err) if (err) { p = ngx_log_errno(p, last, err); } // 记录错误日志时可以执行的回调函数 // 参数是消息缓冲区里剩余的空间 // 只有高于debug才会执行 if (level != NGX_LOG_DEBUG && log->handler) { p = log->handler(log, p, last - p); } // #define NGX_LINEFEED_SIZE 1 if (p > last - NGX_LINEFEED_SIZE) { p = last - NGX_LINEFEED_SIZE; } // #define ngx_linefeed(p) *p++ = LF; ngx_linefeed(p); wrote_stderr = 0; debug_connection = (log->log_level & NGX_LOG_DEBUG_CONNECTION) != 0; // 对整个日志链表执行写入操作 while (log) { if (log->log_level < level && !debug_connection) { break; } if (log->writer) { log->writer(log, level, errstr, p - errstr); goto next; } if (ngx_time() == log->disk_full_time) { /* * on FreeBSD writing to a full filesystem with enabled softupdates * may block process for much longer time than writing to non-full * filesystem, so we skip writing to a log for one second */ goto next; } // 写错误日志消息到关联的文件 // 实际上就是系统调用write,见ngx_files.h n = ngx_write_fd(log->file->fd, errstr, p - errstr); // 写入失败,且错误是磁盘满 if (n == -1 && ngx_errno == NGX_ENOSPC) { log->disk_full_time = ngx_time(); } if (log->file->fd == ngx_stderr) { wrote_stderr = 1; } next: // 使用下一个日志对象记录日志 log = log->next; } if (!ngx_use_stderr || level > NGX_LOG_WARN || wrote_stderr) { return; } msg -= (7 + err_levels[level].len + 3); (void) ngx_sprintf(msg, "nginx: [%V] ", &err_levels[level]); (void) ngx_write_console(ngx_stderr, msg, p - msg); }
void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, const char *fmt, ...){ #else void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, const char *fmt, va_list args){ #endif #if (NGX_HAVE_VARIADIC_MACROS) va_list args; #endif u_char *p,*last,*msg; ssize_t n; ngx_uint_t wrote_stderr,debug_connection; u_char errstr[NGX_MAX_ERROR_STR]; last = errstr + NGX_MAX_ERROR_STR; //it seems do not set the ngx_cached_err_log_time.len value p = ngx_cpymem(errstr,ngx_cached_err_log_time.data, ngx_cached_err_log_time.len); p = ngx_slprintf(p,last,"[%V]",&err_levels[level]); //just for making the datastructure can work correctly,comment the next function /* pid#tid */ // p = ngx_slprintf(p,last,"%P#" NGX_TID_T_FMT ": ", // ngx_log_pid,ngx_log_tid); if (log->connection) { p = ngx_slprintf(p,last,"*%uA ",log->connection); } msg = p; #if (NGX_HAVE_VARIADIC_MACROS) va_start (args,fmt); p = ngx_vslprintf(p,last,fmt,args); va_end (args); #else p = ngx_vslprintf(p,last,fmt,args); #endif if (err) { p = ngx_log_errno(p,last,err); } if (level != NGX_LOG_DEBUG && log->handler){ p = log->handler(log,p,last - p); } if (p > last - NGX_LINEFEED_SIZE){ p = last - NGX_LINEFEED_SIZE; } ngx_linefeed(p); wrote_stderr = 0; debug_connection = (log->log_level & NGX_LOG_DEBUG_CONNECTION) != 0; while(log){ if (log->log_level < level && !debug_connection){ break; } if(log->writer){ log->writer(log,level,errstr,p - errstr); goto next; } if (ngx_time() == log->disk_full_time){ /* * on FreeBSD writing to a full filesystem with enabled softupdates * may block process for much longer time than writing to non-full * filesystem, so we skip writing to a log for one second */ goto next; } n = ngx_write_fd(log->file->fd,errstr,p - errstr); if (n == -1 && ngx_errno == NGX_ENOSPC){ log->disk_full_time = ngx_time(); } if (log->file->fd == ngx_stderr){ wrote_stderr = 1; } next: log = log->next; } if (!ngx_use_stderr || level > NGX_LOG_WARN || wrote_stderr){ return; } msg -= (7 + err_levels[level].len + 3); (void)ngx_sprintf(msg,"nginx: [%V]",&err_levels[level]); (void) ngx_write_console(ngx_stderr,msg,p - msg); }