From f96237c9fca9067fb2a15e0e6f34a0fdb509b5b0 Mon Sep 17 00:00:00 2001 From: Igor Sysoev Date: Thu, 4 Aug 2016 16:27:38 +0300 Subject: [PATCH] nxt_trace files have been missed in the previous commit. --- nxt/nxt_trace.c | 57 +++++++++++++++++++++++++++++++++++++++++ nxt/nxt_trace.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 nxt/nxt_trace.c create mode 100644 nxt/nxt_trace.h diff --git a/nxt/nxt_trace.c b/nxt/nxt_trace.c new file mode 100644 index 00000000..f442ae83 --- /dev/null +++ b/nxt/nxt_trace.c @@ -0,0 +1,57 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include +#include +#include +#include +#include +#include + + +static u_char * +nxt_last_handler(nxt_trace_t *trace, nxt_trace_data_t *td, u_char *start) +{ + int n; + ssize_t size; + + size = td->end - start; + n = vsnprintf((char *) start, size, td->fmt, td->args); + + if (n < size) { + start += n; + } + + return start; +} + + +void +nxt_trace_handler(nxt_trace_t *trace, uint32_t level, const char *fmt, ...) +{ + u_char *start; + nxt_trace_t last; + nxt_trace_data_t td; + + td.level = level; + td.fmt = fmt; + + va_start(td.args, fmt); + + start = alloca(trace->size); + td.end = start + trace->size; + + last.handler = nxt_last_handler; + trace->next = &last; + + while (trace->prev != NULL) { + trace = trace->prev; + } + + (void) trace->handler(trace, &td, start); + + va_end(td.args); +} diff --git a/nxt/nxt_trace.h b/nxt/nxt_trace.h new file mode 100644 index 00000000..26019785 --- /dev/null +++ b/nxt/nxt_trace.h @@ -0,0 +1,68 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#ifndef _NXT_TRACE_H_INCLUDED_ +#define _NXT_TRACE_H_INCLUDED_ + + +typedef enum { + NXT_LEVEL_CRIT = 0, + NXT_LEVEL_ERROR, + NXT_LEVEL_WARN, + NXT_LEVEL_INFO, + NXT_LEVEL_TRACE, +} nxt_trace_level_t; + + +typedef struct { + uint32_t level; + u_char *end; + const char *fmt; + va_list args; +} nxt_trace_data_t; + + +typedef struct nxt_trace_s nxt_trace_t; + +typedef u_char *(*nxt_trace_handler_t)(nxt_trace_t *trace, nxt_trace_data_t *td, + u_char *start); + +struct nxt_trace_s { + uint32_t level; + uint32_t size; + nxt_trace_handler_t handler; + void *data; + nxt_trace_t *prev; + nxt_trace_t *next; +}; + + +#define nxt_alert(_trace, _level, ...) \ + do { \ + nxt_trace_t *_trace_ = _trace; \ + uint32_t _level_ = _level; \ + \ + if (nxt_slow_path(_trace_->level >= _level_)) { \ + nxt_trace_handler(_trace_, _level_, __VA_ARGS__); \ + } \ + } while (0) + + +#define nxt_trace(_trace, ...) \ + do { \ + nxt_trace_t *_trace_ = _trace; \ + \ + if (nxt_slow_path(_trace_->level == NXT_LEVEL_TRACE)) { \ + nxt_trace_handler(_trace_, NXT_LEVEL_TRACE, __VA_ARGS__); \ + } \ + } while (0) + + +NXT_EXPORT void nxt_trace_handler(nxt_trace_t *trace, uint32_t level, + const char *fmt, ...); + + +#endif /* _NXT_TRACE_H_INCLUDED_ */ -- 2.47.3