📄 mod_log_config.c
字号:
it->arg = ap_getword(p, &s, '}'); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': i = *s - '0'; while (apr_isdigit(*++s)) { i = i * 10 + (*s) - '0'; } if (!it->conditions) { it->conditions = apr_array_make(p, 4, sizeof(int)); } *(int *) apr_array_push(it->conditions) = i; break; default: handler = (ap_log_handler *)apr_hash_get(log_hash, s++, 1); if (!handler) { char dummy[2]; dummy[0] = s[-1]; dummy[1] = '\0'; return apr_pstrcat(p, "Unrecognized LogFormat directive %", dummy, NULL); } it->func = handler->func; if (it->want_orig == -1) { it->want_orig = handler->want_orig_default; } *sa = s; return NULL; } } return "Ran off end of LogFormat parsing args to some directive";}static apr_array_header_t *parse_log_string(apr_pool_t *p, const char *s, const char **err){ apr_array_header_t *a = apr_array_make(p, 30, sizeof(log_format_item)); char *res; while (*s) { if ((res = parse_log_item(p, (log_format_item *) apr_array_push(a), &s))) { *err = res; return NULL; } } s = APR_EOL_STR; parse_log_item(p, (log_format_item *) apr_array_push(a), &s); return a;}/***************************************************************** * * Actually logging. */static const char *process_item(request_rec *r, request_rec *orig, log_format_item *item){ const char *cp; /* First, see if we need to process this thing at all... */ if (item->conditions && item->conditions->nelts != 0) { int i; int *conds = (int *) item->conditions->elts; int in_list = 0; for (i = 0; i < item->conditions->nelts; ++i) { if (r->status == conds[i]) { in_list = 1; break; } } if ((item->condition_sense && in_list) || (!item->condition_sense && !in_list)) { return "-"; } } /* We do. Do it... */ cp = (*item->func) (item->want_orig ? orig : r, item->arg); return cp ? cp : "-";}static void flush_log(buffered_log *buf){ if (buf->outcnt && buf->handle != NULL) { apr_file_write(buf->handle, buf->outbuf, &buf->outcnt); buf->outcnt = 0; }}static int config_log_transaction(request_rec *r, config_log_state *cls, apr_array_header_t *default_format){ log_format_item *items; const char **strs; int *strl; request_rec *orig; int i; apr_size_t len = 0; apr_array_header_t *format; char *envar; apr_status_t rv; if (cls->fname == NULL) { return DECLINED; } /* * See if we've got any conditional envariable-controlled logging decisions * to make. */ if (cls->condition_var != NULL) { envar = cls->condition_var; if (*envar != '!') { if (apr_table_get(r->subprocess_env, envar) == NULL) { return DECLINED; } } else { if (apr_table_get(r->subprocess_env, &envar[1]) != NULL) { return DECLINED; } } } format = cls->format ? cls->format : default_format; strs = apr_palloc(r->pool, sizeof(char *) * (format->nelts)); strl = apr_palloc(r->pool, sizeof(int) * (format->nelts)); items = (log_format_item *) format->elts; orig = r; while (orig->prev) { orig = orig->prev; } while (r->next) { r = r->next; } for (i = 0; i < format->nelts; ++i) { strs[i] = process_item(r, orig, &items[i]); } for (i = 0; i < format->nelts; ++i) { len += strl[i] = strlen(strs[i]); } if (!log_writer) { ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_EGENERAL, r, "log writer isn't correctly setup"); return HTTP_INTERNAL_SERVER_ERROR; } rv = log_writer(r, cls->log_writer, strs, strl, format->nelts, len); /* xxx: do we return an error on log_writer? */ return OK;}static int multi_log_transaction(request_rec *r){ multi_log_state *mls = ap_get_module_config(r->server->module_config, &log_config_module); config_log_state *clsarray; int i; /* * Log this transaction.. */ if (mls->config_logs->nelts) { clsarray = (config_log_state *) mls->config_logs->elts; for (i = 0; i < mls->config_logs->nelts; ++i) { config_log_state *cls = &clsarray[i]; config_log_transaction(r, cls, mls->default_format); } } else if (mls->server_config_logs) { clsarray = (config_log_state *) mls->server_config_logs->elts; for (i = 0; i < mls->server_config_logs->nelts; ++i) { config_log_state *cls = &clsarray[i]; config_log_transaction(r, cls, mls->default_format); } } return OK;}/***************************************************************** * * Module glue... */static void *make_config_log_state(apr_pool_t *p, server_rec *s){ multi_log_state *mls; mls = (multi_log_state *) apr_palloc(p, sizeof(multi_log_state)); mls->config_logs = apr_array_make(p, 1, sizeof(config_log_state)); mls->default_format_string = NULL; mls->default_format = NULL; mls->server_config_logs = NULL; mls->formats = apr_table_make(p, 4); apr_table_setn(mls->formats, "CLF", DEFAULT_LOG_FORMAT); return mls;}/* * Use the merger to simply add a pointer from the vhost log state * to the log of logs specified for the non-vhost configuration. Make sure * vhosts inherit any globally-defined format names. */static void *merge_config_log_state(apr_pool_t *p, void *basev, void *addv){ multi_log_state *base = (multi_log_state *) basev; multi_log_state *add = (multi_log_state *) addv; add->server_config_logs = base->config_logs; if (!add->default_format) { add->default_format_string = base->default_format_string; add->default_format = base->default_format; } add->formats = apr_table_overlay(p, base->formats, add->formats); return add;}/* * Set the default logfile format, or define a nickname for a format string. */static const char *log_format(cmd_parms *cmd, void *dummy, const char *fmt, const char *name){ const char *err_string = NULL; multi_log_state *mls = ap_get_module_config(cmd->server->module_config, &log_config_module); /* * If we were given two arguments, the second is a name to be given to the * format. This syntax just defines the nickname - it doesn't actually * make the format the default. */ if (name != NULL) { parse_log_string(cmd->pool, fmt, &err_string); if (err_string == NULL) { apr_table_setn(mls->formats, name, fmt); } } else { mls->default_format_string = fmt; mls->default_format = parse_log_string(cmd->pool, fmt, &err_string); } return err_string;}static const char *add_custom_log(cmd_parms *cmd, void *dummy, const char *fn, const char *fmt, const char *envclause){ const char *err_string = NULL; multi_log_state *mls = ap_get_module_config(cmd->server->module_config, &log_config_module); config_log_state *cls; cls = (config_log_state *) apr_array_push(mls->config_logs); cls->condition_var = NULL; if (envclause != NULL) { if (strncasecmp(envclause, "env=", 4) != 0) { return "error in condition clause"; } if ((envclause[4] == '\0') || ((envclause[4] == '!') && (envclause[5] == '\0'))) { return "missing environment variable name"; } cls->condition_var = apr_pstrdup(cmd->pool, &envclause[4]); } cls->fname = fn; cls->format_string = fmt; if (fmt == NULL) { cls->format = NULL; } else { cls->format = parse_log_string(cmd->pool, fmt, &err_string); } cls->log_writer = NULL; return err_string;}static const char *set_transfer_log(cmd_parms *cmd, void *dummy, const char *fn){ return add_custom_log(cmd, dummy, fn, NULL, NULL);}static const char *set_cookie_log(cmd_parms *cmd, void *dummy, const char *fn){ return add_custom_log(cmd, dummy, fn, "%{Cookie}n \"%r\" %t", NULL);}static const char *set_buffered_logs_on(cmd_parms *parms, void *dummy, int flag){ buffered_logs = flag; if (buffered_logs) { ap_log_set_writer_init(ap_buffered_log_writer_init); ap_log_set_writer(ap_buffered_log_writer); } return NULL;}static const command_rec config_log_cmds[] ={AP_INIT_TAKE23("CustomLog", add_custom_log, NULL, RSRC_CONF, "a file name, a custom log format string or format name, " "and an optional \"env=\" clause (see docs)"),AP_INIT_TAKE1("TransferLog", set_transfer_log, NULL, RSRC_CONF, "the filename of the access log"),AP_INIT_TAKE12("LogFormat", log_format, NULL, RSRC_CONF, "a log format string (see docs) and an optional format name"),AP_INIT_TAKE1("CookieLog", set_cookie_log, NULL, RSRC_CONF, "the filename of the cookie log"),AP_INIT_FLAG("BufferedLogs", set_buffered_logs_on, NULL, RSRC_CONF, "Enable Buffered Logging (experimental)"), {NULL}};static config_log_state *open_config_log(server_rec *s, apr_pool_t *p, config_log_state *cls, apr_array_header_t *default_format){ if (cls->log_writer != NULL) { return cls; /* virtual config shared w/main server */ } if (cls->fname == NULL) { return cls; /* Leave it NULL to decline. */ } cls->log_writer = log_writer_init(p, s, cls->fname); if (cls->log_writer == NULL) return NULL; return cls;}static int open_multi_logs(server_rec *s, apr_pool_t *p)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -