nginx原始碼分析——初始化log
nginx在main函式中首先對log進行初始化,將配置檔案中的log檔名開啟,將開啟log檔案控制程式碼等相應的屬性存在log變數中,本人覺得該程式碼寫的挺好,有借鑑之處,因而在次貼出。
struct ngx_log_s {
ngx_uint_t log_level;ngx_open_file_t *file;
ngx_atomic_uint_t connection;
ngx_log_handler_pt handler;
void *data;
/*
* we declare "action" as "char *" because the actions are usually
* the static strings and in the "u_char *" case we have to override
* their types all the time
*/
ngx_log_t *next;
};(src/core/ngx_log.h)
typedef struct ngx_log_s ngx_log_t; (src/core/ngx_core.h)
ngx_log_t *log; (src/core/nginx.c)
log = ngx_log_init(ngx_prefix); (src/core/nginx.c)
ngx_log_init 定義在src/core/ngx_log.c中
ngx_log_t *
ngx_log_init(u_char *prefix)
{
u_char *p, *name;
size_t nlen, plen;
ngx_log.file = &ngx_log_file;
ngx_log.log_level = NGX_LOG_NOTICE;
name = (u_char *) NGX_ERROR_LOG_PATH;
/*
* we use ngx_strlen() here since BCC warns about
* condition is always false and unreachable code
*/
nlen = ngx_strlen(name);
if (nlen == 0) {
ngx_log_file.fd = ngx_stderr;
return &ngx_log;
}
p = NULL;
#if (NGX_WIN32)
if (name[1] != ':') {
#else
if (name[0] != '/') {
#endif
if (prefix) {
plen = ngx_strlen(prefix);
} else {
#ifdef NGX_PREFIX
prefix = (u_char *) NGX_PREFIX;
plen = ngx_strlen(prefix);
#else
plen = 0;
#endif
}
if (plen) {
name = malloc(plen + nlen + 2);
if (name == NULL) {
return NULL;
}
p = ngx_cpymem(name, prefix, plen);
if (!ngx_path_separator(*(p - 1))) {
*p++ = '/';
}
ngx_cpystrn(p, (u_char *) NGX_ERROR_LOG_PATH, nlen + 1);
p = name;
}
}
ngx_log_file.fd = ngx_open_file(name, NGX_FILE_APPEND,
NGX_FILE_CREATE_OR_OPEN,
NGX_FILE_DEFAULT_ACCESS);
if (ngx_log_file.fd == NGX_INVALID_FILE) {
ngx_log_stderr(ngx_errno,
"[alert] could not open error log file: "
ngx_open_file_n " \"%s\" failed", name);
#if (NGX_WIN32)
ngx_event_log(ngx_errno,
"could not open error log file: "
ngx_open_file_n " \"%s\" failed", name);
#endif
ngx_log_file.fd = ngx_stderr;
}
if (p) {
ngx_free(p);
}
return &ngx_log;
}
相關文章
- monolog 原始碼分析Mono原始碼
- DialogFragment原始碼分析Fragment原始碼
- Dialog原始碼分析原始碼
- 【Android原始碼】AlertDialog 原始碼分析Android原始碼
- Nginx 原始碼分析:ngx_array_tNginx原始碼
- Log4j漏洞原始碼分析原始碼
- Spring原始碼分析(四)SpringMVC初始化原始碼SpringMVC
- MapReduce job在JobTracker初始化原始碼級分析原始碼
- kube-scheduler原始碼分析(1)-初始化與啟動分析原始碼
- k8s client-go原始碼分析 informer原始碼分析(2)-初始化與啟動分析K8SclientGo原始碼ORM
- Storm-原始碼分析-TopologySubmit-TaskORM原始碼MIT
- Spring IoC容器初始化 — Resource定位原始碼分析Spring原始碼
- ThinkPHP6 原始碼分析之應用初始化PHP原始碼
- Linux 核心排程器原始碼分析 - 初始化Linux原始碼
- Spring原始碼分析:Spring IOC容器初始化Spring原始碼
- 精盡MyBatis原始碼分析 - MyBatis初始化(三)之 SQL 初始化(上)MyBatis原始碼SQL
- 精盡MyBatis原始碼分析 - MyBatis初始化(四)之 SQL 初始化(下)MyBatis原始碼SQL
- Nginx篇--Nginx原始碼搭建Nginx原始碼
- redis個人原始碼分析1----hyperloglog(golang實現)Redis原始碼Golang
- slab原始碼分析--從slab初始化說起原始碼
- commons-logging + log4j原始碼分析原始碼
- Dubbo原始碼分析(二)Dubbo是從哪裡初始化的?原始碼
- spring-IOC容器原始碼分析(一)bean初始化流程Spring原始碼Bean
- Vue 原始碼分析(v2.5.15)part 1:Vue 初始化Vue原始碼
- AWStats分析nginx日誌(access.log)Nginx
- 用console.log分析Vue原始碼Vue原始碼
- 【Zookeeper】原始碼分析之持久化(三)之FileTxnSnapLog原始碼持久化
- 【Zookeeper】原始碼分析之持久化(一)之FileTxnLog原始碼持久化
- nodejs原始碼—初始化NodeJS原始碼
- nginx原始碼安裝Nginx原始碼
- NGINX原始碼閱讀Nginx原始碼
- Nginx 原始碼安裝Nginx原始碼
- 原始碼安裝Nginx原始碼Nginx
- Retrofit原始碼分析三 原始碼分析原始碼
- 比特幣原始碼分析–P2P網路初始化比特幣原始碼
- React原始碼分析與實現(一):元件的初始化與渲染React原始碼元件
- Spring原始碼分析(一)Spring的初始化和XML解析Spring原始碼XML
- 比特幣原始碼分析--P2P網路初始化比特幣原始碼