| 167 | |
| 168 | #if defined(__linux__) |
| 169 | void KernelLogger(android_lkchan::base::LogId, android_lkchan::base::LogSeverity severity, |
| 170 | const char* tag, const char*, unsigned int, const char* msg) { |
| 171 | // clang-format off |
| 172 | static constexpr int kLogSeverityToKernelLogLevel[] = { |
| 173 | [android_lkchan::base::VERBOSE] = 7, // KERN_DEBUG (there is no verbose kernel log |
| 174 | // level) |
| 175 | [android_lkchan::base::DEBUG] = 7, // KERN_DEBUG |
| 176 | [android_lkchan::base::INFO] = 6, // KERN_INFO |
| 177 | [android_lkchan::base::WARNING] = 4, // KERN_WARNING |
| 178 | [android_lkchan::base::ERROR] = 3, // KERN_ERROR |
| 179 | [android_lkchan::base::FATAL_WITHOUT_ABORT] = 2, // KERN_CRIT |
| 180 | [android_lkchan::base::FATAL] = 2, // KERN_CRIT |
| 181 | }; |
| 182 | // clang-format on |
| 183 | static_assert(arraysize(kLogSeverityToKernelLogLevel) == android_lkchan::base::FATAL + 1, |
| 184 | "Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity"); |
| 185 | |
| 186 | static int klog_fd = TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC)); |
| 187 | if (klog_fd == -1) return; |
| 188 | |
| 189 | int level = kLogSeverityToKernelLogLevel[severity]; |
| 190 | |
| 191 | // The kernel's printk buffer is only 1024 bytes. |
| 192 | // TODO: should we automatically break up long lines into multiple lines? |
| 193 | // Or we could log but with something like "..." at the end? |
| 194 | char buf[1024]; |
| 195 | size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %s\n", level, tag, msg); |
| 196 | if (size > sizeof(buf)) { |
| 197 | size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n", |
| 198 | level, tag, size); |
| 199 | } |
| 200 | |
| 201 | iovec iov[1]; |
| 202 | iov[0].iov_base = buf; |
| 203 | iov[0].iov_len = size; |
| 204 | TEMP_FAILURE_RETRY(writev(klog_fd, iov, 1)); |
| 205 | } |
| 206 | #endif |
| 207 | |
| 208 | void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file, unsigned int line, |
nothing calls this directly
no outgoing calls
no test coverage detected