]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/TcpLogger.h
Source Format Enforcement (#963)
[thirdparty/squid.git] / src / log / TcpLogger.h
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 #ifndef _SQUID_SRC_LOG_TCPLOGGER_H
10 #define _SQUID_SRC_LOG_TCPLOGGER_H
11
12 #include "base/AsyncJob.h"
13 #include "base/JobWait.h"
14 #include "comm/forward.h"
15 #include "ip/Address.h"
16
17 #include <list>
18
19 class MemBlob;
20 typedef RefCount<MemBlob> MemBlobPointer;
21
22 namespace Log
23 {
24
25 /**
26 * Sends log records to a remote TCP logger at the configured IP:port address.
27 * Handles loss of connectivity, record buffering, and buffer overflows.
28 */
29 class TcpLogger : public AsyncJob
30 {
31 CBDATA_CLASS(TcpLogger);
32
33 public:
34 typedef CbcPointer<TcpLogger> Pointer;
35
36 /* Logfile API */
37 static int Open(Logfile *lf, const char *path, size_t bufSz, int fatalFlag);
38
39 protected:
40 TcpLogger(size_t, bool, Ip::Address);
41 virtual ~TcpLogger();
42
43 /// Called when Squid is reconfiguring (or exiting) to give us a chance to
44 /// flush remaining buffers and end this job w/o loss of data. No new log
45 /// records are expected. Must be used as (or inside) an async job call and
46 /// will result in [eventual] job termination.
47 void endGracefully();
48
49 /// buffers record and possibly writes it to the remote logger
50 void logRecord(const char *buf, size_t len);
51
52 /// write all currently buffered records ASAP
53 void flush();
54
55 /* AsyncJob API */
56 virtual void start();
57 virtual bool doneAll() const;
58 virtual void swanSong();
59
60 private:
61 /* Logfile API. Map c-style Logfile calls to TcpLogger method calls. */
62 static void Flush(Logfile *lf);
63 static void WriteLine(Logfile *lf, const char *buf, size_t len);
64 static void StartLine(Logfile *lf);
65 static void EndLine(Logfile *lf);
66 static void Rotate(Logfile *lf, const int16_t);
67 static void Close(Logfile *lf);
68
69 static TcpLogger *StillLogging(Logfile *lf);
70
71 static void DelayedReconnect(void *data);
72 void delayedReconnect();
73
74 bool canFit(const size_t len) const;
75 void appendRecord(const char *buf, size_t len);
76 void appendChunk(const char *chunk, const size_t len);
77 void writeIfNeeded();
78 void writeIfPossible();
79 void doConnect();
80 void disconnect();
81
82 /* comm callbacks */
83 void connectDone(const CommConnectCbParams &conn);
84 void writeDone(const CommIoCbParams &io);
85 void handleClosure(const CommCloseCbParams &io);
86
87 static const size_t IoBufSize; ///< fixed I/O buffer size
88 static const size_t BufferCapacityMin; ///< minimum bufferCapacity value
89
90 /// Whether this job must kill Squid on the first unrecoverable error.
91 /// Note that we may be able to recover from a failure to connect, but we
92 /// cannot recover from forgetting (dropping) a record while connecting.
93 bool dieOnError;
94
95 std::list<MemBlobPointer> buffers; ///< I/O buffers
96 size_t bufferCapacity; ///< bufferedSize limit
97 size_t bufferedSize; ///< number of log record bytes stored in RAM now
98 size_t flushDebt; ///< how many record bytes we still need to write ASAP
99
100 bool quitOnEmpty; ///< whether this job should quit when buffers are empty
101 bool reconnectScheduled; ///< we are sleeping before the next connection attempt
102 bool writeScheduled; ///< we are waiting for the latest write() results
103
104 Comm::ConnectionPointer conn; ///< opened connection to the remote logger
105 Ip::Address remote; ///< where the remote logger expects our records
106 AsyncCall::Pointer closer; ///< handles unexpected/external conn closures
107
108 /// waits for a connection to the remote logger to be established/opened
109 JobWait<Comm::ConnOpener> connWait;
110
111 uint64_t connectFailures; ///< number of sequential connection failures
112 uint64_t drops; ///< number of records dropped during the current outage
113 };
114
115 } // namespace Log
116
117 #endif /* _SQUID_SRC_LOG_TCPLOGGER_H */
118