diff options
author | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
commit | 16cbf3c076a0aca6d47adaf3f719493674cf2363 (patch) | |
tree | e6530480020f62a2bdbf249988ec3e2a751d3927 /src/nxt_log.c | |
download | unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.gz unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.bz2 |
Initial version.
Diffstat (limited to 'src/nxt_log.c')
-rw-r--r-- | src/nxt_log.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/nxt_log.c b/src/nxt_log.c new file mode 100644 index 00000000..95591f18 --- /dev/null +++ b/src/nxt_log.c @@ -0,0 +1,112 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +nxt_uint_t nxt_debug; + +nxt_log_t nxt_main_log = { + NXT_LOG_INFO, + 0, + nxt_log_handler, + NULL, + NULL +}; + + +nxt_str_t nxt_log_levels[8] = { + nxt_string("emerg"), + nxt_string("alert"), + nxt_string("crit"), + nxt_string("error"), + nxt_string("warn"), + nxt_string("notice"), + nxt_string("info"), + nxt_string("debug") +}; + + +static const u_char *nxt_log_prefix; + + +void +nxt_log_start(const char *prefix) +{ + if (prefix != NULL && *prefix != '\0') { + nxt_log_prefix = (u_char *) prefix; + } +} + + +/* STUB */ +nxt_log_t * +nxt_log_set_ctx(nxt_log_t *log, nxt_log_ctx_handler_t handler, void *ctx) +{ + nxt_log_t *old; + nxt_thread_t *thr; + + thr = nxt_thread(); + old = thr->log; + + log->level = old->level; + log->handler = old->handler; + log->ctx_handler = handler; + log->ctx = ctx; + + thr->log = log; + + return old; +} + + +void nxt_cdecl +nxt_log_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...) +{ + u_char *p, *syslogmsg, *end; + va_list args; + u_char msg[NXT_MAX_ERROR_STR]; + + p = msg; + end = msg + NXT_MAX_ERROR_STR; + + if (nxt_log_prefix != NULL) { + p = nxt_cpystrn(p, nxt_log_prefix, end - p); + *p++ = ':'; + *p++ = ' '; + } + + syslogmsg = p; + + p = nxt_sprintf(p, end, (log->ident != 0) ? "[%V] *%D " : "[%V] ", + &nxt_log_levels[level], log->ident); + + va_start(args, fmt); + p = nxt_vsprintf(p, end, fmt, args); + va_end(args); + + if (level != NXT_LOG_DEBUG && log->ctx_handler != NULL) { + p = log->ctx_handler(log->ctx, p, end); + } + + if (p > end - NXT_LINEFEED_SIZE) { + p = end - NXT_LINEFEED_SIZE; + } + + nxt_linefeed(p); + + (void) nxt_write_console(nxt_stderr, msg, p - msg); + + if (level <= NXT_LOG_ALERT) { + *(p - NXT_LINEFEED_SIZE) = '\0'; + + /* + * Syslog LOG_ALERT level is enough, because + * LOG_EMERG level broadcast a message to all users. + */ + nxt_write_syslog(LOG_ALERT, syslogmsg); + } +} |