diff options
author | Elliott Hughes | 2016-06-29 18:16:41 -0500 |
---|---|---|
committer | Elliott Hughes | 2016-06-29 18:16:41 -0500 |
commit | 171a829c39d9298432505fe943ad7128aeefe2b2 (patch) | |
tree | 96ecfe65e6910e41db3867bda7967d50735bbf25 /libcutils/klog.cpp | |
parent | b0d062a497bee38ff2a7c6c965e29af4199c3fe0 (diff) | |
download | platform-system-core-171a829c39d9298432505fe943ad7128aeefe2b2.tar.gz platform-system-core-171a829c39d9298432505fe943ad7128aeefe2b2.tar.xz platform-system-core-171a829c39d9298432505fe943ad7128aeefe2b2.zip |
Make klog_fd thread-safe and make klog_init a no-op.
I'll come back and remove klog_init when I've removed other calls to it.
Change-Id: Iad7fd26d853b4ddc54e9abd44516b6f138cbbfcb
Test: booted N9, looked at "adb shell dmesg" output.
Diffstat (limited to 'libcutils/klog.cpp')
-rw-r--r-- | libcutils/klog.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/libcutils/klog.cpp b/libcutils/klog.cpp new file mode 100644 index 000000000..11ebf88fb --- /dev/null +++ b/libcutils/klog.cpp | |||
@@ -0,0 +1,79 @@ | |||
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 | |||
17 | #include <errno.h> | ||
18 | #include <fcntl.h> | ||
19 | #include <stdarg.h> | ||
20 | #include <stdio.h> | ||
21 | #include <stdlib.h> | ||
22 | #include <string.h> | ||
23 | #include <sys/stat.h> | ||
24 | #include <sys/types.h> | ||
25 | #include <unistd.h> | ||
26 | |||
27 | #include <cutils/klog.h> | ||
28 | |||
29 | static int klog_level = KLOG_DEFAULT_LEVEL; | ||
30 | |||
31 | int klog_get_level(void) { | ||
32 | return klog_level; | ||
33 | } | ||
34 | |||
35 | void klog_set_level(int level) { | ||
36 | klog_level = level; | ||
37 | } | ||
38 | |||
39 | void klog_init(void) { | ||
40 | } | ||
41 | |||
42 | static int __open_klog(void) { | ||
43 | int fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC); | ||
44 | if (fd == -1) { | ||
45 | static const char* name = "/dev/__kmsg__"; | ||
46 | if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) { | ||
47 | fd = open(name, O_WRONLY | O_CLOEXEC); | ||
48 | unlink(name); | ||
49 | } | ||
50 | } | ||
51 | return fd; | ||
52 | } | ||
53 | |||
54 | #define LOG_BUF_MAX 512 | ||
55 | |||
56 | void klog_writev(int level, const struct iovec* iov, int iov_count) { | ||
57 | if (level > klog_level) return; | ||
58 | |||
59 | static int klog_fd = __open_klog(); | ||
60 | if (klog_fd == -1) return; | ||
61 | TEMP_FAILURE_RETRY(writev(klog_fd, iov, iov_count)); | ||
62 | } | ||
63 | |||
64 | void klog_write(int level, const char* fmt, ...) { | ||
65 | if (level > klog_level) return; | ||
66 | |||
67 | char buf[LOG_BUF_MAX]; | ||
68 | va_list ap; | ||
69 | va_start(ap, fmt); | ||
70 | vsnprintf(buf, sizeof(buf), fmt, ap); | ||
71 | va_end(ap); | ||
72 | |||
73 | buf[LOG_BUF_MAX - 1] = 0; | ||
74 | |||
75 | struct iovec iov[1]; | ||
76 | iov[0].iov_base = buf; | ||
77 | iov[0].iov_len = strlen(buf); | ||
78 | klog_writev(level, iov, 1); | ||
79 | } | ||