blob: 410499ca83db0bfc8c4f7e89547a59ce28617f9e [file] [log] [blame]
robert.swiecki3bb518c2010-10-14 00:48:24 +00001/*
2
3 honggfuzz - log messages
4 -----------------------------------------
5
6 Author: Robert Swiecki <swiecki@google.com>
7
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +00008 Copyright 2010-2015 by Google Inc. All Rights Reserved.
robert.swiecki3bb518c2010-10-14 00:48:24 +00009
10 Licensed under the Apache License, Version 2.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
13
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 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 implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21
22*/
23
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>
29#include <stdarg.h>
30#include <stdio.h>
31#include <stdlib.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000032#include <string.h>
33#include <sys/time.h>
34#include <time.h>
robert.swiecki@gmail.comebc1cac2011-07-02 03:15:51 +000035#include <unistd.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000036
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +000037static unsigned int log_minLevel;
38static bool log_isStdioTTY;
robert.swiecki3bb518c2010-10-14 00:48:24 +000039
40__attribute__ ((constructor))
41void log_init(void
42 )
43{
44 log_minLevel = l_INFO;
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +000045 if (isatty(STDOUT_FILENO) == 1) {
robert.swiecki@gmail.comebc1cac2011-07-02 03:15:51 +000046 log_isStdioTTY = true;
47 } else {
48 log_isStdioTTY = false;
49 }
robert.swiecki3bb518c2010-10-14 00:48:24 +000050}
51
52void log_setMinLevel(log_level_t dl)
53{
54 log_minLevel = dl;
55}
56
57void log_msg(log_level_t dl,
58 bool perr, const char *file, const char *func, int line, const char *fmt, ...
59 )
60{
61 struct {
62 char *descr;
63 char *prefix;
64 } logLevels[] = {
65 {
66 "[FATAL]", "\033[1;31m"}, {
67 "[ERROR]", "\033[1;35m"}, {
68 "[WARNING]", "\033[1;34m"}, {
69 "[INFO]", "\033[1m"}, {
70 "[DEBUG]", "\033[0;37m"}
71 };
72
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +000073 if (dl > log_minLevel)
74 return;
75
robert.swiecki3bb518c2010-10-14 00:48:24 +000076 char strerr[512];
77 if (perr) {
robert.swiecki@gmail.com528fa202011-06-22 17:25:07 +000078 snprintf(strerr, sizeof(strerr), "%s", strerror(errno));
robert.swiecki3bb518c2010-10-14 00:48:24 +000079 }
80
robert.swiecki3bb518c2010-10-14 00:48:24 +000081 struct tm tm;
82 struct timeval tv;
83
84 gettimeofday(&tv, NULL);
85 localtime_r((const time_t *)&tv.tv_sec, &tm);
86
robert.swiecki@gmail.comebc1cac2011-07-02 03:15:51 +000087 if (log_isStdioTTY) {
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +000088 dprintf(STDOUT_FILENO, "%s", logLevels[dl].prefix);
robert.swiecki@gmail.comebc1cac2011-07-02 03:15:51 +000089 }
robert.swiecki3bb518c2010-10-14 00:48:24 +000090
robert.swiecki@gmail.comebc1cac2011-07-02 03:15:51 +000091 if (log_minLevel >= l_DEBUG || !log_isStdioTTY) {
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +000092 dprintf
93 (STDOUT_FILENO, "%s [%d] %d/%02d/%02d %02d:%02d:%02d (%s:%s %d) ",
groebert@google.com1bd4c212013-06-19 11:13:56 +000094 logLevels[dl].descr, getpid(), tm.tm_year + 1900, tm.tm_mon + 1,
robert.swiecki3bb518c2010-10-14 00:48:24 +000095 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, file, func, line);
96 } else {
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +000097 dprintf(STDOUT_FILENO, "%s ", logLevels[dl].descr);
robert.swiecki3bb518c2010-10-14 00:48:24 +000098 }
99
100 va_list args;
101 va_start(args, fmt);
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +0000102 vdprintf(STDOUT_FILENO, fmt, args);
robert.swiecki3bb518c2010-10-14 00:48:24 +0000103 va_end(args);
104
105 if (perr) {
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +0000106 dprintf(STDOUT_FILENO, ": %s", strerr);
robert.swiecki3bb518c2010-10-14 00:48:24 +0000107 }
108
robert.swiecki@gmail.comebc1cac2011-07-02 03:15:51 +0000109 if (log_isStdioTTY) {
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +0000110 dprintf(STDOUT_FILENO, "\033[0m");
robert.swiecki@gmail.comebc1cac2011-07-02 03:15:51 +0000111 }
112
robert.swiecki@gmail.comcdf18f92015-02-11 22:22:18 +0000113 dprintf(STDOUT_FILENO, "\n");
robert.swiecki3bb518c2010-10-14 00:48:24 +0000114 fflush(stdout);
115}