David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 1 | // Copyright 2018 The Fuchsia Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | #include <atomic> |
| 8 | #include <cassert> |
| 9 | #include <cstdarg> |
| 10 | #include <cstdint> |
| 11 | #include <cstdio> |
| 12 | #include <thread> |
Gurchetan Singh | 8fb0642 | 2022-01-13 13:30:24 -0800 | [diff] [blame] | 13 | #include <cstring> |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 14 | |
Gurchetan Singh | ff76c16 | 2021-10-25 14:58:00 -0700 | [diff] [blame] | 15 | #if defined(__Fuchsia__) |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 16 | #include <lib/syslog/global.h> |
Gurchetan Singh | 8fb0642 | 2022-01-13 13:30:24 -0800 | [diff] [blame] | 17 | #else |
| 18 | #include <libgen.h> |
Gurchetan Singh | ff76c16 | 2021-10-25 14:58:00 -0700 | [diff] [blame] | 19 | #endif |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 20 | |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 21 | #include "cutils/log.h" |
| 22 | #include "cutils/properties.h" |
| 23 | #include "cutils/threads.h" |
| 24 | |
| 25 | extern "C" { |
| 26 | |
Gurchetan Singh | ff76c16 | 2021-10-25 14:58:00 -0700 | [diff] [blame] | 27 | #if !defined(__Fuchsia__) |
| 28 | static void linux_log_prefix(const char *prefix, const char *file, int line, const char *format, |
| 29 | va_list ap, ...) |
| 30 | { |
| 31 | char buf[50]; |
Gurchetan Singh | 8fb0642 | 2022-01-13 13:30:24 -0800 | [diff] [blame] | 32 | char *dup = strdup(file); |
| 33 | if (!dup) |
| 34 | return; |
| 35 | |
| 36 | snprintf(buf, sizeof(buf), "[%s(%d)]", basename(dup), line); |
| 37 | fprintf(stderr, "%s\n", buf); |
Gurchetan Singh | ff76c16 | 2021-10-25 14:58:00 -0700 | [diff] [blame] | 38 | vfprintf(stderr, format, ap); |
Gurchetan Singh | 8fb0642 | 2022-01-13 13:30:24 -0800 | [diff] [blame] | 39 | |
| 40 | free(dup); |
Gurchetan Singh | ff76c16 | 2021-10-25 14:58:00 -0700 | [diff] [blame] | 41 | } |
| 42 | #endif |
| 43 | |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 44 | int property_get(const char* key, char* value, const char* default_value) { |
| 45 | return 0; |
| 46 | } |
| 47 | |
Suraj Malhotra | 1cf3016 | 2020-11-25 17:31:11 -0800 | [diff] [blame] | 48 | int __android_log_print(int priority, const char* tag, const char* file, |
| 49 | int line, const char* format, ...) { |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 50 | const char* local_tag = tag; |
| 51 | if (!local_tag) { |
| 52 | local_tag = "<NO_TAG>"; |
| 53 | } |
Gurchetan Singh | ff76c16 | 2021-10-25 14:58:00 -0700 | [diff] [blame] | 54 | |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 55 | va_list ap; |
| 56 | va_start(ap, format); |
Gurchetan Singh | ff76c16 | 2021-10-25 14:58:00 -0700 | [diff] [blame] | 57 | #if defined(__Fuchsia__) |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 58 | switch (priority) { |
Yilong Li | 0289f1c | 2020-06-24 22:21:07 -0700 | [diff] [blame] | 59 | case ANDROID_LOG_VERBOSE: |
| 60 | case ANDROID_LOG_DEBUG: |
Suraj Malhotra | d66818a | 2020-12-21 17:39:27 -0800 | [diff] [blame] | 61 | FX_LOGVF(DEBUG, local_tag, file, line, format, ap); |
Yilong Li | 0289f1c | 2020-06-24 22:21:07 -0700 | [diff] [blame] | 62 | break; |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 63 | case ANDROID_LOG_WARN: |
Suraj Malhotra | d66818a | 2020-12-21 17:39:27 -0800 | [diff] [blame] | 64 | FX_LOGVF(WARNING, local_tag, file, line, format, ap); |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 65 | break; |
| 66 | case ANDROID_LOG_ERROR: |
Suraj Malhotra | d66818a | 2020-12-21 17:39:27 -0800 | [diff] [blame] | 67 | FX_LOGVF(ERROR, local_tag, file, line, format, ap); |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 68 | break; |
Yilong Li | 0289f1c | 2020-06-24 22:21:07 -0700 | [diff] [blame] | 69 | case ANDROID_LOG_FATAL: |
Suraj Malhotra | d66818a | 2020-12-21 17:39:27 -0800 | [diff] [blame] | 70 | FX_LOGVF(FATAL, local_tag, file, line, format, ap); |
Yilong Li | 0289f1c | 2020-06-24 22:21:07 -0700 | [diff] [blame] | 71 | break; |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 72 | case ANDROID_LOG_INFO: |
| 73 | default: |
Suraj Malhotra | d66818a | 2020-12-21 17:39:27 -0800 | [diff] [blame] | 74 | FX_LOGVF(INFO, local_tag, file, line, format, ap); |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 75 | break; |
| 76 | } |
Gurchetan Singh | ff76c16 | 2021-10-25 14:58:00 -0700 | [diff] [blame] | 77 | #else |
| 78 | linux_log_prefix(local_tag, file, line, format, ap); |
| 79 | #endif |
| 80 | |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 81 | return 1; |
| 82 | } |
| 83 | |
Yilong Li | c2867e2 | 2020-11-26 21:28:20 -0800 | [diff] [blame] | 84 | void __android_log_assert(const char* condition, const char* tag, |
| 85 | const char* file, int line, const char* format, ...) { |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 86 | const char* local_tag = tag; |
| 87 | if (!local_tag) { |
| 88 | local_tag = "<NO_TAG>"; |
| 89 | } |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 90 | va_list ap; |
| 91 | va_start(ap, format); |
Gurchetan Singh | ff76c16 | 2021-10-25 14:58:00 -0700 | [diff] [blame] | 92 | #if defined(__Fuchsia__) |
Suraj Malhotra | d66818a | 2020-12-21 17:39:27 -0800 | [diff] [blame] | 93 | FX_LOGVF(ERROR, local_tag, file, line, format, ap); |
Gurchetan Singh | ff76c16 | 2021-10-25 14:58:00 -0700 | [diff] [blame] | 94 | #else |
| 95 | linux_log_prefix(local_tag, file, line, format, ap); |
| 96 | #endif |
| 97 | |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 98 | va_end(ap); |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 99 | |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 100 | abort(); |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int sync_wait(int fd, int timeout) { |
| 104 | return -1; |
| 105 | } |
| 106 | |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 107 | pid_t gettid() { |
| 108 | static thread_local pid_t id = 0; |
| 109 | if (!id) { |
| 110 | static std::atomic<pid_t> next_thread_id{1}; |
| 111 | id = next_thread_id++; |
| 112 | } |
| 113 | return id; |
| 114 | } |
| 115 | |
| 116 | } |