Changeset fed0d5d2bff89cbba5ece81f815d8e9a51fd8560
- Timestamp:
- 07/30/10 15:47:19
(3 years ago)
- Author:
- Theo Schlossnagle <jesus@omniti.com>
- git-committer:
- Theo Schlossnagle <jesus@omniti.com> 1280504839 +0000
- git-parent:
[15d06bff275aeaf7ede0668111821c63343ffac0]
- git-author:
- Theo Schlossnagle <jesus@omniti.com> 1280504839 +0000
- Message:
much better support for timestamps and debugging information, refs #87
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r44fa704 |
rfed0d5d |
|
| 878 | 878 | char name[256], type[256], path[256]; |
|---|
| 879 | 879 | noit_hash_table *config; |
|---|
| 880 | | noit_boolean disabled; |
|---|
| 881 | | noit_boolean debug; |
|---|
| | 880 | noit_boolean disabled, debug, timestamps; |
|---|
| 882 | 881 | |
|---|
| 883 | 882 | if(!noit_conf_get_stringbuf(log_configs[i], |
|---|
| … | … | |
| 915 | 914 | &debug) && debug) |
|---|
| 916 | 915 | ls->debug = 1; |
|---|
| | 916 | |
|---|
| | 917 | if(noit_conf_get_boolean(log_configs[i], |
|---|
| | 918 | "ancestor-or-self::node()/@timestamps", |
|---|
| | 919 | ×tamps) && timestamps) |
|---|
| | 920 | ls->timestamps = 1; |
|---|
| 917 | 921 | |
|---|
| 918 | 922 | outlets = noit_conf_get_sections(log_configs[i], |
|---|
| rff59a23 |
rfed0d5d |
|
| 513 | 513 | noit_console_logio_reopen, |
|---|
| 514 | 514 | noit_console_logio_write, |
|---|
| | 515 | NULL, |
|---|
| 515 | 516 | noit_console_logio_close, |
|---|
| | 517 | NULL, |
|---|
| 516 | 518 | NULL |
|---|
| 517 | 519 | }; |
|---|
| r5bb306c |
rfed0d5d |
|
| 133 | 133 | noit_livestream_logio_reopen, |
|---|
| 134 | 134 | noit_livestream_logio_write, |
|---|
| | 135 | NULL, |
|---|
| 135 | 136 | noit_livestream_logio_close, |
|---|
| | 137 | NULL, |
|---|
| 136 | 138 | NULL |
|---|
| 137 | 139 | }; |
|---|
| r48a51fb |
rfed0d5d |
|
| 37 | 37 | #include <fcntl.h> |
|---|
| 38 | 38 | #include <unistd.h> |
|---|
| | 39 | #include <sys/uio.h> |
|---|
| 39 | 40 | #include <sys/time.h> |
|---|
| 40 | 41 | #include <assert.h> |
|---|
| … | … | |
| 73 | 74 | unsigned enabled:1; |
|---|
| 74 | 75 | unsigned debug:1; |
|---|
| | 76 | unsigned timestamps:1; |
|---|
| 75 | 77 | /* Above is exposed... */ |
|---|
| 76 | 78 | char *type; |
|---|
| … | … | |
| 83 | 85 | struct _noit_log_stream_outlet_list *outlets; |
|---|
| 84 | 86 | pthread_rwlock_t *lock; |
|---|
| | 87 | unsigned deps_materialized:1; |
|---|
| | 88 | unsigned debug_below:1; |
|---|
| | 89 | unsigned timestamps_below:1; |
|---|
| 85 | 90 | }; |
|---|
| 86 | 91 | |
|---|
| … | … | |
| 95 | 100 | } |
|---|
| 96 | 101 | |
|---|
| | 102 | #define MATERIALIZE_DEPS(ls) do { \ |
|---|
| | 103 | if(!(ls)->deps_materialized) materialize_deps(ls); \ |
|---|
| | 104 | } while(0) |
|---|
| | 105 | |
|---|
| | 106 | static void materialize_deps(noit_log_stream_t ls) { |
|---|
| | 107 | if(ls->deps_materialized) return; |
|---|
| | 108 | if(ls->debug) ls->debug_below = 1; |
|---|
| | 109 | if(ls->timestamps) ls->timestamps_below = 1; |
|---|
| | 110 | if(ls->debug_below == 0 || ls->timestamps_below == 0) { |
|---|
| | 111 | /* we might have children than need these */ |
|---|
| | 112 | struct _noit_log_stream_outlet_list *node; |
|---|
| | 113 | for(node = ls->outlets; node; node = node->next) { |
|---|
| | 114 | MATERIALIZE_DEPS(node->outlet); |
|---|
| | 115 | if(!ls->debug) ls->debug_below = node->outlet->debug; |
|---|
| | 116 | if(!ls->timestamps) ls->timestamps_below = node->outlet->timestamps; |
|---|
| | 117 | } |
|---|
| | 118 | } |
|---|
| | 119 | ls->deps_materialized = 1; |
|---|
| | 120 | } |
|---|
| 97 | 121 | static int |
|---|
| 98 | 122 | posix_logio_open(noit_log_stream_t ls) { |
|---|
| … | … | |
| 111 | 135 | posix_logio_reopen(noit_log_stream_t ls) { |
|---|
| 112 | 136 | if(ls->path) { |
|---|
| 113 | | int newfd, oldfd; |
|---|
| | 137 | int newfd, oldfd, rv = -1; |
|---|
| | 138 | if(ls->lock) pthread_rwlock_wrlock(ls->lock); |
|---|
| 114 | 139 | oldfd = (int)(vpsized_int)ls->op_ctx; |
|---|
| 115 | 140 | newfd = open(ls->path, O_CREAT|O_WRONLY|O_APPEND, ls->mode); |
|---|
| … | … | |
| 117 | 142 | ls->op_ctx = (void *)(vpsized_int)newfd; |
|---|
| 118 | 143 | if(oldfd >= 0) close(oldfd); |
|---|
| 119 | | return 0; |
|---|
| 120 | | } |
|---|
| | 144 | rv = 0; |
|---|
| | 145 | } |
|---|
| | 146 | if(ls->lock) pthread_rwlock_unlock(ls->lock); |
|---|
| | 147 | return rv; |
|---|
| 121 | 148 | } |
|---|
| 122 | 149 | return -1; |
|---|
| … | … | |
| 124 | 151 | static int |
|---|
| 125 | 152 | posix_logio_write(noit_log_stream_t ls, const void *buf, size_t len) { |
|---|
| 126 | | int fd; |
|---|
| | 153 | int fd, rv = -1; |
|---|
| | 154 | if(ls->lock) pthread_rwlock_rdlock(ls->lock); |
|---|
| 127 | 155 | fd = (int)(vpsized_int)ls->op_ctx; |
|---|
| 128 | 156 | debug_printf("writing to %d\n", fd); |
|---|
| 129 | | if(fd < 0) return -1; |
|---|
| 130 | | return write(fd, buf, len); |
|---|
| | 157 | if(fd >= 0) rv = write(fd, buf, len); |
|---|
| | 158 | if(ls->lock) pthread_rwlock_unlock(ls->lock); |
|---|
| | 159 | return rv; |
|---|
| | 160 | } |
|---|
| | 161 | static int |
|---|
| | 162 | posix_logio_writev(noit_log_stream_t ls, const struct iovec *iov, int iovcnt) { |
|---|
| | 163 | int fd, rv = -1; |
|---|
| | 164 | if(ls->lock) pthread_rwlock_rdlock(ls->lock); |
|---|
| | 165 | fd = (int)(vpsized_int)ls->op_ctx; |
|---|
| | 166 | debug_printf("writ(v)ing to %d\n", fd); |
|---|
| | 167 | if(fd >= 0) rv = writev(fd, iov, iovcnt); |
|---|
| | 168 | if(ls->lock) pthread_rwlock_unlock(ls->lock); |
|---|
| | 169 | return rv; |
|---|
| 131 | 170 | } |
|---|
| 132 | 171 | static int |
|---|
| 133 | 172 | posix_logio_close(noit_log_stream_t ls) { |
|---|
| 134 | | int fd; |
|---|
| | 173 | int fd, rv; |
|---|
| | 174 | if(ls->lock) pthread_rwlock_wrlock(ls->lock); |
|---|
| 135 | 175 | fd = (int)(vpsized_int)ls->op_ctx; |
|---|
| 136 | | return close(fd); |
|---|
| | 176 | rv = close(fd); |
|---|
| | 177 | if(ls->lock) pthread_rwlock_unlock(ls->lock); |
|---|
| | 178 | return rv; |
|---|
| 137 | 179 | } |
|---|
| 138 | 180 | static size_t |
|---|
| 139 | 181 | posix_logio_size(noit_log_stream_t ls) { |
|---|
| 140 | 182 | int fd; |
|---|
| | 183 | size_t s = -1; |
|---|
| 141 | 184 | struct stat sb; |
|---|
| | 185 | if(ls->lock) pthread_rwlock_rdlock(ls->lock); |
|---|
| 142 | 186 | fd = (int)(vpsized_int)ls->op_ctx; |
|---|
| 143 | 187 | if(fstat(fd, &sb) == 0) { |
|---|
| 144 | | return (size_t)sb.st_size; |
|---|
| 145 | | } |
|---|
| | 188 | s = (size_t)sb.st_size; |
|---|
| | 189 | } |
|---|
| | 190 | if(ls->lock) pthread_rwlock_unlock(ls->lock); |
|---|
| | 191 | return s; |
|---|
| | 192 | } |
|---|
| | 193 | static int |
|---|
| | 194 | posix_logio_rename(noit_log_stream_t ls, const char *name) { |
|---|
| | 195 | int rv = 0; |
|---|
| | 196 | if(!strcmp(name, ls->path)) return 0; /* noop */ |
|---|
| | 197 | if(ls->lock) pthread_rwlock_rdlock(ls->lock); |
|---|
| | 198 | rv = rename(ls->path, name); |
|---|
| | 199 | if(ls->lock) pthread_rwlock_unlock(ls->lock); |
|---|
| 146 | 200 | return -1; |
|---|
| 147 | 201 | } |
|---|
| … | … | |
| 150 | 204 | posix_logio_reopen, |
|---|
| 151 | 205 | posix_logio_write, |
|---|
| | 206 | posix_logio_writev, |
|---|
| 152 | 207 | posix_logio_close, |
|---|
| 153 | | posix_logio_size |
|---|
| | 208 | posix_logio_size, |
|---|
| | 209 | posix_logio_rename |
|---|
| 154 | 210 | }; |
|---|
| 155 | 211 | |
|---|
| … | … | |
| 329 | 385 | return size; |
|---|
| 330 | 386 | } |
|---|
| | 387 | static int |
|---|
| | 388 | jlog_logio_rename(noit_log_stream_t ls, const char *newname) { |
|---|
| | 389 | /* Not supported (and makes no sense) */ |
|---|
| | 390 | return -1; |
|---|
| | 391 | } |
|---|
| 331 | 392 | static logops_t jlog_logio_ops = { |
|---|
| 332 | 393 | jlog_logio_open, |
|---|
| 333 | 394 | jlog_logio_reopen, |
|---|
| 334 | 395 | jlog_logio_write, |
|---|
| | 396 | NULL, |
|---|
| 335 | 397 | jlog_logio_close, |
|---|
| 336 | | jlog_logio_size |
|---|
| | 398 | jlog_logio_size, |
|---|
| | 399 | jlog_logio_rename |
|---|
| 337 | 400 | }; |
|---|
| 338 | 401 | |
|---|
| … | … | |
| 344 | 407 | noit_register_logops("jlog", &jlog_logio_ops); |
|---|
| 345 | 408 | noit_stderr = noit_log_stream_new_on_fd("stderr", 2, NULL); |
|---|
| | 409 | noit_stderr->timestamps = 1; |
|---|
| 346 | 410 | noit_error = noit_log_stream_new("error", NULL, NULL, NULL, NULL); |
|---|
| 347 | 411 | noit_debug = noit_log_stream_new("debug", NULL, NULL, NULL, NULL); |
|---|
| … | … | |
| 522 | 586 | } |
|---|
| 523 | 587 | |
|---|
| | 588 | int noit_log_stream_rename(noit_log_stream_t ls, const char *newname) { |
|---|
| | 589 | return (ls->ops && ls->ops->renameop) ? ls->ops->renameop(ls, newname) : -1; |
|---|
| | 590 | } |
|---|
| | 591 | |
|---|
| 524 | 592 | void |
|---|
| 525 | 593 | noit_log_stream_close(noit_log_stream_t ls) { |
|---|
| … | … | |
| 557 | 625 | |
|---|
| 558 | 626 | static int |
|---|
| 559 | | noit_log_line(noit_log_stream_t ls, char *buffer, size_t len) { |
|---|
| | 627 | noit_log_writev(noit_log_stream_t ls, const struct iovec *iov, int iovcnt) { |
|---|
| | 628 | /* This emulates writev into a buffer for ops that don't support it */ |
|---|
| | 629 | char stackbuff[4096], *tofree = NULL, *buff = NULL; |
|---|
| | 630 | int i, s = 0, ins = 0; |
|---|
| | 631 | |
|---|
| | 632 | if(!ls->ops) return -1; |
|---|
| | 633 | if(ls->ops->writevop) return ls->ops->writevop(ls, iov, iovcnt); |
|---|
| | 634 | if(!ls->ops->writeop) return -1; |
|---|
| | 635 | if(iovcnt == 1) return ls->ops->writeop(ls, iov[0].iov_base, iov[0].iov_len); |
|---|
| | 636 | |
|---|
| | 637 | for(i=0;i<iovcnt;i++) s+=iov[i].iov_len; |
|---|
| | 638 | if(s > sizeof(stackbuff)) { |
|---|
| | 639 | tofree = buff = malloc(s); |
|---|
| | 640 | if(tofree == NULL) return -1; |
|---|
| | 641 | } |
|---|
| | 642 | else buff = stackbuff; |
|---|
| | 643 | for(i=0;i<iovcnt;i++) { |
|---|
| | 644 | memcpy(buff + ins, iov[i].iov_base, iov[i].iov_len); |
|---|
| | 645 | ins += iov[i].iov_len; |
|---|
| | 646 | } |
|---|
| | 647 | i = ls->ops->writeop(ls, buff, s); |
|---|
| | 648 | if(tofree) free(tofree); |
|---|
| | 649 | return i; |
|---|
| | 650 | } |
|---|
| | 651 | |
|---|
| | 652 | static int |
|---|
| | 653 | noit_log_line(noit_log_stream_t ls, |
|---|
| | 654 | const char *timebuf, int timebuflen, |
|---|
| | 655 | const char *debugbuf, int debugbuflen, |
|---|
| | 656 | const char *buffer, size_t len) { |
|---|
| 560 | 657 | int rv = 0; |
|---|
| 561 | 658 | struct _noit_log_stream_outlet_list *node; |
|---|
| 562 | | if(ls->ops) |
|---|
| 563 | | rv = ls->ops->writeop(ls, buffer, len); /* Not much one can do about errors */ |
|---|
| | 659 | if(ls->ops) { |
|---|
| | 660 | int iovcnt = 0; |
|---|
| | 661 | struct iovec iov[3]; |
|---|
| | 662 | if(ls->timestamps) { |
|---|
| | 663 | iov[iovcnt].iov_base = (void *)timebuf; |
|---|
| | 664 | iov[iovcnt].iov_len = timebuflen; |
|---|
| | 665 | iovcnt++; |
|---|
| | 666 | } |
|---|
| | 667 | if(ls->debug) { |
|---|
| | 668 | iov[iovcnt].iov_base = (void *)debugbuf; |
|---|
| | 669 | iov[iovcnt].iov_len = debugbuflen; |
|---|
| | 670 | iovcnt++; |
|---|
| | 671 | } |
|---|
| | 672 | iov[iovcnt].iov_base = (void *)buffer; |
|---|
| | 673 | iov[iovcnt].iov_len = len; |
|---|
| | 674 | iovcnt++; |
|---|
| | 675 | rv = noit_log_writev(ls, iov, iovcnt); |
|---|
| | 676 | } |
|---|
| 564 | 677 | for(node = ls->outlets; node; node = node->next) { |
|---|
| 565 | 678 | int srv = 0; |
|---|
| 566 | 679 | debug_printf(" %s -> %s\n", ls->name, node->outlet->name); |
|---|
| 567 | | srv = noit_log_line(node->outlet, buffer, len); |
|---|
| | 680 | srv = noit_log_line(node->outlet, timebuf, timebuflen, debugbuf, debugbuflen, buffer, len); |
|---|
| 568 | 681 | if(srv) rv = srv; |
|---|
| 569 | 682 | } |
|---|
| … | … | |
| 576 | 689 | int rv = 0, allocd = 0; |
|---|
| 577 | 690 | char buffer[4096], *dynbuff = NULL; |
|---|
| 578 | | char fbuf[1024]; |
|---|
| 579 | 691 | #ifdef va_copy |
|---|
| 580 | 692 | va_list copy; |
|---|
| … | … | |
| 583 | 695 | if(ls->enabled || NOIT_LOG_LOG_ENABLED()) { |
|---|
| 584 | 696 | int len; |
|---|
| 585 | | if(ls->debug) { |
|---|
| | 697 | char tbuf[48], dbuf[80]; |
|---|
| | 698 | int tbuflen = 0, dbuflen = 0; |
|---|
| | 699 | MATERIALIZE_DEPS(ls); |
|---|
| | 700 | if(ls->timestamps_below) { |
|---|
| 586 | 701 | struct tm _tm, *tm; |
|---|
| 587 | | char tbuf[32]; |
|---|
| | 702 | char tempbuf[32]; |
|---|
| 588 | 703 | time_t s = (time_t)now->tv_sec; |
|---|
| 589 | 704 | tm = localtime_r(&s, &_tm); |
|---|
| 590 | | strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", tm); |
|---|
| 591 | | snprintf(fbuf, sizeof(fbuf), "[%s.%06d %s:%d] %s", |
|---|
| 592 | | tbuf, (int)now->tv_usec, file, line, format); |
|---|
| 593 | | format = fbuf; |
|---|
| 594 | | } |
|---|
| | 705 | strftime(tempbuf, sizeof(tempbuf), "%Y-%m-%d %H:%M:%S", tm); |
|---|
| | 706 | snprintf(tbuf, sizeof(tbuf), "[%s.%06d] ", tempbuf, (int)now->tv_usec); |
|---|
| | 707 | tbuflen = strlen(tbuf); |
|---|
| | 708 | } |
|---|
| | 709 | else tbuf[0] = '\0'; |
|---|
| | 710 | if(ls->debug_below) { |
|---|
| | 711 | snprintf(dbuf, sizeof(dbuf), "[%s:%d] ", file, line); |
|---|
| | 712 | dbuflen = strlen(dbuf); |
|---|
| | 713 | } |
|---|
| | 714 | else dbuf[0] = '\0'; |
|---|
| 595 | 715 | #ifdef va_copy |
|---|
| 596 | 716 | va_copy(copy, arg); |
|---|
| … | … | |
| 617 | 737 | NOIT_LOG_LOG(ls->name, (char *)file, line, dynbuff); |
|---|
| 618 | 738 | if(ls->enabled) |
|---|
| 619 | | rv = noit_log_line(ls, dynbuff, len); |
|---|
| | 739 | rv = noit_log_line(ls, tbuf, tbuflen, dbuf, dbuflen, dynbuff, len); |
|---|
| 620 | 740 | free(dynbuff); |
|---|
| 621 | 741 | } |
|---|
| … | … | |
| 623 | 743 | NOIT_LOG_LOG(ls->name, (char *)file, line, buffer); |
|---|
| 624 | 744 | if(ls->enabled) |
|---|
| 625 | | rv = noit_log_line(ls, buffer, len); |
|---|
| | 745 | rv = noit_log_line(ls, tbuf, tbuflen, dbuf, dbuflen, buffer, len); |
|---|
| 626 | 746 | } |
|---|
| 627 | 747 | if(rv == len) return 0; |
|---|
| r3f527b7 |
rfed0d5d |
|
| 37 | 37 | #include <pthread.h> |
|---|
| 38 | 38 | #include <stdarg.h> |
|---|
| | 39 | #include <sys/uio.h> |
|---|
| 39 | 40 | #include <sys/time.h> |
|---|
| 40 | 41 | #include "utils/noit_hash.h" |
|---|
| … | … | |
| 49 | 50 | int (*reopenop)(struct _noit_log_stream *); |
|---|
| 50 | 51 | int (*writeop)(struct _noit_log_stream *, const void *, size_t); |
|---|
| | 52 | int (*writevop)(struct _noit_log_stream *, const struct iovec *iov, int iovcnt); |
|---|
| 51 | 53 | int (*closeop)(struct _noit_log_stream *); |
|---|
| 52 | 54 | size_t (*sizeop)(struct _noit_log_stream *); |
|---|
| | 55 | int (*renameop)(struct _noit_log_stream *, const char *); |
|---|
| 53 | 56 | } logops_t; |
|---|
| 54 | 57 | |
|---|
| … | … | |
| 59 | 62 | unsigned enabled:1; |
|---|
| 60 | 63 | unsigned debug:1; |
|---|
| | 64 | unsigned timestamps:1; |
|---|
| 61 | 65 | /* totally private type, don't even think about it */ |
|---|
| 62 | 66 | } * noit_log_stream_t; |
|---|
| … | … | |
| 92 | 96 | const char *name); |
|---|
| 93 | 97 | API_EXPORT(void) noit_log_stream_reopen(noit_log_stream_t ls); |
|---|
| | 98 | API_EXPORT(int) noit_log_stream_rename(noit_log_stream_t ls, const char *); |
|---|
| 94 | 99 | API_EXPORT(void) noit_log_stream_close(noit_log_stream_t ls); |
|---|
| 95 | 100 | API_EXPORT(size_t) noit_log_stream_size(noit_log_stream_t ls); |
|---|