xref: /unit/src/nxt_app_log.c (revision 704)
10Sigor@sysoev.ru 
20Sigor@sysoev.ru /*
30Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
40Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
50Sigor@sysoev.ru  */
60Sigor@sysoev.ru 
70Sigor@sysoev.ru #include <nxt_main.h>
820Sigor@sysoev.ru #include <nxt_runtime.h>
90Sigor@sysoev.ru 
100Sigor@sysoev.ru 
110Sigor@sysoev.ru static nxt_time_string_t  nxt_log_error_time_cache;
120Sigor@sysoev.ru static u_char *nxt_log_error_time(u_char *buf, nxt_realtime_t *now,
130Sigor@sysoev.ru     struct tm *tm, size_t size, const char *format);
140Sigor@sysoev.ru static nxt_time_string_t  nxt_log_debug_time_cache;
150Sigor@sysoev.ru static u_char *nxt_log_debug_time(u_char *buf, nxt_realtime_t *now,
160Sigor@sysoev.ru     struct tm *tm, size_t size, const char *format);
170Sigor@sysoev.ru 
180Sigor@sysoev.ru 
190Sigor@sysoev.ru void nxt_cdecl
200Sigor@sysoev.ru nxt_log_time_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...)
210Sigor@sysoev.ru {
220Sigor@sysoev.ru     u_char             *p, *syslogmsg, *end;
230Sigor@sysoev.ru     va_list            args;
240Sigor@sysoev.ru     nxt_thread_t       *thr;
250Sigor@sysoev.ru     nxt_time_string_t  *time_cache;
260Sigor@sysoev.ru     u_char             msg[NXT_MAX_ERROR_STR];
270Sigor@sysoev.ru 
280Sigor@sysoev.ru     thr = nxt_thread();
290Sigor@sysoev.ru 
300Sigor@sysoev.ru     end = msg + NXT_MAX_ERROR_STR;
310Sigor@sysoev.ru 
320Sigor@sysoev.ru     time_cache = (log->level != NXT_LOG_DEBUG) ? &nxt_log_error_time_cache:
330Sigor@sysoev.ru                                                  &nxt_log_debug_time_cache;
340Sigor@sysoev.ru 
350Sigor@sysoev.ru     p = nxt_thread_time_string(thr, time_cache, msg);
360Sigor@sysoev.ru 
370Sigor@sysoev.ru     syslogmsg = p;
380Sigor@sysoev.ru 
39326Svbart@nginx.com #if 0
40326Svbart@nginx.com     nxt_fid_t    fid;
41326Svbart@nginx.com     const char   *id;
42326Svbart@nginx.com     nxt_fiber_t  *fib;
43326Svbart@nginx.com 
440Sigor@sysoev.ru     fib = nxt_fiber_self(thr);
450Sigor@sysoev.ru 
460Sigor@sysoev.ru     if (fib != NULL) {
470Sigor@sysoev.ru         id = "[%V] %PI#%PT#%PF ";
480Sigor@sysoev.ru         fid = nxt_fiber_id(fib);
490Sigor@sysoev.ru 
500Sigor@sysoev.ru     } else {
510Sigor@sysoev.ru         id = "[%V] %PI#%PT ";
520Sigor@sysoev.ru         fid = 0;
530Sigor@sysoev.ru     }
540Sigor@sysoev.ru 
550Sigor@sysoev.ru     p = nxt_sprintf(p, end, id, &nxt_log_levels[level], nxt_pid,
560Sigor@sysoev.ru                     nxt_thread_tid(thr), fid);
57326Svbart@nginx.com #else
58326Svbart@nginx.com     p = nxt_sprintf(p, end, "[%V] %PI#%PT ", &nxt_log_levels[level], nxt_pid,
59326Svbart@nginx.com                     nxt_thread_tid(thr));
60326Svbart@nginx.com #endif
610Sigor@sysoev.ru 
620Sigor@sysoev.ru     if (log->ident != 0) {
630Sigor@sysoev.ru         p = nxt_sprintf(p, end, "*%D ", log->ident);
640Sigor@sysoev.ru     }
650Sigor@sysoev.ru 
660Sigor@sysoev.ru     va_start(args, fmt);
670Sigor@sysoev.ru     p = nxt_vsprintf(p, end, fmt, args);
680Sigor@sysoev.ru     va_end(args);
690Sigor@sysoev.ru 
700Sigor@sysoev.ru     if (level != NXT_LOG_DEBUG && log->ctx_handler != NULL) {
710Sigor@sysoev.ru         p = log->ctx_handler(log->ctx, p, end);
720Sigor@sysoev.ru     }
730Sigor@sysoev.ru 
74*704Sigor@sysoev.ru     if (p > end - nxt_length("\n")) {
75*704Sigor@sysoev.ru         p = end - nxt_length("\n");
760Sigor@sysoev.ru     }
770Sigor@sysoev.ru 
78*704Sigor@sysoev.ru     *p++ = '\n';
790Sigor@sysoev.ru 
800Sigor@sysoev.ru     (void) nxt_write_console(nxt_stderr, msg, p - msg);
810Sigor@sysoev.ru 
82564Svbart@nginx.com     if (level == NXT_LOG_ALERT) {
83*704Sigor@sysoev.ru         *(p - nxt_length("\n")) = '\0';
840Sigor@sysoev.ru 
850Sigor@sysoev.ru         /*
860Sigor@sysoev.ru          * The syslog LOG_ALERT level is enough, because
870Sigor@sysoev.ru          * LOG_EMERG level broadcasts a message to all users.
880Sigor@sysoev.ru          */
890Sigor@sysoev.ru         nxt_write_syslog(LOG_ALERT, syslogmsg);
900Sigor@sysoev.ru     }
910Sigor@sysoev.ru }
920Sigor@sysoev.ru 
930Sigor@sysoev.ru 
940Sigor@sysoev.ru static nxt_time_string_t  nxt_log_error_time_cache = {
950Sigor@sysoev.ru     (nxt_atomic_uint_t) -1,
960Sigor@sysoev.ru     nxt_log_error_time,
970Sigor@sysoev.ru     "%4d/%02d/%02d %02d:%02d:%02d ",
98703Svbart@nginx.com     nxt_length("1970/09/28 12:00:00 "),
990Sigor@sysoev.ru     NXT_THREAD_TIME_LOCAL,
1000Sigor@sysoev.ru     NXT_THREAD_TIME_MSEC,
1010Sigor@sysoev.ru };
1020Sigor@sysoev.ru 
1030Sigor@sysoev.ru 
1040Sigor@sysoev.ru static u_char *
1050Sigor@sysoev.ru nxt_log_error_time(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size,
1060Sigor@sysoev.ru     const char *format)
1070Sigor@sysoev.ru {
1080Sigor@sysoev.ru     return nxt_sprintf(buf, buf + size, format,
1090Sigor@sysoev.ru                        tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1100Sigor@sysoev.ru                        tm->tm_hour, tm->tm_min, tm->tm_sec);
1110Sigor@sysoev.ru }
1120Sigor@sysoev.ru 
1130Sigor@sysoev.ru 
1140Sigor@sysoev.ru static nxt_time_string_t  nxt_log_debug_time_cache = {
1150Sigor@sysoev.ru     (nxt_atomic_uint_t) -1,
1160Sigor@sysoev.ru     nxt_log_debug_time,
1170Sigor@sysoev.ru     "%4d/%02d/%02d %02d:%02d:%02d.%03d ",
118703Svbart@nginx.com     nxt_length("1970/09/28 12:00:00.000 "),
1190Sigor@sysoev.ru     NXT_THREAD_TIME_LOCAL,
1200Sigor@sysoev.ru     NXT_THREAD_TIME_MSEC,
1210Sigor@sysoev.ru };
1220Sigor@sysoev.ru 
1230Sigor@sysoev.ru 
1240Sigor@sysoev.ru static u_char *
1250Sigor@sysoev.ru nxt_log_debug_time(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size,
1260Sigor@sysoev.ru     const char *format)
1270Sigor@sysoev.ru {
1280Sigor@sysoev.ru     return nxt_sprintf(buf, buf + size, format,
1290Sigor@sysoev.ru                        tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1300Sigor@sysoev.ru                        tm->tm_hour, tm->tm_min, tm->tm_sec,
1310Sigor@sysoev.ru                        now->nsec / 1000000);
1320Sigor@sysoev.ru }
133