]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_log.cc
SourceLayout: Move time related tools to time/libtime.la (#1001)
[thirdparty/squid.git] / src / store_log.cc
1 /*
2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 20 Storage Manager Logging Functions */
10
11 #include "squid.h"
12 #include "debug/Messages.h"
13 #include "format/Token.h"
14 #include "HttpReply.h"
15 #include "log/File.h"
16 #include "MemObject.h"
17 #include "mgr/Registration.h"
18 #include "SquidConfig.h"
19 #include "Store.h"
20 #include "store_log.h"
21
22 static const char *storeLogTags[] = {
23 "CREATE",
24 "SWAPIN",
25 "SWAPOUT",
26 "RELEASE",
27 "SO_FAIL",
28 };
29
30 static int storeLogTagsCounts[STORE_LOG_SWAPOUTFAIL+1];
31 static OBJH storeLogTagsHist;
32
33 static Logfile *storelog = NULL;
34
35 static String str_unknown;
36
37 void
38 storeLog(int tag, const StoreEntry * e)
39 {
40 MemObject *mem = e->mem_obj;
41 HttpReply const *reply;
42
43 if (str_unknown.size()==0)
44 str_unknown="unknown"; //hack. Delay initialization as string doesn't support global variables..
45
46 if (NULL == storelog)
47 return;
48
49 ++storeLogTagsCounts[tag];
50 if (mem != NULL) {
51 reply = &mem->freshestReply();
52 /*
53 * XXX Ok, where should we print the dir number here?
54 * Because if we print it before the swap file number, it'll break
55 * the existing log format.
56 */
57
58 String ctype=(reply->content_type.size() ? reply->content_type.termedBuf() : str_unknown);
59
60 // mem_obj may still lack logging details; especially in RELEASE cases
61 const char *logUri = mem->hasUris() ? mem->logUri() : "?";
62
63 logfileLineStart(storelog);
64 logfilePrintf(storelog, "%9d.%03d %-7s %02d %08X %s %4d %9d %9d %9d " SQUIDSTRINGPH " %" PRId64 "/%" PRId64 " " SQUIDSBUFPH " %s\n",
65 (int) current_time.tv_sec,
66 (int) current_time.tv_usec / 1000,
67 storeLogTags[tag],
68 e->swap_dirn,
69 e->swap_filen,
70 e->getMD5Text(),
71 reply->sline.status(),
72 (int) reply->date,
73 (int) reply->last_modified,
74 (int) reply->expires,
75 SQUIDSTRINGPRINT(ctype),
76 reply->content_length,
77 e->contentLen(),
78 SQUIDSBUFPRINT(mem->method.image()),
79 logUri);
80 logfileLineEnd(storelog);
81 } else {
82 /* no mem object. Most RELEASE cases */
83 logfileLineStart(storelog);
84 logfilePrintf(storelog, "%9d.%03d %-7s %02d %08X %s ? ? ? ? ?/? ?/? ? ?\n",
85 (int) current_time.tv_sec,
86 (int) current_time.tv_usec / 1000,
87 storeLogTags[tag],
88 e->swap_dirn,
89 e->swap_filen,
90 e->getMD5Text());
91 logfileLineEnd(storelog);
92 }
93 }
94
95 void
96 storeLogRotate(void)
97 {
98 if (NULL == storelog)
99 return;
100
101 logfileRotate(storelog, Config.Log.rotateNumber);
102 }
103
104 void
105 storeLogClose(void)
106 {
107 if (NULL == storelog)
108 return;
109
110 logfileClose(storelog);
111
112 storelog = NULL;
113 }
114
115 static void
116 storeLogRegisterWithCacheManager(void)
117 {
118 Mgr::RegisterAction("store_log_tags", "Histogram of store.log tags",
119 storeLogTagsHist, 0, 1);
120 }
121
122 void
123 storeLogOpen(void)
124 {
125 storeLogRegisterWithCacheManager();
126
127 if (Config.Log.store == NULL || strcmp(Config.Log.store, "none") == 0) {
128 debugs(20, Important(42), "Store logging disabled");
129 return;
130 }
131
132 storelog = logfileOpen(Config.Log.store, 0, 1);
133 }
134
135 void
136 storeLogTagsHist(StoreEntry *e)
137 {
138 int tag;
139 for (tag = 0; tag <= STORE_LOG_SWAPOUTFAIL; ++tag) {
140 storeAppendPrintf(e, "%s %d\n",
141 storeLogTags[tag],
142 storeLogTagsCounts[tag]);
143 }
144 }
145