Move logger into util/log.h

This commit is contained in:
pixl 2020-06-24 18:10:25 -04:00
parent 4ba9248038
commit 1106133f3c
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
13 changed files with 162 additions and 83 deletions

View File

@ -10,7 +10,7 @@ find_package(PkgConfig REQUIRED)
add_executable(logid add_executable(logid
logid.cpp logid.cpp
util.cpp util/log.cpp
DeviceManager.cpp DeviceManager.cpp
Device.cpp Device.cpp
Receiver.cpp Receiver.cpp

View File

@ -16,7 +16,7 @@
* *
*/ */
#include "util.h" #include "util/log.h"
#include "Device.h" #include "Device.h"
using namespace logid; using namespace logid;
@ -25,22 +25,22 @@ using namespace logid::backend;
Device::Device(std::string path, backend::hidpp::DeviceIndex index) : Device::Device(std::string path, backend::hidpp::DeviceIndex index) :
_hidpp20 (path, index), _path (std::move(path)), _index (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<backend::raw::RawDevice>& raw_device, Device::Device(const std::shared_ptr<backend::raw::RawDevice>& raw_device,
hidpp::DeviceIndex index) : _hidpp20(raw_device, index), _path hidpp::DeviceIndex index) : _hidpp20(raw_device, index), _path
(raw_device->hidrawPath()), _index (index) (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() 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() void Device::wakeup()
{ {
log_printf(INFO, "%s:%d woke up.", _path.c_str(), _index); logPrintf(INFO, "%s:%d woke up.", _path.c_str(), _index);
} }

View File

@ -21,7 +21,7 @@
#include "DeviceManager.h" #include "DeviceManager.h"
#include "Receiver.h" #include "Receiver.h"
#include "util.h" #include "util/log.h"
#include "backend/hidpp10/Error.h" #include "backend/hidpp10/Error.h"
#include "backend/dj/Receiver.h" #include "backend/dj/Receiver.h"
@ -43,13 +43,13 @@ void DeviceManager::addDevice(std::string path)
} catch(hidpp::Device::InvalidDevice &e) { // Ignore } catch(hidpp::Device::InvalidDevice &e) { // Ignore
defaultExists = false; defaultExists = false;
} catch(std::system_error &e) { } 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()); path.c_str(), e.what());
return; return;
} }
if(isReceiver) { 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<Receiver>(path); auto receiver = std::make_shared<Receiver>(path);
receiver->run(); receiver->run();
_receivers.emplace(path, receiver); _receivers.emplace(path, receiver);
@ -69,13 +69,13 @@ void DeviceManager::addDevice(std::string path)
if(e.code() != hidpp10::Error::UnknownDevice) if(e.code() != hidpp10::Error::UnknownDevice)
throw; throw;
else else
log_printf(WARN, logPrintf(WARN,
"HID++ 1.0 error while trying to initialize %s:" "HID++ 1.0 error while trying to initialize %s:"
"%s", path.c_str(), e.what()); "%s", path.c_str(), e.what());
} catch(hidpp::Device::InvalidDevice &e) { // Ignore } catch(hidpp::Device::InvalidDevice &e) { // Ignore
} catch(std::system_error &e) { } catch(std::system_error &e) {
// This error should have been thrown previously // 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()); e.what());
} }
} }
@ -88,12 +88,12 @@ void DeviceManager::removeDevice(std::string path)
if(receiver != _receivers.end()) { if(receiver != _receivers.end()) {
_receivers.erase(receiver); _receivers.erase(receiver);
log_printf(INFO, "Receiver on %s disconnected", path.c_str()); logPrintf(INFO, "Receiver on %s disconnected", path.c_str());
} else { } else {
auto device = _devices.find(path); auto device = _devices.find(path);
if(device != _devices.find(path)) { if(device != _devices.find(path)) {
_devices.erase(device); _devices.erase(device);
log_printf(INFO, "Device on %s disconnected", path.c_str()); logPrintf(INFO, "Device on %s disconnected", path.c_str());
} }
} }
} }

View File

