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
nxt_log_time_handler(nxt_uint_t level,nxt_log_t * log,const char * fmt,...)200Sigor@sysoev.ru nxt_log_time_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...)
210Sigor@sysoev.ru {
22*1817Svbart@nginx.com u_char *p, *end;
23*1817Svbart@nginx.com #if 0
24*1817Svbart@nginx.com u_char *syslogmsg;
25*1817Svbart@nginx.com #endif
260Sigor@sysoev.ru va_list args;
270Sigor@sysoev.ru nxt_thread_t *thr;
280Sigor@sysoev.ru nxt_time_string_t *time_cache;
290Sigor@sysoev.ru u_char msg[NXT_MAX_ERROR_STR];
300Sigor@sysoev.ru
310Sigor@sysoev.ru thr = nxt_thread();
320Sigor@sysoev.ru
330Sigor@sysoev.ru end = msg + NXT_MAX_ERROR_STR;
340Sigor@sysoev.ru
350Sigor@sysoev.ru time_cache = (log->level != NXT_LOG_DEBUG) ? &nxt_log_error_time_cache:
360Sigor@sysoev.ru &nxt_log_debug_time_cache;
370Sigor@sysoev.ru
380Sigor@sysoev.ru p = nxt_thread_time_string(thr, time_cache, msg);
390Sigor@sysoev.ru
40*1817Svbart@nginx.com #if 0
410Sigor@sysoev.ru syslogmsg = p;
42*1817Svbart@nginx.com #endif
430Sigor@sysoev.ru
44326Svbart@nginx.com #if 0
45326Svbart@nginx.com nxt_fid_t fid;
46326Svbart@nginx.com const char *id;
47326Svbart@nginx.com nxt_fiber_t *fib;
48326Svbart@nginx.com
490Sigor@sysoev.ru fib = nxt_fiber_self(thr);
500Sigor@sysoev.ru
510Sigor@sysoev.ru if (fib != NULL) {
520Sigor@sysoev.ru id = "[%V] %PI#%PT#%PF ";
530Sigor@sysoev.ru fid = nxt_fiber_id(fib);
540Sigor@sysoev.ru
550Sigor@sysoev.ru } else {
560Sigor@sysoev.ru id = "[%V] %PI#%PT ";
570Sigor@sysoev.ru fid = 0;
580Sigor@sysoev.ru }
590Sigor@sysoev.ru
600Sigor@sysoev.ru p = nxt_sprintf(p, end, id, &nxt_log_levels[level], nxt_pid,
610Sigor@sysoev.ru nxt_thread_tid(thr), fid);
62326Svbart@nginx.com #else
63326Svbart@nginx.com p = nxt_sprintf(p, end, "[%V] %PI#%PT ", &nxt_log_levels[level], nxt_pid,
64326Svbart@nginx.com nxt_thread_tid(thr));
65326Svbart@nginx.com #endif
660Sigor@sysoev.ru
670Sigor@sysoev.ru if (log->ident != 0) {
680Sigor@sysoev.ru p = nxt_sprintf(p, end, "*%D ", log->ident);
690Sigor@sysoev.ru }
700Sigor@sysoev.ru
710Sigor@sysoev.ru va_start(args, fmt);
720Sigor@sysoev.ru p = nxt_vsprintf(p, end, fmt, args);
730Sigor@sysoev.ru va_end(args);
740Sigor@sysoev.ru
750Sigor@sysoev.ru if (level != NXT_LOG_DEBUG && log->ctx_handler != NULL) {
760Sigor@sysoev.ru p = log->ctx_handler(log->ctx, p, end);
770Sigor@sysoev.ru }
780Sigor@sysoev.ru
79704Sigor@sysoev.ru if (p > end - nxt_length("\n")) {
80704Sigor@sysoev.ru p = end - nxt_length("\n");
810Sigor@sysoev.ru }
820Sigor@sysoev.ru
83704Sigor@sysoev.ru *p++ = '\n';
840Sigor@sysoev.ru
850Sigor@sysoev.ru (void) nxt_write_console(nxt_stderr, msg, p - msg);
860Sigor@sysoev.ru
87*1817Svbart@nginx.com #if 0
88564Svbart@nginx.com if (level == NXT_LOG_ALERT) {
89704Sigor@sysoev.ru *(p - nxt_length("\n")) = '\0';
900Sigor@sysoev.ru
910Sigor@sysoev.ru /*
920Sigor@sysoev.ru * The syslog LOG_ALERT level is enough, because
930Sigor@sysoev.ru * LOG_EMERG level broadcasts a message to all users.
940Sigor@sysoev.ru */
950Sigor@sysoev.ru nxt_write_syslog(LOG_ALERT, syslogmsg);
960Sigor@sysoev.ru }
97*1817Svbart@nginx.com #endif
980Sigor@sysoev.ru }
990Sigor@sysoev.ru
1000Sigor@sysoev.ru
1010Sigor@sysoev.ru static nxt_time_string_t nxt_log_error_time_cache = {
1020Sigor@sysoev.ru (nxt_atomic_uint_t) -1,
1030Sigor@sysoev.ru nxt_log_error_time,
1040Sigor@sysoev.ru "%4d/%02d/%02d %02d:%02d:%02d ",
105703Svbart@nginx.com nxt_length("1970/09/28 12:00:00 "),
1060Sigor@sysoev.ru NXT_THREAD_TIME_LOCAL,
107796Svbart@nginx.com NXT_THREAD_TIME_SEC,
1080Sigor@sysoev.ru };
1090Sigor@sysoev.ru
1100Sigor@sysoev.ru
1110Sigor@sysoev.ru static u_char *
nxt_log_error_time(u_char * buf,nxt_realtime_t * now,struct tm * tm,size_t size,const char * format)1120Sigor@sysoev.ru nxt_log_error_time(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size,
1130Sigor@sysoev.ru const char *format)
1140Sigor@sysoev.ru {
1150Sigor@sysoev.ru return nxt_sprintf(buf, buf + size, format,
1160Sigor@sysoev.ru tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1170Sigor@sysoev.ru tm->tm_hour, tm->tm_min, tm->tm_sec);
1180Sigor@sysoev.ru }
1190Sigor@sysoev.ru
1200Sigor@sysoev.ru
1210Sigor@sysoev.ru static nxt_time_string_t nxt_log_debug_time_cache = {
1220Sigor@sysoev.ru (nxt_atomic_uint_t) -1,
1230Sigor@sysoev.ru nxt_log_debug_time,
1240Sigor@sysoev.ru "%4d/%02d/%02d %02d:%02d:%02d.%03d ",
125703Svbart@nginx.com nxt_length("1970/09/28 12:00:00.000 "),
1260Sigor@sysoev.ru NXT_THREAD_TIME_LOCAL,
1270Sigor@sysoev.ru NXT_THREAD_TIME_MSEC,
1280Sigor@sysoev.ru };
1290Sigor@sysoev.ru
1300Sigor@sysoev.ru
1310Sigor@sysoev.ru static u_char *
nxt_log_debug_time(u_char * buf,nxt_realtime_t * now,struct tm * tm,size_t size,const char * format)1320Sigor@sysoev.ru nxt_log_debug_time(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size,
1330Sigor@sysoev.ru const char *format)
1340Sigor@sysoev.ru {
1350Sigor@sysoev.ru return nxt_sprintf(buf, buf + size, format,
1360Sigor@sysoev.ru tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1370Sigor@sysoev.ru tm->tm_hour, tm->tm_min, tm->tm_sec,
1380Sigor@sysoev.ru now->nsec / 1000000);
1390Sigor@sysoev.ru }
140