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>
37 #include "shrpx_log_config.h"
45 #define LOG_ENABLED(SEVERITY) (ENABLE_LOG && Log::log_enabled(SEVERITY))
47 #define LOG(SEVERITY) Log(SEVERITY, __FILE__, __LINE__)
50 #define LLOG(SEVERITY, LISTEN) \
51 (Log(SEVERITY, __FILE__, __LINE__) << "[LISTEN:" << LISTEN << "] ")
54 #define WLOG(SEVERITY, WORKER) \
55 (Log(SEVERITY, __FILE__, __LINE__) << "[WORKER:" << WORKER << "] ")
58 #define CLOG(SEVERITY, CLIENT_HANDLER) \
59 (Log(SEVERITY, __FILE__, __LINE__) << "[CLIENT_HANDLER:" << CLIENT_HANDLER \
63 #define ULOG(SEVERITY, UPSTREAM) \
64 (Log(SEVERITY, __FILE__, __LINE__) << "[UPSTREAM:" << UPSTREAM << "] ")
67 #define DLOG(SEVERITY, DOWNSTREAM) \
68 (Log(SEVERITY, __FILE__, __LINE__) << "[DOWNSTREAM:" << DOWNSTREAM << "] ")
70 // Downstream connection log
71 #define DCLOG(SEVERITY, DCONN) \
72 (Log(SEVERITY, __FILE__, __LINE__) << "[DCONN:" << DCONN << "] ")
74 // Downstream HTTP2 session log
75 #define SSLOG(SEVERITY, HTTP2) \
76 (Log(SEVERITY, __FILE__, __LINE__) << "[DHTTP2:" << HTTP2 << "] ")
78 enum SeverityLevel { INFO, NOTICE, WARN, ERROR, FATAL };
82 Log(int severity, const char *filename, int linenum);
84 template <typename Type> Log &operator<<(Type s) {
88 static void set_severity_level(int severity);
89 static int set_severity_level_by_name(const char *name);
90 static bool log_enabled(int severity) { return severity >= severity_thres_; }
93 std::stringstream stream_;
94 const char *filename_;
97 static int severity_thres_;
100 #define TTY_HTTP_HD (log_config()->errorlog_tty ? "\033[1;34m" : "")
101 #define TTY_RST (log_config()->errorlog_tty ? "\033[0m" : "")
103 enum LogFragmentType {
106 SHRPX_LOGF_REMOTE_ADDR,
107 SHRPX_LOGF_TIME_LOCAL,
108 SHRPX_LOGF_TIME_ISO8601,
111 SHRPX_LOGF_BODY_BYTES_SENT,
113 SHRPX_LOGF_REMOTE_PORT,
114 SHRPX_LOGF_SERVER_PORT,
115 SHRPX_LOGF_REQUEST_TIME,
121 LogFragmentType type;
122 std::unique_ptr<char[]> value;
126 Downstream *downstream;
127 const char *remote_addr;
131 std::chrono::system_clock::time_point time_now;
132 std::chrono::high_resolution_clock::time_point request_start_time;
133 std::chrono::high_resolution_clock::time_point request_end_time;
136 int64_t body_bytes_sent;
137 const char *remote_port;
138 uint16_t server_port;
142 void upstream_accesslog(const std::vector<LogFragment> &lf,
143 const LogSpec &lgsp);
145 int reopen_log_files();
147 void redirect_stderr_to_errorlog();
151 #endif // SHRPX_LOG_H