Skip to content

Commit 10dc595

Browse files
authored
Merge pull request #297 from tritonuas/feat/modify-http-handler-logging
added high density option for logging http responses
2 parents 6be9ec1 + 166ff08 commit 10dc595

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

include/network/gcs_macros.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,22 @@
121121
response.set_content(body, mime); \
122122
response.status = response_code
123123

124+
// A special case of the 5 param log function for high density HTTP responses
125+
#define LOG_RESPONSE_5_HD(LOG_LEVEL, msg, response_code, body, mime) \
126+
if (msg != body) LOG_F(LOG_LEVEL, "%s", msg); \
127+
LOG_F(LOG_LEVEL, "HTTP %d: %s", response_code, HTTP_STATUS_TO_STRING(response_code)); \
128+
LOG_F(LOG_LEVEL, "Response body: [%zu bytes] (truncated for brevity)", strlen(body)); \
129+
response.set_content(body, mime); \
130+
response.status = response_code
131+
124132
// Essentially a special case of the 5 param log function, where
125133
// the message body is in plaintext and is also what you want to log
126134
#define LOG_RESPONSE_3(LOG_LEVEL, msg, response_code) \
127135
LOG_RESPONSE_5(LOG_LEVEL, msg, response_code, msg, mime::plaintext)
128136

137+
// A special case of the 3 param log function for high density HTTP responses
138+
#define LOG_RESPONSE_3_HD(LOG_LEVEL, msg, response_code) \
139+
LOG_RESPONSE_5_HD(LOG_LEVEL, msg, response_code, msg, mime::plaintext)
129140

130141
// Logs important information about the HTTP request. There are two different
131142
// ways to call: a 3 parameter version and a 5 parameter version.
@@ -139,4 +150,16 @@
139150
#define LOG_RESPONSE(...) \
140151
GET_MACRO_5(__VA_ARGS__, LOG_RESPONSE_5, _4, LOG_RESPONSE_3)(__VA_ARGS__)
141152

153+
// High-density version of LOG_RESPONSE that truncates large response bodies
154+
// in logs while still sending the full response to the client.
155+
// Usage is identical to LOG_RESPONSE but with _HD suffix:
156+
//
157+
// LOG_RESPONSE_HD(LOG_LEVEL, msg, response_code)
158+
// High-density version of 3-parameter LOG_RESPONSE
159+
//
160+
// LOG_RESPONSE_HD(LOG_LEVEL, msg, response_code, body, mime)
161+
// High-density version of 5-parameter LOG_RESPONSE
162+
#define LOG_RESPONSE_HD(...) \
163+
GET_MACRO_5(__VA_ARGS__, LOG_RESPONSE_5_HD, _4, LOG_RESPONSE_3_HD)(__VA_ARGS__)
164+
142165
#endif // INCLUDE_NETWORK_GCS_MACROS_HPP_

0 commit comments

Comments
 (0)