1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/*
* Copyright 1999-2006 University of Chicago
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "globus_debug.h"
#include "globus_libc.h"
#include "globus_module.h"
#include "globus_common.h"
#ifdef BUILD_DEBUG
#if !defined(strdup)
char * strdup(const char *);
#endif
static
void
globus_l_debug_parse_level_names(
char * my_names,
char ** my_levels)
{
char * name;
int i;
/* check for level names */
/* prune whitespace */
name = my_names + strspn(my_names, " \t\n");
for(i = 0; i < 32; i++)
{
if(*name)
{
my_levels[i] = name;
/* find end of name */
name += strcspn(name, " \t\n");
/* terminate previous name */
if(*name)
{
*(name++) = 0;
/* prune whitespace */
name += strspn(name, " \t\n");
}
}
else
{
my_levels[i] = GLOBUS_NULL;
}
}
}
static
unsigned
globus_l_debug_get_level(
const char * env_name,
char ** my_levels,
char * levels /* gets mangled */)
{
unsigned level;
level = (unsigned) strtoul(levels, NULL, 10);
if(level == 0)
{
char * next_name;
int i;
globus_bool_t negate = GLOBUS_FALSE;
if(*levels == '^')
{
levels++;
negate = GLOBUS_TRUE;
}
/* map all names in levels to my_levels */
do
{
next_name = strchr(levels, '|');
if(next_name)
{
*(next_name++) = 0;
}
for(i = 0;
i < 32 && my_levels[i] && strcmp(levels, my_levels[i]) != 0;
i++);
if(i < 32 && my_levels[i])
{
/* matched name */
level |= 1U << i;
}
else if(strcmp(levels, "ALL") == 0)
{
level = ~(0U);
}
else
{
fprintf(stderr,
_GCSL("Invalid level name (%s) in %s env variable... ignoring\n"),
levels,
env_name);
}
} while((levels = next_name));
if(negate)
{
level = ~level;
}
}
return level;
}
void
globus_debug_init(
const char * env_name,
const char * level_names,
globus_debug_handle_t * handle)
{
char * tmp;
if(handle->file)
{
return;
}
handle->levels = 0;
handle->timestamp_levels = 0;
handle->file = stderr;
handle->thread_ids = GLOBUS_FALSE;
handle->using_file = GLOBUS_FALSE;
tmp = globus_module_getenv(env_name);
if(tmp && *tmp)
{
char * my_names;
char * my_levels[32];
char * levels;
char * filename;
char * show_tid;
char * timestamp_levels;
levels = strdup(tmp);
if(!levels)
{
return;
}
my_names = strdup(level_names);
if(!my_names)
{
free(levels);
return;
}
globus_l_debug_parse_level_names(my_names, my_levels);
show_tid = GLOBUS_NULL;
timestamp_levels = GLOBUS_NULL;
filename = strchr(levels, ',');
if(filename)
{
*(filename++) = 0;
show_tid = strchr(filename, ',');
if(show_tid)
{
*(show_tid++) = 0;
timestamp_levels = strchr(show_tid, ',');
if(timestamp_levels)
{
*(timestamp_levels++) = 0;
}
}
}
handle->levels =
globus_l_debug_get_level(env_name, my_levels, levels);
if(handle->levels)
{
globus_bool_t append_pid = GLOBUS_FALSE;
if(show_tid && *show_tid)
{
int flags;
flags = atoi(show_tid);
if((flags & 0x1))
{
handle->thread_ids = GLOBUS_TRUE;
}
if((flags & 0x2))
{
append_pid = GLOBUS_TRUE;
}
}
if(filename && *filename)
{
char buf[1024];
if(append_pid)
{
sprintf(buf, "%s.%d", filename, (int) getpid());
filename = buf;
}
if(*filename == '#')
{
filename += 1;
#ifndef WIN32
truncate(filename, 0);
#else
{
/* lazy way to truncate by name */
FILE * h = fopen(filename, "w");
if(h)
fclose(h);
}
#endif
}
handle->file = fopen(filename, "a");
if(handle->file)
{
handle->using_file = GLOBUS_TRUE;
setvbuf(handle->file, GLOBUS_NULL, _IONBF, 0);
fprintf(
handle->file, "### %d: %s ###\n", getpid(), env_name);
}
else
{
handle->file = stderr;
fprintf(stderr,
_GCSL("%s: Could not open %s, "
"using stderr for debug messages\n"),
env_name,
filename);
}
}
if(timestamp_levels)
{
handle->timestamp_levels =
globus_l_debug_get_level(
env_name, my_levels, timestamp_levels);
}
}
free(my_names);
free(levels);
}
}
#endif
|