]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_log.cc
- Separated raw HTTP headers from their "compiled" values. Squid is now
[thirdparty/squid.git] / src / store_log.cc
1
2 #include "squid.h"
3
4 static char *storeLogTags[] =
5 {
6 "CREATE",
7 "SWAPIN",
8 "SWAPOUT",
9 "RELEASE"
10 };
11
12 static int storelog_fd = -1;
13
14 void
15 storeLog(int tag, const StoreEntry * e)
16 {
17 LOCAL_ARRAY(char, logmsg, MAX_URL << 1);
18 MemObject *mem = e->mem_obj;
19 HttpReply *reply;
20 if (storelog_fd < 0)
21 return;
22 if (mem == NULL)
23 return;
24 if (mem->log_url == NULL) {
25 debug(20, 1) ("storeLog: NULL log_url for %s\n", mem->url);
26 storeMemObjectDump(mem);
27 mem->log_url = xstrdup(mem->url);
28 }
29 reply = mem->reply;
30 snprintf(logmsg, MAX_URL << 1, "%9d.%03d %-7s %08X %4d %9d %9d %9d %s %d/%d %s %s\n",
31 (int) current_time.tv_sec,
32 (int) current_time.tv_usec / 1000,
33 storeLogTags[tag],
34 e->swap_file_number,
35 reply->sline.status,
36 (int) reply->date,
37 (int) reply->last_modified,
38 (int) reply->expires,
39 strBuf(reply->content_type) ? strBuf(reply->content_type) : "unknown",
40 reply->content_length,
41 (int) (mem->inmem_hi - mem->reply->hdr_sz),
42 RequestMethodStr[mem->method],
43 mem->log_url);
44 file_write(storelog_fd,
45 -1,
46 xstrdup(logmsg),
47 strlen(logmsg),
48 NULL,
49 NULL,
50 xfree);
51 }
52
53 void
54 storeLogRotate(void)
55 {
56 char *fname = NULL;
57 int i;
58 LOCAL_ARRAY(char, from, MAXPATHLEN);
59 LOCAL_ARRAY(char, to, MAXPATHLEN);
60 #ifdef S_ISREG
61 struct stat sb;
62 #endif
63
64 if (storelog_fd > -1) {
65 file_close(storelog_fd);
66 storelog_fd = -1;
67 }
68 if ((fname = Config.Log.store) == NULL)
69 return;
70 if (strcmp(fname, "none") == 0)
71 return;
72 #ifdef S_ISREG
73 if (stat(fname, &sb) == 0)
74 if (S_ISREG(sb.st_mode) == 0)
75 return;
76 #endif
77
78 debug(20, 1) ("storeLogRotate: Rotating.\n");
79
80 /* Rotate numbers 0 through N up one */
81 for (i = Config.Log.rotateNumber; i > 1;) {
82 i--;
83 snprintf(from, MAXPATHLEN, "%s.%d", fname, i - 1);
84 snprintf(to, MAXPATHLEN, "%s.%d", fname, i);
85 rename(from, to);
86 }
87 /* Rotate the current log to .0 */
88 if (Config.Log.rotateNumber > 0) {
89 snprintf(to, MAXPATHLEN, "%s.%d", fname, 0);
90 rename(fname, to);
91 }
92 storelog_fd = file_open(fname, O_WRONLY | O_CREAT, NULL, NULL, NULL);
93 if (storelog_fd < 0) {
94 debug(50, 0) ("storeLogRotate: %s: %s\n", fname, xstrerror());
95 debug(20, 1) ("Store logging disabled\n");
96 }
97 }
98
99 void
100 storeLogClose(void)
101 {
102 if (storelog_fd >= 0)
103 file_close(storelog_fd);
104 }
105
106 void
107 storeLogOpen(void)
108 {
109 if (strcmp(Config.Log.store, "none") == 0)
110 storelog_fd = -1;
111 else
112 storelog_fd = file_open(Config.Log.store,
113 O_WRONLY | O_CREAT,
114 NULL,
115 NULL,
116 NULL);
117 if (storelog_fd < 0)
118 debug(20, 1) ("Store logging disabled\n");
119 }