@ -18,7 +18,7 @@
#include <cassert> #include <cassert>
#include "Receiver.h" #include "Receiver.h"
#include "util.h" #include "util/log.h"
#include "backend/hidpp10/Error.h" #include "backend/hidpp10/Error.h"
#include "backend/hidpp20/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) 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) void Receiver::addDevice(hidpp::DeviceConnectionEvent event)
@ -50,7 +50,7 @@ void Receiver::addDevice(hidpp::DeviceConnectionEvent event)
auto version = hidpp_device.version(); auto version = hidpp_device.version();
if(std::get<0>(version) < 2) { 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); _path.c_str(), event.index);
return; return;
} }
@ -61,11 +61,11 @@ void Receiver::addDevice(hidpp::DeviceConnectionEvent event)
_devices.emplace(event.index, device); _devices.emplace(event.index, device);
} catch(hidpp10::Error &e) { } catch(hidpp10::Error &e) {
log_printf(ERROR, logPrintf(ERROR,
"Caught HID++ 1.0 error while trying to initialize " "Caught HID++ 1.0 error while trying to initialize "
"%s:%d: %s", _path.c_str(), event.index, e.what()); "%s:%d: %s", _path.c_str(), event.index, e.what());
} catch(hidpp20::Error &e) { } 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()); "%s:%d: %s", _path.c_str(), event.index, e.what());
} }
} }

View File

@ -18,7 +18,7 @@
#include "ReceiverMonitor.h" #include "ReceiverMonitor.h"
#include "../../util/thread.h" #include "../../util/thread.h"
#include "../../util.h" #include "../../util/log.h"
#include <utility> #include <utility>
#include <cassert> #include <cassert>
@ -67,11 +67,11 @@ void ReceiverMonitor::run()
}}, {[report, path=this->_receiver->rawDevice()->hidrawPath()] }}, {[report, path=this->_receiver->rawDevice()->hidrawPath()]
(std::exception& e) { (std::exception& e) {
if(report.subId() == Receiver::DeviceConnection) 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(), "on %s: %s", report.deviceIndex(),
path.c_str(), e.what()); path.c_str(), e.what());
else if(report.subId() == Receiver::DeviceDisconnection) 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() "receiver on %s: %s", report.deviceIndex()
,path.c_str(), e.what()); ,path.c_str(), e.what());
}}); }});

View File

@ -18,7 +18,7 @@
#include "DeviceMonitor.h" #include "DeviceMonitor.h"
#include "../../util/thread.h" #include "../../util/thread.h"
#include "../../util.h" #include "../../util/log.h"
#include <thread> #include <thread>
#include <system_error> #include <system_error>
@ -101,14 +101,14 @@ void DeviceMonitor::run()
thread::spawn([this, name=devnode]() { thread::spawn([this, name=devnode]() {
this->addDevice(name); this->addDevice(name);
}, [name=devnode](std::exception& e){ }, [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()); name.c_str(), e.what());
}); });
else if (action == "remove") else if (action == "remove")
thread::spawn([this, name=devnode]() { thread::spawn([this, name=devnode]() {
this->removeDevice(name); this->removeDevice(name);
}, [name=devnode](std::exception& e){ }, [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()); name.c_str(), e.what());
}); });
@ -160,7 +160,7 @@ void DeviceMonitor::enumerate()
thread::spawn([this, name=devnode]() { thread::spawn([this, name=devnode]() {
this->addDevice(name); this->addDevice(name);
}, [name=devnode](std::exception& e){ }, [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()); name.c_str(), e.what());
}); });
} }

View File

