forked from drehscheibe/bu0836
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.cxx
133 lines (99 loc) · 2.52 KB
/
logging.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// logging utilities
//
// Copyright (C) 2010 Melchior FRANZ <melchior.franz@gmail.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#include <cstring> // strcmp
#include <iomanip>
#include <iostream>
#include <stdlib.h> // getenv, STDOUT_FILENO
#include <sstream>
#include <unistd.h> // isatty
#include "logging.hxx"
using namespace std;
namespace logging {
namespace {
bool has_color(int fd)
{
if (isatty(fd)) {
const char *term = getenv("TERM");
if (term && strcmp(term, "dumb"))
return true;
}
return false;
}
class nullbuf : public streambuf { } nb;
ostream cnull(&nb);
bool cout_color = has_color(STDOUT_FILENO);
bool cerr_color = has_color(STDERR_FILENO);
int log_level = ALERT;
} // namespace
color reset("0");
color bold("1");
color underline("4");
color blink("5");
color reverse("7");
color black("30");
color red("31");
color green("32");
color brown("33");
color blue("34");
color magenta("35");
color cyan("36");
color white("37");
color bg_black("40");
color bg_red("41");
color bg_green("42");
color bg_brown("43");
color bg_blue("44");
color bg_magenta("45");
color bg_cyan("46");
color bg_white("47");
ostream &operator<<(ostream &os, const color &c) {
// if ((os == cerr && cerr_color) || (os == cout && cout_color))
// return os << "\033[" << c._color << 'm';
return os;
}
string operator+(const string &s, int i)
{
ostringstream x;
x << i;
return s + x.str();
}
int get_log_level()
{
return log_level;
}
void set_log_level(int level)
{
log_level = level;
}
ostream &log(int priority = ALWAYS)
{
return priority >= log_level ? cerr : cnull;
}
string bytes(const unsigned char *p, unsigned int num, size_t width)
{
ostringstream x;
x << hex << setfill('0');
while (num--)
x << setw(2) << int(*p++) << ' ';
string s = x.str();
if (width > s.length())
s.resize(width, ' ');
return s;
}
} // namespace logging