Skip to content

Commit 5a6cb30

Browse files
committed
Color for alog
1 parent ccd5be0 commit 5a6cb30

3 files changed

Lines changed: 94 additions & 10 deletions

File tree

common/alog.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,31 @@ class BaseLogOutput : public ILogOutput {
4040
uint64_t count = 0;
4141
time_t ts = 0;
4242
int log_file_fd;
43+
struct iovec level_prefix[ALOG_AUDIT + 1]{}, level_suffix[ALOG_AUDIT + 1]{};
4344

44-
constexpr BaseLogOutput(int fd = 0) : log_file_fd(fd) { }
45+
constexpr BaseLogOutput(int fd = 0) : log_file_fd(fd) {}
4546

46-
void write(int, const char* begin, const char* end) override {
47-
std::ignore = ::write(log_file_fd, begin, end - begin);
47+
void set_level_color(int level, const char* color) override {
48+
level_prefix[level] = {(void*)color, strlen(color)};
49+
if (level_prefix[level].iov_base == nullptr ||
50+
level_prefix[level].iov_len == 0) {
51+
level_suffix[level] = {(void*)"", 0};
52+
} else {
53+
level_suffix[level] = {(void*)ALOG_COLOR_RESET,
54+
sizeof(ALOG_COLOR_RESET) - 1};
55+
}
56+
}
57+
58+
void write(int level, const char* begin, const char* end) override {
59+
struct iovec iov[3] = {
60+
level_prefix[level],
61+
{
62+
.iov_base = (void*)begin,
63+
.iov_len = (size_t)(end - begin),
64+
},
65+
level_suffix[level]
66+
};
67+
std::ignore = ::writev(log_file_fd, iov, 3);
4868
throttle_block();
4969
}
5070
void throttle_block() {
@@ -251,15 +271,19 @@ class LogOutputFile final : public BaseLogOutput {
251271
return open(fn, O_CREAT | O_WRONLY | O_APPEND, mode);
252272
}
253273

254-
void write(int, const char* begin, const char* end) override {
274+
void write(int level, const char* begin, const char* end) override {
255275
if (log_file_fd < 0) return;
256276
uint64_t length = end - begin;
257-
iovec iov{(void*)begin, length};
258-
#ifndef _WIN64
259-
std::ignore = ::writev(log_file_fd, &iov, 1); // writev() is atomic, whereas write() is not
260-
#else
261-
std::ignore = ::write(log_file_fd, iov.iov_base, iov.iov_len);
262-
#endif
277+
// iovec iov{(void*)begin, length};
278+
struct iovec iov[3] = {
279+
level_prefix[level],
280+
{
281+
.iov_base = (void*)begin,
282+
.iov_len = length,
283+
},
284+
level_suffix[level]
285+
};
286+
std::ignore = ::writev(log_file_fd, iov, 3);
263287
throttle_block();
264288
if (log_file_name && log_file_size_limit) {
265289
log_file_size += length;

common/alog.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ limitations under the License.
2828
#include <photon/common/conststr.h>
2929
#include <photon/common/retval.h>
3030

31+
#define ALOG_COLOR_RESET "\033[0m"
32+
#define ALOG_COLOR_BLACK "\033[30m"
33+
#define ALOG_COLOR_RED "\033[31m"
34+
#define ALOG_COLOR_GREEN "\033[32m"
35+
#define ALOG_COLOR_YELLOW "\033[33m"
36+
#define ALOG_COLOR_BLUE "\033[34m"
37+
#define ALOG_COLOR_MAGENTA "\033[35m"
38+
#define ALOG_COLOR_CYAN "\033[36m"
39+
#define ALOG_COLOR_LIGHTGRAY "\033[37m"
40+
#define ALOG_COLOR_DARKGRAY "\033[90m"
41+
#define ALOG_COLOR_LIGHTRED "\033[91m"
42+
#define ALOG_COLOR_LIGHTGREEN "\033[92m"
43+
#define ALOG_COLOR_LIGHTYELLOW "\033[93m"
44+
#define ALOG_COLOR_LIGHTBLUE "\033[94m"
45+
#define ALOG_COLOR_LIGHTMAGENTA "\033[95m"
46+
#define ALOG_COLOR_LIGHTCYAN "\033[96m"
47+
#define ALOG_COLOR_LIGHTWHITE "\033[97m"
48+
#define ALOG_COLOR_NOTHING ""
49+
3150
class ILogOutput {
3251
protected:
3352
// output object should be destructed via `destruct()`
@@ -40,6 +59,11 @@ class ILogOutput {
4059
virtual uint64_t set_throttle(uint64_t t = -1UL) = 0;
4160
virtual uint64_t get_throttle() = 0;
4261
virtual void destruct() = 0;
62+
virtual void set_level_color(int level, const char* color) {
63+
// not implemented by default
64+
}
65+
void preset_color();
66+
void clear_color();
4367
};
4468

4569
extern ILogOutput * const log_output_null;
@@ -201,6 +225,25 @@ struct ALogBuffer
201225
#define ALOG_TEMP 5
202226
#define ALOG_AUDIT 6
203227

228+
inline void ILogOutput::preset_color() {
229+
set_level_color(ALOG_DEBUG, ALOG_COLOR_DARKGRAY);
230+
set_level_color(ALOG_INFO, ALOG_COLOR_RESET);
231+
set_level_color(ALOG_WARN, ALOG_COLOR_YELLOW);
232+
set_level_color(ALOG_ERROR, ALOG_COLOR_RED);
233+
set_level_color(ALOG_FATAL, ALOG_COLOR_MAGENTA);
234+
set_level_color(ALOG_TEMP, ALOG_COLOR_CYAN);
235+
set_level_color(ALOG_AUDIT, ALOG_COLOR_GREEN);
236+
}
237+
inline void ILogOutput::clear_color() {
238+
set_level_color(ALOG_DEBUG, ALOG_COLOR_NOTHING);
239+
set_level_color(ALOG_INFO, ALOG_COLOR_NOTHING);
240+
set_level_color(ALOG_WARN, ALOG_COLOR_NOTHING);
241+
set_level_color(ALOG_ERROR, ALOG_COLOR_NOTHING);
242+
set_level_color(ALOG_FATAL, ALOG_COLOR_NOTHING);
243+
set_level_color(ALOG_TEMP, ALOG_COLOR_NOTHING);
244+
set_level_color(ALOG_AUDIT, ALOG_COLOR_NOTHING);
245+
}
246+
204247
class LogFormatter
205248
{
206249
public:

common/test/test_alog.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,23 @@ TEST(ALOG, signed_zero) {
584584
EXPECT_STREQ("0", log_output_test.log_start());
585585
}
586586

587+
TEST(ALOG, log_with_color) {
588+
default_logger.log_output->preset_color();
589+
LOG_DEBUG("some debug log");
590+
LOG_INFO("some info log");
591+
LOG_WARN("some warning log");
592+
LOG_ERROR("some error log");
593+
LOG_FATAL("some fatal log");
594+
default_logger << LOG_AUDIT("some audit log");
595+
default_logger.log_output->clear_color();
596+
LOG_DEBUG("some debug log");
597+
LOG_INFO("some info log");
598+
LOG_WARN("some warning log");
599+
LOG_ERROR("some error log");
600+
LOG_FATAL("some fatal log");
601+
default_logger << LOG_AUDIT("some audit log");
602+
}
603+
587604
int main(int argc, char **argv)
588605
{
589606
if (!photon::is_using_default_engine()) return 0;

0 commit comments

Comments
 (0)