| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2005-2009, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#ifndef _UTILS_NOIT_LOG_H |
|---|
| 34 |
#define _UTILS_NOIT_LOG_H |
|---|
| 35 |
|
|---|
| 36 |
#include "noit_defines.h" |
|---|
| 37 |
#include <pthread.h> |
|---|
| 38 |
#include <stdarg.h> |
|---|
| 39 |
#include <sys/uio.h> |
|---|
| 40 |
#include <sys/time.h> |
|---|
| 41 |
#include "utils/noit_hash.h" |
|---|
| 42 |
|
|---|
| 43 |
struct _noit_log_stream_outlet_list { |
|---|
| 44 |
struct _noit_log_stream *outlet; |
|---|
| 45 |
struct _noit_log_stream_outlet_list *next; |
|---|
| 46 |
}; |
|---|
| 47 |
|
|---|
| 48 |
typedef struct { |
|---|
| 49 |
int (*openop)(struct _noit_log_stream *); |
|---|
| 50 |
int (*reopenop)(struct _noit_log_stream *); |
|---|
| 51 |
int (*writeop)(struct _noit_log_stream *, const void *, size_t); |
|---|
| 52 |
int (*writevop)(struct _noit_log_stream *, const struct iovec *iov, int iovcnt); |
|---|
| 53 |
int (*closeop)(struct _noit_log_stream *); |
|---|
| 54 |
size_t (*sizeop)(struct _noit_log_stream *); |
|---|
| 55 |
int (*renameop)(struct _noit_log_stream *, const char *); |
|---|
| 56 |
} logops_t; |
|---|
| 57 |
|
|---|
| 58 |
#ifdef noit_log_impl |
|---|
| 59 |
typedef struct _noit_log_stream * noit_log_stream_t; |
|---|
| 60 |
#else |
|---|
| 61 |
typedef struct _noit_log_stream { |
|---|
| 62 |
unsigned enabled:1; |
|---|
| 63 |
unsigned debug:1; |
|---|
| 64 |
unsigned timestamps:1; |
|---|
| 65 |
/* totally private type, don't even think about it */ |
|---|
| 66 |
} * noit_log_stream_t; |
|---|
| 67 |
#endif |
|---|
| 68 |
|
|---|
| 69 |
extern noit_log_stream_t noit_stderr; |
|---|
| 70 |
extern noit_log_stream_t noit_debug; |
|---|
| 71 |
extern noit_log_stream_t noit_error; |
|---|
| 72 |
|
|---|
| 73 |
API_EXPORT(int) noit_log_global_enabled(); |
|---|
| 74 |
API_EXPORT(void) noit_log_init(); |
|---|
| 75 |
API_EXPORT(int) noit_log_reopen_all(); |
|---|
| 76 |
API_EXPORT(void) noit_register_logops(const char *name, logops_t *ops); |
|---|
| 77 |
API_EXPORT(void *) noit_log_stream_get_ctx(noit_log_stream_t); |
|---|
| 78 |
API_EXPORT(void) noit_log_stream_set_ctx(noit_log_stream_t, void *); |
|---|
| 79 |
API_EXPORT(const char *) noit_log_stream_get_type(noit_log_stream_t); |
|---|
| 80 |
API_EXPORT(const char *) noit_log_stream_get_name(noit_log_stream_t); |
|---|
| 81 |
API_EXPORT(const char *) noit_log_stream_get_path(noit_log_stream_t); |
|---|
| 82 |
|
|---|
| 83 |
API_EXPORT(noit_log_stream_t) |
|---|
| 84 |
noit_log_stream_new(const char *, const char *, const char *, |
|---|
| 85 |
void *, noit_hash_table *); |
|---|
| 86 |
API_EXPORT(noit_log_stream_t) |
|---|
| 87 |
noit_log_stream_new_on_fd(const char *, int, noit_hash_table *); |
|---|
| 88 |
API_EXPORT(noit_log_stream_t) |
|---|
| 89 |
noit_log_stream_new_on_file(const char *, noit_hash_table *); |
|---|
| 90 |
API_EXPORT(noit_log_stream_t) noit_log_stream_find(const char *); |
|---|
| 91 |
API_EXPORT(void) noit_log_stream_remove(const char *name); |
|---|
| 92 |
API_EXPORT(void) noit_log_stream_add_stream(noit_log_stream_t ls, |
|---|
| 93 |
noit_log_stream_t outlet); |
|---|
| 94 |
API_EXPORT(noit_log_stream_t) |
|---|
| 95 |
noit_log_stream_remove_stream(noit_log_stream_t ls, |
|---|
| 96 |
const char *name); |
|---|
| 97 |
API_EXPORT(void) noit_log_stream_reopen(noit_log_stream_t ls); |
|---|
| 98 |
|
|---|
| 99 |
#define NOIT_LOG_RENAME_AUTOTIME ((const char *)-1) |
|---|
| 100 |
|
|---|
| 101 |
API_EXPORT(int) noit_log_stream_rename(noit_log_stream_t ls, const char *); |
|---|
| 102 |
API_EXPORT(void) noit_log_stream_close(noit_log_stream_t ls); |
|---|
| 103 |
API_EXPORT(size_t) noit_log_stream_size(noit_log_stream_t ls); |
|---|
| 104 |
API_EXPORT(size_t) noit_log_stream_written(noit_log_stream_t ls); |
|---|
| 105 |
API_EXPORT(const char *) noit_log_stream_get_property(noit_log_stream_t ls, |
|---|
| 106 |
const char *); |
|---|
| 107 |
API_EXPORT(void) noit_log_stream_set_property(noit_log_stream_t ls, |
|---|
| 108 |
const char *, const char *); |
|---|
| 109 |
API_EXPORT(void) noit_log_stream_free(noit_log_stream_t ls); |
|---|
| 110 |
API_EXPORT(int) noit_vlog(noit_log_stream_t ls, struct timeval *, |
|---|
| 111 |
const char *file, int line, |
|---|
| 112 |
const char *format, va_list arg); |
|---|
| 113 |
API_EXPORT(int) noit_log(noit_log_stream_t ls, struct timeval *, |
|---|
| 114 |
const char *file, int line, |
|---|
| 115 |
const char *format, ...) |
|---|
| 116 |
#ifdef __GNUC__ |
|---|
| 117 |
__attribute__ ((format (printf, 5, 6))) |
|---|
| 118 |
#endif |
|---|
| 119 |
; |
|---|
| 120 |
|
|---|
| 121 |
#define noitLT(ls, t, args...) do { \ |
|---|
| 122 |
if((ls) && (noit_log_global_enabled() || (ls)->enabled)) \ |
|---|
| 123 |
noit_log(ls, t, __FILE__, __LINE__, args); \ |
|---|
| 124 |
} while(0) |
|---|
| 125 |
#define noitL(ls, args...) do { \ |
|---|
| 126 |
if((ls) && (noit_log_global_enabled() || (ls)->enabled)) { \ |
|---|
| 127 |
struct timeval __noitL_now; \ |
|---|
| 128 |
gettimeofday(&__noitL_now, NULL); \ |
|---|
| 129 |
noit_log(ls, &__noitL_now, __FILE__, __LINE__, args); \ |
|---|
| 130 |
} \ |
|---|
| 131 |
} while(0) |
|---|
| 132 |
|
|---|
| 133 |
#define SETUP_LOG(a, b) do { if(!a##_log) a##_log = noit_log_stream_find(#a); \ |
|---|
| 134 |
if(!a##_log) { b; } } while(0) |
|---|
| 135 |
|
|---|
| 136 |
#endif |
|---|