1 /*
2 * Copyright (C) 2021 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 #include <android-base/stringprintf.h>
24
25 #include <unwindstack/Log.h>
26
27 namespace unwindstack {
28
29 namespace Log {
30
PrintToStdout(uint8_t indent,const char * format,va_list args)31 static void PrintToStdout(uint8_t indent, const char* format, va_list args) {
32 std::string real_format;
33 if (indent > 0) {
34 real_format = android::base::StringPrintf("%*s%s", 2 * indent, " ", format);
35 } else {
36 real_format = format;
37 }
38 real_format += '\n';
39
40 vprintf(real_format.c_str(), args);
41 }
42
Info(const char * format,...)43 void Info(const char* format, ...) {
44 va_list args;
45 va_start(args, format);
46 PrintToStdout(0, format, args);
47 va_end(args);
48 }
49
Info(uint8_t indent,const char * format,...)50 void Info(uint8_t indent, const char* format, ...) {
51 va_list args;
52 va_start(args, format);
53 PrintToStdout(indent, format, args);
54 va_end(args);
55 }
56
Error(const char * format,...)57 void Error(const char* format, ...) {
58 va_list args;
59 va_start(args, format);
60 PrintToStdout(0, format, args);
61 va_end(args);
62 }
63
AsyncSafe(const char * format,...)64 void AsyncSafe(const char* format, ...) {
65 va_list args;
66 va_start(args, format);
67 // Only call vprintf to avoid allocating as much as possible, PrintToStdout uses a std::string.
68 vprintf(format, args);
69 printf("\n");
70 va_end(args);
71 }
72
73 } // namespace Log
74
75 } // namespace unwindstack
76