11static int log_color_ = -1;
13static const char *
const log_names_[] = {
17static const char *
const log_colors_[] = {
32static bool log_env_on_(
const char *name) {
33 const char *v = getenv(name);
34 return v != NULL && v[0] !=
'\0' && strcmp(v,
"0") != 0;
38 const char *no_color = getenv(
"NO_COLOR");
39 if (no_color != NULL && no_color[0] !=
'\0')
return false;
40 if (log_env_on_(
"CLICOLOR_FORCE") || log_env_on_(
"FORCE_COLOR"))
42 if (!isatty(fileno(f)))
return false;
44 const char *term = getenv(
"TERM");
45 return term != NULL && term[0] !=
'\0' && strcmp(term,
"dumb") != 0;
49static FILE *log_vwrite_(
LogLevel level, const
char *file,
int line,
50 const
char *fmt, va_list ap) {
51 FILE *out = log_out_ != NULL ? log_out_ : stderr;
53 const char *on = log_color_ ? log_colors_[level] :
"";
54 const char *off = on[0] !=
'\0' ?
LOG_RESET :
"";
59 bool ok =
str_appendf(&s,
"%s%s%s %s:%d: ", on, log_names_[level], off,
66 fprintf(out,
"%s %s:%d: (out of memory formatting this message)\n",
67 log_names_[level], file, line);
72void log_write_(
LogLevel level,
const char *file,
int line,
const char *fmt,
76 log_vwrite_(level, file, line, fmt, ap);
80void log_fatal_(
const char *file,
int line,
const char *fmt, ...) {
83 FILE *out = log_vwrite_(
LOG_FATAL, file, line, fmt, ap);
void log_set_output(FILE *f)
Write messages to f from now on.
#define LOG_YELLOW
Yellow text.
void log_set_level(LogLevel level)
Drop messages below level from now on.
#define LOG_GREEN
Green text.
#define LOG_RESET
Back to the terminal's default style.
bool log_color_enabled(FILE *f)
True when colour escapes written to f will render as colour.
LogLevel
How much a message matters, from chattiest to most severe.
@ LOG_ERROR
An operation failed.
@ LOG_TRACE
Step-by-step detail, usually off.
@ LOG_INFO
Normal progress worth recording.
@ LOG_DEBUG
Detail for whoever is debugging.
@ LOG_FATAL
The program cannot continue.
@ LOG_WARN
Something odd that the program survived.
size_t len
Bytes of content, excluding the terminator.
char * data
Buffer, or null when nothing is allocated.
void str_free(Str *s)
Free the buffer and reset s to the zero state.
bool str_appendf(Str *s, const char *fmt,...)
Append the printf-style expansion of fmt.
bool str_push(Str *s, char c)
Append the single byte c.
bool str_vappendf(Str *s, const char *fmt, va_list ap)
str_appendf() for a va_list, like vprintf.
Owned, growable byte string.