xref: /unit/src/nxt_log.c (revision 1817:5bf7ec778c86)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 
9 
10 nxt_uint_t  nxt_debug;
11 nxt_uint_t  nxt_trace;
12 
13 
14 nxt_log_t   nxt_main_log = {
15     NXT_LOG_INFO,
16     0,
17     nxt_log_handler,
18     NULL,
19     NULL
20 };
21 
22 
23 nxt_str_t  nxt_log_levels[6] = {
24     nxt_string("alert"),
25     nxt_string("error"),
26     nxt_string("warn"),
27     nxt_string("notice"),
28     nxt_string("info"),
29     nxt_string("debug")
30 };
31 
32 
33 static const u_char  *nxt_log_prefix;
34 
35 
36 void
nxt_log_start(const char * prefix)37 nxt_log_start(const char *prefix)
38 {
39     if (prefix != NULL && *prefix != '\0') {
40         nxt_log_prefix = (u_char *) prefix;
41     }
42 }
43 
44 
45 /* STUB */
46 nxt_log_t *
nxt_log_set_ctx(nxt_log_t * log,nxt_log_ctx_handler_t handler,void * ctx)47 nxt_log_set_ctx(nxt_log_t *log, nxt_log_ctx_handler_t handler, void *ctx)
48 {
49     nxt_log_t     *old;
50     nxt_thread_t  *thr;
51 
52     thr = nxt_thread();
53     old = thr->log;
54 
55     log->level = old->level;
56     log->handler = old->handler;
57     log->ctx_handler = handler;
58     log->ctx = ctx;
59 
60     thr->log = log;
61 
62     return old;
63 }
64 
65 
66 void nxt_cdecl
nxt_log_handler(nxt_uint_t level,nxt_log_t * log,const char * fmt,...)67 nxt_log_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...)
68 {
69     u_char   *p, *end;
70 #if 0
71     u_char   *syslogmsg;
72 #endif
73     va_list  args;
74     u_char   msg[NXT_MAX_ERROR_STR];
75 
76     p = msg;
77     end = msg + NXT_MAX_ERROR_STR;
78 
79     if (nxt_log_prefix != NULL) {
80         p = nxt_cpystrn(p, nxt_log_prefix, end - p);
81         *p++ = ':';
82         *p++ = ' ';
83     }
84 
85 #if 0
86     syslogmsg = p;
87 #endif
88 
89     p = nxt_sprintf(p, end, (log->ident != 0) ? "[%V] *%D " : "[%V] ",
90                     &nxt_log_levels[level], log->ident);
91 
92     va_start(args, fmt);
93     p = nxt_vsprintf(p, end, fmt, args);
94     va_end(args);
95 
96     if (level != NXT_LOG_DEBUG && log->ctx_handler != NULL) {
97         p = log->ctx_handler(log->ctx, p, end);
98     }
99 
100     if (p > end - nxt_length("\n")) {
101         p = end - nxt_length("\n");
102     }
103 
104     *p++ = '\n';
105 
106     (void) nxt_write_console(nxt_stderr, msg, p - msg);
107 
108 #if 0
109     if (level == NXT_LOG_ALERT) {
110         *(p - nxt_length("\n")) = '\0';
111 
112         /*
113          * Syslog LOG_ALERT level is enough, because
114          * LOG_EMERG level broadcast a message to all users.
115          */
116         nxt_write_syslog(LOG_ALERT, syslogmsg);
117     }
118 #endif
119 }
120