hyprland: convert std::cout and std::cerr to std::println()

This commit is contained in:
Toni500git 2024-10-13 14:24:10 +02:00 committed by Vaxry
parent b61d4c3636
commit 05a5e0b4f1
3 changed files with 37 additions and 32 deletions

View file

@ -9,7 +9,9 @@
#include "managers/eventLoop/EventLoopManager.hpp" #include "managers/eventLoop/EventLoopManager.hpp"
#include <aquamarine/output/Output.hpp> #include <aquamarine/output/Output.hpp>
#include <bit> #include <bit>
#include <ctime>
#include <random> #include <random>
#include <print>
#include <cstring> #include <cstring>
#include <filesystem> #include <filesystem>
#include <unordered_set> #include <unordered_set>
@ -138,37 +140,37 @@ CCompositor::CCompositor() {
m_szHyprTempDataRoot = std::string{getenv("XDG_RUNTIME_DIR")} + "/hypr"; m_szHyprTempDataRoot = std::string{getenv("XDG_RUNTIME_DIR")} + "/hypr";
if (m_szHyprTempDataRoot.starts_with("/hypr")) { if (m_szHyprTempDataRoot.starts_with("/hypr")) {
std::cout << "Bailing out, XDG_RUNTIME_DIR is invalid\n"; std::println("Bailing out, $XDG_RUNTIME_DIR is invalid");
throw std::runtime_error("CCompositor() failed"); throw std::runtime_error("CCompositor() failed");
} }
if (!m_szHyprTempDataRoot.starts_with("/run/user")) if (!m_szHyprTempDataRoot.starts_with("/run/user"))
std::cout << "[!!WARNING!!] XDG_RUNTIME_DIR looks non-standard. Proceeding anyways...\n"; std::println("[!!WARNING!!] XDG_RUNTIME_DIR looks non-standard. Proceeding anyways...");
std::random_device dev; std::random_device dev;
std::mt19937 engine(dev()); std::mt19937 engine(dev());
std::uniform_int_distribution<> distribution(0, INT32_MAX); std::uniform_int_distribution<> distribution(0, INT32_MAX);
m_szInstanceSignature = GIT_COMMIT_HASH + std::string("_") + std::to_string(time(NULL)) + "_" + std::to_string(distribution(engine)); m_szInstanceSignature = std::format("{}_{}_{}", GIT_COMMIT_HASH, std::time(NULL), distribution(engine));
setenv("HYPRLAND_INSTANCE_SIGNATURE", m_szInstanceSignature.c_str(), true); setenv("HYPRLAND_INSTANCE_SIGNATURE", m_szInstanceSignature.c_str(), true);
if (!std::filesystem::exists(m_szHyprTempDataRoot)) if (!std::filesystem::exists(m_szHyprTempDataRoot))
mkdir(m_szHyprTempDataRoot.c_str(), S_IRWXU); mkdir(m_szHyprTempDataRoot.c_str(), S_IRWXU);
else if (!std::filesystem::is_directory(m_szHyprTempDataRoot)) { else if (!std::filesystem::is_directory(m_szHyprTempDataRoot)) {
std::cout << "Bailing out, " << m_szHyprTempDataRoot << " is not a directory\n"; std::println("Bailing out, {} is not a directory", m_szHyprTempDataRoot);
throw std::runtime_error("CCompositor() failed"); throw std::runtime_error("CCompositor() failed");
} }
m_szInstancePath = m_szHyprTempDataRoot + "/" + m_szInstanceSignature; m_szInstancePath = m_szHyprTempDataRoot + "/" + m_szInstanceSignature;
if (std::filesystem::exists(m_szInstancePath)) { if (std::filesystem::exists(m_szInstancePath)) {
std::cout << "Bailing out, " << m_szInstancePath << " exists??\n"; std::println("Bailing out, {} exists??", m_szInstancePath);
throw std::runtime_error("CCompositor() failed"); throw std::runtime_error("CCompositor() failed");
} }
if (mkdir(m_szInstancePath.c_str(), S_IRWXU) < 0) { if (mkdir(m_szInstancePath.c_str(), S_IRWXU) < 0) {
std::cout << "Bailing out, couldn't create " << m_szInstancePath << "\n"; std::println("Bailing out, couldn't create {}", m_szInstancePath);
throw std::runtime_error("CCompositor() failed"); throw std::runtime_error("CCompositor() failed");
} }

View file

@ -4,7 +4,7 @@
#include "RollingLogFollow.hpp" #include "RollingLogFollow.hpp"
#include <fstream> #include <fstream>
#include <iostream> #include <print>
#include <fcntl.h> #include <fcntl.h>
void Debug::init(const std::string& IS) { void Debug::init(const std::string& IS) {
@ -69,5 +69,5 @@ void Debug::log(LogLevel level, std::string str) {
// log it to the stdout too. // log it to the stdout too.
if (!disableStdout) if (!disableStdout)
std::cout << ((coloredLogs && !**coloredLogs) ? str : coloredStr) << "\n"; std::println("{}", ((coloredLogs && !**coloredLogs) ? str : coloredStr));
} }

View file

@ -5,7 +5,9 @@
#include "init/initHelpers.hpp" #include "init/initHelpers.hpp"
#include "debug/HyprCtl.hpp" #include "debug/HyprCtl.hpp"
#include <cstdio>
#include <hyprutils/string/String.hpp> #include <hyprutils/string/String.hpp>
#include <print>
using namespace Hyprutils::String; using namespace Hyprutils::String;
#include <fcntl.h> #include <fcntl.h>
@ -17,15 +19,15 @@ using namespace Hyprutils::String;
#include <filesystem> #include <filesystem>
void help() { void help() {
std::cout << "usage: Hyprland [arg [...]].\n"; std::println("usage: Hyprland [arg [...]].\n");
std::cout << "\nArguments:\n"; std::println(R"(Arguments:
std::cout << " --help -h - Show this message again\n"; --help -h - Show this message again
std::cout << " --config FILE -c FILE - Specify config file to use\n"; --config FILE -c FILE - Specify config file to use
std::cout << " --socket NAME - Sets the Wayland socket name (for Wayland socket handover)\n"; --socket NAME - Sets the Wayland socket name (for Wayland socket handover)
std::cout << " --wayland-fd FD - Sets the Wayland socket fd (for Wayland socket handover)\n"; --wayland-fd FD - Sets the Wayland socket fd (for Wayland socket handover)
std::cout << " --systeminfo - Prints system infos\n"; --systeminfo - Prints system infos
std::cout << " --i-am-really-stupid - Omits root user privileges check (why would you do that?)\n"; --i-am-really-stupid - Omits root user privileges check (why would you do that?)
std::cout << " --version -v - Print this binary's version\n"; --version -v - Print this binary's version)");
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
@ -53,7 +55,7 @@ int main(int argc, char** argv) {
for (auto it = args.begin(); it != args.end(); it++) { for (auto it = args.begin(); it != args.end(); it++) {
if (it->compare("--i-am-really-stupid") == 0 && !ignoreSudo) { if (it->compare("--i-am-really-stupid") == 0 && !ignoreSudo) {
std::cout << "[ WARNING ] Running Hyprland with superuser privileges might damage your system\n"; std::println("[ WARNING ] Running Hyprland with superuser privileges might damage your system");
ignoreSudo = true; ignoreSudo = true;
} else if (it->compare("--socket") == 0) { } else if (it->compare("--socket") == 0) {
@ -79,7 +81,7 @@ int main(int argc, char** argv) {
if (fcntl(socketFd, F_GETFD) == -1) if (fcntl(socketFd, F_GETFD) == -1)
throw std::exception(); throw std::exception();
} catch (...) { } catch (...) {
std::cerr << "[ ERROR ] Invalid Wayland FD!\n"; std::println(stderr, "[ ERROR ] Invalid Wayland FD!");
help(); help();
return 1; return 1;
@ -101,7 +103,7 @@ int main(int argc, char** argv) {
throw std::exception(); throw std::exception();
} }
} catch (...) { } catch (...) {
std::cerr << "[ ERROR ] Config file '" << configPath << "' doesn't exist!\n"; std::println(stderr, "[ ERROR ] Config file '{}' doesn't exist!", configPath);
help(); help();
return 1; return 1;
@ -117,14 +119,13 @@ int main(int argc, char** argv) {
return 0; return 0;
} else if (it->compare("-v") == 0 || it->compare("--version") == 0) { } else if (it->compare("-v") == 0 || it->compare("--version") == 0) {
std::cout << versionRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, "") << std::endl; std::println("{}", versionRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, ""));
return 0; return 0;
} else if (it->compare("--systeminfo") == 0) { } else if (it->compare("--systeminfo") == 0) {
const auto SYSINFO = systemInfoRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, ""); std::println("{}", systemInfoRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, ""));
std::cout << SYSINFO << "\n";
return 0; return 0;
} else { } else {
std::cerr << "[ ERROR ] Unknown option '" << it->c_str() << "'!\n"; std::println(stderr, "[ ERROR ] Unknown option '{}' !", it->c_str());
help(); help();
return 1; return 1;
@ -132,30 +133,32 @@ int main(int argc, char** argv) {
} }
if (!ignoreSudo && Init::isSudo()) { if (!ignoreSudo && Init::isSudo()) {
std::cerr << "[ ERROR ] Hyprland was launched with superuser privileges, but the privileges check is not omitted.\n"; std::println(stderr,
std::cerr << " Hint: Use the --i-am-really-stupid flag to omit that check.\n"; "[ ERROR ] Hyprland was launched with superuser privileges, but the privileges check is not omitted.\n"
" Hint: Use the --i-am-really-stupid flag to omit that check.");
return 1; return 1;
} else if (ignoreSudo && Init::isSudo()) { } else if (ignoreSudo && Init::isSudo()) {
std::cout << "Superuser privileges check is omitted. I hope you know what you're doing.\n"; std::println("Superuser privileges check is omitted. I hope you know what you're doing.");
} }
if (socketName.empty() ^ (socketFd == -1)) { if (socketName.empty() ^ (socketFd == -1)) {
std::cerr << "[ ERROR ] Hyprland was launched with only one of --socket and --wayland-fd.\n"; std::println(stderr,
std::cerr << " Hint: Pass both --socket and --wayland-fd to perform Wayland socket handover.\n"; "[ ERROR ] Hyprland was launched with only one of --socket and --wayland-fd.\n"
" Hint: Pass both --socket and --wayland-fd to perform Wayland socket handover.");
return 1; return 1;
} }
std::cout << "Welcome to Hyprland!\n"; std::println("Welcome to Hyprland!");
// let's init the compositor. // let's init the compositor.
// it initializes basic Wayland stuff in the constructor. // it initializes basic Wayland stuff in the constructor.
try { try {
g_pCompositor = std::make_unique<CCompositor>(); g_pCompositor = std::make_unique<CCompositor>();
g_pCompositor->explicitConfigPath = configPath; g_pCompositor->explicitConfigPath = configPath;
} catch (std::exception& e) { } catch (const std::exception& e) {
std::cout << "Hyprland threw in ctor: " << e.what() << "\nCannot continue.\n"; std::println(stderr, "Hyprland threw in ctor: {}\nCannot continue.", e.what());
return 1; return 1;
} }