log_android.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include <grpc/support/port_platform.h>
  19. #ifdef GPR_ANDROID
  20. #include <android/log.h>
  21. #include <grpc/support/log.h>
  22. #include <grpc/support/time.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. static android_LogPriority severity_to_log_priority(gpr_log_severity severity) {
  27. switch (severity) {
  28. case GPR_LOG_SEVERITY_DEBUG:
  29. return ANDROID_LOG_DEBUG;
  30. case GPR_LOG_SEVERITY_INFO:
  31. return ANDROID_LOG_INFO;
  32. case GPR_LOG_SEVERITY_ERROR:
  33. return ANDROID_LOG_ERROR;
  34. }
  35. return ANDROID_LOG_DEFAULT;
  36. }
  37. void gpr_log(const char *file, int line, gpr_log_severity severity,
  38. const char *format, ...) {
  39. char *message = NULL;
  40. va_list args;
  41. va_start(args, format);
  42. vasprintf(&message, format, args);
  43. va_end(args);
  44. gpr_log_message(file, line, severity, message);
  45. free(message);
  46. }
  47. void gpr_default_log(gpr_log_func_args *args) {
  48. char *final_slash;
  49. const char *display_file;
  50. char *output = NULL;
  51. final_slash = strrchr(args->file, '/');
  52. if (final_slash == NULL)
  53. display_file = args->file;
  54. else
  55. display_file = final_slash + 1;
  56. asprintf(&output, "%s:%d] %s", display_file, args->line, args->message);
  57. __android_log_write(severity_to_log_priority(args->severity), "GRPC", output);
  58. /* allocated by asprintf => use free, not gpr_free */
  59. free(output);
  60. }
  61. #endif /* GPR_ANDROID */