blob: 0d3ac0fe522b0fbd46b2c69dfbafbc0ec79c93c7 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
19#include <grpc/support/port_platform.h>
20
21#ifdef GPR_ANDROID
22
Craig Tillerf40df232016-03-25 13:38:14 -070023#include <android/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080024#include <grpc/support/log.h>
25#include <grpc/support/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080026#include <stdarg.h>
Craig Tillerf40df232016-03-25 13:38:14 -070027#include <stdio.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080028#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080029
30static 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
ncteisenadbfbd52017-11-16 15:35:45 -080042void gpr_log(const char* file, int line, gpr_log_severity severity,
ncteisen66dfcf52017-11-16 16:18:51 -080043 const char* format, ...) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070044 char* message = NULL;
ctiller0cd69562015-01-09 14:22:10 -080045 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
ncteisenadbfbd52017-11-16 15:35:45 -080053void gpr_default_log(gpr_log_func_args* args) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070054 const char* final_slash;
55 const char* display_file;
56 char* output = NULL;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057
ctiller0cd69562015-01-09 14:22:10 -080058 final_slash = strrchr(args->file, '/');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059 if (final_slash == NULL)
Todd Poynorcb7abfd2015-01-23 18:31:03 -080060 display_file = args->file;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061 else
62 display_file = final_slash + 1;
63
Todd Poynorcb7abfd2015-01-23 18:31:03 -080064 asprintf(&output, "%s:%d] %s", display_file, args->line, args->message);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065
ctiller0cd69562015-01-09 14:22:10 -080066 __android_log_write(severity_to_log_priority(args->severity), "GRPC", output);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067
68 /* allocated by asprintf => use free, not gpr_free */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069 free(output);
70}
71
Craig Tiller190d3602015-02-18 09:23:38 -080072#endif /* GPR_ANDROID */