]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/FormatSquidNative.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / log / FormatSquidNative.cc
1 /*
2 * Copyright (C) 1996-2023 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 46 Access Log - Squid format */
10
11 #include "squid.h"
12 #include "AccessLogEntry.h"
13 #include "format/Quoting.h"
14 #include "format/Token.h"
15 #include "globals.h"
16 #include "HttpRequest.h"
17 #include "log/File.h"
18 #include "log/Formats.h"
19 #include "SquidConfig.h"
20
21 void
22 Log::Format::SquidNative(const AccessLogEntry::Pointer &al, Logfile * logfile)
23 {
24 char hierHost[MAX_IPSTRLEN];
25
26 const char *user = nullptr;
27
28 #if USE_AUTH
29 if (al->request && al->request->auth_user_request != nullptr)
30 user = ::Format::QuoteUrlEncodeUsername(al->request->auth_user_request->username());
31 #endif
32
33 if (!user)
34 user = ::Format::QuoteUrlEncodeUsername(al->getExtUser());
35
36 #if USE_OPENSSL
37 if (!user)
38 user = ::Format::QuoteUrlEncodeUsername(al->cache.ssluser);
39 #endif
40
41 if (!user)
42 user = ::Format::QuoteUrlEncodeUsername(al->getClientIdent());
43
44 if (user && !*user)
45 safe_free(user);
46
47 char clientip[MAX_IPSTRLEN];
48 al->getLogClientIp(clientip, MAX_IPSTRLEN);
49
50 const SBuf method(al->getLogMethod());
51
52 logfilePrintf(logfile, "%9ld.%03d %6ld %s %s/%03d %" PRId64 " " SQUIDSBUFPH " " SQUIDSBUFPH " %s %s%s/%s %s%s",
53 (long int) current_time.tv_sec,
54 (int) current_time.tv_usec / 1000,
55 tvToMsec(al->cache.trTime),
56 clientip,
57 al->cache.code.c_str(),
58 al->http.code,
59 al->http.clientReplySz.messageTotal(),
60 SQUIDSBUFPRINT(method),
61 SQUIDSBUFPRINT(al->url),
62 user ? user : dash_str,
63 al->hier.ping.timedout ? "TIMEOUT_" : "",
64 hier_code_str[al->hier.code],
65 al->hier.tcpServer != nullptr ? al->hier.tcpServer->remote.toStr(hierHost, sizeof(hierHost)) : "-",
66 al->http.content_type,
67 (Config.onoff.log_mime_hdrs?"":"\n"));
68
69 safe_free(user);
70
71 if (Config.onoff.log_mime_hdrs) {
72 char *ereq = ::Format::QuoteMimeBlob(al->headers.request);
73 MemBuf mb;
74 mb.init();
75 al->packReplyHeaders(mb);
76 auto erep = ::Format::QuoteMimeBlob(mb.content());
77 logfilePrintf(logfile, " [%s] [%s]\n", ereq, erep);
78 safe_free(ereq);
79 safe_free(erep);
80 }
81 }
82