add ext4,vfat and tar.bz2
[u-tools.git] / u-tools / apps / adb / log_service.c
blob6e9bdeee650c32798c36b0d1f6c9270dccfaef0f
1 /*
2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <sys/socket.h>
25 #include <cutils/logger.h>
26 #include "sysdeps.h"
27 #include "adb.h"
29 #define LOG_FILE_DIR "/dev/log/"
31 void write_log_entry(int fd, struct logger_entry *buf);
33 void log_service(int fd, void *cookie)
35 /* get the name of the log filepath to read */
36 char * log_filepath = cookie;
38 /* open the log file. */
39 int logfd = unix_open(log_filepath, O_RDONLY);
40 if (logfd < 0) {
41 goto done;
44 // temp buffer to read the entries
45 unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1] __attribute__((aligned(4)));
46 struct logger_entry *entry = (struct logger_entry *) buf;
48 while (1) {
49 int ret;
51 ret = unix_read(logfd, entry, LOGGER_ENTRY_MAX_LEN);
52 if (ret < 0) {
53 if (errno == EINTR || errno == EAGAIN)
54 continue;
55 // perror("logcat read");
56 goto done;
58 else if (!ret) {
59 // fprintf(stderr, "read: Unexpected EOF!\n");
60 goto done;
63 /* NOTE: driver guarantees we read exactly one full entry */
65 entry->msg[entry->len] = '\0';
67 write_log_entry(fd, entry);
70 done:
71 unix_close(fd);
72 free(log_filepath);
75 /* returns the full path to the log file in a newly allocated string */
76 char * get_log_file_path(const char * log_name) {
77 char *log_device = malloc(strlen(LOG_FILE_DIR) + strlen(log_name) + 1);
79 strcpy(log_device, LOG_FILE_DIR);
80 strcat(log_device, log_name);
82 return log_device;
86 /* prints one log entry into the file descriptor fd */
87 void write_log_entry(int fd, struct logger_entry *buf)
89 size_t size = sizeof(struct logger_entry) + buf->len;
91 writex(fd, buf, size);