Shuo Wang Hsu | 67249fc | 2024-01-02 16:54:20 -0800 | [diff] [blame^] | 1 | // |
| 2 | // |
| 3 | // Copyright 2015 gRPC authors. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | // |
| 17 | // |
Jeff Vander Stoep | 3adfea8 | 2020-10-14 15:35:59 +0200 | [diff] [blame] | 18 | |
| 19 | #include <grpc/support/port_platform.h> |
| 20 | |
| 21 | #ifdef GPR_ANDROID |
| 22 | |
| 23 | #include <android/log.h> |
Jeff Vander Stoep | 3adfea8 | 2020-10-14 15:35:59 +0200 | [diff] [blame] | 24 | #include <stdarg.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | |
Ivan Lozano | 86747a9 | 2023-06-21 14:25:47 +0000 | [diff] [blame] | 28 | #include <grpc/support/log.h> |
| 29 | #include <grpc/support/time.h> |
| 30 | |
Shuo Wang Hsu | 67249fc | 2024-01-02 16:54:20 -0800 | [diff] [blame^] | 31 | #include "src/core/lib/gprpp/crash.h" |
| 32 | |
Jeff Vander Stoep | 3adfea8 | 2020-10-14 15:35:59 +0200 | [diff] [blame] | 33 | static android_LogPriority severity_to_log_priority(gpr_log_severity severity) { |
| 34 | switch (severity) { |
| 35 | case GPR_LOG_SEVERITY_DEBUG: |
| 36 | return ANDROID_LOG_DEBUG; |
| 37 | case GPR_LOG_SEVERITY_INFO: |
| 38 | return ANDROID_LOG_INFO; |
| 39 | case GPR_LOG_SEVERITY_ERROR: |
| 40 | return ANDROID_LOG_ERROR; |
| 41 | } |
| 42 | return ANDROID_LOG_DEFAULT; |
| 43 | } |
| 44 | |
| 45 | void gpr_log(const char* file, int line, gpr_log_severity severity, |
| 46 | const char* format, ...) { |
Shuo Wang Hsu | 67249fc | 2024-01-02 16:54:20 -0800 | [diff] [blame^] | 47 | // Avoid message construction if gpr_log_message won't log |
Jeff Vander Stoep | 3adfea8 | 2020-10-14 15:35:59 +0200 | [diff] [blame] | 48 | if (gpr_should_log(severity) == 0) { |
| 49 | return; |
| 50 | } |
| 51 | char* message = NULL; |
| 52 | va_list args; |
| 53 | va_start(args, format); |
| 54 | vasprintf(&message, format, args); |
| 55 | va_end(args); |
| 56 | gpr_log_message(file, line, severity, message); |
| 57 | free(message); |
| 58 | } |
| 59 | |
| 60 | void gpr_default_log(gpr_log_func_args* args) { |
| 61 | const char* final_slash; |
| 62 | const char* display_file; |
| 63 | char* output = NULL; |
| 64 | |
| 65 | final_slash = strrchr(args->file, '/'); |
| 66 | if (final_slash == NULL) |
| 67 | display_file = args->file; |
| 68 | else |
| 69 | display_file = final_slash + 1; |
| 70 | |
| 71 | asprintf(&output, "%s:%d] %s", display_file, args->line, args->message); |
| 72 | |
| 73 | __android_log_write(severity_to_log_priority(args->severity), "GRPC", output); |
| 74 | |
Shuo Wang Hsu | 67249fc | 2024-01-02 16:54:20 -0800 | [diff] [blame^] | 75 | // allocated by asprintf => use free, not gpr_free |
Jeff Vander Stoep | 3adfea8 | 2020-10-14 15:35:59 +0200 | [diff] [blame] | 76 | free(output); |
| 77 | } |
| 78 | |
Shuo Wang Hsu | 67249fc | 2024-01-02 16:54:20 -0800 | [diff] [blame^] | 79 | #endif // GPR_ANDROID |