Adding spdlog

This commit is contained in:
Lucas Lazare 2019-05-18 19:44:45 -04:00
parent 6ffc7ee3b3
commit 51be97f9aa
21 changed files with 89 additions and 69 deletions

View File

@ -38,13 +38,13 @@ class Bar {
bool vertical = false;
private:
static inline const std::string MIN_HEIGHT_MSG =
static constexpr const char* MIN_HEIGHT_MSG =
"Requested height: {} exceeds the minimum height: {} required by the modules";
static inline const std::string MIN_WIDTH_MSG =
static constexpr const char* MIN_WIDTH_MSG =
"Requested width: {} exceeds the minimum width: {} required by the modules";
static inline const std::string BAR_SIZE_MSG =
static constexpr const char* BAR_SIZE_MSG =
"Bar configured (width: {}, height: {}) for output: {}";
static inline const std::string SIZE_DEFINED =
static constexpr const char* SIZE_DEFINED =
"{} size is defined in the config file so it will stay like that";
static void layerSurfaceHandleConfigure(void *, struct zwlr_layer_surface_v1 *, uint32_t,
uint32_t, uint32_t);

View File

@ -9,7 +9,8 @@
#include <sys/inotify.h>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "ALabel.hpp"
#include "util/sleeper_thread.hpp"

View File

@ -3,9 +3,11 @@
#include <fmt/format.h>
#include <sys/sysinfo.h>
#include <fstream>
#include <iostream>
#include <cstdint>
#include <numeric>
#include <vector>
#include <string>
#include <utility>
#include "ALabel.hpp"
#include "util/sleeper_thread.hpp"

View File

@ -2,7 +2,7 @@
#include <fmt/format.h>
#include <csignal>
#include <iostream>
#include <string>
#include "ALabel.hpp"
#include "util/command.hpp"
#include "util/json.hpp"

View File

@ -48,6 +48,7 @@ add_global_link_arguments(cpp_link_args, language : 'cpp')
thread_dep = dependency('threads')
libinput = dependency('libinput')
fmt = dependency('fmt', version : ['>=5.3.0'], fallback : ['fmt', 'fmt_dep'])
spdlog = dependency('spdlog', version : ['>=1.3.1'], fallback : ['spdlog', 'spdlog_dep'])
wayland_client = dependency('wayland-client')
wayland_cursor = dependency('wayland-cursor')
wayland_protos = dependency('wayland-protocols')
@ -129,6 +130,7 @@ executable(
client_protos,
wayland_client,
fmt,
spdlog,
sigcpp,
jsoncpp,
libinput,

View File

@ -1,4 +1,5 @@
#include "bar.hpp"
#include <spdlog/spdlog.h>
#include "client.hpp"
#include "factory.hpp"
@ -114,7 +115,7 @@ void waybar::Bar::setMarginsAndZone(uint32_t height, uint32_t width) {
.left = std::stoi(margins[3], nullptr, 10)};
}
} catch (...) {
std::cerr << "Invalid margins: " + config["margin"].asString() << std::endl;
spdlog::warn("Invalid margins: {}", config["margin"].asString());
}
} else if (config["margin"].isInt()) {
auto gaps = config["margin"].asInt();
@ -132,10 +133,10 @@ void waybar::Bar::onConfigure(GdkEventConfigure* ev) {
if (ev->height > static_cast<int>(height_)) {
// Default minimal value
if (height_ != 1) {
std::cout << fmt::format(MIN_HEIGHT_MSG, height_, ev->height) << std::endl;
spdlog::warn(MIN_HEIGHT_MSG, height_, ev->height);
}
if (config["height"].isUInt()) {
std::cout << fmt::format(SIZE_DEFINED, "Height") << std::endl;
spdlog::info(SIZE_DEFINED, "Height");
} else {
tmp_height = ev->height;
}
@ -143,10 +144,10 @@ void waybar::Bar::onConfigure(GdkEventConfigure* ev) {
if (ev->width > static_cast<int>(width_)) {
// Default minimal value
if (width_ != 1) {
std::cout << fmt::format(MIN_WIDTH_MSG, width_, ev->width) << std::endl;
spdlog::warn(MIN_WIDTH_MSG, width_, ev->width);
}
if (config["width"].isUInt()) {
std::cout << fmt::format(SIZE_DEFINED, "Width") << std::endl;
spdlog::info(SIZE_DEFINED, "Width");
} else {
tmp_width = ev->width;
}
@ -227,11 +228,10 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surf
o->window.resize(o->width_, o->height_);
auto zone = o->vertical ? width + o->margins_.right : height + o->margins_.bottom;
zwlr_layer_surface_v1_set_exclusive_zone(o->layer_surface, zone);
std::cout << fmt::format(BAR_SIZE_MSG,
spdlog::info(BAR_SIZE_MSG,
o->width_ == 1 ? "auto" : std::to_string(o->width_),
o->height_ == 1 ? "auto" : std::to_string(o->height_),
o->output->name)
<< std::endl;
o->output->name);
wl_surface_commit(o->surface);
}
zwlr_layer_surface_v1_ack_configure(surface, serial);
@ -277,12 +277,12 @@ void waybar::Bar::getModules(const Factory& factory, const std::string& pos) {
try {
module->update();
} catch (const std::exception& e) {
std::cerr << name.asString() + ": " + e.what() << std::endl;
spdlog::error("{}: {}", name.asString(), e.what());
}
});
});
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
spdlog::warn("module {}: {}", name.asString(), e.what());
}
}
}

