Skip to content

Commit

Permalink
Merge pull request #332 from Organic-Code/enhancement/spdlog
Browse files Browse the repository at this point in the history
Adding spdlog
  • Loading branch information
Alexays authored May 20, 2019
2 parents f2edc8f + cff39bc commit 67593b8
Show file tree
Hide file tree
Showing 21 changed files with 89 additions and 69 deletions.
8 changes: 4 additions & 4 deletions include/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion include/modules/battery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 3 additions & 1 deletion include/modules/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion include/modules/custom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -127,6 +128,7 @@ executable(
client_protos,
wayland_client,
fmt,
spdlog,
sigcpp,
jsoncpp,
libinput,
Expand Down
20 changes: 10 additions & 10 deletions src/bar.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "bar.hpp"
#include <spdlog/spdlog.h>
#include "client.hpp"
#include "factory.hpp"

Expand Down Expand Up @@ -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();
Expand All @@ -132,21 +133,21 @@ 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;
}
}
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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/client.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "client.hpp"
#include <fstream>
#include <iostream>
#include <spdlog/spdlog.h>
#include "util/clara.hpp"
#include "util/json.hpp"

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <csignal>
#include <iostream>
#include <spdlog/spdlog.h>
#include "client.hpp"

int main(int argc, char* argv[]) {
Expand All @@ -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;
}
}
3 changes: 2 additions & 1 deletion src/modules/battery.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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"};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/modules/cpu.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/modules/custom.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
24 changes: 12 additions & 12 deletions src/modules/mpd.cpp
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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");
Expand All @@ -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();
}
Expand All @@ -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;
}
}
Expand All @@ -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());
}
}
});
Expand Down Expand Up @@ -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 "";
}
Expand All @@ -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 "";
}
Expand All @@ -251,17 +251,17 @@ 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;
}
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();
}
Expand Down
8 changes: 4 additions & 4 deletions src/modules/network.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "modules/network.hpp"
#include <spdlog/spdlog.h>
#include <sys/eventfd.h>
#include <fstream>
#include <iostream>

namespace {

Expand All @@ -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);
Expand All @@ -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 {};
}

Expand All @@ -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 {};
}

Expand Down
Loading

0 comments on commit 67593b8

Please sign in to comment.