The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 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" |
| 28 | |
| 29 | #define LOG_FILE_DIR "/dev/log/" |
| 30 | |
| 31 | void write_log_entry(int fd, struct logger_entry *buf); |
| 32 | |
| 33 | void log_service(int fd, void *cookie) |
| 34 | { |
| 35 | /* get the name of the log filepath to read */ |
| 36 | char * log_filepath = cookie; |
| 37 | |
| 38 | /* open the log file. */ |
| 39 | int logfd = unix_open(log_filepath, O_RDONLY); |
| 40 | if (logfd < 0) { |
| 41 | goto done; |
| 42 | } |
| 43 | |
| 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; |
| 47 | |
| 48 | while (1) { |
| 49 | int ret; |
| 50 | |
| 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; |
| 57 | } |
| 58 | else if (!ret) { |
| 59 | // fprintf(stderr, "read: Unexpected EOF!\n"); |
| 60 | goto done; |
| 61 | } |
| 62 | |
| 63 | /* NOTE: driver guarantees we read exactly one full entry */ |
| 64 | |
| 65 | entry->msg[entry->len] = '\0'; |
| 66 | |
| 67 | write_log_entry(fd, entry); |
| 68 | } |
| 69 | |
| 70 | done: |
| 71 | unix_close(fd); |
| 72 | free(log_filepath); |
| 73 | } |
| 74 | |
| 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); |
| 78 | |
| 79 | strcpy(log_device, LOG_FILE_DIR); |
| 80 | strcat(log_device, log_name); |
| 81 | |
| 82 | return log_device; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* prints one log entry into the file descriptor fd */ |
| 87 | void write_log_entry(int fd, struct logger_entry *buf) |
| 88 | { |
| 89 | size_t size = sizeof(struct logger_entry) + buf->len; |
| 90 | |
| 91 | writex(fd, buf, size); |
| 92 | } |