@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
1414limitations 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
3738static struct tm alog_time = {0 };
3839
40+ #define ALOG_COLOR_RESET " \033 [0m"
41+
3942class BaseLogOutput : public ILogOutput {
4043public:
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);
0 commit comments