Navigation


Changeset 966:8862d9dece66 in freeDiameter


Ignore:
Timestamp:
Mar 13, 2013, 1:38:24 AM (11 years ago)
Author:
Sebastien Decugis <sdecugis@freediameter.net>
Branch:
default
Phase:
public
Message:

Use color output on consoles

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libfdproto/log.c

    r965 r966  
    4545
    4646static void fd_internal_logger( int, const char *, va_list );
     47static int use_colors = 0; /* 0: not init, 1: yes, 2: no */
    4748
    4849/* These may be used to pass specific debug requests via the command-line parameters */
     
    9192    char buf[25];
    9293    FILE *fstr = fd_g_debug_fstr ?: stdout;
     94    int local_use_color = 0;
    9395
    9496    /* logging has been decided by macros outside already */
    9597
    9698    /* add timestamp */
    97     fprintf(fd_g_debug_fstr, "%s  ", fd_log_time(NULL, buf, sizeof(buf)));
     99    fprintf(fstr, "%s  ", fd_log_time(NULL, buf, sizeof(buf)));
     100   
     101    /* Use colors on stdout ? */
     102    if (!use_colors) {
     103        if (isatty(STDOUT_FILENO))
     104                use_colors = 1;
     105        else
     106                use_colors = 2;
     107    }
     108   
     109    /* now, this time log, do we use colors? */
     110    if ((fstr == stdout) && (use_colors == 1))
     111            local_use_color = 1;
     112   
    98113    switch(loglevel) {
    99             case FD_LOG_DEBUG:  fprintf(fd_g_debug_fstr, " DBG   "); break;
    100             case FD_LOG_NOTICE: fprintf(fd_g_debug_fstr, "NOTI   "); break;
    101             case FD_LOG_ERROR:  fprintf(fd_g_debug_fstr, "ERROR  "); break;
    102             default:            fprintf(fd_g_debug_fstr, " ???   ");
     114            case FD_LOG_DEBUG:  fprintf(fstr, local_use_color ? "\e[0;37m" : " DBG   "); break;
     115            case FD_LOG_NOTICE: fprintf(fstr, local_use_color ? "\e[1;37m" : "NOTI   "); break;
     116            case FD_LOG_ERROR:  fprintf(fstr, local_use_color ? "\e[0;31m" : "ERROR  "); break;
     117            default:            fprintf(fstr, local_use_color ? "\e[0;31m" : " ???   ");
    103118    }
    104     vfprintf(fd_g_debug_fstr, format, ap);
     119    vfprintf(fstr, format, ap);
    105120    if (format && (format[strlen(format)-1] != '\n')) {
    106         fprintf(fd_g_debug_fstr, "\n");
     121        fprintf(fstr, "\n");
    107122    }
    108     fflush(fd_g_debug_fstr);
     123    if (local_use_color)
     124             fprintf(fstr, "\e[00m");
     125   
     126    fflush(fstr);
    109127}
    110128
Note: See TracChangeset for help on using the changeset viewer.