robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 1 | /* |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 2 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 3 | nsjail - logging |
| 4 | ----------------------------------------- |
| 5 | |
| 6 | Copyright 2014 Google Inc. All Rights Reserved. |
| 7 | |
| 8 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | you may not use this file except in compliance with the License. |
| 10 | You may obtain a copy of the License at |
| 11 | |
| 12 | http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | |
| 14 | Unless required by applicable law or agreed to in writing, software |
| 15 | distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | See the License for the specific language governing permissions and |
| 18 | limitations under the License. |
| 19 | |
| 20 | */ |
Jagger | 0326734 | 2016-03-11 02:40:47 +0100 | [diff] [blame] | 21 | |
| 22 | #include "common.h" |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 23 | #include "log.h" |
| 24 | |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 25 | #include <errno.h> |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 26 | #include <fcntl.h> |
| 27 | #include <getopt.h> |
| 28 | #include <limits.h> |
Jagger | 9402137 | 2015-10-11 17:01:53 +0200 | [diff] [blame] | 29 | #include <pthread.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 30 | #include <stdarg.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 33 | #include <string.h> |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 34 | #include <sys/stat.h> |
robert.swiecki@gmail.com | dfde1c7 | 2015-02-18 13:22:55 +0000 | [diff] [blame] | 35 | #include <sys/types.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 36 | #include <time.h> |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 37 | #include <unistd.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 38 | |
Robert Swiecki | 94d54ff | 2016-09-06 14:38:21 +0200 | [diff] [blame] | 39 | #include "util.h" |
Robert Swiecki | 0212d69 | 2016-08-30 16:45:33 +0200 | [diff] [blame] | 40 | |
Anestis Bechtsoudis | c947332 | 2015-10-09 12:59:17 -0700 | [diff] [blame] | 41 | #if defined(_HF_ARCH_LINUX) |
| 42 | #include <sys/syscall.h> |
| 43 | #define __hf_pid() (pid_t) syscall(__NR_gettid) |
| 44 | #else /* defined(_HF_ARCH_LINUX) */ |
| 45 | #define __hf_pid() getpid() |
| 46 | #endif /* defined(_HF_ARCH_LINUX) */ |
| 47 | |
Robert Swiecki | 1c04003 | 2016-02-16 18:51:23 +0100 | [diff] [blame] | 48 | static int log_fd; |
| 49 | static bool log_fd_isatty; |
| 50 | enum llevel_t log_level; |
Robert Swiecki | 0212d69 | 2016-08-30 16:45:33 +0200 | [diff] [blame] | 51 | static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 52 | |
Robert Swiecki | 1c04003 | 2016-02-16 18:51:23 +0100 | [diff] [blame] | 53 | __attribute__ ((constructor)) |
| 54 | static void log_init(void) |
| 55 | { |
| 56 | log_level = INFO; |
Robert Swiecki | 54a195d | 2016-03-16 16:34:32 +0100 | [diff] [blame] | 57 | log_fd = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 0); |
| 58 | if (log_fd == -1) { |
| 59 | log_fd = STDERR_FILENO; |
| 60 | } |
Robert Swiecki | 1c04003 | 2016-02-16 18:51:23 +0100 | [diff] [blame] | 61 | log_fd_isatty = isatty(log_fd); |
| 62 | } |
| 63 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 64 | /* |
| 65 | * Log to stderr by default. Use a dup()d fd, because in the future we'll associate the |
| 66 | * connection socket with fd (0, 1, 2). |
| 67 | */ |
| 68 | bool logInitLogFile(const char *logfile, enum llevel_t ll) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 69 | { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 70 | log_level = ll; |
Jagger | 9402137 | 2015-10-11 17:01:53 +0200 | [diff] [blame] | 71 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 72 | if (logfile == NULL) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 73 | return true; |
| 74 | } |
Jagger | 9402137 | 2015-10-11 17:01:53 +0200 | [diff] [blame] | 75 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 76 | log_fd = open(logfile, O_CREAT | O_RDWR | O_APPEND, 0640); |
| 77 | if (log_fd == -1) { |
| 78 | log_fd = STDERR_FILENO; |
| 79 | PLOG_E("Couldn't open logfile open('%s')", logfile); |
| 80 | return false; |
| 81 | } |
| 82 | log_fd_isatty = (isatty(log_fd) == 1 ? true : false); |
| 83 | return true; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 86 | void logLog(enum llevel_t ll, const char *fn, int ln, bool perr, const char *fmt, ...) |
Robert Swiecki | 30254b8 | 2015-10-08 16:36:08 +0200 | [diff] [blame] | 87 | { |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 88 | char strerr[512]; |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 89 | if (perr == true) { |
robert.swiecki@gmail.com | 528fa20 | 2011-06-22 17:25:07 +0000 | [diff] [blame] | 90 | snprintf(strerr, sizeof(strerr), "%s", strerror(errno)); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 91 | } |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 92 | struct ll_t { |
Jagger | c62c2a9 | 2016-09-19 03:29:53 +0200 | [diff] [blame] | 93 | const char *descr; |
| 94 | const char *prefix; |
| 95 | const bool print_funcline; |
Robert Swiecki | b2b89b0 | 2016-10-20 18:09:49 +0200 | [diff] [blame] | 96 | const bool print_time; |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 97 | }; |
Jagger | 18f680f | 2016-09-19 03:30:30 +0200 | [diff] [blame] | 98 | static const struct ll_t logLevels[] = { |
Robert Swiecki | b2b89b0 | 2016-10-20 18:09:49 +0200 | [diff] [blame] | 99 | {"F", "\033[7;35m", true, true}, |
| 100 | {"E", "\033[1;31m", true, true}, |
| 101 | {"W", "\033[0;33m", true, true}, |
| 102 | {"I", "\033[1m", false, true}, |
| 103 | {"D", "\033[0;4m", true, true}, |
| 104 | {"HR", "\033[0m", false, false}, |
| 105 | {"HB", "\033[1m", false, false}, |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 106 | }; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 107 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 108 | time_t ltstamp = time(NULL); |
| 109 | struct tm utctime; |
| 110 | localtime_r(<stamp, &utctime); |
| 111 | char timestr[32]; |
| 112 | if (strftime(timestr, sizeof(timestr) - 1, "%FT%T%z", &utctime) == 0) { |
| 113 | timestr[0] = '\0'; |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 114 | } |
robert.swiecki@gmail.com | dfde1c7 | 2015-02-18 13:22:55 +0000 | [diff] [blame] | 115 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 116 | /* Start printing logs */ |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 117 | { |
Robert Swiecki | 0212d69 | 2016-08-30 16:45:33 +0200 | [diff] [blame] | 118 | MX_SCOPED_LOCK(&log_mutex); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 119 | |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 120 | if (log_fd_isatty) { |
| 121 | dprintf(log_fd, "%s", logLevels[ll].prefix); |
| 122 | } |
Robert Swiecki | b2b89b0 | 2016-10-20 18:09:49 +0200 | [diff] [blame] | 123 | if (logLevels[ll].print_time) { |
| 124 | dprintf(log_fd, "[%s][%s][%d] ", timestr, logLevels[ll].descr, __hf_pid()); |
Robert Swiecki | 9c90094 | 2016-10-20 18:10:14 +0200 | [diff] [blame] | 125 | } |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 126 | if (logLevels[ll].print_funcline) { |
Robert Swiecki | b2b89b0 | 2016-10-20 18:09:49 +0200 | [diff] [blame] | 127 | dprintf(log_fd, "%s():%d ", fn, ln); |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 128 | } |
Jagger | 9402137 | 2015-10-11 17:01:53 +0200 | [diff] [blame] | 129 | |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 130 | va_list args; |
| 131 | va_start(args, fmt); |
| 132 | vdprintf(log_fd, fmt, args); |
| 133 | va_end(args); |
| 134 | |
| 135 | if (perr == true) { |
| 136 | dprintf(log_fd, ": %s", strerr); |
| 137 | } |
| 138 | if (log_fd_isatty) { |
| 139 | dprintf(log_fd, "\033[0m"); |
| 140 | } |
| 141 | dprintf(log_fd, "\n"); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 142 | } |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 143 | /* End printing logs */ |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 144 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 145 | if (ll == FATAL) { |
| 146 | exit(1); |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 147 | } |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 148 | } |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 149 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 150 | void logStop(int sig) |
| 151 | { |
| 152 | LOG_I("Server stops due to fatal signal (%d) caught. Exiting", sig); |
| 153 | } |
| 154 | |
| 155 | void logRedirectLogFD(int fd) |
| 156 | { |
| 157 | log_fd = fd; |
| 158 | } |
| 159 | |
| 160 | void logDirectlyToFD(const char *msg) |
| 161 | { |
| 162 | dprintf(log_fd, "%s", msg); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 163 | } |
Robert Swiecki | 0212d69 | 2016-08-30 16:45:33 +0200 | [diff] [blame] | 164 | |
| 165 | pthread_mutex_t *logMutexGet(void) |
| 166 | { |
| 167 | return &log_mutex; |
| 168 | } |