bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / loggers / mod_logio.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * Written by Bojan Smojver <bojan@rexursive.com>:
19  *
20  * The argument to LogFormat and CustomLog is a string, which can include
21  * literal characters copied into the log files, and '%' directives as
22  * follows:
23  *
24  * %...I:  bytes received, including request and headers, cannot be zero
25  * %...O:  bytes sent, including headers, cannot be zero
26  *
27  */
28
29 #include "apr_strings.h"
30 #include "apr_lib.h"
31 #include "apr_hash.h"
32 #include "apr_optional.h"
33
34 #define APR_WANT_STRFUNC
35 #include "apr_want.h"
36
37 #include "ap_config.h"
38 #include "mod_log_config.h"
39 #include "httpd.h"
40 #include "http_core.h"
41 #include "http_config.h"
42 #include "http_connection.h"
43 #include "http_protocol.h"
44
45 module AP_MODULE_DECLARE_DATA logio_module;
46
47 static const char logio_filter_name[] = "LOG_INPUT_OUTPUT";
48
49 /*
50  * Logging of input and output config...
51  */
52
53 typedef struct logio_config_t {
54     apr_off_t bytes_in;
55     apr_off_t bytes_out;
56 } logio_config_t;
57
58 /*
59  * Optional function for the core to add to bytes_out
60  */
61
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);
64
65     cf->bytes_out += bytes;
66 }
67
68 /*
69  * Format items...
70  */
71
72 static const char *log_bytes_in(request_rec *r, char *a)
73 {
74     logio_config_t *cf = ap_get_module_config(r->connection->conn_config,
75                                               &logio_module);
76
77     return apr_off_t_toa(r->pool, cf->bytes_in);
78 }
79
80 static const char *log_bytes_out(request_rec *r, char *a)
81 {
82     logio_config_t *cf = ap_get_module_config(r->connection->conn_config,
83                                               &logio_module);
84
85     return apr_off_t_toa(r->pool, cf->bytes_out);
86 }
87
88 /*
89  * Reset counters after logging...
90  */
91
92 static int logio_transaction(request_rec *r)
93 {
94     logio_config_t *cf = ap_get_module_config(r->connection->conn_config,
95                                               &logio_module);
96
97     cf->bytes_in = cf->bytes_out = 0;
98
99     return OK;
100 }
101
102 /*
103  * Logging of input and output filters...
104  */
105
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) {
111     apr_off_t length;
112     apr_status_t status;
113     logio_config_t *cf = ap_get_module_config(f->c->conn_config, &logio_module);
114
115     status = ap_get_brigade(f->next, bb, mode, block, readbytes);
116
117     apr_brigade_length (bb, 0, &length);
118
119     if (length > 0)
120         cf->bytes_in += length;
121
122     return status;
123 }
124
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);
128
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));
133     }
134
135     return ap_pass_brigade(f->next, bb);
136 }
137
138 /*
139  * The hooks...
140  */
141
142 static int logio_pre_conn(conn_rec *c, void *csd) {
143     logio_config_t *cf = apr_pcalloc(c->pool, sizeof(*cf));
144
145     ap_set_module_config(c->conn_config, &logio_module, cf);
146
147     ap_add_input_filter(logio_filter_name, NULL, NULL, c);
148     ap_add_output_filter(logio_filter_name, NULL, NULL, c);
149
150     return OK;
151 }
152
153 static int logio_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
154 {
155     static APR_OPTIONAL_FN_TYPE(ap_register_log_handler) *log_pfn_register;
156     
157     log_pfn_register = APR_RETRIEVE_OPTIONAL_FN(ap_register_log_handler);
158
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);
162     }
163
164     return OK;
165 }
166
167 static void register_hooks(apr_pool_t *p)
168 {
169     static const char *pre[] = { "mod_log_config.c", NULL };
170
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);
174
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);
179
180     APR_REGISTER_OPTIONAL_FN(ap_logio_add_bytes_out);
181 }
182
183 module AP_MODULE_DECLARE_DATA logio_module =
184 {
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 */
192 };