Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <grpc/support/port_platform.h> |
| 20 | |
| 21 | #ifdef GPR_ANDROID |
| 22 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 23 | #include <android/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 24 | #include <grpc/support/log.h> |
| 25 | #include <grpc/support/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 26 | #include <stdarg.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 27 | #include <stdio.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 28 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 29 | |
| 30 | static android_LogPriority severity_to_log_priority(gpr_log_severity severity) { |
| 31 | switch (severity) { |
| 32 | case GPR_LOG_SEVERITY_DEBUG: |
| 33 | return ANDROID_LOG_DEBUG; |
| 34 | case GPR_LOG_SEVERITY_INFO: |
| 35 | return ANDROID_LOG_INFO; |
| 36 | case GPR_LOG_SEVERITY_ERROR: |
| 37 | return ANDROID_LOG_ERROR; |
| 38 | } |
| 39 | return ANDROID_LOG_DEFAULT; |
| 40 | } |
| 41 | |
ncteisen | adbfbd5 | 2017-11-16 15:35:45 -0800 | [diff] [blame^] | 42 | void gpr_log(const char* file, int line, gpr_log_severity severity, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 43 | const char* format, ...) { |
| 44 | char* message = NULL; |
ctiller | 0cd6956 | 2015-01-09 14:22:10 -0800 | [diff] [blame] | 45 | va_list args; |
| 46 | va_start(args, format); |
| 47 | vasprintf(&message, format, args); |
| 48 | va_end(args); |
| 49 | gpr_log_message(file, line, severity, message); |
| 50 | free(message); |
| 51 | } |
| 52 | |
ncteisen | adbfbd5 | 2017-11-16 15:35:45 -0800 | [diff] [blame^] | 53 | void gpr_default_log(gpr_log_func_args* args) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 54 | const char* final_slash; |
| 55 | const char* display_file; |
| 56 | char* output = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | |
ctiller | 0cd6956 | 2015-01-09 14:22:10 -0800 | [diff] [blame] | 58 | final_slash = strrchr(args->file, '/'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | if (final_slash == NULL) |
Todd Poynor | cb7abfd | 2015-01-23 18:31:03 -0800 | [diff] [blame] | 60 | display_file = args->file; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | else |
| 62 | display_file = final_slash + 1; |
| 63 | |
Todd Poynor | cb7abfd | 2015-01-23 18:31:03 -0800 | [diff] [blame] | 64 | asprintf(&output, "%s:%d] %s", display_file, args->line, args->message); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | |
ctiller | 0cd6956 | 2015-01-09 14:22:10 -0800 | [diff] [blame] | 66 | __android_log_write(severity_to_log_priority(args->severity), "GRPC", output); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | |
| 68 | /* allocated by asprintf => use free, not gpr_free */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | free(output); |
| 70 | } |
| 71 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 72 | #endif /* GPR_ANDROID */ |