diff --git a/src/logid/CMakeLists.txt b/src/logid/CMakeLists.txt index bf7f61a..529c5f7 100644 --- a/src/logid/CMakeLists.txt +++ b/src/logid/CMakeLists.txt @@ -10,7 +10,7 @@ find_package(PkgConfig REQUIRED) add_executable(logid logid.cpp - util.cpp + util/log.cpp DeviceManager.cpp Device.cpp Receiver.cpp diff --git a/src/logid/Device.cpp b/src/logid/Device.cpp index f245fc4..4959afb 100644 --- a/src/logid/Device.cpp +++ b/src/logid/Device.cpp @@ -16,7 +16,7 @@ * */ -#include "util.h" +#include "util/log.h" #include "Device.h" using namespace logid; @@ -25,22 +25,22 @@ using namespace logid::backend; Device::Device(std::string path, backend::hidpp::DeviceIndex index) : _hidpp20 (path, index), _path (std::move(path)), _index (index) { - log_printf(DEBUG, "logid::Device created on %s:%d", _path.c_str(), _index); + logPrintf(DEBUG, "logid::Device created on %s:%d", _path.c_str(), _index); } Device::Device(const std::shared_ptr& raw_device, hidpp::DeviceIndex index) : _hidpp20(raw_device, index), _path (raw_device->hidrawPath()), _index (index) { - log_printf(DEBUG, "logid::Device created on %s:%d", _path.c_str(), _index); + logPrintf(DEBUG, "logid::Device created on %s:%d", _path.c_str(), _index); } void Device::sleep() { - log_printf(INFO, "%s:%d fell asleep.", _path.c_str(), _index); + logPrintf(INFO, "%s:%d fell asleep.", _path.c_str(), _index); } void Device::wakeup() { - log_printf(INFO, "%s:%d woke up.", _path.c_str(), _index); + logPrintf(INFO, "%s:%d woke up.", _path.c_str(), _index); } diff --git a/src/logid/DeviceManager.cpp b/src/logid/DeviceManager.cpp index 7c40687..95448b7 100644 --- a/src/logid/DeviceManager.cpp +++ b/src/logid/DeviceManager.cpp @@ -21,7 +21,7 @@ #include "DeviceManager.h" #include "Receiver.h" -#include "util.h" +#include "util/log.h" #include "backend/hidpp10/Error.h" #include "backend/dj/Receiver.h" @@ -43,13 +43,13 @@ void DeviceManager::addDevice(std::string path) } catch(hidpp::Device::InvalidDevice &e) { // Ignore defaultExists = false; } catch(std::system_error &e) { - log_printf(WARN, "I/O error on %s: %s, skipping device.", + logPrintf(WARN, "I/O error on %s: %s, skipping device.", path.c_str(), e.what()); return; } if(isReceiver) { - log_printf(INFO, "Detected receiver at %s", path.c_str()); + logPrintf(INFO, "Detected receiver at %s", path.c_str()); auto receiver = std::make_shared(path); receiver->run(); _receivers.emplace(path, receiver); @@ -69,13 +69,13 @@ void DeviceManager::addDevice(std::string path) if(e.code() != hidpp10::Error::UnknownDevice) throw; else - log_printf(WARN, + logPrintf(WARN, "HID++ 1.0 error while trying to initialize %s:" "%s", path.c_str(), e.what()); } catch(hidpp::Device::InvalidDevice &e) { // Ignore } catch(std::system_error &e) { // This error should have been thrown previously - log_printf(WARN, "I/O error on %s: %s", path.c_str(), + logPrintf(WARN, "I/O error on %s: %s", path.c_str(), e.what()); } } @@ -88,12 +88,12 @@ void DeviceManager::removeDevice(std::string path) if(receiver != _receivers.end()) { _receivers.erase(receiver); - log_printf(INFO, "Receiver on %s disconnected", path.c_str()); + logPrintf(INFO, "Receiver on %s disconnected", path.c_str()); } else { auto device = _devices.find(path); if(device != _devices.find(path)) { _devices.erase(device); - log_printf(INFO, "Device on %s disconnected", path.c_str()); + logPrintf(INFO, "Device on %s disconnected", path.c_str()); } } } diff --git a/src/logid/Receiver.cpp b/src/logid/Receiver.cpp index de28907..365cb20 100644 --- a/src/logid/Receiver.cpp +++ b/src/logid/Receiver.cpp @@ -18,7 +18,7 @@ #include #include "Receiver.h" -#include "util.h" +#include "util/log.h" #include "backend/hidpp10/Error.h" #include "backend/hidpp20/Error.h" @@ -27,7 +27,7 @@ using namespace logid::backend; Receiver::Receiver(std::string path) : dj::ReceiverMonitor(path), _path (path) { - log_printf(DEBUG, "logid::Receiver created on %s", path.c_str()); + logPrintf(DEBUG, "logid::Receiver created on %s", path.c_str()); } void Receiver::addDevice(hidpp::DeviceConnectionEvent event) @@ -50,7 +50,7 @@ void Receiver::addDevice(hidpp::DeviceConnectionEvent event) auto version = hidpp_device.version(); if(std::get<0>(version) < 2) { - log_printf(INFO, "Unsupported HID++ 1.0 device on %s:%d connected.", + logPrintf(INFO, "Unsupported HID++ 1.0 device on %s:%d connected.", _path.c_str(), event.index); return; } @@ -61,11 +61,11 @@ void Receiver::addDevice(hidpp::DeviceConnectionEvent event) _devices.emplace(event.index, device); } catch(hidpp10::Error &e) { - log_printf(ERROR, + logPrintf(ERROR, "Caught HID++ 1.0 error while trying to initialize " "%s:%d: %s", _path.c_str(), event.index, e.what()); } catch(hidpp20::Error &e) { - log_printf(ERROR, "Caught HID++ 2.0 error while trying to initialize " + logPrintf(ERROR, "Caught HID++ 2.0 error while trying to initialize " "%s:%d: %s", _path.c_str(), event.index, e.what()); } } diff --git a/src/logid/backend/dj/ReceiverMonitor.cpp b/src/logid/backend/dj/ReceiverMonitor.cpp index 4199178..a983171 100644 --- a/src/logid/backend/dj/ReceiverMonitor.cpp +++ b/src/logid/backend/dj/ReceiverMonitor.cpp @@ -18,7 +18,7 @@ #include "ReceiverMonitor.h" #include "../../util/thread.h" -#include "../../util.h" +#include "../../util/log.h" #include #include @@ -67,11 +67,11 @@ void ReceiverMonitor::run() }}, {[report, path=this->_receiver->rawDevice()->hidrawPath()] (std::exception& e) { if(report.subId() == Receiver::DeviceConnection) - log_printf(ERROR, "Failed to add device %d to receiver " + logPrintf(ERROR, "Failed to add device %d to receiver " "on %s: %s", report.deviceIndex(), path.c_str(), e.what()); else if(report.subId() == Receiver::DeviceDisconnection) - log_printf(ERROR, "Failed to remove device %d from " + logPrintf(ERROR, "Failed to remove device %d from " "receiver on %s: %s", report.deviceIndex() ,path.c_str(), e.what()); }}); diff --git a/src/logid/backend/raw/DeviceMonitor.cpp b/src/logid/backend/raw/DeviceMonitor.cpp index 133b471..fcbc6db 100644 --- a/src/logid/backend/raw/DeviceMonitor.cpp +++ b/src/logid/backend/raw/DeviceMonitor.cpp @@ -18,7 +18,7 @@ #include "DeviceMonitor.h" #include "../../util/thread.h" -#include "../../util.h" +#include "../../util/log.h" #include #include @@ -101,14 +101,14 @@ void DeviceMonitor::run() thread::spawn([this, name=devnode]() { this->addDevice(name); }, [name=devnode](std::exception& e){ - log_printf(WARN, "Error adding device %s: %s", + logPrintf(WARN, "Error adding device %s: %s", name.c_str(), e.what()); }); else if (action == "remove") thread::spawn([this, name=devnode]() { this->removeDevice(name); }, [name=devnode](std::exception& e){ - log_printf(WARN, "Error removing device %s: %s", + logPrintf(WARN, "Error removing device %s: %s", name.c_str(), e.what()); }); @@ -160,7 +160,7 @@ void DeviceMonitor::enumerate() thread::spawn([this, name=devnode]() { this->addDevice(name); }, [name=devnode](std::exception& e){ - log_printf(ERROR, "Error adding device %s: %s", + logPrintf(ERROR, "Error adding device %s: %s", name.c_str(), e.what()); }); } diff --git a/src/logid/backend/raw/RawDevice.cpp b/src/logid/backend/raw/RawDevice.cpp index a0af1c2..9719f49 100644 --- a/src/logid/backend/raw/RawDevice.cpp +++ b/src/logid/backend/raw/RawDevice.cpp @@ -20,7 +20,7 @@ #include "../Error.h" #include "../hidpp/defs.h" #include "../dj/defs.h" -#include "../../util.h" +#include "../../util/log.h" #include "../hidpp/Report.h" #include @@ -230,7 +230,7 @@ std::vector RawDevice::_respondToReport int RawDevice::_sendReport(const std::vector& report) { std::lock_guard lock(_dev_io); - if(logid::global_verbosity == LogLevel::RAWREPORT) { + if(logid::global_loglevel == LogLevel::RAWREPORT) { printf("[RAWREPORT] %s OUT: ", _path.c_str()); for(auto &i : report) printf("%02x ", i); @@ -295,7 +295,7 @@ int RawDevice::_readReport(std::vector& report, std::size_t maxDataLeng if(0 == ret) throw backend::TimeoutError(); - if(logid::global_verbosity == LogLevel::RAWREPORT) { + if(logid::global_loglevel == LogLevel::RAWREPORT) { printf("[RAWREPORT] %s IN: ", _path.c_str()); for(auto &i : report) printf("%02x ", i); diff --git a/src/logid/logid.cpp b/src/logid/logid.cpp index 077b841..2d885f4 100644 --- a/src/logid/logid.cpp +++ b/src/logid/logid.cpp @@ -21,7 +21,7 @@ #include #include -#include "util.h" +#include "util/log.h" #include "DeviceManager.h" #include "logid.h" @@ -37,7 +37,7 @@ using namespace logid; std::string config_file = DEFAULT_CONFIG_FILE; -LogLevel logid::global_verbosity = INFO; +LogLevel logid::global_loglevel = INFO; // Configuration* logid::global_config; DeviceManager* logid::finder; @@ -97,25 +97,25 @@ void readCliOptions(int argc, char** argv) option = Option::Help; break; default: - log_printf(WARN, "%s is not a valid option, ignoring.", + logPrintf(WARN, "%s is not a valid option, ignoring.", argv[i]); } switch(option) { case Option::Verbose: { if (++i >= argc) { - global_verbosity = DEBUG; // Assume debug verbosity + global_loglevel = DEBUG; // Assume debug verbosity break; } std::string loglevel = argv[i]; try { - global_verbosity = stringToLogLevel(argv[i]); + global_loglevel = toLogLevel(argv[i]); } catch (std::invalid_argument &e) { if (argv[i][0] == '-') { - global_verbosity = DEBUG; // Assume debug verbosity + global_loglevel = DEBUG; // Assume debug verbosity i--; // Go back to last argument to continue loop. } else { - log_printf(WARN, e.what()); + logPrintf(WARN, e.what()); printf("Valid verbosity levels are: Debug, Info, " "Warn/Warning, or Error.\n"); exit(EXIT_FAILURE); @@ -125,7 +125,7 @@ void readCliOptions(int argc, char** argv) } case Option::Config: { if (++i >= argc) { - log_printf(ERROR, "Config file is not specified."); + logPrintf(ERROR, "Config file is not specified."); exit(EXIT_FAILURE); } config_file = argv[i]; diff --git a/src/logid/util.cpp b/src/logid/util.cpp index ead4def..03383d4 100644 --- a/src/logid/util.cpp +++ b/src/logid/util.cpp @@ -28,32 +28,6 @@ using namespace logid; -void logid::log_printf(LogLevel level, const char* format, ...) -{ - if(global_verbosity > level) return; - - va_list vargs; - va_start(vargs, format); - - FILE* stream = stdout; - if(level == ERROR || level == WARN) stream = stderr; - - fprintf(stream, "[%s] ", level_prefix(level)); - vfprintf(stream, format, vargs); - fprintf(stream, "\n"); -} - -const char* logid::level_prefix(LogLevel level) -{ - if(level == RAWREPORT) return "RAWREPORT"; - if(level == DEBUG) return "DEBUG"; - if(level == INFO) return "INFO" ; - if(level == WARN) return "WARN"; - if(level == ERROR) return "ERROR"; - - return "DEBUG"; -} - /* Direction logid::getDirection(int x, int y) { diff --git a/src/logid/util.h b/src/logid/util.h index b3f7c2d..24ed02b 100644 --- a/src/logid/util.h +++ b/src/logid/util.h @@ -23,28 +23,12 @@ namespace logid { - enum LogLevel - { - RAWREPORT, - DEBUG, - INFO, - WARN, - ERROR - }; - - extern LogLevel global_verbosity; - - void log_printf(LogLevel level, const char* format, ...); - - const char* level_prefix(LogLevel level); - /* Direction getDirection(int x, int y); Direction stringToDirection(std::string s); GestureMode stringToGestureMode(std::string s); Action stringToAction(std::string s); */ - LogLevel stringToLogLevel(std::string s); } #endif //LOGID_UTIL_H \ No newline at end of file diff --git a/src/logid/util/ExceptionHandler.cpp b/src/logid/util/ExceptionHandler.cpp index 3019806..859783f 100644 --- a/src/logid/util/ExceptionHandler.cpp +++ b/src/logid/util/ExceptionHandler.cpp @@ -16,7 +16,7 @@ * */ #include -#include "../util.h" +#include "log.h" #include "ExceptionHandler.h" #include "../backend/hidpp10/Error.h" #include "../backend/hidpp20/Error.h" @@ -28,16 +28,16 @@ void ExceptionHandler::Default(std::exception& error) try { throw error; } catch(backend::hidpp10::Error& e) { - log_printf(WARN, "HID++ 1.0 error ignored on detached thread: %s", + logPrintf(WARN, "HID++ 1.0 error ignored on detached thread: %s", error.what()); } catch(backend::hidpp20::Error& e) { - log_printf(WARN, "HID++ 2.0 error ignored on detached thread: %s", + logPrintf(WARN, "HID++ 2.0 error ignored on detached thread: %s", error.what()); } catch(std::system_error& e) { - log_printf(WARN, "System error ignored on detached thread: %s", + logPrintf(WARN, "System error ignored on detached thread: %s", error.what()); } catch(std::exception& e) { - log_printf(WARN, "Error ignored on detached thread: %s", + logPrintf(WARN, "Error ignored on detached thread: %s", error.what()); } } \ No newline at end of file diff --git a/src/logid/util/log.cpp b/src/logid/util/log.cpp new file mode 100644 index 0000000..af7bfe4 --- /dev/null +++ b/src/logid/util/log.cpp @@ -0,0 +1,80 @@ +/* + * Copyright 2019-2020 PixlOne + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include +#include +#include +#include +#include +#include "log.h" + +using namespace logid; + +void logid::logPrintf(LogLevel level, const char* format, ...) +{ + if(global_loglevel > level) return; + + va_list vargs; + va_start(vargs, format); + + FILE* stream = stdout; + if(level == ERROR || level == WARN) + stream = stderr; + + fprintf(stream, "[%s] ", levelPrefix(level)); + vfprintf(stream, format, vargs); + fprintf(stream, "\n"); +} + +const char* logid::levelPrefix(LogLevel level) +{ + switch(level) { + case RAWREPORT: + return "RAWREPORT"; + case DEBUG: + return "DEBUG"; + case INFO: + return "INFO"; + case WARN: + return "WARN"; + case ERROR: + return "ERROR"; + default: + return "UNKNOWN"; + } +} + + +LogLevel logid::toLogLevel(std::string s) +{ + std::string original_str = s; + std::transform(s.begin(), s.end(), s.begin(), ::tolower); + + if(s == "rawreport") + return RAWREPORT; + if(s == "debug") + return DEBUG; + if(s == "info") + return INFO; + if(s == "warn" || s == "warning") + return WARN; + if(s == "error") + return ERROR; + + throw std::invalid_argument(original_str + " is an invalid log level."); +} \ No newline at end of file diff --git a/src/logid/util/log.h b/src/logid/util/log.h new file mode 100644 index 0000000..fad0b66 --- /dev/null +++ b/src/logid/util/log.h @@ -0,0 +1,41 @@ +/* + * Copyright 2019-2020 PixlOne + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#ifndef LOGID_LOG_H +#define LOGID_LOG_H + +#include + +namespace logid +{ + enum LogLevel + { + RAWREPORT, + DEBUG, + INFO, + WARN, + ERROR + }; + + extern LogLevel global_loglevel; + + void logPrintf(LogLevel level, const char *format, ...); + const char *levelPrefix(LogLevel level); + LogLevel toLogLevel(std::string s); +} + +#endif //LOGID_LOG_H