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 | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 17 | #include <cutils/klog.h> |
| 18 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 29 | #include <cutils/android_get_control_file.h> |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 30 | |
Wei Wang | 677ba31 | 2017-01-12 15:32:26 -0800 | [diff] [blame] | 31 | static int klog_level = KLOG_INFO_LEVEL; |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 32 | |
| 33 | void klog_set_level(int level) { |
| 34 | klog_level = level; |
| 35 | } |
| 36 | |
Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 37 | static int __open_klog(void) { |
Mark Salyzyn | c377843 | 2016-10-27 08:13:42 -0700 | [diff] [blame] | 38 | static const char kmsg_device[] = "/dev/kmsg"; |
| 39 | |
| 40 | int ret = android_get_control_file(kmsg_device); |
| 41 | if (ret >= 0) return ret; |
| 42 | return TEMP_FAILURE_RETRY(open(kmsg_device, O_WRONLY | O_CLOEXEC)); |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | #define LOG_BUF_MAX 512 |
| 46 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 47 | void klog_writev(int level, const struct iovec* iov, int iov_count) { |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 48 | if (level > klog_level) return; |
Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 49 | |
| 50 | static int klog_fd = __open_klog(); |
| 51 | if (klog_fd == -1) return; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 52 | TEMP_FAILURE_RETRY(writev(klog_fd, iov, iov_count)); |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 53 | } |
Stephen Smalley | eb3f421 | 2014-02-12 16:17:00 -0500 | [diff] [blame] | 54 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 55 | void klog_write(int level, const char* fmt, ...) { |
Nick Kralevich | 5db8d6a | 2016-01-16 16:19:17 -0800 | [diff] [blame] | 56 | if (level > klog_level) return; |
Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 57 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 58 | char buf[LOG_BUF_MAX]; |
Stephen Smalley | eb3f421 | 2014-02-12 16:17:00 -0500 | [diff] [blame] | 59 | va_list ap; |
| 60 | va_start(ap, fmt); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 61 | vsnprintf(buf, sizeof(buf), fmt, ap); |
Stephen Smalley | eb3f421 | 2014-02-12 16:17:00 -0500 | [diff] [blame] | 62 | va_end(ap); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 63 | |
| 64 | buf[LOG_BUF_MAX - 1] = 0; |
| 65 | |
| 66 | struct iovec iov[1]; |
| 67 | iov[0].iov_base = buf; |
| 68 | iov[0].iov_len = strlen(buf); |
| 69 | klog_writev(level, iov, 1); |
Stephen Smalley | eb3f421 | 2014-02-12 16:17:00 -0500 | [diff] [blame] | 70 | } |