blob: 8bc6f167bd7fefa9af1606f1303a8d643cf8e53a [file] [log] [blame]
Joe Onorato0578cbc2016-10-19 17:03:06 -07001/*
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 "print.h"
18
19#include <sys/ioctl.h>
Colin Cross7cdbd682021-09-13 16:30:12 -070020#include <stdarg.h>
Joe Onorato0578cbc2016-10-19 17:03:06 -070021#include <stdio.h>
22#include <unistd.h>
23
24#include "util.h"
25
26bool g_stdoutIsTty;
27char const* g_escapeBold;
28char const* g_escapeRedBold;
29char const* g_escapeGreenBold;
30char const* g_escapeYellowBold;
31char const* g_escapeUnderline;
32char const* g_escapeEndColor;
33char const* g_escapeClearLine;
34
35void
36init_print()
37{
38 if (isatty(fileno(stdout))) {
39 g_stdoutIsTty = true;
40 g_escapeBold = "\033[1m";
41 g_escapeRedBold = "\033[91m\033[1m";
42 g_escapeGreenBold = "\033[92m\033[1m";
43 g_escapeYellowBold = "\033[93m\033[1m";
44 g_escapeUnderline = "\033[4m";
45 g_escapeEndColor = "\033[0m";
46 g_escapeClearLine = "\033[K";
47 } else {
48 g_stdoutIsTty = false;
49 g_escapeBold = "";
50 g_escapeRedBold = "";
51 g_escapeGreenBold = "";
52 g_escapeYellowBold = "";
53 g_escapeUnderline = "";
54 g_escapeEndColor = "";
55 g_escapeClearLine = "";
56 }
57}
58
59void
60print_status(const char* format, ...)
61{
62 printf("\n%s%s", g_escapeBold, g_escapeUnderline);
63
64 va_list args;
65 va_start(args, format);
66 vfprintf(stdout, format, args);
67 va_end(args);
68
69 printf("%s\n", g_escapeEndColor);
70}
71
72void
73print_command(const Command& command)
74{
75 fputs(g_escapeBold, stdout);
76 for (map<string,string>::const_iterator it=command.env.begin(); it!=command.env.end(); it++) {
77 fputs(it->first.c_str(), stdout);
78 fputc('=', stdout);
79 fputs(escape_for_commandline(it->second.c_str()).c_str(), stdout);
80 putc(' ', stdout);
81 }
82 fputs(command.prog.c_str(), stdout);
83 for (vector<string>::const_iterator it=command.args.begin(); it!=command.args.end(); it++) {
84 putc(' ', stdout);
85 fputs(escape_for_commandline(it->c_str()).c_str(), stdout);
86 }
87 fputs(g_escapeEndColor, stdout);
88 fputc('\n', stdout);
89}
90
91void
92print_error(const char* format, ...)
93{
94 fputs(g_escapeRedBold, stderr);
95
96 va_list args;
97 va_start(args, format);
98 vfprintf(stderr, format, args);
99 va_end(args);
100
101 fputs(g_escapeEndColor, stderr);
102 fputc('\n', stderr);
103}
104
105void
106print_warning(const char* format, ...)
107{
108 fputs(g_escapeYellowBold, stderr);
109
110 va_list args;
111 va_start(args, format);
112 vfprintf(stderr, format, args);
113 va_end(args);
114
115 fputs(g_escapeEndColor, stderr);
116 fputc('\n', stderr);
117}
118
119void
Joe Onorato70edfa82018-12-14 15:46:27 -0800120print_info(const char* format, ...)
121{
122 fputs(g_escapeBold, stdout);
123
124 va_list args;
125 va_start(args, format);
126 vfprintf(stdout, format, args);
127 va_end(args);
128
129 fputs(g_escapeEndColor, stdout);
130 fputc('\n', stdout);
131}
132
133void
Joe Onorato0578cbc2016-10-19 17:03:06 -0700134print_one_line(const char* format, ...)
135{
136 if (g_stdoutIsTty) {
137 struct winsize ws;
138 ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
139 int size = ws.ws_col + 1;
140 char* buf = (char*)malloc(size);
141
142 va_list args;
143 va_start(args, format);
144 vsnprintf(buf, size, format, args);
145 va_end(args);
146
147 printf("%s%s\r", buf, g_escapeClearLine);
148 free(buf);
149
150 fflush(stdout);
151 } else {
152 va_list args;
153 va_start(args, format);
154 vfprintf(stdout, format, args);
155 va_end(args);
156 printf("\n");
157 }
158}
159
160void
161check_error(int err)
162{
163 if (err != 0) {
164 fputc('\n', stderr);
165 print_error("Stopping due to errors.");
166 exit(1);
167 }
168}
169
170