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