Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 17 | #include <errno.h> |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <stdarg.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <cutils/klog.h> |
| 28 | |
| 29 | static int klog_fd = -1; |
| 30 | static int klog_level = KLOG_DEFAULT_LEVEL; |
| 31 | |
Alex Ray | 157e1b6 | 2014-03-07 12:18:22 -0800 | [diff] [blame] | 32 | int klog_get_level(void) { |
| 33 | return klog_level; |
| 34 | } |
| 35 | |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 36 | void klog_set_level(int level) { |
| 37 | klog_level = level; |
| 38 | } |
| 39 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 40 | void klog_init(void) { |
Ken Sumrall | 7425fd1 | 2013-04-03 13:43:11 -0700 | [diff] [blame] | 41 | if (klog_fd >= 0) return; /* Already initialized */ |
| 42 | |
Elliott Hughes | c02f81c | 2015-03-28 09:51:54 -0700 | [diff] [blame^] | 43 | static const char* name = "/dev/__kmsg__"; |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 44 | if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) { |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 45 | klog_fd = open(name, O_WRONLY | O_CLOEXEC); |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 46 | unlink(name); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | #define LOG_BUF_MAX 512 |
| 51 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 52 | void klog_writev(int level, const struct iovec* iov, int iov_count) { |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 53 | if (level > klog_level) return; |
Ken Sumrall | 7425fd1 | 2013-04-03 13:43:11 -0700 | [diff] [blame] | 54 | if (klog_fd < 0) klog_init(); |
Todd Poynor | 451a6ba | 2013-04-19 12:48:19 -0700 | [diff] [blame] | 55 | if (klog_fd < 0) return; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 56 | TEMP_FAILURE_RETRY(writev(klog_fd, iov, iov_count)); |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 57 | } |
Stephen Smalley | eb3f421 | 2014-02-12 16:17:00 -0500 | [diff] [blame] | 58 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 59 | void klog_write(int level, const char* fmt, ...) { |
| 60 | char buf[LOG_BUF_MAX]; |
Stephen Smalley | eb3f421 | 2014-02-12 16:17:00 -0500 | [diff] [blame] | 61 | va_list ap; |
| 62 | va_start(ap, fmt); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 63 | vsnprintf(buf, sizeof(buf), fmt, ap); |
Stephen Smalley | eb3f421 | 2014-02-12 16:17:00 -0500 | [diff] [blame] | 64 | va_end(ap); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 65 | |
| 66 | buf[LOG_BUF_MAX - 1] = 0; |
| 67 | |
| 68 | struct iovec iov[1]; |
| 69 | iov[0].iov_base = buf; |
| 70 | iov[0].iov_len = strlen(buf); |
| 71 | klog_writev(level, iov, 1); |
Stephen Smalley | eb3f421 | 2014-02-12 16:17:00 -0500 | [diff] [blame] | 72 | } |