Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / shrpx_log.h
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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.
24  */
25 #ifndef SHRPX_LOG_H
26 #define SHRPX_LOG_H
27
28 #include "shrpx.h"
29
30 #include <sys/types.h>
31
32 #include <sstream>
33 #include <memory>
34 #include <vector>
35 #include <chrono>
36
37 #include "shrpx_log_config.h"
38
39 namespace shrpx {
40
41 class Downstream;
42
43 #define ENABLE_LOG 1
44
45 #define LOG_ENABLED(SEVERITY) (ENABLE_LOG && Log::log_enabled(SEVERITY))
46
47 #define LOG(SEVERITY) Log(SEVERITY, __FILE__, __LINE__)
48
49 // Listener log
50 #define LLOG(SEVERITY, LISTEN)                                                 \
51   (Log(SEVERITY, __FILE__, __LINE__) << "[LISTEN:" << LISTEN << "] ")
52
53 // Worker log
54 #define WLOG(SEVERITY, WORKER)                                                 \
55   (Log(SEVERITY, __FILE__, __LINE__) << "[WORKER:" << WORKER << "] ")
56
57 // ClientHandler log
58 #define CLOG(SEVERITY, CLIENT_HANDLER)                                         \
59   (Log(SEVERITY, __FILE__, __LINE__) << "[CLIENT_HANDLER:" << CLIENT_HANDLER   \
60                                      << "] ")
61
62 // Upstream log
63 #define ULOG(SEVERITY, UPSTREAM)                                               \
64   (Log(SEVERITY, __FILE__, __LINE__) << "[UPSTREAM:" << UPSTREAM << "] ")
65
66 // Downstream log
67 #define DLOG(SEVERITY, DOWNSTREAM)                                             \
68   (Log(SEVERITY, __FILE__, __LINE__) << "[DOWNSTREAM:" << DOWNSTREAM << "] ")
69
70 // Downstream connection log
71 #define DCLOG(SEVERITY, DCONN)                                                 \
72   (Log(SEVERITY, __FILE__, __LINE__) << "[DCONN:" << DCONN << "] ")
73
74 // Downstream HTTP2 session log
75 #define SSLOG(SEVERITY, HTTP2)                                                 \
76   (Log(SEVERITY, __FILE__, __LINE__) << "[DHTTP2:" << HTTP2 << "] ")
77
78 enum SeverityLevel { INFO, NOTICE, WARN, ERROR, FATAL };
79
80 class Log {
81 public:
82   Log(int severity, const char *filename, int linenum);
83   ~Log();
84   template <typename Type> Log &operator<<(Type s) {
85     stream_ << s;
86     return *this;
87   }
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_; }
91
92 private:
93   std::stringstream stream_;
94   const char *filename_;
95   int severity_;
96   int linenum_;
97   static int severity_thres_;
98 };
99
100 #define TTY_HTTP_HD (log_config()->errorlog_tty ? "\033[1;34m" : "")
101 #define TTY_RST (log_config()->errorlog_tty ? "\033[0m" : "")
102
103 enum LogFragmentType {
104   SHRPX_LOGF_NONE,
105   SHRPX_LOGF_LITERAL,
106   SHRPX_LOGF_REMOTE_ADDR,
107   SHRPX_LOGF_TIME_LOCAL,
108   SHRPX_LOGF_TIME_ISO8601,
109   SHRPX_LOGF_REQUEST,
110   SHRPX_LOGF_STATUS,
111   SHRPX_LOGF_BODY_BYTES_SENT,
112   SHRPX_LOGF_HTTP,
113   SHRPX_LOGF_REMOTE_PORT,
114   SHRPX_LOGF_SERVER_PORT,
115   SHRPX_LOGF_REQUEST_TIME,
116   SHRPX_LOGF_PID,
117   SHRPX_LOGF_ALPN,
118 };
119
120 struct LogFragment {
121   LogFragmentType type;
122   std::unique_ptr<char[]> value;
123 };
124
125 struct LogSpec {
126   Downstream *downstream;
127   const char *remote_addr;
128   const char *method;
129   const char *path;
130   const char *alpn;
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;
134   int major, minor;
135   unsigned int status;
136   int64_t body_bytes_sent;
137   const char *remote_port;
138   uint16_t server_port;
139   pid_t pid;
140 };
141
142 void upstream_accesslog(const std::vector<LogFragment> &lf,
143                         const LogSpec &lgsp);
144
145 int reopen_log_files();
146
147 void redirect_stderr_to_errorlog();
148
149 } // namespace shrpx
150
151 #endif // SHRPX_LOG_H