Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Logs are now written in tnetstring format.

  • Loading branch information...
commit 137dbcd5d4e0915a32c95ca40564fb734b93f9d7 1 parent ed74c18
Guillermo O. Freschi Tordek authored
Showing with 70 additions and 13 deletions.
  1. +35 −13 src/log.c
  2. +28 −0 src/tnetstrings.c
  3. +7 −0 src/tnetstrings.h
48 src/log.c
View
@@ -37,6 +37,7 @@
#include "request.h"
#include "headers.h"
#include "setting.h"
+#include "tnetstrings.h"
#include <stdio.h>
#include <zmq.h>
#include <pthread.h>
@@ -196,12 +197,12 @@ int Log_poison_workers()
return -1;
}
-static inline bstring make_log_message(Request *req, const char *remote_addr,
+static inline bstring make_log_message(Request *req, const char *remote_addr,
int remote_port, int status, int size)
{
bstring request_method = NULL;
- if(Request_is_json(req)) {
+ if (Request_is_json(req)) {
request_method = &JSON_METHOD;
} else if (Request_is_xml(req)) {
request_method = &XML_METHOD;
@@ -209,19 +210,40 @@ static inline bstring make_log_message(Request *req, const char *remote_addr,
request_method = req->request_method;
}
- bstring log_data = bformat("%s,%.*s,%d,%d,%s,%s,%s,%d,%d\n",
- bdata(req->target_host->name),
- IPADDR_SIZE,
- remote_addr,
- remote_port,
- (int)time(NULL),
- bdata(request_method),
- bdata(Request_path(req)),
- Request_is_json(req) ? "" : bdata(req->version),
- status,
- size);
+ tns_outbuf outbuf = {.buffer = NULL};
+ bstring b_temp;
+
+ check(tns_render_log_start(&outbuf), "Could not initialize buffer");
+
+ tns_render_number_prepend(&outbuf, size);
+ tns_render_number_prepend(&outbuf, status);
+
+ b_temp = bfromcstr(Request_is_json(req) ? "" : bdata(req->version));
+ tns_render_string_prepend(&outbuf, b_temp);
+ bdestroy(b_temp);
+
+ tns_render_string_prepend(&outbuf, Request_path(req));
+ tns_render_string_prepend(&outbuf, request_method);
+ tns_render_number_prepend(&outbuf, (int) time(NULL));
+ tns_render_number_prepend(&outbuf, remote_port);
+
+ b_temp = bfromcstr(remote_addr);
+ tns_render_string_prepend(&outbuf, b_temp);
+ bdestroy(b_temp);
+
+ tns_render_string_prepend(&outbuf, req->target_host->name);
+
+ tns_render_log_end(&outbuf);
+
+ // log_data now owns the outbuf buffer
+ bstring log_data = tns_outbuf_to_bstring(&outbuf);
+ bconchar(log_data, '\n');
return log_data;
+
+error:
+
+ return NULL;
}
static void free_log_msg(void *data, void *hint)
28 src/tnetstrings.c
View
@@ -290,6 +290,18 @@ void tns_render_hash_pair(tns_outbuf *outbuf, bstring key, bstring value)
tns_render_value(&val, outbuf);
}
+void tns_render_number_prepend(tns_outbuf *outbuf, long value)
+{
+ tns_value_t val = {.type = tns_tag_number, .value.number = value};
+ tns_render_value(&val, outbuf);
+}
+
+void tns_render_string_prepend(tns_outbuf *outbuf, bstring value)
+{
+ tns_value_t val = {.type = tns_tag_string, .value.string = value};
+ tns_render_value(&val, outbuf);
+}
+
int tns_render_request_start(tns_outbuf *outbuf)
{
check(tns_outbuf_init(outbuf) != -1, "Failed to init buffer.");
@@ -321,6 +333,22 @@ int tns_render_request_end(tns_outbuf *outbuf, int header_start, bstring uuid, i
return -1;
}
+int tns_render_log_start(tns_outbuf *outbuf)
+{
+ check(tns_outbuf_init(outbuf) != -1, "Failed to init buffer.");
+
+ check(tns_outbuf_putc(outbuf, ']') != -1, "Failed ending request.");
+
+ return outbuf->used_size;
+error:
+ return -1;
+}
+
+void tns_render_log_end(tns_outbuf *outbuf)
+{
+ tns_outbuf_clamp(outbuf, 1);
+}
+
int tns_render_request_headers(tns_outbuf *outbuf, hash_t *headers)
{
hscan_t scan;
7 src/tnetstrings.h
View
@@ -88,6 +88,13 @@ int tns_render_request_start(tns_outbuf *outbuf);
int tns_render_request_end(tns_outbuf *outbuf, int header_start, bstring uuid, int id, bstring path);
+int tns_render_log_start(tns_outbuf *outbuf);
+
+void tns_render_log_end(tns_outbuf *outbuf);
+
+void tns_render_string_prepend(tns_outbuf *outbuf, bstring value);
+void tns_render_number_prepend(tns_outbuf *outbuf, long value);
+
tns_value_t *tns_standard_table(bstring header_data, tns_value_t *rows);
#define tns_get_type(T) (((tns_value_t *)(T)) == NULL ? tns_tag_invalid : ((tns_value_t *)(T))->type)
Please sign in to comment.
Something went wrong with that request. Please try again.