Log.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdarg.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <string>
  20. #define LOG_TAG "unwind"
  21. #include <log/log.h>
  22. #include <android-base/stringprintf.h>
  23. #include <unwindstack/Log.h>
  24. namespace unwindstack {
  25. static bool g_print_to_stdout = false;
  26. void log_to_stdout(bool enable) {
  27. g_print_to_stdout = enable;
  28. }
  29. // Send the data to the log.
  30. void log(uint8_t indent, const char* format, ...) {
  31. std::string real_format;
  32. if (indent > 0) {
  33. real_format = android::base::StringPrintf("%*s%s", 2 * indent, " ", format);
  34. } else {
  35. real_format = format;
  36. }
  37. va_list args;
  38. va_start(args, format);
  39. if (g_print_to_stdout) {
  40. real_format += '\n';
  41. vprintf(real_format.c_str(), args);
  42. } else {
  43. LOG_PRI_VA(ANDROID_LOG_INFO, LOG_TAG, real_format.c_str(), args);
  44. }
  45. va_end(args);
  46. }
  47. } // namespace unwindstack