comparison src/core/ngx_log.c @ 9297:e1f15d47d102

Core: moved ngx_log_set_levels() to a proper position. Previous order is an artifact from the time before 5254:7ecaa9e4bf1b, when ngx_log_set_levels() was a non-static function. No functional changes.
author Maxim Dounin <mdounin@mdounin.ru>
date Tue, 25 Jun 2024 22:58:11 +0300
parents af5b47569cb2
children 14770557be17
comparison
equal deleted inserted replaced
9296:af5b47569cb2 9297:e1f15d47d102
474 return NULL; 474 return NULL;
475 } 475 }
476 476
477 477
478 static char * 478 static char *
479 ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
480 {
481 ngx_log_t *dummy;
482
483 dummy = &cf->cycle->new_log;
484
485 return ngx_log_set_log(cf, &dummy);
486 }
487
488
489 char *
490 ngx_log_set_log(ngx_conf_t *cf, ngx_log_t **head)
491 {
492 ngx_log_t *new_log;
493 ngx_str_t *value, name;
494 ngx_syslog_peer_t *peer;
495
496 if (*head != NULL && (*head)->log_level == 0) {
497 new_log = *head;
498
499 } else {
500
501 new_log = ngx_pcalloc(cf->pool, sizeof(ngx_log_t));
502 if (new_log == NULL) {
503 return NGX_CONF_ERROR;
504 }
505
506 if (*head == NULL) {
507 *head = new_log;
508 }
509 }
510
511 value = cf->args->elts;
512
513 if (ngx_strcmp(value[1].data, "stderr") == 0) {
514 ngx_str_null(&name);
515 cf->cycle->log_use_stderr = 1;
516
517 new_log->file = ngx_conf_open_file(cf->cycle, &name);
518 if (new_log->file == NULL) {
519 return NGX_CONF_ERROR;
520 }
521
522 } else if (ngx_strncmp(value[1].data, "memory:", 7) == 0) {
523
524 #if (NGX_DEBUG)
525 size_t size, needed;
526 ngx_pool_cleanup_t *cln;
527 ngx_log_memory_buf_t *buf;
528
529 value[1].len -= 7;
530 value[1].data += 7;
531
532 needed = sizeof("MEMLOG :" NGX_LINEFEED)
533 + cf->conf_file->file.name.len
534 + NGX_SIZE_T_LEN
535 + NGX_INT_T_LEN
536 + NGX_MAX_ERROR_STR;
537
538 size = ngx_parse_size(&value[1]);
539
540 if (size == (size_t) NGX_ERROR || size < needed) {
541 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
542 "invalid buffer size \"%V\"", &value[1]);
543 return NGX_CONF_ERROR;
544 }
545
546 buf = ngx_pcalloc(cf->pool, sizeof(ngx_log_memory_buf_t));
547 if (buf == NULL) {
548 return NGX_CONF_ERROR;
549 }
550
551 buf->start = ngx_pnalloc(cf->pool, size);
552 if (buf->start == NULL) {
553 return NGX_CONF_ERROR;
554 }
555
556 buf->end = buf->start + size;
557
558 buf->pos = ngx_slprintf(buf->start, buf->end, "MEMLOG %uz %V:%ui%N",
559 size, &cf->conf_file->file.name,
560 cf->conf_file->line);
561
562 ngx_memset(buf->pos, ' ', buf->end - buf->pos);
563
564 cln = ngx_pool_cleanup_add(cf->pool, 0);
565 if (cln == NULL) {
566 return NGX_CONF_ERROR;
567 }
568
569 cln->data = new_log;
570 cln->handler = ngx_log_memory_cleanup;
571
572 new_log->writer = ngx_log_memory_writer;
573 new_log->wdata = buf;
574
575 #else
576 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
577 "nginx was built without debug support");
578 return NGX_CONF_ERROR;
579 #endif
580
581 } else if (ngx_strncmp(value[1].data, "syslog:", 7) == 0) {
582 peer = ngx_pcalloc(cf->pool, sizeof(ngx_syslog_peer_t));
583 if (peer == NULL) {
584 return NGX_CONF_ERROR;
585 }
586
587 if (ngx_syslog_process_conf(cf, peer) != NGX_CONF_OK) {
588 return NGX_CONF_ERROR;
589 }
590
591 new_log->writer = ngx_syslog_writer;
592 new_log->wdata = peer;
593
594 } else {
595 new_log->file = ngx_conf_open_file(cf->cycle, &value[1]);
596 if (new_log->file == NULL) {
597 return NGX_CONF_ERROR;
598 }
599 }
600
601 if (ngx_log_set_levels(cf, new_log) != NGX_CONF_OK) {
602 return NGX_CONF_ERROR;
603 }
604
605 if (*head != new_log) {
606 ngx_log_insert(*head, new_log);
607 }
608
609 return NGX_CONF_OK;
610 }
611
612
613 static char *
479 ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log) 614 ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)
480 { 615 {
481 ngx_uint_t i, n, d, found; 616 ngx_uint_t i, n, d, found;
482 ngx_str_t *value; 617 ngx_str_t *value;
483 618
536 671
537 return NGX_CONF_OK; 672 return NGX_CONF_OK;
538 } 673 }
539 674
540 675
541 static char *
542 ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
543 {
544 ngx_log_t *dummy;
545
546 dummy = &cf->cycle->new_log;
547
548 return ngx_log_set_log(cf, &dummy);
549 }
550
551
552 char *
553 ngx_log_set_log(ngx_conf_t *cf, ngx_log_t **head)
554 {
555 ngx_log_t *new_log;
556 ngx_str_t *value, name;
557 ngx_syslog_peer_t *peer;
558
559 if (*head != NULL && (*head)->log_level == 0) {
560 new_log = *head;
561
562 } else {
563
564 new_log = ngx_pcalloc(cf->pool, sizeof(ngx_log_t));
565 if (new_log == NULL) {
566 return NGX_CONF_ERROR;
567 }
568
569 if (*head == NULL) {
570 *head = new_log;
571 }
572 }
573
574 value = cf->args->elts;
575
576 if (ngx_strcmp(value[1].data, "stderr") == 0) {
577 ngx_str_null(&name);
578 cf->cycle->log_use_stderr = 1;
579
580 new_log->file = ngx_conf_open_file(cf->cycle, &name);
581 if (new_log->file == NULL) {
582 return NGX_CONF_ERROR;
583 }
584
585 } else if (ngx_strncmp(value[1].data, "memory:", 7) == 0) {
586
587 #if (NGX_DEBUG)
588 size_t size, needed;
589 ngx_pool_cleanup_t *cln;
590 ngx_log_memory_buf_t *buf;
591
592 value[1].len -= 7;
593 value[1].data += 7;
594
595 needed = sizeof("MEMLOG :" NGX_LINEFEED)
596 + cf->conf_file->file.name.len
597 + NGX_SIZE_T_LEN
598 + NGX_INT_T_LEN
599 + NGX_MAX_ERROR_STR;
600
601 size = ngx_parse_size(&value[1]);
602
603 if (size == (size_t) NGX_ERROR || size < needed) {
604 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
605 "invalid buffer size \"%V\"", &value[1]);
606 return NGX_CONF_ERROR;
607 }
608
609 buf = ngx_pcalloc(cf->pool, sizeof(ngx_log_memory_buf_t));
610 if (buf == NULL) {
611 return NGX_CONF_ERROR;
612 }
613
614 buf->start = ngx_pnalloc(cf->pool, size);
615 if (buf->start == NULL) {
616 return NGX_CONF_ERROR;
617 }
618
619 buf->end = buf->start + size;
620
621 buf->pos = ngx_slprintf(buf->start, buf->end, "MEMLOG %uz %V:%ui%N",
622 size, &cf->conf_file->file.name,
623 cf->conf_file->line);
624
625 ngx_memset(buf->pos, ' ', buf->end - buf->pos);
626
627 cln = ngx_pool_cleanup_add(cf->pool, 0);
628 if (cln == NULL) {
629 return NGX_CONF_ERROR;
630 }
631
632 cln->data = new_log;
633 cln->handler = ngx_log_memory_cleanup;
634
635 new_log->writer = ngx_log_memory_writer;
636 new_log->wdata = buf;
637
638 #else
639 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
640 "nginx was built without debug support");
641 return NGX_CONF_ERROR;
642 #endif
643
644 } else if (ngx_strncmp(value[1].data, "syslog:", 7) == 0) {
645 peer = ngx_pcalloc(cf->pool, sizeof(ngx_syslog_peer_t));
646 if (peer == NULL) {
647 return NGX_CONF_ERROR;
648 }
649
650 if (ngx_syslog_process_conf(cf, peer) != NGX_CONF_OK) {
651 return NGX_CONF_ERROR;
652 }
653
654 new_log->writer = ngx_syslog_writer;
655 new_log->wdata = peer;
656
657 } else {
658 new_log->file = ngx_conf_open_file(cf->cycle, &value[1]);
659 if (new_log->file == NULL) {
660 return NGX_CONF_ERROR;
661 }
662 }
663
664 if (ngx_log_set_levels(cf, new_log) != NGX_CONF_OK) {
665 return NGX_CONF_ERROR;
666 }
667
668 if (*head != new_log) {
669 ngx_log_insert(*head, new_log);
670 }
671
672 return NGX_CONF_OK;
673 }
674
675
676 static void 676 static void
677 ngx_log_insert(ngx_log_t *log, ngx_log_t *new_log) 677 ngx_log_insert(ngx_log_t *log, ngx_log_t *new_log)
678 { 678 {
679 ngx_log_t tmp; 679 ngx_log_t tmp;
680 680