log_service.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <errno.h>
  22. #include <sys/socket.h>
  23. #include <cutils/logger.h>
  24. #include "sysdeps.h"
  25. #include "adb.h"
  26. #define LOG_FILE_DIR "/dev/log/"
  27. void write_log_entry(int fd, struct logger_entry *buf);
  28. void log_service(int fd, void *cookie)
  29. {
  30. /* get the name of the log filepath to read */
  31. char * log_filepath = cookie;
  32. /* open the log file. */
  33. int logfd = unix_open(log_filepath, O_RDONLY);
  34. if (logfd < 0) {
  35. goto done;
  36. }
  37. // temp buffer to read the entries
  38. unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1] __attribute__((aligned(4)));
  39. struct logger_entry *entry = (struct logger_entry *) buf;
  40. while (1) {
  41. int ret;
  42. ret = unix_read(logfd, entry, LOGGER_ENTRY_MAX_LEN);
  43. if (ret < 0) {
  44. if (errno == EINTR || errno == EAGAIN)
  45. continue;
  46. // perror("logcat read");
  47. goto done;
  48. }
  49. else if (!ret) {
  50. // fprintf(stderr, "read: Unexpected EOF!\n");
  51. goto done;
  52. }
  53. /* NOTE: driver guarantees we read exactly one full entry */
  54. entry->msg[entry->len] = '\0';
  55. write_log_entry(fd, entry);
  56. }
  57. done:
  58. unix_close(fd);
  59. free(log_filepath);
  60. }
  61. /* returns the full path to the log file in a newly allocated string */
  62. char * get_log_file_path(const char * log_name) {
  63. char *log_device = malloc(strlen(LOG_FILE_DIR) + strlen(log_name) + 1);
  64. strcpy(log_device, LOG_FILE_DIR);
  65. strcat(log_device, log_name);
  66. return log_device;
  67. }
  68. /* prints one log entry into the file descriptor fd */
  69. void write_log_entry(int fd, struct logger_entry *buf)
  70. {
  71. size_t size = sizeof(struct logger_entry) + buf->len;
  72. writex(fd, buf, size);
  73. }