@ -20,7 +20,7 @@
#include "../Error.h" #include "../Error.h"
#include "../hidpp/defs.h" #include "../hidpp/defs.h"
#include "../dj/defs.h" #include "../dj/defs.h"
#include "../../util.h" #include "../../util/log.h"
#include "../hidpp/Report.h" #include "../hidpp/Report.h"
#include <string> #include <string>
@ -230,7 +230,7 @@ std::vector<uint8_t> RawDevice::_respondToReport
int RawDevice::_sendReport(const std::vector<uint8_t>& report) int RawDevice::_sendReport(const std::vector<uint8_t>& report)
{ {
std::lock_guard<std::mutex> lock(_dev_io); std::lock_guard<std::mutex> lock(_dev_io);
if(logid::global_verbosity == LogLevel::RAWREPORT) { if(logid::global_loglevel == LogLevel::RAWREPORT) {
printf("[RAWREPORT] %s OUT: ", _path.c_str()); printf("[RAWREPORT] %s OUT: ", _path.c_str());
for(auto &i : report) for(auto &i : report)
printf("%02x ", i); printf("%02x ", i);
@ -295,7 +295,7 @@ int RawDevice::_readReport(std::vector<uint8_t>& report, std::size_t maxDataLeng
if(0 == ret) if(0 == ret)
throw backend::TimeoutError(); throw backend::TimeoutError();
if(logid::global_verbosity == LogLevel::RAWREPORT) { if(logid::global_loglevel == LogLevel::RAWREPORT) {
printf("[RAWREPORT] %s IN: ", _path.c_str()); printf("[RAWREPORT] %s IN: ", _path.c_str());
for(auto &i : report) for(auto &i : report)
printf("%02x ", i); printf("%02x ", i);

View File

@ -21,7 +21,7 @@
#include <cstdlib> #include <cstdlib>
#include <mutex> #include <mutex>
#include "util.h" #include "util/log.h"
#include "DeviceManager.h" #include "DeviceManager.h"
#include "logid.h" #include "logid.h"
@ -37,7 +37,7 @@ using namespace logid;
std::string config_file = DEFAULT_CONFIG_FILE; std::string config_file = DEFAULT_CONFIG_FILE;
LogLevel logid::global_verbosity = INFO; LogLevel logid::global_loglevel = INFO;
// Configuration* logid::global_config; // Configuration* logid::global_config;
DeviceManager* logid::finder; DeviceManager* logid::finder;
@ -97,25 +97,25 @@ void readCliOptions(int argc, char** argv)
option = Option::Help; option = Option::Help;
break; break;
default: default:
log_printf(WARN, "%s is not a valid option, ignoring.", logPrintf(WARN, "%s is not a valid option, ignoring.",
argv[i]); argv[i]);
} }
switch(option) { switch(option) {
case Option::Verbose: { case Option::Verbose: {
if (++i >= argc) { if (++i >= argc) {
global_verbosity = DEBUG; // Assume debug verbosity global_loglevel = DEBUG; // Assume debug verbosity
break; break;
} }
std::string loglevel = argv[i]; std::string loglevel = argv[i];
try { try {
global_verbosity = stringToLogLevel(argv[i]); global_loglevel = toLogLevel(argv[i]);
} catch (std::invalid_argument &e) { } catch (std::invalid_argument &e) {
if (argv[i][0] == '-') { 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. i--; // Go back to last argument to continue loop.
} else { } else {
log_printf(WARN, e.what()); logPrintf(WARN, e.what());
printf("Valid verbosity levels are: Debug, Info, " printf("Valid verbosity levels are: Debug, Info, "
"Warn/Warning, or Error.\n"); "Warn/Warning, or Error.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -125,7 +125,7 @@ void readCliOptions(int argc, char** argv)
} }
case Option::Config: { case Option::Config: {
if (++i >= argc) { if (++i >= argc) {
log_printf(ERROR, "Config file is not specified."); logPrintf(ERROR, "Config file is not specified.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
config_file = argv[i]; config_file = argv[i];

View File

@ -28,32 +28,6 @@
using namespace logid; 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) Direction logid::getDirection(int x, int y)
{ {

View File

@ -23,28 +23,12 @@
namespace logid 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 getDirection(int x, int y);
Direction stringToDirection(std::string s); Direction stringToDirection(std::string s);
GestureMode stringToGestureMode(std::string s); GestureMode stringToGestureMode(std::string s);
Action stringToAction(std::string s); Action stringToAction(std::string s);
*/ */
LogLevel stringToLogLevel(std::string s);
} }
#endif //LOGID_UTIL_H #endif //LOGID_UTIL_H

View File

@ -16,7 +16,7 @@
* *
*/ */
#include <system_error> #include <system_error>
#include "../util.h" #include "log.h"
#include "ExceptionHandler.h" #include "ExceptionHandler.h"
#include "../backend/hidpp10/Error.h" #include "../backend/hidpp10/Error.h"
#include "../backend/hidpp20/Error.h" #include "../backend/hidpp20/Error.h"
@ -28,16 +28,16 @@ void ExceptionHandler::Default(std::exception& error)
try { try {
throw error; throw error;
} catch(backend::hidpp10::Error& e) { } 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()); error.what());
} catch(backend::hidpp20::Error& e) { } 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()); error.what());
} catch(std::system_error& e) { } 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()); error.what());
} catch(std::exception& e) { } catch(std::exception& e) {
log_printf(WARN, "Error ignored on detached thread: %s", logPrintf(WARN, "Error ignored on detached thread: %s",
error.what()); error.what());
} }
} }

80
src/logid/util/log.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
#include <cstdio>
#include <string>
#include <cstdarg>
#include <algorithm>
#include <stdexcept>
#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.");
}

41
src/logid/util/log.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
#ifndef LOGID_LOG_H
#define LOGID_LOG_H
#include <string>
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