View File

@ -1,6 +1,7 @@
#include "client.hpp"
#include <fstream>
#include <iostream>
#include <spdlog/spdlog.h>
#include "util/clara.hpp"
#include "util/json.hpp"
@ -55,7 +56,7 @@ void waybar::Client::handleGlobalRemove(void * data, struct wl_registry * /*re
auto output_name = (*it)->output->name;
(*it)->window.close();
it = client->bars.erase(it);
std::cout << "Bar removed from output: " + output_name << std::endl;
spdlog::info("Bar removed from output: {}", output_name);
} else {
++it;
}
@ -191,7 +192,7 @@ void waybar::Client::setupConfigs(const std::string &config, const std::string &
if (css_file_.empty() || config_file_.empty()) {
throw std::runtime_error("Missing required resources files");
}
std::cout << "Resources files: " + config_file_ + ", " + css_file_ << std::endl;
spdlog::info("Resources files: {}, {}", config_file_, css_file_);
}
auto waybar::Client::setupConfig() -> void {
@ -249,7 +250,7 @@ int waybar::Client::main(int argc, char *argv[]) {
clara::detail::Opt(bar_id, "id")["-b"]["--bar"]("Bar id");
auto res = cli.parse(clara::detail::Args(argc, argv));
if (!res) {
std::cerr << "Error in command line: " << res.errorMessage() << std::endl;
spdlog::error("Error in command line: {}", res.errorMessage());
return 1;
}
if (show_help) {

View File

@ -1,5 +1,5 @@
#include <csignal>
#include <iostream>
#include <spdlog/spdlog.h>
#include "client.hpp"
int main(int argc, char* argv[]) {
@ -23,10 +23,10 @@ int main(int argc, char* argv[]) {
delete client;
return ret;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
spdlog::error("{}", e.what());
return 1;
} catch (const Glib::Exception& e) {
std::cerr << e.what().c_str() << std::endl;
spdlog::error("{}", static_cast<std::string>(e.what()));
return 1;
}
}

View File

@ -1,4 +1,5 @@
#include "modules/battery.hpp"
#include <spdlog/spdlog.h>
waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config)
: ALabel(config, "{capacity}%", 60) {
@ -103,7 +104,7 @@ const std::tuple<uint8_t, uint32_t, std::string> waybar::modules::Battery::getIn
uint16_t capacity = total / batteries_.size();
return {capacity, total_current, status};
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
spdlog::error("Battery: {}", e.what());
return {0, 0, "Unknown"};
}
}

View File

@ -1,4 +1,5 @@
#include "modules/cpu.hpp"
#include <numeric>
waybar::modules::Cpu::Cpu(const std::string& id, const Json::Value& config)
: ALabel(config, "{usage}%", 10) {

View File

@ -1,4 +1,5 @@
#include "modules/custom.hpp"
#include <spdlog/spdlog.h>
waybar::modules::Custom::Custom(const std::string& name, const Json::Value& config)
: ALabel(config, "{}"), name_(name), fp_(nullptr), pid_(-1) {
@ -58,7 +59,7 @@ void waybar::modules::Custom::continuousWorker() {
if (exit_code != 0) {
output_ = {exit_code, ""};
dp.emit();
std::cerr << name_ + " just stopped unexpectedly, is it endless?" << std::endl;
spdlog::error("{} stopped unexpectedly, is it endless?", name_);
}
return;
}

View File

@ -1,7 +1,7 @@
#include "modules/mpd.hpp"
#include <fmt/chrono.h>
#include <iostream>
#include <spdlog/spdlog.h>
waybar::modules::MPD::MPD(const std::string& id, const Json::Value& config)
: ALabel(config, "{album} - {artist} - {title}", 5),
@ -14,11 +14,11 @@ waybar::modules::MPD::MPD(const std::string& id, const Json::Value& config)
status_(nullptr, &mpd_status_free),
song_(nullptr, &mpd_song_free) {
if (!config_["port"].isNull() && !config_["port"].isUInt()) {
std::cerr << module_name_ << ": `port` configuration should be an unsigned int" << std::endl;
spdlog::warn("{}: `port` configuration should be an unsigned int", module_name_);
}
if (!config_["timeout"].isNull() && !config_["timeout"].isUInt()) {
std::cerr << module_name_ << ": `timeout` configuration should be an unsigned int" << std::endl;
spdlog::warn("{}: `timeout` configuration should be an unsigned int", module_name_);
}
label_.set_name("mpd");
@ -28,7 +28,7 @@ waybar::modules::MPD::MPD(const std::string& id, const Json::Value& config)
if (!config["server"].isNull()) {
if (!config_["server"].isString()) {
std::cerr << module_name_ << "`server` configuration should be a string" << std::endl;
spdlog::warn("{}:`server` configuration should be a string", module_name_);
}
server_ = config["server"].asCString();
}
@ -51,7 +51,7 @@ auto waybar::modules::MPD::update() -> void {
periodic_updater().detach();
}
} catch (const std::exception& e) {
std::cerr << module_name_ + ": " + e.what() << std::endl;
spdlog::error("{}: {}", module_name_, e.what());
state_ = MPD_STATE_UNKNOWN;
}
}
@ -72,7 +72,7 @@ std::thread waybar::modules::MPD::event_listener() {
dp.emit();
}
} catch (const std::exception& e) {
std::cerr << module_name_ + ": " + e.what() << std::endl;
spdlog::warn("{}: {}", module_name_, e.what());
}
}
});
@ -206,12 +206,12 @@ std::string waybar::modules::MPD::getStateIcon() {
}
if (connection_ == nullptr) {
std::cerr << module_name_ << ": Trying to fetch state icon while disconnected" << std::endl;
spdlog::warn("{}: Trying to fetch state icon while disconnected", module_name_ )
return "";
}
if (stopped()) {
std::cerr << module_name_ << ": Trying to fetch state icon while stopped" << std::endl;
spdlog::warn("{}: Trying to fetch state icon while stopped", module_name_);
return "";
}
@ -228,7 +228,7 @@ std::string waybar::modules::MPD::getOptionIcon(std::string optionName, bool act
}
if (connection_ == nullptr) {
std::cerr << module_name_ << ": Trying to fetch option icon while disconnected" << std::endl;
spdlog::warn("{}: Trying to fetch option icon while disconnected", module_name);
return "";
}
@ -251,7 +251,7 @@ void waybar::modules::MPD::tryConnect() {
unique_connection(mpd_connection_new(server_, port_, timeout_), &mpd_connection_free);
if (connection_ == nullptr || alternate_connection_ == nullptr) {
std::cerr << module_name_ << ": Failed to connect to MPD" << std::endl;
spdlog::error("{}: Failed to connect to MPD", module_name_);
connection_.reset();
alternate_connection_.reset();
return;
@ -259,9 +259,9 @@ void waybar::modules::MPD::tryConnect() {
try {
checkErrors(connection_.get());
std::cerr << module_name_ << ": Connected to MPD" << std::endl;
spdlog::info("{}: Connected to MPD", module_name_);
} catch (std::runtime_error& e) {
std::cerr << module_name_ << ": Failed to connect to MPD: " << e.what() << std::endl;
spdlog::error("{}: Failed to connect to MPD: {}", module_name_, e.what());
connection_.reset();
alternate_connection_.reset();
}

View File

@ -1,7 +1,7 @@
#include "modules/network.hpp"
#include <spdlog/spdlog.h>
#include <sys/eventfd.h>
#include <fstream>
#include <iostream>
namespace {
@ -13,7 +13,7 @@ namespace {
std::ifstream netstat(NETSTAT_FILE);
std::optional<unsigned long long> read_netstat(std::string_view category, std::string_view key) {
if (!netstat) {
std::cerr << "Failed to open netstat file " << NETSTAT_FILE << '\n' << std::flush;
spdlog::warn("Failed to open netstat file {}", NETSTAT_FILE);
return {};
}
netstat.seekg(std::ios_base::beg);
@ -28,7 +28,7 @@ namespace {
std::string read;
while (std::getline(netstat, read) && !starts_with(read, category));
if (!starts_with(read, category)) {
std::cerr << "Category '" << category << "' not found in netstat file " << NETSTAT_FILE << '\n' << std::flush;
spdlog::warn("Category '{}' not found in netstat file {}", category, NETSTAT_FILE);
return {};
}
@ -52,7 +52,7 @@ namespace {
}
if (r_it == read.end() && k_it != key.end()) {
std::cerr << "Key '" << key << "' not found in category '" << category << "' of netstat file " << NETSTAT_FILE << '\n' << std::flush;
spdlog::warn("Key '{}' not found in category '{}' of netstat file {}", key, category, NETSTAT_FILE);
return {};
}

View File

@ -1,5 +1,6 @@
#include "modules/sni/host.hpp"
#include <iostream>
#include <spdlog/spdlog.h>
#include <fmt/ostream.h>
namespace waybar::modules::SNI {
@ -63,14 +64,14 @@ void Host::proxyReady(GObject* src, GAsyncResult* res, gpointer data) {
GError* error = nullptr;
SnWatcher* watcher = sn_watcher_proxy_new_finish(res, &error);
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
std::cerr << error->message << std::endl;
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
auto host = static_cast<SNI::Host*>(data);
host->watcher_ = watcher;
if (error != nullptr) {
std::cerr << error->message << std::endl;
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
@ -82,13 +83,13 @@ void Host::registerHost(GObject* src, GAsyncResult* res, gpointer data) {
GError* error = nullptr;
sn_watcher_call_register_host_finish(SN_WATCHER(src), res, &error);
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
std::cerr << error->message << std::endl;
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
auto host = static_cast<SNI::Host*>(data);
if (error != nullptr) {
std::cerr << error->message << std::endl;
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
@ -139,4 +140,4 @@ void Host::addRegisteredItem(std::string service) {
}
}
} // namespace waybar::modules::SNI
} // namespace waybar::modules::SNI

View File

@ -1,6 +1,6 @@
#include "modules/sni/item.hpp"
#include <glibmm/main.h>
#include <iostream>
#include <spdlog/spdlog.h>
namespace waybar::modules::SNI {
@ -47,8 +47,7 @@ void Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& result) {
this->proxy_->signal_signal().connect(sigc::mem_fun(*this, &Item::onSignal));
if (this->id.empty() || this->category.empty() || this->status.empty()) {
std::cerr << "Invalid Status Notifier Item: " + this->bus_name + "," + this->object_path
<< std::endl;
spdlog::error("Invalid Status Notifier Item: {}, {}", bus_name, object_path);
return;
}
this->updateImage();
@ -235,7 +234,7 @@ void Item::updateImage() {
image.set(getIconByName(icon_name, icon_size));
}
} catch (Glib::Error& e) {
std::cerr << "Exception: " << e.what() << std::endl;
spdlog::error("Item '{}': {}", id, static_cast<std::string>(e.what()));
}
} else if (icon_pixmap) {
// An icon extracted may be the wrong size for the tray
@ -321,4 +320,4 @@ bool Item::handleClick(GdkEventButton* const& ev) {
return false;
}
} // namespace waybar::modules::SNI
} // namespace waybar::modules::SNI

View File

@ -1,5 +1,4 @@
#include "modules/sni/tray.hpp"
#include <iostream>
namespace waybar::modules::SNI {
@ -39,4 +38,4 @@ auto Tray::update() -> void {
Tray::operator Gtk::Widget&() { return box_; }
}
}

View File

@ -1,6 +1,5 @@
#include "modules/sni/watcher.hpp"
#include <iostream>
#include <spdlog/spdlog.h>
using namespace waybar::modules::SNI;
@ -34,7 +33,7 @@ void Watcher::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib:
if (error != nullptr) {
// Don't print an error when a watcher is already present
if (error->code != 2) {
std::cerr << error->message << std::endl;
spdlog::error("Watcher {}: {}", watcher_id_, error->message); // FIXME: watcher_id_ is neither actually used nor initialized AFAICT
}
g_error_free(error);
return;
@ -193,4 +192,4 @@ void Watcher::updateRegisteredItems(SnWatcher* obj) {
sn_watcher_set_registered_items(obj, items);
g_variant_unref(variant);
g_free(items);
}
}

View File

@ -1,4 +1,5 @@
#include "modules/sway/mode.hpp"
#include <spdlog/spdlog.h>
namespace waybar::modules::sway {
@ -24,7 +25,7 @@ void Mode::onEvent(const struct Ipc::ipc_response& res) {
}
dp.emit();
} catch (const std::exception& e) {
std::cerr << "Mode: " << e.what() << std::endl;
spdlog::error("Mode: {}", e.what());
}
}
@ -33,7 +34,7 @@ void Mode::worker() {
try {
ipc_.handleEvent();
} catch (const std::exception& e) {
std::cerr << "Mode: " << e.what() << std::endl;
spdlog::error("Mode: {}", e.what());
}
};
}
@ -50,4 +51,4 @@ auto Mode::update() -> void {
}
}
} // namespace waybar::modules::sway
} // namespace waybar::modules::sway

View File

@ -1,4 +1,5 @@
#include "modules/sway/window.hpp"
#include <spdlog/spdlog.h>
namespace waybar::modules::sway {
@ -55,7 +56,7 @@ void Window::onCmd(const struct Ipc::ipc_response& res) {
dp.emit();
}
} catch (const std::exception& e) {
std::cerr << "Window: " << e.what() << std::endl;
spdlog::error("Window: {}", e.what());
}
}
@ -64,7 +65,7 @@ void Window::worker() {
try {
ipc_.handleEvent();
} catch (const std::exception& e) {
std::cerr << "Window: " << e.what() << std::endl;
spdlog::error("Window: {}", e.what());
}
};
}
@ -102,7 +103,7 @@ void Window::getTree() {
try {
ipc_.sendCmd(IPC_GET_TREE);
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
spdlog::error("Window: {}", e.what());
}
}

View File

@ -1,4 +1,5 @@
#include "modules/sway/workspaces.hpp"
#include <spdlog/spdlog.h>
namespace waybar::modules::sway {
@ -28,7 +29,7 @@ void Workspaces::onEvent(const struct Ipc::ipc_response &res) {
try {
ipc_.sendCmd(IPC_GET_WORKSPACES);
} catch (const std::exception &e) {
std::cerr << "Workspaces: " << e.what() << std::endl;
spdlog::error("Workspaces: {}", e.what());
}
}
@ -50,7 +51,7 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
dp.emit();
}
} catch (const std::exception &e) {
std::cerr << "Workspaces: " << e.what() << std::endl;
spdlog::error("Workspaces: {}", e.what());
}
} else {
if (scrolling_) {
@ -64,7 +65,7 @@ void Workspaces::worker() {
try {
ipc_.handleEvent();
} catch (const std::exception &e) {
std::cerr << "Workspaces: " << e.what() << std::endl;
spdlog::error("Workspaces: {}", e.what());
}
};
}
@ -139,7 +140,7 @@ Gtk::Button &Workspaces::addButton(const Json::Value &node) {
try {
ipc_.sendCmd(IPC_COMMAND, fmt::format("workspace \"{}\"", pair.first->first));
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
spdlog::error("Workspaces: {}", e.what());
}
});
if (!config_["disable-scroll"].asBool()) {
@ -208,7 +209,7 @@ bool Workspaces::handleScroll(GdkEventScroll *e) {
try {
ipc_.sendCmd(IPC_COMMAND, fmt::format("workspace \"{}\"", name));
} catch (const std::exception &e) {
std::cerr << "Workspaces: " << e.what() << std::endl;
spdlog::error("Workspaces: {}", e.what());
}
return true;
}

10
subprojects/spdlog.wrap Normal file
View File

@ -0,0 +1,10 @@
[wrap-file]
directory = spdlog-1.3.1
source_url = https://github.com/gabime/spdlog/archive/v1.3.1.tar.gz
source_filename = v1.3.1.tar.gz
source_hash = 160845266e94db1d4922ef755637f6901266731c4cb3b30b45bf41efa0e6ab70
patch_url = https://wrapdb.mesonbuild.com/v1/projects/spdlog/1.3.1/1/get_zip
patch_filename = spdlog-1.3.1-1-wrap.zip
patch_hash = 715a0229781019b853d409cc0bf891ee4b9d3a17bec0cf87f4ad30b28bbecc87