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