blob: 63dfd596bd8914e8307b12ff230510b042074b1d [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28#include <time.h>
29#include <stdio.h>
30#include <pthread.h>
31#include <unistd.h>
32#include <sys/types.h>
33#include <sys/uio.h>
34#include <arpa/inet.h>
35#include <errno.h>
36#include <string.h>
37#include <stdlib.h>
38#include <stdarg.h>
39#include <fcntl.h>
40
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041#include "logd.h"
42
David 'Digit' Turner6c8a2f22010-06-10 23:34:24 -070043/* should match system/core/include/cutils/logger.h */
44#define LOGGER_LOG_MAIN "log/main"
45#define LOGGER_LOG_RADIO "log/radio"
46#define LOGGER_LOG_EVENTS "log/events"
47#define LOGGER_LOG_SYSTEM "log/system"
48
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049#include <pthread.h>
50
David 'Digit' Turner6c8a2f22010-06-10 23:34:24 -070051#define LOG_BUF_SIZE 1024
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
53typedef enum {
Alexey Tarasovcc05d122008-12-13 08:24:44 +100054 LOG_ID_NONE = 0,
55 LOG_ID_MAIN,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056 LOG_ID_RADIO,
57 LOG_ID_MAX
58} log_id_t;
59
Alexey Tarasovcc05d122008-12-13 08:24:44 +100060/* logger handles writing to object, pointed by log channel id */
61typedef int (*logger_function_t)(log_id_t log_id, struct iovec *vec);
62
63typedef struct {
64 logger_function_t logger;
65 int fd;
66 const char *path;
67} log_channel_t;
68
69static int __write_to_log_init(log_id_t log_id, struct iovec *vec);
70static int __write_to_log_null(log_id_t log_id, struct iovec *vec);
71
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
73
Andy McFadden5cdb2b72009-11-30 17:09:45 -080074static log_channel_t log_channels[LOG_ID_MAX] = {
Alexey Tarasovcc05d122008-12-13 08:24:44 +100075 { __write_to_log_null, -1, NULL },
76 { __write_to_log_init, -1, "/dev/"LOGGER_LOG_MAIN },
77 { __write_to_log_init, -1, "/dev/"LOGGER_LOG_RADIO }
78};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079
Alexey Tarasovcc05d122008-12-13 08:24:44 +100080static int __write_to_log_null(log_id_t log_id, struct iovec *vec)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081{
Mathew Inwood368ee1e2011-07-06 16:51:54 +010082 /*
Alexey Tarasovcc05d122008-12-13 08:24:44 +100083 * ALTERED behaviour from previous version
84 * always returns successful result
85 */
86 int i = 0;
87 size_t res = 0;
88
89 for ( ; i < 3; ++i) {
90 res += vec[i].iov_len;
91 }
92
93 return (int)res;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094}
95
Alexey Tarasovcc05d122008-12-13 08:24:44 +100096/*
97 * it's supposed, that log_id contains valid id always.
98 * this check must be performed in higher level functions
99 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800100static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec)
101{
Mathew Inwood368ee1e2011-07-06 16:51:54 +0100102 ssize_t ret;
103
104 do {
105 ret = writev(log_channels[log_id].fd, vec, 3);
106 } while ((ret < 0) && (errno == EINTR));
107
108 return ret;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109}
110
111static int __write_to_log_init(log_id_t log_id, struct iovec *vec)
112{
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000113 if ((LOG_ID_NONE < log_id) && (log_id < LOG_ID_MAX)) {
114 pthread_mutex_lock(&log_init_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115
Mathew Inwood368ee1e2011-07-06 16:51:54 +0100116 int fd = open(log_channels[log_id].path, O_WRONLY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000118 log_channels[log_id].logger =
119 (fd < 0) ? __write_to_log_null : __write_to_log_kernel;
Andy McFadden5cdb2b72009-11-30 17:09:45 -0800120 log_channels[log_id].fd = fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121
Alexey Tarasovc22da7e2009-12-03 11:26:22 +1000122 log_channels[log_id].fd = fd;
123
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000124 pthread_mutex_unlock(&log_init_lock);
125
126 return log_channels[log_id].logger(log_id, vec);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127 }
128
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000129 /* log_id is invalid */
130 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131}
132
Mathew Inwood368ee1e2011-07-06 16:51:54 +0100133static int __android_log_write(int prio, const char *tag, const char *msg)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134{
135 struct iovec vec[3];
136 log_id_t log_id = LOG_ID_MAIN;
137
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000138 if (tag == NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139 tag = "";
140
141 if (!strcmp(tag, "HTC_RIL"))
142 log_id = LOG_ID_RADIO;
143
144 vec[0].iov_base = (unsigned char *) &prio;
145 vec[0].iov_len = 1;
146 vec[1].iov_base = (void *) tag;
147 vec[1].iov_len = strlen(tag) + 1;
148 vec[2].iov_base = (void *) msg;
149 vec[2].iov_len = strlen(msg) + 1;
150
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000151 return log_channels[log_id].logger(log_id, vec);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800152}
153
Mathew Inwood368ee1e2011-07-06 16:51:54 +0100154
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200155int __libc_android_log_vprint(int prio, const char *tag, const char *fmt,
156 va_list ap)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157{
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200158 char buf[LOG_BUF_SIZE];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159
160 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
161
Mathew Inwood368ee1e2011-07-06 16:51:54 +0100162 return __android_log_write(prio, tag, buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163}
164
165int __libc_android_log_print(int prio, const char *tag, const char *fmt, ...)
166{
167 va_list ap;
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200168 char buf[LOG_BUF_SIZE];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169
170 va_start(ap, fmt);
171 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
172 va_end(ap);
173
Mathew Inwood368ee1e2011-07-06 16:51:54 +0100174 return __android_log_write(prio, tag, buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175}
176
177int __libc_android_log_assert(const char *cond, const char *tag,
178 const char *fmt, ...)
179{
180 va_list ap;
Mathew Inwood368ee1e2011-07-06 16:51:54 +0100181 char buf[LOG_BUF_SIZE];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182
183 va_start(ap, fmt);
184 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
185 va_end(ap);
186
Mathew Inwood368ee1e2011-07-06 16:51:54 +0100187 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188
189 exit(1);
190
191 return -1;
192}