Skip to content

Commit

Permalink
Use spdlog for logcat and support colors
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Apr 13, 2023
1 parent 69a1b73 commit 7aff0a6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 36 deletions.
56 changes: 29 additions & 27 deletions tool/cmd_logcat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,56 @@
*/

#include "cmd_logcat.hpp"
#include "color_sink.hpp"
#include "connector.hpp"

#include <google/protobuf/util/time_util.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>

#include <cassert>
#include <chrono>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>

using namespace rocvad;

namespace {

std::string map_time(google::protobuf::Timestamp timestamp)
std::chrono::system_clock::time_point map_time(google::protobuf::Timestamp timestamp)
{
const int64_t nanoseconds =
google::protobuf::util::TimeUtil::TimestampToNanoseconds(timestamp);

const auto time_point = std::chrono::system_clock::time_point(
return std::chrono::system_clock::time_point(
std::chrono::duration_cast<std::chrono::system_clock::duration>(
std::chrono::nanoseconds(nanoseconds)));

std::time_t c_time = std::chrono::system_clock::to_time_t(time_point);

const uint64_t c_milliseconds =
uint64_t(std::chrono::duration_cast<std::chrono::milliseconds>(
time_point.time_since_epoch())
.count() %
1000);

std::stringstream ss;

ss << std::put_time(std::localtime(&c_time), "%H:%M:%S.") << std::setfill('0')
<< std::setw(3) << c_milliseconds;

return ss.str();
}

const char* map_level(MesgLogEntry::Level level)
spdlog::level::level_enum map_level(MesgLogEntry::Level level)
{
switch (level) {
case MesgLogEntry::CRIT:
return "[CC]";
return spdlog::level::critical;

case MesgLogEntry::ERROR:
return "[EE]";
return spdlog::level::err;

case MesgLogEntry::WARN:
return "[WW]";
return spdlog::level::warn;

case MesgLogEntry::INFO:
return "[II]";
return spdlog::level::info;

case MesgLogEntry::DEBUG:
return "[DD]";
return spdlog::level::debug;

default:
break;
}

return "[TT]";
return spdlog::level::trace;
}

} // namespace
Expand All @@ -82,6 +68,22 @@ CmdLogcat::CmdLogcat(CLI::App& parent)
}

bool CmdLogcat::execute(const Environment& env)
{
setup_(env);

return run_();
}

void CmdLogcat::setup_(const Environment& env)
{
logger_ = std::make_shared<spdlog::logger>(
"logcat", std::make_shared<ColorSink>(stdout, env.color_mode));

logger_->set_level(spdlog::level::trace);
logger_->set_pattern("%^%H:%M:%S.%e [%L%L] %v%$");
}

bool CmdLogcat::run_()
{
bool has_tried = false, has_connected = false;

Expand Down Expand Up @@ -157,7 +159,7 @@ void CmdLogcat::session_()
return;
}

std::cout << map_time(entry.time()) << " " << map_level(entry.level()) << " "
<< entry.text() << "\n";
assert(logger_);
logger_->log(map_time(entry.time()), {}, map_level(entry.level()), entry.text());
}
}
5 changes: 5 additions & 0 deletions tool/cmd_logcat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ class CmdLogcat : public CmdBase
bool execute(const Environment& env) override;

private:
void setup_(const Environment& env);
bool run_();

bool connect_();
void disconnect_();
void session_();

std::unique_ptr<Connector> conn_;
DriverProtocol::Stub* stub_ = nullptr;

std::shared_ptr<spdlog::logger> logger_;
};

} // namespace rocvad
35 changes: 35 additions & 0 deletions tool/color_sink.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#pragma once

#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>

#include <cstdio>
#include <mutex>

namespace rocvad {

// Log sink with custom color set.
class ColorSink : public spdlog::sinks::ansicolor_sink<spdlog::details::console_mutex>
{
public:
ColorSink(FILE* target_file, spdlog::color_mode color_mode)
: ansicolor_sink(target_file, color_mode)
{
set_color(spdlog::level::critical, red);
set_color(spdlog::level::err, red);
set_color(spdlog::level::warn, yellow);
set_color(spdlog::level::info, green);
set_color(spdlog::level::debug, reset);
set_color(spdlog::level::trace, white);
}
};

} // namespace rocvad
13 changes: 4 additions & 9 deletions tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include "cmd_root.hpp"
#include "color_sink.hpp"

#include <grpc/impl/codegen/log.h>
#include <spdlog/sinks/stdout_color_sinks.h>
Expand All @@ -22,15 +23,9 @@ namespace {

void spdlog_init(const Environment& env)
{
auto sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>(env.color_mode);
sink->set_color(spdlog::level::critical, sink->red);
sink->set_color(spdlog::level::err, sink->red);
sink->set_color(spdlog::level::warn, sink->yellow);
sink->set_color(spdlog::level::info, sink->green);
sink->set_color(spdlog::level::debug, sink->white);
sink->set_color(spdlog::level::trace, sink->white);

auto logger = std::make_shared<spdlog::logger>("console", sink);
auto logger = std::make_shared<spdlog::logger>(
"default", std::make_shared<ColorSink>(stderr, env.color_mode));

spdlog::set_default_logger(logger);

spdlog::set_level(env.log_level);
Expand Down

0 comments on commit 7aff0a6

Please sign in to comment.