• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #ifdef GPR_ANDROID
22 
23 #include <hilog/log.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/time.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 
30 const unsigned int DEVELOPTOOLS_GPRC_TAG = 0xD003300;
31 
severity_to_log_priority(gpr_log_severity severity)32 static LogLevel severity_to_log_priority(gpr_log_severity severity) {
33   switch (severity) {
34     case GPR_LOG_SEVERITY_DEBUG:
35       return LOG_DEBUG;
36     case GPR_LOG_SEVERITY_INFO:
37       return LOG_INFO;
38     case GPR_LOG_SEVERITY_ERROR:
39       return LOG_ERROR;
40   }
41   return LOG_LEVEL_MIN;
42 }
43 
gpr_log(const char * file,int line,gpr_log_severity severity,const char * format,...)44 void gpr_log(const char* file, int line, gpr_log_severity severity,
45              const char* format, ...) {
46   /* Avoid message construction if gpr_log_message won't log */
47   if (gpr_should_log(severity) == 0) {
48     return;
49   }
50   char* message = NULL;
51   va_list args;
52   va_start(args, format);
53   vasprintf(&message, format, args);
54   va_end(args);
55   gpr_log_message(file, line, severity, message);
56   free(message);
57 }
58 
gpr_default_log(gpr_log_func_args * args)59 void gpr_default_log(gpr_log_func_args* args) {
60   const char* final_slash;
61   const char* display_file;
62 
63   final_slash = strrchr(args->file, '/');
64   if (final_slash == NULL)
65     display_file = args->file;
66   else
67     display_file = final_slash + 1;
68 
69   HiLogPrint(LOG_CORE, severity_to_log_priority(args->severity), DEVELOPTOOLS_GPRC_TAG, "GRPC", "%s:%d] %s", display_file, args->line, args->message);
70 
71 }
72 
73 #endif /* GPR_ANDROID */
74