Home | History | Annotate | Download | only in bit
      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
    119 print_one_line(const char* format, ...)
    120 {
    121     if (g_stdoutIsTty) {
    122         struct winsize ws;
    123         ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
    124         int size = ws.ws_col + 1;
    125         char* buf = (char*)malloc(size);
    126 
    127         va_list args;
    128         va_start(args, format);
    129         vsnprintf(buf, size, format, args);
    130         va_end(args);
    131 
    132         printf("%s%s\r", buf, g_escapeClearLine);
    133         free(buf);
    134 
    135         fflush(stdout);
    136     } else {
    137         va_list args;
    138         va_start(args, format);
    139         vfprintf(stdout, format, args);
    140         va_end(args);
    141         printf("\n");
    142     }
    143 }
    144 
    145 void
    146 check_error(int err)
    147 {
    148     if (err != 0) {
    149         fputc('\n', stderr);
    150         print_error("Stopping due to errors.");
    151         exit(1);
    152     }
    153 }
    154 
    155 
    156