spew.c
1 /*
2  shm-arena shared memory arena
3  Copyright (C) 2006-2008 Lance Arsenault (LGPL v3)
4 
5 
6  This file is part of shm-arena.
7 
8  shm-arena is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12 
13  shm-arena is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this program. If not, see
20  <http://www.gnu.org/licenses/>.
21 */
22 
30 #include "config.h"
31 
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdarg.h>
38 #include <errno.h>
39 #include <limits.h>
40 #include <pthread.h>
41 
42 #include "spew.h"
43 
44 
45 #ifdef TTY_COLOR
46 
47 /* see the colors by running in a bash shell
48  * on a interactive terminal:
49 
50 i=30; \
51 while [ "$i" -lt "48" ] ; \
52 do \
53  echo -e " \033[0${i}mCOLOR \\\033[0${i}m\033[000m"; \
54  i=$(($i + 1)); \
55 done
56 
57 */
58 
59 # define WARN_COLOR "\033[031m"
60 # define INFO_COLOR "\033[032m"
61 # define DEBUG_COLOR "\033[036m"
62 # define END_COLOR "\033[000m"
63 
64 DLL_LOCAL
65 const char *_shm_tty_color[4] =
66  { END_COLOR, WARN_COLOR, INFO_COLOR, DEBUG_COLOR };
67 
68 DLL_LOCAL
69 int _shm_with_tty_color = 1;
70 
71 #endif /* #ifdef TTY_COLOR */
72 
73 
74 
75 
76 #define DEFAULT_SPEW_FILE (stdout)
77 
80 static FILE *spew_file = NULL;
81 static int spew_level = DEFAULT_SPEW_LEVEL;
82 
83 DLL_LOCAL
84 const char *_shm_spew_levels[] =
85 {
86  "", "WARN", "INFO", "DEBUG"
87 };
88 
89 DLL_LOCAL
90 void _shm_spew_init(void)
91 {
92  if(spew_file) return;
93 
94  {
95  char *env;
96 
97  /*****************************************************************
98  set SPEW LEVEL
99  *****************************************************************/
100 
101  env = getenv("SHM_SPEW");
102 
103 
104  if(env) /* The user overrides the default compiled in spew level. */
105  {
106  long val;
107  int errno_save;
108 
109  /* try a number value for env */
110  errno_save = errno;
111  errno = 0;
112  val = strtol(env, (char **) NULL, 10);
113  if(errno || val == 0) val = LONG_MIN;
114  errno = errno_save;
115  /* Tried to think of all cases that a user might guess to use
116  * while working as documented. */
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))
122  spew_level = _WARN;
123  else if(!strncasecmp("DEBUG", env, 1) ||
124  (val != LONG_MIN && val >= 3))
125  spew_level = _DEBUG;
126  else if(!strncasecmp("OFF", env, 2) ||
127  !strncasecmp("NONE", env, 1) ||
128  !strncasecmp("SILENT", env, 1) ||
129  !strcmp("0", env) ||
130  (val != LONG_MIN && val <= 0))
131  spew_level = _SILENT;
132  else /* INFO is the default if env is set at all */
133  spew_level = _INFO;
134  }
135 
136  /*****************************************************************
137  set SPEW FILE
138  *****************************************************************/
139  spew_file = DEFAULT_SPEW_FILE;
140  env = getenv("SHM_SPEW_FILE");
141  if(env) /* The user overrides the default spew file. */
142  {
143  /* Tried to think of all cases that a user might guess to use
144  * while working as documented. */
145  if(!strcasecmp("STDOUT", env) ||
146  !strcmp("1", env))
147  spew_file = stdout;
148  else if(!strcasecmp("STDERR", env) ||
149  !strcmp("2", env))
150  spew_file = stderr;
151  else
152  spew_file = fopen(env, "a");
153 
154  if(!spew_file)
155  spew_file = DEFAULT_SPEW_FILE;
156  }
157 
158  /*****************************************************************
159  set TTY_COLOR
160  *****************************************************************/
161 
162 #ifdef TTY_COLOR
163 
164  env = getenv("SHM_TTY_COLOR");
165  if(!env)
166  env = getenv("SHM_COLOR");
167 
168  if(env) /* The user overrides the default compiled in. */
169  {
170  /* Tried to think of all cases that a user might guess to use
171  * while working as documented. */
172  if(!strncasecmp("OFF", env, 2) ||
173  !strcmp("0", env) ||
174  !strncasecmp("NO", env, 1) ||
175  (!strncasecmp("AUTO", env, 1) && !isatty(fileno(spew_file))))
176  /* turn off the color escape strings */
177  _shm_with_tty_color = 0;
178  /* else It's already on. */
179  }
180  else if(!isatty(fileno(spew_file)))
181  /* turn off the color escape strings */
182  _shm_with_tty_color = 0;
183 
184 
185 #endif /* #ifdef TTY_COLOR */
186 
187 
188  SPEW(_DEBUG, "SHM_SPEW level is set to DEBUG");
189  }
190 }
191 
192 
193 DLL_LOCAL
194 void _shm_spew(int level, const char *fmt, ...)
195 {
196  if(spew_level >= level)
197  {
198  va_list ap;
199  va_start(ap, fmt);
200  vfprintf(spew_file, fmt, ap);
201  va_end(ap);
202  }
203 }

Shared Memory Arena version RC-0.0.25