Move logger into util/log.h

master
pixl 4 years ago
parent 4ba9248038
commit 1106133f3c
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
  1. 2
      src/logid/CMakeLists.txt
  2. 10
      src/logid/Device.cpp
  3. 14
      src/logid/DeviceManager.cpp
  4. 10
      src/logid/Receiver.cpp
  5. 6
      src/logid/backend/dj/ReceiverMonitor.cpp
  6. 8
      src/logid/backend/raw/DeviceMonitor.cpp
  7. 6
      src/logid/backend/raw/RawDevice.cpp
  8. 16
      src/logid/logid.cpp
  9. 26
      src/logid/util.cpp
  10. 16
      src/logid/util.h
  11. 10
      src/logid/util/ExceptionHandler.cpp
  12. 80
      src/logid/util/log.cpp
  13. 41
      src/logid/util/log.h

@ -10,7 +10,7 @@ find_package(PkgConfig REQUIRED)
add_executable(logid
logid.cpp
util.cpp
util/log.cpp
DeviceManager.cpp
Device.cpp
Receiver.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<backend::raw::RawDevice>& 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);
}

@ -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<Receiver>(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());
}
}
}

@ -18,7 +18,7 @@
#include <cassert>
#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());
}
}

@ -18,7 +18,7 @@
#include "ReceiverMonitor.h"
#include "../../util/thread.h"
#include "../../util.h"
#include "../../util/log.h"
#include <utility>
#include <cassert>
@ -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());
}});

@ -18,7 +18,7 @@
#include "DeviceMonitor.h"
#include "../../util/thread.h"
#include "../../util.h"
#include "../../util/log.h"
#include <thread>
#include <system_error>
@ -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());
});
}

@ -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 <string>
@ -230,7 +230,7 @@ std::vector<uint8_t> RawDevice::_respondToReport
int RawDevice::_sendReport(const std::vector<uint8_t>& report)
{
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());
for(auto &i : report)
printf("%02x ", i);
@ -295,7 +295,7 @@ int RawDevice::_readReport(std::vector<uint8_t>& 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);

@ -21,7 +21,7 @@
#include <cstdlib>
#include <mutex>
#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];

@ -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)
{

@ -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

@ -16,7 +16,7 @@
*
*/
#include <system_error>
#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());
}
}

@ -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.");
}

@ -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
Loading…
Cancel
Save