Move logger into util/log.h

This commit is contained in:
pixl
2020-06-24 18:10:25 -04:00
parent 4ba9248038
commit 1106133f3c
13 changed files with 162 additions and 83 deletions

View File

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

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