Skip to content

Commit a16a0b9

Browse files
committed
alog supports for always logged item(s)
1 parent e12bec3 commit a16a0b9

2 files changed

Lines changed: 46 additions & 15 deletions

File tree

common/alog.h

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ limitations under the License.
2323
#include <ctime>
2424
#include <utility>
2525
#include <type_traits>
26+
#include <tuple>
2627

2728
#include <photon/common/utility.h>
2829
#include <photon/common/conststr.h>
@@ -365,6 +366,16 @@ struct LogBuffer : public ALogBuffer
365366
log_formatter.put(*this, alog_forwarding(x));
366367
return *this;
367368
}
369+
template<typename...Ts>
370+
LogBuffer& operator << (const std::tuple<Ts...>& x)
371+
{
372+
return printf_tuple(x, std::make_index_sequence<sizeof...(Ts)>{});
373+
}
374+
template<typename Tuple, std::size_t...I>
375+
LogBuffer& printf_tuple(const Tuple& x, std::index_sequence<I...>)
376+
{
377+
return printf(std::get<I>(x)...);
378+
}
368379
LogBuffer& printf()
369380
{
370381
return *this;
@@ -456,11 +467,11 @@ struct STFMTLogBuffer : public LogBuffer
456467
}
457468
};
458469

459-
template<typename FMT, typename...Ts> inline
460-
STFMTLogBuffer __log__(int level, ILogOutput* output, const Prologue& prolog, FMT fmt, Ts&&...xs)
461-
{
470+
template<typename...Ps, typename FMT, typename...Ts> inline
471+
STFMTLogBuffer __log__(int level, ILogOutput* output, const Prologue& prolog,
472+
const std::tuple<Ps...>& alt, FMT fmt, Ts&&...xs) {
462473
STFMTLogBuffer log(output);
463-
log << prolog;
474+
log << prolog << alt;
464475
log.print_fmt(fmt, std::forward<Ts>(xs)..., '\n');
465476
log.level = level;
466477
return log;
@@ -504,15 +515,19 @@ struct LogBuilder {
504515
#define _IS_LITERAL_STRING(x) \
505516
(sizeof(#x) > 2 && (#x[0] == '"') && (#x[sizeof(#x) - 2] == '"'))
506517

518+
#ifndef ALWAYS_LOGGED
519+
constexpr static std::tuple<> ALWAYS_LOGGED{};
520+
#endif
521+
507522
#define __LOG__(attr, logger, level, first, ...) ({ \
508523
DEFINE_PROLOGUE(level); \
509524
auto L = [&](ILogOutput* out) __attribute__(attr) { \
510525
if (_IS_LITERAL_STRING(first)) { \
511-
return __log__(level, out, prolog, \
526+
return __log__(level, out, prolog, ALWAYS_LOGGED, \
512527
TSTRING(#first).template strip<'\"'>(), \
513528
##__VA_ARGS__); \
514529
} else { \
515-
return __log__(level, out, prolog, \
530+
return __log__(level, out, prolog, ALWAYS_LOGGED, \
516531
ConstString::TString<>(), first, \
517532
##__VA_ARGS__); \
518533
} \
@@ -522,17 +537,14 @@ struct LogBuilder {
522537

523538

524539
#define LOG_DEBUG(...) (__LOG__((noinline, cold), default_logger, ALOG_DEBUG, __VA_ARGS__))
525-
#define LOG_INFO(...) (__LOG__((noinline, cold), default_logger, ALOG_INFO, __VA_ARGS__))
526-
#define LOG_WARN(...) (__LOG__((noinline, cold), default_logger, ALOG_WARN, __VA_ARGS__))
540+
#define LOG_INFO(...) (__LOG__((noinline, cold), default_logger, ALOG_INFO, __VA_ARGS__))
541+
#define LOG_WARN(...) (__LOG__((noinline, cold), default_logger, ALOG_WARN, __VA_ARGS__))
527542
#define LOG_ERROR(...) (__LOG__((noinline, cold), default_logger, ALOG_ERROR, __VA_ARGS__))
528543
#define LOG_FATAL(...) (__LOG__((noinline, cold), default_logger, ALOG_FATAL, __VA_ARGS__))
529-
#define LOG_TEMP(...) \
530-
{ \
531-
auto _err_bak = errno; \
532-
__LOG__((noinline, cold), default_logger, \
533-
ALOG_TEMP, __VA_ARGS__); \
534-
errno = _err_bak; \
535-
}
544+
#define LOG_TEMP(...) {auto _err_bak = errno; \
545+
__LOG__((noinline, cold), default_logger, ALOG_TEMP, __VA_ARGS__); \
546+
errno = _err_bak; \
547+
}
536548
#ifndef DISABLE_AUDIT
537549
#define LOG_AUDIT(...) (__LOG__((), default_audit_logger, ALOG_AUDIT, __VA_ARGS__))
538550
#else

common/test/test_alog.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,25 @@ TEST(ALog, ALog)
376376
puts(_log_buf);
377377
}
378378

379+
using photon::get_vcpu;
380+
#define ALWAYS_LOGGED std::make_tuple(VALUE(get_vcpu()), ' ', ALogStringL( \
381+
"{this is an always logged item "), 7, ALogStringL("wahaha}"), ' ')
382+
383+
TEST(ALog, always_log) {
384+
log_output = &log_output_test;
385+
LOG_DEBUG(32, " ",
386+
DEC(2345678).comma(true).width(10),
387+
DEC(678).comma(true).width(10),
388+
DEC(8).comma(true).width(10),
389+
DEC(5678).comma(true).width(10));
390+
puts(_log_buf);
391+
memset((char*)log_output_test.log_start() + 12, 'x', 16);
392+
EXPECT_EQ(log_output_test.log_start(), string(
393+
"[get_vcpu()=xxxxxxxxxxxxxxxx] {this is an always logged item 7wahaha} 32 2,345,678 678 8 5,678"));
394+
}
395+
396+
#undef ALWAYS_LOGGED
397+
379398
#define test_type(x, T, len) { \
380399
auto xx = alog_forwarding(x); \
381400
static_assert(std::is_same<decltype(xx), T>::value, "..."); \

0 commit comments

Comments
 (0)