Skip to content

Commit

Permalink
libmblog: Shorten tag by default
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Gunnerson <[email protected]>
  • Loading branch information
chenxiaolong committed Jul 2, 2018
1 parent 2081afd commit 4451c7a
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion libmblog/src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ using Tid = pid_t;
static std::shared_ptr<BaseLogger> g_logger;
static std::mutex g_mutex;

static std::string g_format{"[%t][%P:%T][%l] %n: %m"};
static std::string g_format{"[%t][%P:%T][%l] %N: %m"};

// %l - Level
// %m - Message
// %n - Tag
// %N - Short tag
// %t - Time
// %P - Process ID
// %T - Thread ID
Expand Down Expand Up @@ -307,6 +308,20 @@ static char _format_prio(LogLevel prio)
}
}

static std::string _format_short_tag(std::string_view tag)
{
// Keep first letter in each component
auto pieces = split_sv(tag, '/');
if (!pieces.empty()) {
for (auto it = pieces.begin(); it != pieces.end() - 1; ++it) {
if (!it->empty() && isalnum(it->front())) {
*it = it->substr(0, 1);
}
}
}
return join(pieces, '/');
}

static std::string _format_rec(const LogRecord &rec)
{
std::string buf;
Expand Down Expand Up @@ -334,6 +349,10 @@ static std::string _format_rec(const LogRecord &rec)
buf += rec.tag;
break;

case 'N':
buf += _format_short_tag(rec.tag);
break;

case 't':
if (!time_buf) {
std::tm tm;
Expand Down

0 comments on commit 4451c7a

Please sign in to comment.