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