Skip to content

Commit db89ebd

Browse files
committed
LogOutput to stdout/stderr set with color by default
1 parent c2906f5 commit db89ebd

3 files changed

Lines changed: 51 additions & 26 deletions

File tree

common/alog.cpp

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
#include <initializer_list>
1718
#ifdef _WIN64
1819
#define _POSIX_C_SOURCE 1
1920
#endif
@@ -36,21 +37,43 @@ using namespace std;
3637

3738
static struct tm alog_time = {0};
3839

40+
#define ALOG_COLOR_RESET "\033[0m"
41+
3942
class BaseLogOutput : public ILogOutput {
4043
public:
4144
uint64_t throttle = -1UL;
4245
uint64_t count = 0;
4346
time_t ts = 0;
4447
int log_file_fd;
45-
struct iovec level_prefix[ALOG_AUDIT + 1]{}, level_suffix[ALOG_AUDIT + 1]{};
46-
47-
constexpr BaseLogOutput(int fd = 0) : log_file_fd(fd) {}
48-
49-
void set_level_color(int level, const char* color) override {
50-
level_prefix[level] = {(void*)color, strlen(color)};
48+
struct iovec level_prefix[ALOG_AUDIT + 1], level_suffix[ALOG_AUDIT + 1];
49+
50+
constexpr BaseLogOutput(int fd = 0)
51+
: log_file_fd(fd),
52+
level_prefix{
53+
{(void*)ALOG_COLOR_DARKGRAY, sizeof(ALOG_COLOR_DARKGRAY) - 1},
54+
{(void*)ALOG_COLOR_LIGHTGRAY, sizeof(ALOG_COLOR_LIGHTGRAY) - 1},
55+
{(void*)ALOG_COLOR_YELLOW, sizeof(ALOG_COLOR_YELLOW) - 1},
56+
{(void*)ALOG_COLOR_RED, sizeof(ALOG_COLOR_RED) - 1},
57+
{(void*)ALOG_COLOR_MAGENTA, sizeof(ALOG_COLOR_MAGENTA) - 1},
58+
{(void*)ALOG_COLOR_CYAN, sizeof(ALOG_COLOR_CYAN) - 1},
59+
{(void*)ALOG_COLOR_GREEN, sizeof(ALOG_COLOR_GREEN) - 1},
60+
},
61+
level_suffix{
62+
{(void*)ALOG_COLOR_RESET, sizeof(ALOG_COLOR_RESET) - 1},
63+
{(void*)ALOG_COLOR_RESET, sizeof(ALOG_COLOR_RESET) - 1},
64+
{(void*)ALOG_COLOR_RESET, sizeof(ALOG_COLOR_RESET) - 1},
65+
{(void*)ALOG_COLOR_RESET, sizeof(ALOG_COLOR_RESET) - 1},
66+
{(void*)ALOG_COLOR_RESET, sizeof(ALOG_COLOR_RESET) - 1},
67+
{(void*)ALOG_COLOR_RESET, sizeof(ALOG_COLOR_RESET) - 1},
68+
{(void*)ALOG_COLOR_RESET, sizeof(ALOG_COLOR_RESET) - 1},
69+
} {}
70+
71+
void set_level_color(int level, const char* color, size_t len) override {
72+
if (level < 0 || level > ALOG_AUDIT) return;
73+
level_prefix[level] = {(void*)color, len};
5174
if (level_prefix[level].iov_base == nullptr ||
5275
level_prefix[level].iov_len == 0) {
53-
level_suffix[level] = {(void*)"", 0};
76+
level_suffix[level] = {0, 0};
5477
} else {
5578
level_suffix[level] = {(void*)ALOG_COLOR_RESET,
5679
sizeof(ALOG_COLOR_RESET) - 1};
@@ -262,6 +285,11 @@ class LogOutputFile final : public BaseLogOutput {
262285
atomic<uint64_t> log_file_size{0};
263286
unsigned int log_file_max_cnt = 10;
264287

288+
LogOutputFile() {
289+
// no colors by default when log into files
290+
BaseLogOutput::clear_color();
291+
}
292+
265293
virtual void destruct() override {
266294
log_output_file_close();
267295
delete this;
@@ -276,15 +304,7 @@ class LogOutputFile final : public BaseLogOutput {
276304
if (log_file_fd < 0) return;
277305
uint64_t length = end - begin;
278306
// iovec iov{(void*)begin, length};
279-
struct iovec iov[3] = {
280-
level_prefix[level],
281-
{
282-
.iov_base = (void*)begin,
283-
.iov_len = length,
284-
},
285-
level_suffix[level]
286-
};
287-
std::ignore = ::writev(log_file_fd, iov, 3);
307+
BaseLogOutput::write(level, begin, end);
288308
throttle_block();
289309
if (log_file_name && log_file_size_limit) {
290310
log_file_size += length;
@@ -406,11 +426,12 @@ class AsyncLogOutput final : public ILogOutput {
406426

407427
AsyncLogOutput(ILogOutput* output) : log_output(output) {
408428
background = std::thread([&] {
409-
auto log_file_fd = log_output->get_log_file_fd();
410-
auto wb = [this, log_file_fd] {
429+
auto wb = [this] {
411430
iovec iov;
412431
while (pending.pop(iov)) {
413-
log_output->write(log_file_fd, (char*)iov.iov_base, (char*)iov.iov_base + iov.iov_len);
432+
int level = iov.iov_len & 0x07;
433+
log_output->write(level, (char*)iov.iov_base,
434+
(char*)iov.iov_base + (iov.iov_len >> 3));
414435
delete[] (char*)iov.iov_base;
415436
}
416437
};
@@ -427,11 +448,11 @@ class AsyncLogOutput final : public ILogOutput {
427448
});
428449
}
429450

430-
void write(int, const char* begin, const char* end) override {
451+
void write(int level, const char* begin, const char* end) override {
431452
uint64_t length = end - begin;
432453
auto buf = new char[length];
433454
memcpy(buf, begin, length);
434-
iovec iov{buf, length};
455+
iovec iov{buf, (length << 3) | level};
435456
bool pushed = ({
436457
SCOPED_LOCK(lock);
437458
pending.push(iov);

common/alog.h

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

31-
#define ALOG_COLOR_RESET "\033[0m"
3231
#define ALOG_COLOR_BLACK "\033[30m"
3332
#define ALOG_COLOR_RED "\033[31m"
3433
#define ALOG_COLOR_GREEN "\033[32m"
@@ -59,9 +58,12 @@ class ILogOutput {
5958
virtual uint64_t set_throttle(uint64_t t = -1UL) = 0;
6059
virtual uint64_t get_throttle() = 0;
6160
virtual void destruct() = 0;
62-
virtual void set_level_color(int level, const char* color) {
63-
// not implemented by default
61+
template <size_t N>
62+
void set_level_color(int level, const char (&color)[N]) {
63+
set_level_color(level, color, N - 1);
6464
}
65+
virtual void set_level_color(int level, const char* color,
66+
size_t length) { /* ignored by default */ }
6567
void preset_color();
6668
void clear_color();
6769
};
@@ -227,7 +229,7 @@ struct ALogBuffer
227229

228230
inline void ILogOutput::preset_color() {
229231
set_level_color(ALOG_DEBUG, ALOG_COLOR_DARKGRAY);
230-
set_level_color(ALOG_INFO, ALOG_COLOR_RESET);
232+
set_level_color(ALOG_INFO, ALOG_COLOR_LIGHTGRAY);
231233
set_level_color(ALOG_WARN, ALOG_COLOR_YELLOW);
232234
set_level_color(ALOG_ERROR, ALOG_COLOR_RED);
233235
set_level_color(ALOG_FATAL, ALOG_COLOR_MAGENTA);

common/test/test_alog.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,20 +585,22 @@ TEST(ALOG, signed_zero) {
585585
}
586586

587587
TEST(ALOG, log_with_color) {
588-
default_logger.log_output->preset_color();
589588
LOG_DEBUG("some debug log");
590589
LOG_INFO("some info log");
591590
LOG_WARN("some warning log");
592591
LOG_ERROR("some error log");
593592
LOG_FATAL("some fatal log");
593+
LOG_TEMP("some temp log");
594594
default_logger << LOG_AUDIT("some audit log");
595595
default_logger.log_output->clear_color();
596596
LOG_DEBUG("some debug log");
597597
LOG_INFO("some info log");
598598
LOG_WARN("some warning log");
599599
LOG_ERROR("some error log");
600600
LOG_FATAL("some fatal log");
601+
LOG_TEMP("some temp log");
601602
default_logger << LOG_AUDIT("some audit log");
603+
default_logger.log_output->preset_color();
602604
}
603605

604606
int main(int argc, char **argv)

0 commit comments

Comments
 (0)