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