root/src/utils/noit_log.h

Revision c730adec9b98429ce1368131ab29151fde18d34b, 7.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 months ago)

Add a "memory" logger type and expose it via console and rest.
This logger is allocation free on the write path. Logging was
refactored to pass the timeval all the way into the write(v)
operations; it is ignored by previous loggers.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2005-2009, OmniTI Computer Consulting, Inc.
3  * Copyright (c) 2013, Circonus, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *    * Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *    * Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials provided
15  *      with the distribution.
16  *    * Neither the name OmniTI Computer Consulting, Inc. nor the names
17  *      of its contributors may be used to endorse or promote products
18  *      derived from this software without specific prior written
19  *      permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _UTILS_NOIT_LOG_H
35 #define _UTILS_NOIT_LOG_H
36
37 #include "noit_defines.h"
38 #include <pthread.h>
39 #include <stdarg.h>
40 #include <sys/uio.h>
41 #include <sys/time.h>
42 #include "utils/noit_hash.h"
43
44 #ifdef noit_log_impl
45 typedef struct _noit_log_stream * noit_log_stream_t;
46 #else
47 typedef void * noit_log_stream_t;
48 #endif
49
50 struct _noit_log_stream_outlet_list {
51   noit_log_stream_t outlet;
52   struct _noit_log_stream_outlet_list *next;
53 };
54
55 typedef struct {
56   int (*openop)(noit_log_stream_t);
57   int (*reopenop)(noit_log_stream_t);
58   int (*writeop)(noit_log_stream_t, const struct timeval *whence, const void *, size_t);
59   int (*writevop)(noit_log_stream_t, const struct timeval *whence, const struct iovec *iov, int iovcnt);
60   int (*closeop)(noit_log_stream_t);
61   size_t (*sizeop)(noit_log_stream_t);
62   int (*renameop)(noit_log_stream_t, const char *);
63 } logops_t;
64
65 #define NOIT_LOG_STREAM_ENABLED         0x01
66 #define NOIT_LOG_STREAM_DEBUG           0x02
67 #define NOIT_LOG_STREAM_TIMESTAMPS      0x04
68 #define NOIT_LOG_STREAM_FACILITY        0x08
69 #define NOIT_LOG_STREAM_RECALCULATE     0x10
70 #define NOIT_LOG_STREAM_FEATURES        (NOIT_LOG_STREAM_DEBUG|NOIT_LOG_STREAM_TIMESTAMPS|NOIT_LOG_STREAM_FACILITY)
71
72 extern noit_log_stream_t noit_stderr;
73 extern noit_log_stream_t noit_debug;
74 extern noit_log_stream_t noit_error;
75 extern noit_log_stream_t noit_notice;
76
77 #define N_L_S_ON(ls) (*((unsigned *)ls) & NOIT_LOG_STREAM_ENABLED)
78
79 API_EXPORT(int) noit_log_global_enabled();
80 API_EXPORT(void) noit_log_init(int debug_on);
81 API_EXPORT(int) noit_log_reopen_all();
82 API_EXPORT(void) noit_register_logops(const char *name, logops_t *ops);
83 API_EXPORT(void *) noit_log_stream_get_ctx(noit_log_stream_t);
84 API_EXPORT(void) noit_log_stream_set_ctx(noit_log_stream_t, void *);
85 API_EXPORT(int) noit_log_stream_get_flags(noit_log_stream_t);
86 API_EXPORT(int) noit_log_stream_set_flags(noit_log_stream_t, int);
87 API_EXPORT(const char *) noit_log_stream_get_type(noit_log_stream_t);
88 API_EXPORT(const char *) noit_log_stream_get_name(noit_log_stream_t);
89 API_EXPORT(const char *) noit_log_stream_get_path(noit_log_stream_t);
90
91 API_EXPORT(noit_log_stream_t)
92   noit_log_stream_new(const char *, const char *, const char *,
93                       void *, noit_hash_table *);
94 API_EXPORT(noit_log_stream_t)
95   noit_log_stream_new_on_fd(const char *, int, noit_hash_table *);
96 API_EXPORT(noit_log_stream_t)
97   noit_log_stream_new_on_file(const char *, noit_hash_table *);
98 API_EXPORT(noit_log_stream_t) noit_log_stream_find(const char *);
99 API_EXPORT(void) noit_log_stream_remove(const char *name);
100 API_EXPORT(void) noit_log_stream_add_stream(noit_log_stream_t ls,
101                                             noit_log_stream_t outlet);
102 API_EXPORT(noit_log_stream_t)
103                  noit_log_stream_remove_stream(noit_log_stream_t ls,
104                                                const char *name);
105 API_EXPORT(void) noit_log_stream_reopen(noit_log_stream_t ls);
106
107 #define NOIT_LOG_RENAME_AUTOTIME ((const char *)-1)
108
109 API_EXPORT(int) noit_log_stream_rename(noit_log_stream_t ls, const char *);
110 API_EXPORT(void) noit_log_stream_close(noit_log_stream_t ls);
111 API_EXPORT(size_t) noit_log_stream_size(noit_log_stream_t ls);
112 API_EXPORT(size_t) noit_log_stream_written(noit_log_stream_t ls);
113 API_EXPORT(const char *) noit_log_stream_get_property(noit_log_stream_t ls,
114                                                       const char *);
115 API_EXPORT(void) noit_log_stream_set_property(noit_log_stream_t ls,
116                                               const char *, const char *);
117 API_EXPORT(void) noit_log_stream_free(noit_log_stream_t ls);
118 API_EXPORT(int) noit_vlog(noit_log_stream_t ls, struct timeval *,
119                           const char *file, int line,
120                           const char *format, va_list arg);
121 API_EXPORT(int) noit_log(noit_log_stream_t ls, struct timeval *,
122                          const char *file, int line,
123                          const char *format, ...)
124 #ifdef __GNUC__
125   __attribute__ ((format (printf, 5, 6)))
126 #endif
127   ;
128
129 /* fills logger with up to nsize loggers.
130  * If there are more loggers thatn nsize, -total is returned.
131  * Otherwise, the number loggers is returned.
132  */
133 API_EXPORT(int) noit_log_list(noit_log_stream_t *loggers, int nsize);
134
135 /* finds log_lines most recent log lines and calls f with their
136  * sequence number and content.  If f returns non-zero, the iteration
137  * is aborted early.
138  */
139 API_EXPORT(int)
140   noit_log_memory_lines(noit_log_stream_t ls, int log_lines,
141                         int (*f)(u_int64_t, const struct timeval *,
142                                  const char *, size_t, void *),
143                         void *closure);
144
145 API_EXPORT(int)
146   noit_log_memory_lines_since(noit_log_stream_t ls, u_int64_t afterwhich,
147                               int (*f)(u_int64_t, const struct timeval *,
148                                       const char *, size_t, void *),
149                               void *closure);
150
151 #define noitLT(ls, t, args...) do { \
152   if((ls) && (noit_log_global_enabled() || N_L_S_ON(ls))) \
153     noit_log(ls, t, __FILE__, __LINE__, args); \
154 } while(0)
155 #define noitL(ls, args...) do { \
156   if((ls) && (noit_log_global_enabled() || N_L_S_ON(ls))) { \
157     struct timeval __noitL_now; \
158     gettimeofday(&__noitL_now, NULL); \
159     noit_log(ls, &__noitL_now, __FILE__, __LINE__, args); \
160   } \
161 } while(0)
162
163 #define SETUP_LOG(a, b) do { if(!a##_log) a##_log = noit_log_stream_find(#a); \
164                              if(!a##_log) { b; } } while(0)
165
166 #endif
Note: See TracBrowser for help on using the browser.