33 #include <sys/types.h>
59 # define WARN_COLOR "\033[031m"
60 # define INFO_COLOR "\033[032m"
61 # define DEBUG_COLOR "\033[036m"
62 # define END_COLOR "\033[000m"
65 const char *_shm_tty_color[4] =
66 { END_COLOR, WARN_COLOR, INFO_COLOR, DEBUG_COLOR };
69 int _shm_with_tty_color = 1;
76 #define DEFAULT_SPEW_FILE (stdout)
80 static FILE *spew_file = NULL;
81 static int spew_level = DEFAULT_SPEW_LEVEL;
84 const char *_shm_spew_levels[] =
86 "",
"WARN",
"INFO",
"DEBUG"
90 void _shm_spew_init(
void)
101 env = getenv(
"SHM_SPEW");
112 val = strtol(env, (
char **) NULL, 10);
113 if(errno || val == 0) val = LONG_MIN;
117 if(!strncasecmp(
"WARN", env, 1) ||
118 !strncasecmp(
"NOTICE", env, 4) ||
119 !strncasecmp(
"ON", env, 2) ||
120 !strncasecmp(
"YES", env, 1) ||
121 (val != LONG_MIN && val == 1))
123 else if(!strncasecmp(
"DEBUG", env, 1) ||
124 (val != LONG_MIN && val >= 3))
126 else if(!strncasecmp(
"OFF", env, 2) ||
127 !strncasecmp(
"NONE", env, 1) ||
128 !strncasecmp(
"SILENT", env, 1) ||
130 (val != LONG_MIN && val <= 0))
131 spew_level = _SILENT;
139 spew_file = DEFAULT_SPEW_FILE;
140 env = getenv(
"SHM_SPEW_FILE");
145 if(!strcasecmp(
"STDOUT", env) ||
148 else if(!strcasecmp(
"STDERR", env) ||
152 spew_file = fopen(env,
"a");
155 spew_file = DEFAULT_SPEW_FILE;
164 env = getenv(
"SHM_TTY_COLOR");
166 env = getenv(
"SHM_COLOR");
172 if(!strncasecmp(
"OFF", env, 2) ||
174 !strncasecmp(
"NO", env, 1) ||
175 (!strncasecmp(
"AUTO", env, 1) && !isatty(fileno(spew_file))))
177 _shm_with_tty_color = 0;
180 else if(!isatty(fileno(spew_file)))
182 _shm_with_tty_color = 0;
188 SPEW(_DEBUG,
"SHM_SPEW level is set to DEBUG");
194 void _shm_spew(
int level,
const char *fmt, ...)
196 if(spew_level >= level)
200 vfprintf(spew_file, fmt, ap);