2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include <sys/types.h>
36 #include "shrpx_config.h"
37 #include "shrpx_log_config.h"
42 using namespace nghttp2;
46 #define LOG_ENABLED(SEVERITY) (ENABLE_LOG && shrpx::Log::log_enabled(SEVERITY))
48 #define LOG(SEVERITY) shrpx::Log(SEVERITY, __FILE__, __LINE__)
51 #define LLOG(SEVERITY, LISTEN) \
52 (shrpx::Log(SEVERITY, __FILE__, __LINE__) << "[LISTEN:" << LISTEN << "] ")
55 #define WLOG(SEVERITY, WORKER) \
56 (shrpx::Log(SEVERITY, __FILE__, __LINE__) << "[WORKER:" << WORKER << "] ")
59 #define CLOG(SEVERITY, CLIENT_HANDLER) \
60 (shrpx::Log(SEVERITY, __FILE__, __LINE__) \
61 << "[CLIENT_HANDLER:" << CLIENT_HANDLER << "] ")
64 #define ULOG(SEVERITY, UPSTREAM) \
65 (shrpx::Log(SEVERITY, __FILE__, __LINE__) << "[UPSTREAM:" << UPSTREAM \
70 #define DLOG(SEVERITY, DOWNSTREAM) \
71 (shrpx::Log(SEVERITY, __FILE__, __LINE__) \
72 << "[DOWNSTREAM:" << DOWNSTREAM << "] ")
74 // Downstream connection log
75 #define DCLOG(SEVERITY, DCONN) \
76 (shrpx::Log(SEVERITY, __FILE__, __LINE__) << "[DCONN:" << DCONN << "] ")
78 // Downstream HTTP2 session log
79 #define SSLOG(SEVERITY, HTTP2) \
80 (shrpx::Log(SEVERITY, __FILE__, __LINE__) << "[DHTTP2:" << HTTP2 << "] ")
82 // Memcached connection log
83 #define MCLOG(SEVERITY, MCONN) \
84 (shrpx::Log(SEVERITY, __FILE__, __LINE__) << "[MCONN:" << MCONN << "] ")
89 struct DownstreamAddr;
91 enum SeverityLevel { INFO, NOTICE, WARN, ERROR, FATAL };
93 using LogBuffer = std::array<uint8_t, 4_k>;
97 Log(int severity, const char *filename, int linenum);
99 Log &operator<<(const std::string &s);
100 Log &operator<<(const char *s);
101 Log &operator<<(const StringRef &s);
102 Log &operator<<(const ImmutableString &s);
103 Log &operator<<(short n) { return *this << static_cast<long long>(n); }
104 Log &operator<<(int n) { return *this << static_cast<long long>(n); }
105 Log &operator<<(long n) { return *this << static_cast<long long>(n); }
106 Log &operator<<(long long n);
107 Log &operator<<(unsigned short n) {
108 return *this << static_cast<unsigned long long>(n);
110 Log &operator<<(unsigned int n) {
111 return *this << static_cast<unsigned long long>(n);
113 Log &operator<<(unsigned long n) {
114 return *this << static_cast<unsigned long long>(n);
116 Log &operator<<(unsigned long long n);
117 Log &operator<<(float n) { return *this << static_cast<double>(n); }
118 Log &operator<<(double n);
119 Log &operator<<(long double n);
120 Log &operator<<(bool n);
121 Log &operator<<(const void *p);
122 template <typename T> Log &operator<<(const std::shared_ptr<T> &ptr) {
123 return *this << ptr.get();
125 Log &operator<<(void (*func)(Log &log)) {
129 template <typename InputIt> void write_seq(InputIt first, InputIt last) {
134 auto d = std::distance(first, last);
135 auto n = std::min(wleft(), static_cast<size_t>(d));
136 last_ = std::copy(first, first + n, last_);
140 template <typename T> void write_hex(T n) {
146 if (wleft() < 4 /* for "0x00" */) {
159 for (auto t = n; t; t >>= 8, ++nlen)
164 if (wleft() < 2 /* for "0x" */ + nlen) {
177 uint8_t b = n & 0xff;
178 *p-- = util::LOWER_XDIGITS[b & 0xf];
179 *p-- = util::LOWER_XDIGITS[b >> 4];
182 static void set_severity_level(int severity);
183 // Returns the severity level by |name|. Returns -1 if |name| is
185 static int get_severity_level_by_name(const StringRef &name);
186 static bool log_enabled(int severity) { return severity >= severity_thres_; }
193 void set_flags(int flags) { flags_ = flags; }
196 size_t rleft() { return last_ - begin_; }
197 size_t wleft() { return end_ - last_; }
198 void update_full() { full_ = last_ == end_; }
204 const char *filename_;
209 static int severity_thres_;
217 #define TTY_HTTP_HD (log_config()->errorlog_tty ? "\033[1;34m" : "")
218 #define TTY_RST (log_config()->errorlog_tty ? "\033[0m" : "")
220 enum class LogFragmentType {
237 SSL_CIPHER = TLS_CIPHER,
239 SSL_PROTOCOL = TLS_PROTOCOL,
241 SSL_SESSION_ID = TLS_SESSION_ID,
243 SSL_SESSION_REUSED = TLS_SESSION_REUSED,
245 TLS_CLIENT_FINGERPRINT_SHA1,
246 TLS_CLIENT_FINGERPRINT_SHA256,
247 TLS_CLIENT_ISSUER_NAME,
249 TLS_CLIENT_SUBJECT_NAME,
259 LogFragment(LogFragmentType type, StringRef value = StringRef::from_lit(""))
260 : type(type), value(std::move(value)) {}
261 LogFragmentType type;
266 Downstream *downstream;
267 StringRef remote_addr;
271 std::chrono::high_resolution_clock::time_point request_end_time;
272 StringRef remote_port;
273 uint16_t server_port;
277 void upstream_accesslog(const std::vector<LogFragment> &lf,
278 const LogSpec &lgsp);
280 int reopen_log_files(const LoggingConfig &loggingconf);
282 // Logs message when process whose pid is |pid| and exist status is
283 // |rstatus| exited. The |msg| is prepended to the log message.
284 void log_chld(pid_t pid, int rstatus, const char *msg);
286 void redirect_stderr_to_errorlog(const LoggingConfig &loggingconf);
288 // Makes internal copy of stderr (and possibly stdout in the future),
289 // which is then used as pointer to /dev/stderr or /proc/self/fd/2
290 void store_original_fds();
292 // Restores the original stderr that was stored with copy_original_fds
293 // Used just before execv
294 void restore_original_fds();
296 // Closes |fd| which was returned by open_log_file (see below)
297 // and sets it to -1. In the case that |fd| points to stdout or
298 // stderr, or is -1, the descriptor is not closed (but still set to -1).
299 void close_log_file(int &fd);
301 // Opens |path| with O_APPEND enabled. If file does not exist, it is
302 // created first. This function returns file descriptor referring the
303 // opened file if it succeeds, or -1.
304 int open_log_file(const char *path);
308 #endif // SHRPX_LOG_H