Skip to content

Instantly share code, notes, and snippets.

@unbit
Created July 16, 2014 16:04
Show Gist options
  • Save unbit/7970e4faa5f5edc108ba to your computer and use it in GitHub Desktop.
Save unbit/7970e4faa5f5edc108ba to your computer and use it in GitHub Desktop.
uWSGI log encoder example
#include <uwsgi.h>
static char *log_encoder_add_worker(struct uwsgi_log_encoder *ule, char *msg, size_t len, size_t *rlen) {
char *buf = NULL;
// 64 bytes for worker id should be enough, btw the buffer will be adapted automatically
struct uwsgi_buffer *ub = uwsgi_buffer_new(len + 64);
// add current pid
if (uwsgi_buffer_num64(ub, getpid())) goto end;
// add a space
if (uwsgi_buffer_byte(ub, ' ')) goto end;
// append the original message
if (uwsgi_buffer_append(ub, msg, len)) goto end;
// set the size of the new string
*rlen = ub->pos;
// set the new log line
buf = ub->buf;
// avoid the memory to be destroyed
ub->buf = NULL;
end:
uwsgi_buffer_destroy(ub);
return buf;
}
static void myencoder_register() {
uwsgi_register_log_encoder("myencoder", log_encoder_add_worker);
}
struct uwsgi_plugin myencoder_plugin = {
.name = "myencoder",
.on_load = myencoder_register,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment