]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_log.cc
file_open() is entirely blocking now -- there is no need for the
[thirdparty/squid.git] / src / store_log.cc
1
2 /*
3 * $Id: store_log.cc,v 1.7 1999/05/26 17:08:04 wessels Exp $
4 *
5 * DEBUG: section 20 Storage Manager Logging Functions
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * Duane Wessels and the University of California San Diego. Please
16 * see the COPYRIGHT file for full details. Squid incorporates
17 * software developed and/or copyrighted by other sources. Please see
18 * the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37
38 static char *storeLogTags[] =
39 {
40 "CREATE",
41 "SWAPIN",
42 "SWAPOUT",
43 "RELEASE"
44 };
45
46 static int storelog_fd = -1;
47
48 void
49 storeLog(int tag, const StoreEntry * e)
50 {
51 MemBuf mb;
52 MemObject *mem = e->mem_obj;
53 HttpReply *reply;
54 if (storelog_fd < 0)
55 return;
56 if (mem == NULL)
57 return;
58 if (mem->log_url == NULL) {
59 debug(20, 1) ("storeLog: NULL log_url for %s\n", mem->url);
60 storeMemObjectDump(mem);
61 mem->log_url = xstrdup(mem->url);
62 }
63 memBufDefInit(&mb);
64 reply = mem->reply;
65 memBufPrintf(&mb, "%9d.%03d %-7s %08X %4d %9d %9d %9d %s %d/%d %s %s\n",
66 (int) current_time.tv_sec,
67 (int) current_time.tv_usec / 1000,
68 storeLogTags[tag],
69 e->swap_file_number,
70 reply->sline.status,
71 (int) reply->date,
72 (int) reply->last_modified,
73 (int) reply->expires,
74 strBuf(reply->content_type) ? strBuf(reply->content_type) : "unknown",
75 reply->content_length,
76 (int) (mem->inmem_hi - mem->reply->hdr_sz),
77 RequestMethodStr[mem->method],
78 mem->log_url);
79 file_write_mbuf(storelog_fd, -1, mb, NULL, NULL);
80 }
81
82 void
83 storeLogRotate(void)
84 {
85 char *fname = NULL;
86 int i;
87 LOCAL_ARRAY(char, from, MAXPATHLEN);
88 LOCAL_ARRAY(char, to, MAXPATHLEN);
89 #ifdef S_ISREG
90 struct stat sb;
91 #endif
92
93 if (storelog_fd > -1) {
94 file_close(storelog_fd);
95 storelog_fd = -1;
96 }
97 if ((fname = Config.Log.store) == NULL)
98 return;
99 if (strcmp(fname, "none") == 0)
100 return;
101 #ifdef S_ISREG
102 if (stat(fname, &sb) == 0)
103 if (S_ISREG(sb.st_mode) == 0)
104 return;
105 #endif
106
107 debug(20, 1) ("storeLogRotate: Rotating.\n");
108
109 /* Rotate numbers 0 through N up one */
110 for (i = Config.Log.rotateNumber; i > 1;) {
111 i--;
112 snprintf(from, MAXPATHLEN, "%s.%d", fname, i - 1);
113 snprintf(to, MAXPATHLEN, "%s.%d", fname, i);
114 rename(from, to);
115 }
116 /* Rotate the current log to .0 */
117 if (Config.Log.rotateNumber > 0) {
118 snprintf(to, MAXPATHLEN, "%s.%d", fname, 0);
119 rename(fname, to);
120 }
121 storelog_fd = file_open(fname, O_WRONLY | O_CREAT);
122 if (storelog_fd < 0) {
123 debug(50, 0) ("storeLogRotate: %s: %s\n", fname, xstrerror());
124 debug(20, 1) ("Store logging disabled\n");
125 }
126 }
127
128 void
129 storeLogClose(void)
130 {
131 if (storelog_fd >= 0)
132 file_close(storelog_fd);
133 }
134
135 void
136 storeLogOpen(void)
137 {
138 if (strcmp(Config.Log.store, "none") == 0)
139 storelog_fd = -1;
140 else
141 storelog_fd = file_open(Config.Log.store, O_WRONLY | O_CREAT);
142 if (storelog_fd < 0)
143 debug(20, 1) ("Store logging disabled\n");
144 }