blob: abf643f4f38b91127b3d6d2728cc7757a4bbfb7a [file] [log] [blame]
Dima Zavin8f912822011-08-31 18:26:17 -07001/*
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 Hughesda40c002015-03-27 23:20:44 -070017#include <errno.h>
Dima Zavin8f912822011-08-31 18:26:17 -070018#include <fcntl.h>
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070023#include <sys/stat.h>
24#include <sys/types.h>
Dima Zavin8f912822011-08-31 18:26:17 -070025#include <unistd.h>
26
27#include <cutils/klog.h>
28
Dima Zavin8f912822011-08-31 18:26:17 -070029static int klog_level = KLOG_DEFAULT_LEVEL;
30
Alex Ray157e1b62014-03-07 12:18:22 -080031int klog_get_level(void) {
32 return klog_level;
33}
34
Dima Zavin8f912822011-08-31 18:26:17 -070035void klog_set_level(int level) {
36 klog_level = level;
37}
38
Elliott Hughes171a8292016-06-29 16:16:41 -070039static int __open_klog(void) {
40 int fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC);
41 if (fd == -1) {
42 static const char* name = "/dev/__kmsg__";
43 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
44 fd = open(name, O_WRONLY | O_CLOEXEC);
45 unlink(name);
46 }
Nick Kralevich7eb3abd2015-05-18 18:41:23 -070047 }
Elliott Hughes171a8292016-06-29 16:16:41 -070048 return fd;
Dima Zavin8f912822011-08-31 18:26:17 -070049}
50
51#define LOG_BUF_MAX 512
52
Elliott Hughesda40c002015-03-27 23:20:44 -070053void klog_writev(int level, const struct iovec* iov, int iov_count) {
Dima Zavin8f912822011-08-31 18:26:17 -070054 if (level > klog_level) return;
Elliott Hughes171a8292016-06-29 16:16:41 -070055
56 static int klog_fd = __open_klog();
57 if (klog_fd == -1) return;
Elliott Hughesda40c002015-03-27 23:20:44 -070058 TEMP_FAILURE_RETRY(writev(klog_fd, iov, iov_count));
Dima Zavin8f912822011-08-31 18:26:17 -070059}
Stephen Smalleyeb3f4212014-02-12 16:17:00 -050060
Elliott Hughesda40c002015-03-27 23:20:44 -070061void klog_write(int level, const char* fmt, ...) {
Nick Kralevich5db8d6a2016-01-16 16:19:17 -080062 if (level > klog_level) return;
Elliott Hughes171a8292016-06-29 16:16:41 -070063
Elliott Hughesda40c002015-03-27 23:20:44 -070064 char buf[LOG_BUF_MAX];
Stephen Smalleyeb3f4212014-02-12 16:17:00 -050065 va_list ap;
66 va_start(ap, fmt);
Elliott Hughesda40c002015-03-27 23:20:44 -070067 vsnprintf(buf, sizeof(buf), fmt, ap);
Stephen Smalleyeb3f4212014-02-12 16:17:00 -050068 va_end(ap);
Elliott Hughesda40c002015-03-27 23:20:44 -070069
70 buf[LOG_BUF_MAX - 1] = 0;
71
72 struct iovec iov[1];
73 iov[0].iov_base = buf;
74 iov[0].iov_len = strlen(buf);
75 klog_writev(level, iov, 1);
Stephen Smalleyeb3f4212014-02-12 16:17:00 -050076}