void logMessage(int level, struct __sourceloc whence, const char *fmt, ...) { const char *levelstr = "UNKWN:"; switch (level) { case LOG_LEVEL_FATAL: levelstr = "FATAL:"; break; case LOG_LEVEL_ERROR: levelstr = "ERROR:"; break; case LOG_LEVEL_INFO: levelstr = "INFO:"; break; case LOG_LEVEL_WARN: levelstr = "WARN:"; break; case LOG_LEVEL_DEBUG: levelstr = "DEBUG:"; break; } fprintf(stderr, "%s ", levelstr); if (whence.file) { fprintf(stderr, "%s", _trimbuildpath(whence.file)); if (whence.line) fprintf(stderr, ":%u", whence.line); if (whence.function) fprintf(stderr, ":%s()", whence.function); fputc(' ', stderr); } else if (whence.function) { fprintf(stderr, "%s() ", whence.function); } va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fputc('\n', stderr); }
static int _log_prepare(int level, struct __sourceloc where) { if (level == LOG_LEVEL_SILENT) return 0; if (strbuf_is_empty(&logbuf)) strbuf_init(&logbuf, _log_buf, sizeof _log_buf); open_logging(); // Put initial INFO message at start of log file #ifndef ANDROID const char *levelstr = "UNKWN:"; switch (level) { case LOG_LEVEL_FATAL: levelstr = "FATAL:"; break; case LOG_LEVEL_ERROR: levelstr = "ERROR:"; break; case LOG_LEVEL_INFO: levelstr = "INFO:"; break; case LOG_LEVEL_WARN: levelstr = "WARN:"; break; case LOG_LEVEL_DEBUG: levelstr = "DEBUG:"; break; } strbuf_sprintf(&logbuf, "%-6.6s ", levelstr); #endif if (show_pid()) strbuf_sprintf(&logbuf, "[%5u] ", getpid()); if (show_time()) { struct timeval tv; if (gettimeofday(&tv, NULL) == -1) { strbuf_puts(&logbuf, "NOTIME______ "); } else { struct tm tm; char buf[20]; if (strftime(buf, sizeof buf, "%T", localtime_r(&tv.tv_sec, &tm)) == 0) strbuf_puts(&logbuf, "EMPTYTIME___ "); else strbuf_sprintf(&logbuf, "%s.%03u ", buf, tv.tv_usec / 1000); } } if (where.file) { strbuf_sprintf(&logbuf, "%s", _trimbuildpath(where.file)); if (where.line) strbuf_sprintf(&logbuf, ":%u", where.line); if (where.function) strbuf_sprintf(&logbuf, ":%s()", where.function); strbuf_putc(&logbuf, ' '); } else if (where.function) { strbuf_sprintf(&logbuf, "%s() ", where.function); } strbuf_putc(&logbuf, ' '); return 1; }
static void _log_prefix_whence(_log_iterator *it, struct __sourceloc whence) { if (whence.file && whence.file[0]) { xprintf(it->xpf, "%s", _trimbuildpath(whence.file)); if (whence.line) xprintf(it->xpf, ":%u", whence.line); if (whence.function) xprintf(it->xpf, ":%s()", whence.function); xputs(" ", it->xpf); } else if (whence.function && whence.function[0]) { xprintf(it->xpf, "%s() ", whence.function); } }