1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Written by Bojan Smojver <bojan@rexursive.com>:
20 * The argument to LogFormat and CustomLog is a string, which can include
21 * literal characters copied into the log files, and '%' directives as
24 * %...I: bytes received, including request and headers, cannot be zero
25 * %...O: bytes sent, including headers, cannot be zero
29 #include "apr_strings.h"
32 #include "apr_optional.h"
34 #define APR_WANT_STRFUNC
37 #include "ap_config.h"
38 #include "mod_log_config.h"
40 #include "http_core.h"
41 #include "http_config.h"
42 #include "http_connection.h"
43 #include "http_protocol.h"
45 module AP_MODULE_DECLARE_DATA logio_module;
47 static const char logio_filter_name[] = "LOG_INPUT_OUTPUT";
50 * Logging of input and output config...
53 typedef struct logio_config_t {
59 * Optional function for the core to add to bytes_out
62 static void ap_logio_add_bytes_out(conn_rec *c, apr_off_t bytes){
63 logio_config_t *cf = ap_get_module_config(c->conn_config, &logio_module);
65 cf->bytes_out += bytes;
72 static const char *log_bytes_in(request_rec *r, char *a)
74 logio_config_t *cf = ap_get_module_config(r->connection->conn_config,
77 return apr_off_t_toa(r->pool, cf->bytes_in);
80 static const char *log_bytes_out(request_rec *r, char *a)
82 logio_config_t *cf = ap_get_module_config(r->connection->conn_config,
85 return apr_off_t_toa(r->pool, cf->bytes_out);
89 * Reset counters after logging...
92 static int logio_transaction(request_rec *r)
94 logio_config_t *cf = ap_get_module_config(r->connection->conn_config,
97 cf->bytes_in = cf->bytes_out = 0;
103 * Logging of input and output filters...
106 static apr_status_t logio_in_filter(ap_filter_t *f,
107 apr_bucket_brigade *bb,
108 ap_input_mode_t mode,
109 apr_read_type_e block,
110 apr_off_t readbytes) {
113 logio_config_t *cf = ap_get_module_config(f->c->conn_config, &logio_module);
115 status = ap_get_brigade(f->next, bb, mode, block, readbytes);
117 apr_brigade_length (bb, 0, &length);
120 cf->bytes_in += length;
125 static apr_status_t logio_out_filter(ap_filter_t *f,
126 apr_bucket_brigade *bb) {
127 apr_bucket *b = APR_BRIGADE_LAST(bb);
129 /* End of data, make sure we flush */
130 if (APR_BUCKET_IS_EOS(b)) {
131 APR_BUCKET_INSERT_BEFORE(b,
132 apr_bucket_flush_create(f->c->bucket_alloc));
135 return ap_pass_brigade(f->next, bb);
142 static int logio_pre_conn(conn_rec *c, void *csd) {
143 logio_config_t *cf = apr_pcalloc(c->pool, sizeof(*cf));
145 ap_set_module_config(c->conn_config, &logio_module, cf);
147 ap_add_input_filter(logio_filter_name, NULL, NULL, c);
148 ap_add_output_filter(logio_filter_name, NULL, NULL, c);
153 static int logio_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
155 static APR_OPTIONAL_FN_TYPE(ap_register_log_handler) *log_pfn_register;
157 log_pfn_register = APR_RETRIEVE_OPTIONAL_FN(ap_register_log_handler);
159 if (log_pfn_register) {
160 log_pfn_register(p, "I", log_bytes_in, 0);
161 log_pfn_register(p, "O", log_bytes_out, 0);
167 static void register_hooks(apr_pool_t *p)
169 static const char *pre[] = { "mod_log_config.c", NULL };
171 ap_hook_pre_connection(logio_pre_conn, NULL, NULL, APR_HOOK_MIDDLE);
172 ap_hook_pre_config(logio_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
173 ap_hook_log_transaction(logio_transaction, pre, NULL, APR_HOOK_MIDDLE);
175 ap_register_input_filter(logio_filter_name, logio_in_filter, NULL,
176 AP_FTYPE_NETWORK - 1);
177 ap_register_output_filter(logio_filter_name, logio_out_filter, NULL,
178 AP_FTYPE_NETWORK - 1);
180 APR_REGISTER_OPTIONAL_FN(ap_logio_add_bytes_out);
183 module AP_MODULE_DECLARE_DATA logio_module =
185 STANDARD20_MODULE_STUFF,
186 NULL, /* create per-dir config */
187 NULL, /* merge per-dir config */
188 NULL, /* server config */
189 NULL, /* merge server config */
190 NULL, /* command apr_table_t */
191 register_hooks /* register hooks */