WvStreams
wvcolorlogconsole.cc
1/*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * A version of WvColorLogConsole that colorizes the output
6 */
7
8#include "wvcolorlogconsole.h"
9
10#ifdef _WIN32
11
12bool WvColorLogConsole::is_tty(int fd)
13{
14 return false;
15}
16
17#else // !_WIN32
18
19#include <termios.h>
20bool WvColorLogConsole::is_tty(int fd)
21{
22 struct termios termios;
23 return tcgetattr(fd, &termios) == 0;
24}
25
26#endif // !_WIN32
27
28
29bool WvColorLogConsole::can_colorize(int fd, const char *TERM)
30{
31 return is_tty(fd)
32 && TERM != NULL
33 && (strcmp(TERM, "linux") == 0
34 || strcmp(TERM, "ansi") == 0
35 || strcmp(TERM, "xterm") == 0
36 || strcmp(TERM, "rxvt") == 0);
37}
38
39
40WvColorLogConsole::WvColorLogConsole(int _fd, WvLog::LogLevel _max_level) :
41 WvLogConsole(_fd, _max_level),
42 colorize(WvColorLogConsole::can_colorize(_fd, getenv("TERM")))
43{
44}
45
46
47WvColorLogConsole::~WvColorLogConsole()
48{
49}
50
51
53{
54 if (colorize)
55 {
56 const char *seq = WvColorLogConsole::color_start_seq(last_level);
57 uwrite(seq, strlen(seq));
58 }
60 if (colorize)
61 {
62 const char *seq;
63 seq = WvColorLogConsole::clear_to_eol_seq(last_level);
64 uwrite(seq, strlen(seq));
65 seq = WvColorLogConsole::color_end_seq(last_level);
66 uwrite(seq, strlen(seq));
67 }
68}
69
70
71void WvColorLogConsole::_mid_line(const char *str, size_t len)
72{
73 if (colorize)
74 {
75 const char *seq;
76 seq = WvColorLogConsole::color_start_seq(last_level);
77 uwrite(seq, strlen(seq));
78 }
80 if (colorize)
81 {
82 const char *seq;
83 seq = WvColorLogConsole::clear_to_eol_seq(last_level);
84 uwrite(seq, strlen(seq));
85 seq = WvColorLogConsole::color_end_seq(last_level);
86 uwrite(seq, strlen(seq));
87 }
88}
89
90
92{
93 if (colorize)
94 {
95 const char *seq;
96 seq = WvColorLogConsole::color_start_seq(last_level);
97 uwrite(seq, strlen(seq));
98 seq = WvColorLogConsole::clear_to_eol_seq(last_level);
99 uwrite(seq, strlen(seq));
100 seq = WvColorLogConsole::color_end_seq(last_level);
101 uwrite(seq, strlen(seq));
102 }
104}
105
106
107const char *WvColorLogConsole::color_start_seq(WvLog::LogLevel log_level)
108{
109 if (int(log_level) <= int(WvLog::Error))
110 return "\e[41;37;1m";
111 else if (int(log_level) <= int(WvLog::Warning))
112 return "\e[43;37;1m";
113 else
114 return "\e[40;37;1m";
115}
116
117
118const char *WvColorLogConsole::clear_to_eol_seq(WvLog::LogLevel log_level)
119{
120 return "\e[0K";
121}
122
123
124const char *WvColorLogConsole::color_end_seq(WvLog::LogLevel log_level)
125{
126 return "\e[0m";
127}
WvLogRcv adds some intelligence to WvLogRcvBase, to keep track of line-prefix-printing and other form...
virtual void _begin_line()
Start a new log line (print prefix)
virtual void _mid_line(const char *str, size_t len)
add text to the current log line.
virtual void _end_line()
End this (Guaranteed NonEmpty) log line.
virtual size_t uwrite(const void *buf, size_t count)
unbuffered I/O functions; these ignore the buffer, which is handled by write().
Definition: wvfdstream.cc:162
Captures formatted log messages and outputs them to the specified file descriptor.
Definition: wvlogrcv.h:108
virtual void _mid_line(const char *str, size_t len)
add text to the current log line.
Definition: wvlog.cc:432
virtual void _end_line()
End this (Guaranteed NonEmpty) log line.
Definition: wvlog.cc:254
virtual void _begin_line()
Start a new log line (print prefix)
Definition: wvlog.cc:248