OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google 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 disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written 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 |
| 34 #include "src/core/debug/trace.h" |
| 35 |
| 36 #include <string.h> |
| 37 |
| 38 #include <grpc/grpc.h> |
| 39 #include <grpc/support/alloc.h> |
| 40 #include <grpc/support/log.h> |
| 41 #include "src/core/support/env.h" |
| 42 |
| 43 typedef struct tracer { |
| 44 const char *name; |
| 45 int *flag; |
| 46 struct tracer *next; |
| 47 } tracer; |
| 48 static tracer *tracers; |
| 49 |
| 50 void grpc_register_tracer(const char *name, int *flag) { |
| 51 tracer *t = gpr_malloc(sizeof(*t)); |
| 52 t->name = name; |
| 53 t->flag = flag; |
| 54 t->next = tracers; |
| 55 *flag = 0; |
| 56 tracers = t; |
| 57 } |
| 58 |
| 59 static void add(const char *beg, const char *end, char ***ss, size_t *ns) { |
| 60 size_t n = *ns; |
| 61 size_t np = n + 1; |
| 62 char *s; |
| 63 size_t len; |
| 64 GPR_ASSERT(end >= beg); |
| 65 len = (size_t)(end - beg); |
| 66 s = gpr_malloc(len + 1); |
| 67 memcpy(s, beg, len); |
| 68 s[len] = 0; |
| 69 *ss = gpr_realloc(*ss, sizeof(char **) * np); |
| 70 (*ss)[n] = s; |
| 71 *ns = np; |
| 72 } |
| 73 |
| 74 static void split(const char *s, char ***ss, size_t *ns) { |
| 75 const char *c = strchr(s, ','); |
| 76 if (c == NULL) { |
| 77 add(s, s + strlen(s), ss, ns); |
| 78 } else { |
| 79 add(s, c, ss, ns); |
| 80 split(c + 1, ss, ns); |
| 81 } |
| 82 } |
| 83 |
| 84 static void parse(const char *s) { |
| 85 char **strings = NULL; |
| 86 size_t nstrings = 0; |
| 87 size_t i; |
| 88 split(s, &strings, &nstrings); |
| 89 |
| 90 for (i = 0; i < nstrings; i++) { |
| 91 grpc_tracer_set_enabled(strings[i], 1); |
| 92 } |
| 93 |
| 94 for (i = 0; i < nstrings; i++) { |
| 95 gpr_free(strings[i]); |
| 96 } |
| 97 gpr_free(strings); |
| 98 } |
| 99 |
| 100 void grpc_tracer_init(const char *env_var) { |
| 101 char *e = gpr_getenv(env_var); |
| 102 if (e != NULL) { |
| 103 parse(e); |
| 104 gpr_free(e); |
| 105 } |
| 106 } |
| 107 |
| 108 void grpc_tracer_shutdown(void) { |
| 109 while (tracers) { |
| 110 tracer *t = tracers; |
| 111 tracers = t->next; |
| 112 gpr_free(t); |
| 113 } |
| 114 } |
| 115 |
| 116 int grpc_tracer_set_enabled(const char *name, int enabled) { |
| 117 tracer *t; |
| 118 if (0 == strcmp(name, "all")) { |
| 119 for (t = tracers; t; t = t->next) { |
| 120 *t->flag = 1; |
| 121 } |
| 122 } else { |
| 123 int found = 0; |
| 124 for (t = tracers; t; t = t->next) { |
| 125 if (0 == strcmp(name, t->name)) { |
| 126 *t->flag = enabled; |
| 127 found = 1; |
| 128 } |
| 129 } |
| 130 if (!found) { |
| 131 gpr_log(GPR_ERROR, "Unknown trace var: '%s'", name); |
| 132 return 0; /* early return */ |
| 133 } |
| 134 } |
| 135 return 1; |
| 136 } |
OLD | NEW |