blob: 0befc09f4a1704f5e3a703399e60653f7f0dc1af [file] [log] [blame]
robert.swiecki3bb518c2010-10-14 00:48:24 +00001/*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00002 *
robert.swiecki@gmail.com81189852015-02-14 23:44:55 +00003 * honggfuzz - log messages
4 * -----------------------------------------
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00005 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +00006 * Author: Robert Swiecki <swiecki@google.com>
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00007 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +00008 * Copyright 2010-2015 by Google Inc. All Rights Reserved.
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00009 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License. You may obtain
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000012 * a copy of the License at
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000013 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000014 * http://www.apache.org/licenses/LICENSE-2.0
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000015 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000016 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
19 * implied. See the License for the specific language governing
20 * permissions and limitations under the License.
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000021 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000022 */
robert.swiecki3bb518c2010-10-14 00:48:24 +000023
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000024#include "common.h"
25#include "log.h"
26
robert.swiecki3bb518c2010-10-14 00:48:24 +000027#include <ctype.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000028#include <errno.h>
robert.swiecki@gmail.com292d60a2015-02-13 12:51:57 +000029#include <pthread.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000030#include <stdarg.h>
31#include <stdio.h>
32#include <stdlib.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000033#include <string.h>
34#include <sys/time.h>
robert.swiecki@gmail.comdfde1c72015-02-18 13:22:55 +000035#include <sys/types.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000036#include <time.h>
robert.swiecki@gmail.comebc1cac2011-07-02 03:15:51 +000037#include <unistd.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000038
robert.swiecki@gmail.com6f319912015-02-28 05:01:37 +000039#include "util.h"
40
robert.swiecki@gmail.com1dd36d42015-02-18 13:08:51 +000041static unsigned int log_minLevel = l_INFO;
robert.swiecki@gmail.com70d31302015-03-01 18:51:13 +000042static bool log_isStdioTTY;
robert.swiecki@gmail.com292d60a2015-02-13 12:51:57 +000043static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
robert.swiecki3bb518c2010-10-14 00:48:24 +000044
robert.swiecki@gmail.com3a1845c2015-03-01 15:25:58 +000045/* *INDENT-OFF* */
robert.swiecki@gmail.come9571a02015-02-12 00:22:50 +000046static const struct {
47 const char *descr;
48 const char *prefix;
49} logLevels[] = {
robert.swiecki@gmail.com3a1845c2015-03-01 15:25:58 +000050 { "[FATAL]", "\033[1;41m" },
51 { "[ERROR]", "\033[1;31m" },
52 { "[WARNING]", "\033[1;35m" },
53 { "[INFO]", "\033[1m" },
54 { "[DEBUG]", "\033[0;37m" },
robert.swiecki@gmail.come9571a02015-02-12 00:22:50 +000055};
robert.swiecki@gmail.com3a1845c2015-03-01 15:25:58 +000056/* *INDENT-ON* */
robert.swiecki@gmail.come9571a02015-02-12 00:22:50 +000057
robert.swiecki3bb518c2010-10-14 00:48:24 +000058__attribute__ ((constructor))
robert.swiecki@gmail.com3a1845c2015-03-01 15:25:58 +000059void log_init(void)
robert.swiecki3bb518c2010-10-14 00:48:24 +000060{
robert.swiecki@gmail.com3a1845c2015-03-01 15:25:58 +000061 log_isStdioTTY = (isatty(STDOUT_FILENO) == 1);
robert.swiecki3bb518c2010-10-14 00:48:24 +000062}
63
64void log_setMinLevel(log_level_t dl)
65{
66 log_minLevel = dl;
67}
68
robert.swiecki@gmail.combb5d2642015-02-25 20:00:00 +000069void log_msg(log_level_t dl, bool perr, const char *file, const char *func, int line,
robert.swiecki@gmail.com6f319912015-02-28 05:01:37 +000070 const char *fmt, ...)
robert.swiecki3bb518c2010-10-14 00:48:24 +000071{
robert.swiecki@gmail.com70d31302015-03-01 18:51:13 +000072 char msg[8192] = { "\0" };
robert.swiecki@gmail.com6f319912015-02-28 05:01:37 +000073
robert.swiecki@gmail.com341dd122015-02-18 13:05:20 +000074 if (dl > log_minLevel) {
robert.swiecki@gmail.com1dd36d42015-02-18 13:08:51 +000075 if (dl == l_FATAL) {
76 exit(EXIT_FAILURE);
robert.swiecki@gmail.com341dd122015-02-18 13:05:20 +000077 }
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +000078 return;
robert.swiecki@gmail.com341dd122015-02-18 13:05:20 +000079 }
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +000080
robert.swiecki3bb518c2010-10-14 00:48:24 +000081 char strerr[512];
82 if (perr) {
robert.swiecki@gmail.com528fa202011-06-22 17:25:07 +000083 snprintf(strerr, sizeof(strerr), "%s", strerror(errno));
robert.swiecki3bb518c2010-10-14 00:48:24 +000084 }
85
robert.swiecki3bb518c2010-10-14 00:48:24 +000086 struct tm tm;
87 struct timeval tv;
88
89 gettimeofday(&tv, NULL);
90 localtime_r((const time_t *)&tv.tv_sec, &tm);
91
robert.swiecki@gmail.com70d31302015-03-01 18:51:13 +000092 if (log_isStdioTTY == true) {
robert.swiecki@gmail.com6f319912015-02-28 05:01:37 +000093 util_ssnprintf(msg, sizeof(msg), "%s", logLevels[dl].prefix);
robert.swiecki@gmail.comebc1cac2011-07-02 03:15:51 +000094 }
robert.swiecki5fa9d902015-02-25 15:31:56 +000095#if defined(_HF_ARCH_LINUX)
robert.swiecki@gmail.comdfde1c72015-02-18 13:22:55 +000096#include <unistd.h>
97#include <sys/syscall.h>
robert.swiecki@gmail.com4fc19692015-02-25 15:45:11 +000098 pid_t pid = (pid_t) syscall(__NR_gettid);
robert.swiecki@gmail.comdb8145a2015-03-01 16:19:10 +000099#else /* defined(_HF_ARCH_LINUX) */
robert.swiecki5fa9d902015-02-25 15:31:56 +0000100 pid_t pid = getpid();
robert.swiecki@gmail.comdb8145a2015-03-01 16:19:10 +0000101#endif /* defined(_HF_ARCH_LINUX) */
robert.swiecki@gmail.comdfde1c72015-02-18 13:22:55 +0000102
robert.swiecki@gmail.com1d8a3c12015-02-22 14:54:05 +0000103 if (log_minLevel >= l_DEBUG || log_minLevel == l_FATAL || !log_isStdioTTY) {
robert.swiecki@gmail.com6f319912015-02-28 05:01:37 +0000104 util_ssnprintf(msg, sizeof(msg), "%s [%d] %d/%02d/%02d %02d:%02d:%02d (%s:%s %d) ",
105 logLevels[dl].descr, pid, tm.tm_year + 1900,
106 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, file, func,
107 line);
robert.swiecki3bb518c2010-10-14 00:48:24 +0000108 } else {
robert.swiecki@gmail.com6f319912015-02-28 05:01:37 +0000109 util_ssnprintf(msg, sizeof(msg), "%s ", logLevels[dl].descr);
robert.swiecki3bb518c2010-10-14 00:48:24 +0000110 }
111
112 va_list args;
113 va_start(args, fmt);
robert.swiecki@gmail.com3d096162015-02-28 05:08:51 +0000114 util_vssnprintf(msg, sizeof(msg), fmt, args);
robert.swiecki3bb518c2010-10-14 00:48:24 +0000115 va_end(args);
116
117 if (perr) {
robert.swiecki@gmail.com6f319912015-02-28 05:01:37 +0000118 util_ssnprintf(msg, sizeof(msg), ": %s", strerr);
robert.swiecki3bb518c2010-10-14 00:48:24 +0000119 }
120
robert.swiecki@gmail.com70d31302015-03-01 18:51:13 +0000121 if (log_isStdioTTY == true) {
robert.swiecki@gmail.com6f319912015-02-28 05:01:37 +0000122 util_ssnprintf(msg, sizeof(msg), "\033[0m");
robert.swiecki@gmail.comebc1cac2011-07-02 03:15:51 +0000123 }
124
robert.swiecki@gmail.com6f319912015-02-28 05:01:37 +0000125 util_ssnprintf(msg, sizeof(msg), "\n");
robert.swiecki@gmail.com292d60a2015-02-13 12:51:57 +0000126
robert.swiecki@gmail.com6f319912015-02-28 05:01:37 +0000127 while (pthread_mutex_lock(&log_mutex)) ;
128 if (write(STDOUT_FILENO, msg, strlen(msg)) == -1) {
129 }
robert.swiecki@gmail.com292d60a2015-02-13 12:51:57 +0000130 while (pthread_mutex_unlock(&log_mutex)) ;
robert.swiecki@gmail.come7190b92015-02-14 23:05:42 +0000131
132 if (dl == l_FATAL) {
133 exit(EXIT_FAILURE);
134 }
robert.swiecki3bb518c2010-10-14 00:48:24 +0000135}