summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDima Zavin2011-08-31 20:26:17 -0500
committerDima Zavin2011-09-02 14:11:17 -0500
commit8f91282ebe1963b9d27f8779ad1342302b293bd2 (patch)
tree5589b2fe97aee2a37d1758e5ac285d5747dfe758 /libcutils/klog.c
parent2d55e02d0f3c27f0c99ab889ab7b73126280a21c (diff)
downloadplatform-system-core-8f91282ebe1963b9d27f8779ad1342302b293bd2.tar.gz
platform-system-core-8f91282ebe1963b9d27f8779ad1342302b293bd2.tar.xz
platform-system-core-8f91282ebe1963b9d27f8779ad1342302b293bd2.zip
init/cutils: move kernel logging interface to libcutils from init
Change-Id: Ia0f91b1fcd6cae69d76bf3dd841340958db938a8 Signed-off-by: Dima Zavin <dima@android.com>
Diffstat (limited to 'libcutils/klog.c')
-rw-r--r--libcutils/klog.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/libcutils/klog.c b/libcutils/klog.c
new file mode 100644
index 000000000..b586a575b
--- /dev/null
+++ b/libcutils/klog.c
@@ -0,0 +1,60 @@
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 <sys/stat.h>
18#include <sys/types.h>
19#include <fcntl.h>
20#include <stdarg.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25
26#include <cutils/klog.h>
27
28static int klog_fd = -1;
29static int klog_level = KLOG_DEFAULT_LEVEL;
30
31void klog_set_level(int level) {
32 klog_level = level;
33}
34
35void klog_init(void)
36{
37 static const char *name = "/dev/__kmsg__";
38 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
39 klog_fd = open(name, O_WRONLY);
40 fcntl(klog_fd, F_SETFD, FD_CLOEXEC);
41 unlink(name);
42 }
43}
44
45#define LOG_BUF_MAX 512
46
47void klog_write(int level, const char *fmt, ...)
48{
49 char buf[LOG_BUF_MAX];
50 va_list ap;
51
52 if (level > klog_level) return;
53 if (klog_fd < 0) return;
54
55 va_start(ap, fmt);
56 vsnprintf(buf, LOG_BUF_MAX, fmt, ap);
57 buf[LOG_BUF_MAX - 1] = 0;
58 va_end(ap);
59 write(klog_fd, buf, strlen(buf));
60}