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
17 #include <stdarg.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include <string>
23
24 #define LOG_TAG "unwind"
25 #include <log/log.h>
26
27 #if defined(__BIONIC__)
28 #include <async_safe/log.h>
29 #endif
30 #include <android-base/stringprintf.h>
31
32 #include <unwindstack/Log.h>
33
34 namespace unwindstack {
35
36 namespace Log {
37
38 // Send the data to the log.
LogWithPriority(int priority,uint8_t indent,const char * format,va_list args)39 static void LogWithPriority(int priority, uint8_t indent, const char* format, va_list args) {
40 std::string real_format;
41 if (indent > 0) {
42 real_format = android::base::StringPrintf("%*s%s", 2 * indent, " ", format);
43 } else {
44 real_format = format;
45 }
46 LOG_PRI_VA(priority, LOG_TAG, real_format.c_str(), args);
47 }
48
Info(const char * format,...)49 void Info(const char* format, ...) {
50 va_list args;
51 va_start(args, format);
52 LogWithPriority(ANDROID_LOG_INFO, 0, format, args);
53 va_end(args);
54 }
55
Info(uint8_t indent,const char * format,...)56 void Info(uint8_t indent, const char* format, ...) {
57 va_list args;
58 va_start(args, format);
59 LogWithPriority(ANDROID_LOG_INFO, indent, format, args);
60 va_end(args);
61 }
62
Error(const char * format,...)63 void Error(const char* format, ...) {
64 va_list args;
65 va_start(args, format);
66 LogWithPriority(ANDROID_LOG_ERROR, 0, format, args);
67 va_end(args);
68 }
69
Fatal(const char * format,...)70 void Fatal(const char* format, ...) {
71 va_list args;
72 va_start(args, format);
73 LogWithPriority(ANDROID_LOG_FATAL, 0, format, args);
74 va_end(args);
75 abort();
76 }
77
78 #if defined(__BIONIC__)
AsyncSafe(const char * format,...)79 void AsyncSafe(const char* format, ...) {
80 va_list args;
81 va_start(args, format);
82 async_safe_format_log_va_list(ANDROID_LOG_ERROR, "libunwindstack", format, args);
83 va_end(args);
84 }
85 #else
AsyncSafe(const char * format,...)86 void AsyncSafe(const char* format, ...) {
87 va_list args;
88 va_start(args, format);
89 vprintf(format, args);
90 printf("\n");
91 va_end(args);
92 }
93 #endif
94
95 } // namespace Log
96
97 } // namespace unwindstack
98