migrate std::string to qstring + add sysinfo via json (#412)

* std::string -> qstring part 1

* more string migration

* more string migration ...

* ...

* more qstring mogrations
add sysinfo via json

* remove unneccessary includes

* integrate sysinfo into webui
This commit is contained in:
redPanther
2017-03-04 22:17:42 +01:00
committed by GitHub
parent 19f8928869
commit bfb9a08c80
90 changed files with 539 additions and 529 deletions

View File

@@ -62,7 +62,7 @@ add_library(hyperion-utils
${PROFILER_SOURCE}
)
qt5_use_modules(hyperion-utils Core Gui)
qt5_use_modules(hyperion-utils Core Gui Network)
target_link_libraries(hyperion-utils
${QT_LIBRARIES})

View File

@@ -12,43 +12,42 @@ static const int LogLevelSysLog[] = { LOG_DEBUG, LOG_DEBUG, LOG_INFO, LOG_
static unsigned int loggerCount = 0;
static unsigned int loggerId = 0;
std::map<std::string,Logger*> *Logger::LoggerMap = nullptr;
std::map<QString,Logger*> *Logger::LoggerMap = nullptr;
Logger::LogLevel Logger::GLOBAL_MIN_LOG_LEVEL = Logger::UNSET;
LoggerManager* LoggerManager::_instance = nullptr;
Logger* Logger::getInstance(QString name, Logger::LogLevel minLevel)
{
qRegisterMetaType<Logger::T_LOG_MESSAGE>();
std::string loggerName = name.toStdString();
Logger* log = nullptr;
if (LoggerMap == nullptr)
{
LoggerMap = new std::map<std::string,Logger*>;
LoggerMap = new std::map<QString,Logger*>;
}
if ( LoggerMap->find(loggerName) == LoggerMap->end() )
if ( LoggerMap->find(name) == LoggerMap->end() )
{
log = new Logger(loggerName,minLevel);
LoggerMap->insert(std::pair<std::string,Logger*>(loggerName,log)); // compat version, replace it with following line if we have 100% c++11
//LoggerMap->emplace(loggerName,log); // not compat with older linux distro's e.g. wheezy
log = new Logger(name,minLevel);
LoggerMap->insert(std::pair<QString,Logger*>(name,log)); // compat version, replace it with following line if we have 100% c++11
//LoggerMap->emplace(name,log); // not compat with older linux distro's e.g. wheezy
connect(log, SIGNAL(newLogMessage(Logger::T_LOG_MESSAGE)), LoggerManager::getInstance(), SLOT(handleNewLogMessage(Logger::T_LOG_MESSAGE)));
}
else
{
log = LoggerMap->at(loggerName);
log = LoggerMap->at(name);
}
return log;
}
void Logger::deleteInstance(std::string name)
void Logger::deleteInstance(QString name)
{
if (LoggerMap == nullptr)
return;
if ( name.empty() )
if ( name.isEmpty() )
{
std::map<std::string,Logger*>::iterator it;
std::map<QString,Logger*>::iterator it;
for ( it=LoggerMap->begin(); it != LoggerMap->end(); it++)
{
delete it->second;
@@ -63,31 +62,31 @@ void Logger::deleteInstance(std::string name)
}
void Logger::setLogLevel(LogLevel level,std::string name)
void Logger::setLogLevel(LogLevel level,QString name)
{
if ( name.empty() )
if ( name.isEmpty() )
{
GLOBAL_MIN_LOG_LEVEL = level;
}
else
{
Logger* log = Logger::getInstance(QString::fromStdString(name),level);
Logger* log = Logger::getInstance(name,level);
log->setMinLevel(level);
}
}
Logger::LogLevel Logger::getLogLevel(std::string name)
Logger::LogLevel Logger::getLogLevel(QString name)
{
if ( name.empty() )
if ( name.isEmpty() )
{
return GLOBAL_MIN_LOG_LEVEL;
}
Logger* log = Logger::getInstance(QString::fromStdString(name));
Logger* log = Logger::getInstance(name);
return log->getMinLevel();
}
Logger::Logger ( std::string name, LogLevel minLevel )
Logger::Logger ( QString name, LogLevel minLevel )
: QObject()
, _name(name)
, _minLevel(minLevel)
@@ -99,8 +98,8 @@ Logger::Logger ( std::string name, LogLevel minLevel )
#else
const char* _appname_char = getprogname();
#endif
_appname = std::string(_appname_char);
std::transform(_appname.begin(), _appname.end(),_appname.begin(), ::toupper);
_appname = QString(_appname_char).toLower();
loggerCount++;
@@ -112,7 +111,7 @@ Logger::Logger ( std::string name, LogLevel minLevel )
Logger::~Logger()
{
Debug(this, "logger '%s' destroyed", _name.c_str() );
Debug(this, "logger '%s' destroyed", QSTRING_CSTR(_name) );
loggerCount--;
if ( loggerCount == 0 )
closelog();
@@ -134,15 +133,15 @@ void Logger::Message(LogLevel level, const char* sourceFile, const char* func, u
Logger::T_LOG_MESSAGE logMsg;
logMsg.appName = QString::fromStdString(_appname);
logMsg.loggerName = QString::fromStdString(_name);
logMsg.appName = _appname;
logMsg.loggerName = _name;
logMsg.function = QString(func);
logMsg.line = line;
logMsg.fileName = FileUtils::getBaseName(sourceFile);
time(&(logMsg.utime));
logMsg.message = QString(msg);
logMsg.level = level;
logMsg.levelString = QString::fromStdString(LogLevelStrings[level]);
logMsg.levelString = LogLevelStrings[level];
emit newLogMessage(logMsg);
@@ -152,10 +151,7 @@ void Logger::Message(LogLevel level, const char* sourceFile, const char* func, u
location = "<" + logMsg.fileName + ":" + QString::number(line)+":"+ logMsg.function + "()> ";
}
std::cout
<< "[" << _appname << " " << _name << "] <"
<< LogLevelStrings[level] << "> " << location.toStdString() << msg
<< std::endl;
std::cout << QString("[" + _appname + " " + _name + "] <" + LogLevelStrings[level] + "> " + location + msg).toStdString() << std::endl;
if ( _syslogEnabled && level >= Logger::WARNING )
syslog (LogLevelSysLog[level], "%s", msg);

View File

@@ -3,7 +3,6 @@
#include <QCoreApplication>
#include <QStringList>
#include <string>
#include <unistd.h>
#include <cstdio>
@@ -41,7 +40,7 @@ void restartHyperion(bool asNewProcess)
QByteArray command_exec(QString cmd, QByteArray data)
{
char buffer[128];
std::string result = "";
QString result = "";
std::shared_ptr<FILE> pipe(popen(cmd.toLocal8Bit().constData(), "r"), pclose);
if (pipe)
@@ -52,7 +51,7 @@ QByteArray command_exec(QString cmd, QByteArray data)
result += buffer;
}
}
return result.c_str();
return QSTRING_CSTR(result);
}
};

View File

@@ -13,7 +13,7 @@ struct StopWatchItem {
};
static unsigned int blockCounter = 0;
static std::map<std::string,StopWatchItem> GlobalProfilerMap;
static std::map<QString,StopWatchItem> GlobalProfilerMap;
Logger* Profiler::_logger = nullptr;
double getClockDelta(clock_t start)
@@ -44,9 +44,9 @@ void Profiler::initLogger()
_logger = Logger::getInstance("PROFILER", Logger::DEBUG);
}
void Profiler::TimerStart(const std::string timerName, const char* sourceFile, const char* func, unsigned int line)
void Profiler::TimerStart(const QString timerName, const char* sourceFile, const char* func, unsigned int line)
{
std::pair<std::map<std::string,StopWatchItem>::iterator,bool> ret;
std::pair<std::map<QString,StopWatchItem>::iterator,bool> ret;
Profiler::initLogger();
StopWatchItem item = {sourceFile, func, line};
@@ -56,34 +56,34 @@ void Profiler::TimerStart(const std::string timerName, const char* sourceFile, c
{
if ( ret.first->second.sourceFile == sourceFile && ret.first->second.func == func && ret.first->second.line == line )
{
_logger->Message(Logger::DEBUG, sourceFile, func, line, "restart timer '%s'", timerName.c_str() );
_logger->Message(Logger::DEBUG, sourceFile, func, line, "restart timer '%s'", QSTRING_CSTR(timerName) );
ret.first->second.startTime = clock();
}
else
{
_logger->Message(Logger::DEBUG, sourceFile, func, line, "ERROR timer '%s' started in multiple locations. First occurence %s:%d:%s()",
timerName.c_str(), FileUtils::getBaseName(ret.first->second.sourceFile).toLocal8Bit().constData(), ret.first->second.line, ret.first->second.func );
QSTRING_CSTR(timerName), FileUtils::getBaseName(ret.first->second.sourceFile).toLocal8Bit().constData(), ret.first->second.line, ret.first->second.func );
}
}
else
{
_logger->Message(Logger::DEBUG, sourceFile, func, line, "start timer '%s'", timerName.c_str() );
_logger->Message(Logger::DEBUG, sourceFile, func, line, "start timer '%s'", QSTRING_CSTR(timerName) );
}
}
void Profiler::TimerGetTime(const std::string timerName, const char* sourceFile, const char* func, unsigned int line)
void Profiler::TimerGetTime(const QString timerName, const char* sourceFile, const char* func, unsigned int line)
{
std::map<std::string,StopWatchItem>::iterator ret = GlobalProfilerMap.find(timerName);
std::map<QString,StopWatchItem>::iterator ret = GlobalProfilerMap.find(timerName);
Profiler::initLogger();
if (ret != GlobalProfilerMap.end())
{
_logger->Message(Logger::DEBUG, sourceFile, func, line, "timer '%s' started at %s:%d:%s() took %f s execution time until here", timerName.c_str(),
_logger->Message(Logger::DEBUG, sourceFile, func, line, "timer '%s' started at %s:%d:%s() took %f s execution time until here", QSTRING_CSTR(timerName),
FileUtils::getBaseName(ret->second.sourceFile).toLocal8Bit().constData(), ret->second.line, ret->second.func, getClockDelta(ret->second.startTime) );
}
else
{
_logger->Message(Logger::DEBUG, sourceFile, func, line, "ERROR timer '%s' not started", timerName.c_str() );
_logger->Message(Logger::DEBUG, sourceFile, func, line, "ERROR timer '%s' not started", QSTRING_CSTR(timerName) );
}
}

View File

@@ -5,11 +5,11 @@
namespace RGBW {
WhiteAlgorithm stringToWhiteAlgorithm(std::string str)
WhiteAlgorithm stringToWhiteAlgorithm(QString str)
{
if (str == "subtract_minimum") return SUBTRACT_MINIMUM;
if (str == "sub_min_warm_adjust") return SUB_MIN_WARM_ADJUST;
if (str.empty() || str == "white_off") return WHITE_OFF;
if (str.isEmpty() || str == "white_off") return WHITE_OFF;
return INVALID;
}

View File

@@ -1,5 +1,6 @@
#include "utils/SysInfo.h"
#include <QHostInfo>
#include <QSysInfo>
#include <iostream>
#include <sys/utsname.h>
@@ -8,7 +9,6 @@
#include <stdlib.h>
#include <limits.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
@@ -20,23 +20,15 @@ SysInfo::SysInfo()
{
SysInfo::QUnixOSVersion v;
findUnixOsVersion(v);
std::cout
<< currentCpuArchitecture().toStdString() << " "
<< kernelType().toStdString() << " "
<< kernelVersion().toStdString() << " "
<< v.productType.toStdString() << " "
<< v.productVersion.toStdString() << " "
<< v.prettyName.toStdString() << " "
<< std::endl;
_sysinfo.kernelType = kernelType();
_sysinfo.kernelVersion = kernelVersion();
_sysinfo.architecture = currentCpuArchitecture();
_sysinfo.wordSize = QSysInfo::WordSize;
_sysinfo.wordSize = QString::number(QSysInfo::WordSize);
_sysinfo.productType = v.productType;
_sysinfo.productVersion = v.productVersion;
_sysinfo.prettyName = v.prettyName;
_sysinfo.hostName = QHostInfo::localHostName();
}
SysInfo::~SysInfo()

View File

@@ -1,6 +1,5 @@
// stdlib includes
#include <iterator>
#include <sstream>
#include <algorithm>
#include <math.h>
@@ -33,7 +32,7 @@ bool QJsonSchemaChecker::validate(const QJsonObject & value, bool ignoreRequired
_error = false;
_messages.clear();
_currentPath.clear();
_currentPath.push_back("[root]");
_currentPath.append("[root]");
// validate
validate(value, _qSchema);
@@ -116,21 +115,18 @@ void QJsonSchemaChecker::validate(const QJsonValue & value, const QJsonObject &s
{
// no check function defined for this attribute
_error = true;
setMessage(std::string("No check function defined for attribute ") + attribute.toStdString());
setMessage("No check function defined for attribute " + attribute);
continue;
}
}
}
void QJsonSchemaChecker::setMessage(const std::string & message)
void QJsonSchemaChecker::setMessage(const QString & message)
{
std::ostringstream oss;
std::copy(_currentPath.begin(), _currentPath.end(), std::ostream_iterator<std::string>(oss, ""));
oss << ": " << message;
_messages.push_back(oss.str());
_messages.append(_currentPath.join("") +": "+message);
}
const std::list<std::string> & QJsonSchemaChecker::getMessages() const
const QStringList & QJsonSchemaChecker::getMessages() const
{
return _messages;
}
@@ -166,7 +162,7 @@ void QJsonSchemaChecker::checkType(const QJsonValue & value, const QJsonValue &
if (wrongType)
{
_error = true;
setMessage(type.toStdString() + " expected");
setMessage(type + " expected");
}
}
@@ -178,7 +174,7 @@ void QJsonSchemaChecker::checkProperties(const QJsonObject & value, const QJsonO
const QJsonValue & propertyValue = i.value();
_currentPath.push_back(std::string(".") + property.toStdString());
_currentPath.append("." + property);
QJsonObject::const_iterator required = propertyValue.toObject().find("required");
if (value.contains(property))
@@ -190,7 +186,7 @@ void QJsonSchemaChecker::checkProperties(const QJsonObject & value, const QJsonO
_error = true;
setMessage("missing member");
}
_currentPath.pop_back();
_currentPath.removeLast();
}
}
@@ -202,7 +198,7 @@ void QJsonSchemaChecker::checkAdditionalProperties(const QJsonObject & value, co
if (std::find(ignoredProperties.begin(), ignoredProperties.end(), property) == ignoredProperties.end())
{
// property has no property definition. check against the definition for additional properties
_currentPath.push_back(std::string(".") + property.toStdString());
_currentPath.append("." + property);
if (schema.isBool())
{
if (schema.toBool() == false)
@@ -215,7 +211,7 @@ void QJsonSchemaChecker::checkAdditionalProperties(const QJsonObject & value, co
{
validate(value[property].toObject(), schema.toObject());
}
_currentPath.pop_back();
_currentPath.removeLast();
}
}
}
@@ -233,9 +229,7 @@ void QJsonSchemaChecker::checkMinimum(const QJsonValue & value, const QJsonValue
if (value.toDouble() < schema.toDouble())
{
_error = true;
std::ostringstream oss;
oss << "value is too small (minimum=" << schema.toDouble() << ")";
setMessage(oss.str());
setMessage("value is too small (minimum=" + schema.toString() + ")");
}
}
@@ -252,9 +246,7 @@ void QJsonSchemaChecker::checkMaximum(const QJsonValue & value, const QJsonValue
if (value.toDouble() > schema.toDouble())
{
_error = true;
std::ostringstream oss;
oss << "value is too large (maximum=" << schema.toDouble() << ")";
setMessage(oss.str());
setMessage("value is too large (maximum=" + schema.toString() + ")");
}
}
@@ -272,11 +264,9 @@ void QJsonSchemaChecker::checkItems(const QJsonValue & value, const QJsonObject
for(int i = 0; i < jArray.size(); ++i)
{
// validate each item
std::ostringstream oss;
oss << "[" << i << "]";
_currentPath.push_back(oss.str());
_currentPath.append("[" + QString::number(i) + "]");
validate(jArray[i], schema);
_currentPath.pop_back();
_currentPath.removeLast();
}
}
@@ -296,9 +286,7 @@ void QJsonSchemaChecker::checkMinItems(const QJsonValue & value, const QJsonValu
if (static_cast<int>(jArray.size()) < minimum)
{
_error = true;
std::ostringstream oss;
oss << "array is too small (minimum=" << minimum << ")";
setMessage(oss.str());
setMessage("array is too large (minimum=" + QString::number(minimum) + ")");
}
}
@@ -318,9 +306,7 @@ void QJsonSchemaChecker::checkMaxItems(const QJsonValue & value, const QJsonValu
if (static_cast<int>(jArray.size()) > maximum)
{
_error = true;
std::ostringstream oss;
oss << "array is too large (maximum=" << maximum << ")";
setMessage(oss.str());
setMessage("array is too large (maximum=" + QString::number(maximum) + ")");
}
}
@@ -371,10 +357,7 @@ void QJsonSchemaChecker::checkEnum(const QJsonValue & value, const QJsonValue &
// nothing found
_error = true;
std::ostringstream oss;
oss << "Unknown enum value (allowed values are: " << schema.toString().toStdString();
QJsonDocument doc(schema.toArray());
QString strJson(doc.toJson(QJsonDocument::Compact));
oss << strJson.toStdString() << ")";
setMessage(oss.str());
setMessage("Unknown enum value (allowed values are: " + schema.toString() + strJson+ ")");
}