WvStreams
wvcolorlogconsole.cc
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * A version of WvColorLogConsole that colorizes the output
00006  */
00007 
00008 #include "wvcolorlogconsole.h"
00009 
00010 #ifdef _WIN32
00011 
00012 bool WvColorLogConsole::is_tty(int fd)
00013 {
00014     return false;
00015 }
00016 
00017 #else // !_WIN32
00018 
00019 #include <termios.h>
00020 bool WvColorLogConsole::is_tty(int fd)
00021 {
00022     struct termios termios;
00023     return tcgetattr(fd, &termios) == 0;
00024 }
00025 
00026 #endif // !_WIN32
00027 
00028 
00029 bool WvColorLogConsole::can_colorize(int fd, const char *TERM)
00030 {
00031     return is_tty(fd)
00032             && TERM != NULL
00033             && (strcmp(TERM, "linux") == 0
00034                     || strcmp(TERM, "ansi") == 0
00035                     || strcmp(TERM, "xterm") == 0
00036                     || strcmp(TERM, "rxvt") == 0);
00037 }
00038 
00039 
00040 WvColorLogConsole::WvColorLogConsole(int _fd, WvLog::LogLevel _max_level) :
00041     WvLogConsole(_fd, _max_level),
00042     colorize(WvColorLogConsole::can_colorize(_fd, getenv("TERM")))
00043 {
00044 }
00045 
00046 
00047 WvColorLogConsole::~WvColorLogConsole()
00048 {
00049 }
00050 
00051 
00052 void WvColorLogConsole::_begin_line()
00053 {
00054     if (colorize)
00055     {
00056         const char *seq = WvColorLogConsole::color_start_seq(last_level);
00057         uwrite(seq, strlen(seq));
00058     }
00059     WvLogConsole::_begin_line();
00060     if (colorize)
00061     {
00062         const char *seq;
00063         seq = WvColorLogConsole::clear_to_eol_seq(last_level);
00064         uwrite(seq, strlen(seq));
00065         seq = WvColorLogConsole::color_end_seq(last_level);
00066         uwrite(seq, strlen(seq));
00067     }
00068 }
00069 
00070 
00071 void WvColorLogConsole::_mid_line(const char *str, size_t len)
00072 {
00073     if (colorize)
00074     {
00075         const char *seq;
00076         seq = WvColorLogConsole::color_start_seq(last_level);
00077         uwrite(seq, strlen(seq));
00078     }
00079     WvLogConsole::_mid_line(str, len);
00080     if (colorize)
00081     {
00082         const char *seq;
00083         seq = WvColorLogConsole::clear_to_eol_seq(last_level);
00084         uwrite(seq, strlen(seq));
00085         seq = WvColorLogConsole::color_end_seq(last_level);
00086         uwrite(seq, strlen(seq));
00087     }
00088 }
00089 
00090 
00091 void WvColorLogConsole::_end_line()
00092 {
00093     if (colorize)
00094     {
00095         const char *seq;
00096         seq = WvColorLogConsole::color_start_seq(last_level);
00097         uwrite(seq, strlen(seq));
00098         seq = WvColorLogConsole::clear_to_eol_seq(last_level);
00099         uwrite(seq, strlen(seq));
00100         seq = WvColorLogConsole::color_end_seq(last_level);
00101         uwrite(seq, strlen(seq));
00102     }
00103     WvLogConsole::_end_line();
00104 }
00105 
00106 
00107 const char *WvColorLogConsole::color_start_seq(WvLog::LogLevel log_level)
00108 {
00109     if (int(log_level) <= int(WvLog::Error))
00110         return "\e[41;37;1m";
00111     else if (int(log_level) <= int(WvLog::Warning))
00112         return "\e[43;37;1m";
00113     else
00114         return "\e[40;37;1m";
00115 }
00116 
00117 
00118 const char *WvColorLogConsole::clear_to_eol_seq(WvLog::LogLevel log_level)
00119 {
00120     return "\e[0K";
00121 }
00122 
00123 
00124 const char *WvColorLogConsole::color_end_seq(WvLog::LogLevel log_level)
00125 {
00126     return "\e[0m";
00127 }