Improve logging: add HINT level, NOWHENCE() macro

This commit is contained in:
Andrew Bettison 2013-04-09 17:17:22 +09:30
parent 73342a9659
commit b826ac1f1b
3 changed files with 22 additions and 10 deletions

View File

@ -288,14 +288,14 @@ END_STRUCT_ASSIGN
STRUCT(log)
SUB_STRUCT(log_format_file, file,,)
SUB_STRUCT(log_format, console,, important)
SUB_STRUCT(log_format, console,, console)
SUB_STRUCT(log_format, android,, android)
END_STRUCT
STRUCT_DEFAULT(log_format, important)
STRUCT_DEFAULT(log_format, console)
ATOM_DEFAULT(show_pid, 0)
ATOM_DEFAULT(show_time, 0)
ATOM_DEFAULT(level, LOG_LEVEL_WARN)
ATOM_DEFAULT(level, LOG_LEVEL_HINT)
ATOM_DEFAULT(dump_config, 0)
END_STRUCT_DEFAULT

10
log.c
View File

@ -202,6 +202,7 @@ static void _log_prefix_level(_log_iterator *it, int level)
case LOG_LEVEL_FATAL: levelstr = "FATAL:"; break;
case LOG_LEVEL_ERROR: levelstr = "ERROR:"; break;
case LOG_LEVEL_WARN: levelstr = "WARN:"; break;
case LOG_LEVEL_HINT: levelstr = "HINT:"; break;
case LOG_LEVEL_INFO: levelstr = "INFO:"; break;
case LOG_LEVEL_DEBUG: levelstr = "DEBUG:"; break;
}
@ -261,14 +262,14 @@ static const char *_trimbuildpath(const char *path)
static void _log_prefix_whence(_log_iterator *it, struct __sourceloc whence)
{
if (whence.file) {
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) {
} else if (whence.function && whence.function[0]) {
xprintf(it->xpf, "%s() ", whence.function);
}
}
@ -281,8 +282,9 @@ static void _log_end_line(_log_iterator *it, int level)
switch (level) {
case LOG_LEVEL_FATAL: alevel = ANDROID_LOG_FATAL; break;
case LOG_LEVEL_ERROR: alevel = ANDROID_LOG_ERROR; break;
case LOG_LEVEL_INFO: alevel = ANDROID_LOG_INFO; break;
case LOG_LEVEL_WARN: alevel = ANDROID_LOG_WARN; break;
case LOG_LEVEL_HINT:
case LOG_LEVEL_INFO: alevel = ANDROID_LOG_INFO; break;
case LOG_LEVEL_DEBUG: alevel = ANDROID_LOG_DEBUG; break;
default: abort();
}
@ -865,6 +867,7 @@ const char *log_level_as_string(int level)
case LOG_LEVEL_SILENT: return "silent";
case LOG_LEVEL_DEBUG: return "debug";
case LOG_LEVEL_INFO: return "info";
case LOG_LEVEL_HINT: return "hint";
case LOG_LEVEL_WARN: return "warn";
case LOG_LEVEL_ERROR: return "error";
case LOG_LEVEL_FATAL: return "fatal";
@ -879,6 +882,7 @@ int string_to_log_level(const char *text)
if (strcasecmp(text, "fatal") == 0) return LOG_LEVEL_FATAL;
if (strcasecmp(text, "error") == 0) return LOG_LEVEL_ERROR;
if (strcasecmp(text, "warn") == 0) return LOG_LEVEL_WARN;
if (strcasecmp(text, "hint") == 0) return LOG_LEVEL_HINT;
if (strcasecmp(text, "info") == 0) return LOG_LEVEL_INFO;
if (strcasecmp(text, "debug") == 0) return LOG_LEVEL_DEBUG;
if (strcasecmp(text, "silent") == 0) return LOG_LEVEL_SILENT;

16
log.h
View File

@ -29,9 +29,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define LOG_LEVEL_SILENT (0)
#define LOG_LEVEL_DEBUG (1)
#define LOG_LEVEL_INFO (2)
#define LOG_LEVEL_WARN (3)
#define LOG_LEVEL_ERROR (4)
#define LOG_LEVEL_FATAL (5)
#define LOG_LEVEL_HINT (3)
#define LOG_LEVEL_WARN (4)
#define LOG_LEVEL_ERROR (5)
#define LOG_LEVEL_FATAL (6)
#define LOG_LEVEL_NONE (127)
const char *log_level_as_string(int level);
@ -109,6 +110,7 @@ struct strbuf;
#define __HERE__ ((struct __sourceloc){ .file = __FILE__, .line = __LINE__, .function = __FUNCTION__ })
#define __NOWHERE__ ((struct __sourceloc){ .file = NULL, .line = 0, .function = NULL })
#define __NOWHENCE__ ((struct __sourceloc){ .file = "", .line = 0, .function = NULL })
#define __WHENCE__ (__whence.file ? __whence : __HERE__)
@ -118,6 +120,8 @@ struct strbuf;
#define logMessage_perror(L,whence,F,...) (logMessage(L, whence, F ": %s [errno=%d]", ##__VA_ARGS__, strerror(errno), errno))
#define NOWHENCE(LOGSTMT) do { const struct __sourceloc __whence = __NOWHENCE__; LOGSTMT; } while (0)
#define FATALF(F,...) do { LOGF(LOG_LEVEL_FATAL, F, ##__VA_ARGS__); abort(); exit(-1); } while (1)
#define FATAL(X) FATALF("%s", (X))
#define FATALF_perror(F,...) FATALF(F ": %s [errno=%d]", ##__VA_ARGS__, strerror(errno), errno)
@ -129,12 +133,16 @@ struct strbuf;
#define WHYNULL(X) (WHYFNULL("%s", (X)))
#define WHYF_perror(F,...) (LOGF_perror(LOG_LEVEL_ERROR, F, ##__VA_ARGS__), -1)
#define WHY_perror(X) WHYF_perror("%s", (X))
#define WHY_argv(X,ARGC,ARGV) logArgv(LOG_LEVEL_ERROR, __WHENCE__, (X), (ARGC), (ARGV))
#define WARNF(F,...) LOGF(LOG_LEVEL_WARN, F, ##__VA_ARGS__)
#define WARN(X) WARNF("%s", (X))
#define WARNF_perror(F,...) LOGF_perror(LOG_LEVEL_WARN, F, ##__VA_ARGS__)
#define WARN_perror(X) WARNF_perror("%s", (X))
#define WHY_argv(X,ARGC,ARGV) logArgv(LOG_LEVEL_ERROR, __WHENCE__, (X), (ARGC), (ARGV))
#define HINTF(F,...) LOGF(LOG_LEVEL_HINT, F, ##__VA_ARGS__)
#define HINT(X) HINTF("%s", (X))
#define HINT_argv(X,ARGC,ARGV) logArgv(LOG_LEVEL_HINT, __WHENCE__, (X), (ARGC), (ARGV))
#define INFOF(F,...) LOGF(LOG_LEVEL_INFO, F, ##__VA_ARGS__)
#define INFO(X) INFOF("%s", (X))