Вычисление переменных на этапе исполнения

This commit is contained in:
Andrei Astafev 2019-10-09 05:35:12 +03:00
parent 26e43221d1
commit d54886620f
2 changed files with 34 additions and 21 deletions

View File

@ -1,9 +1,11 @@
#include "paths.hpp" #include "paths.hpp"
#include <myx/base/config.hpp> #include <myx/base/config.hpp>
#include "whereami++.h" #include "whereami++.h"
#include <paths.h> #include <paths.h>
#include <QString> #include <QString>
#include <QCoreApplication>
namespace myx { namespace myx {
@ -57,19 +59,22 @@ void Paths::setConfigFilePath( const QFileInfo& configFilePath )
} }
Paths::Paths() : Paths::Paths()
m_prefixDirectory( "/opt/" ORGANIZATION_NAME_LOWER "/" PROJECT_NAME_LOWER ),
m_binaryDirectory( m_prefixDirectory.absolutePath() + "/bin" ),
m_configDirectory( m_prefixDirectory.absolutePath() + "/etc" ),
m_cacheDirectory( m_prefixDirectory.absolutePath() + "/var" ),
m_tempDirectory( QString::fromLocal8Bit( qgetenv( qPrintable( "TMPDIR" ) ) ) ),
m_dataDirectory( m_prefixDirectory.absolutePath() + "/share" ),
m_homeDirectory( QString::fromLocal8Bit( qgetenv( qPrintable( "HOME" ) ) ) ),
m_executableFileName( PROJECT_NAME_LOWER ),
m_configFileName( PROJECT_NAME_LOWER ".conf" ),
m_executableFilePath( m_binaryDirectory.absolutePath() + "/" + m_executableFileName ),
m_configFilePath( m_binaryDirectory.absolutePath() + "/" + m_configFileName )
{ {
m_prefixDirectory = "/opt/" + QCoreApplication::organizationName().toLower() +
"/" + QCoreApplication::applicationName().toLower();
m_binaryDirectory = m_prefixDirectory.absolutePath() + "/bin";
m_configDirectory = m_prefixDirectory.absolutePath() + "/etc";
m_cacheDirectory = m_prefixDirectory.absolutePath() + "/var";
m_tempDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "TMPDIR" ) ) );
m_dataDirectory = m_prefixDirectory.absolutePath() + "/share";
m_homeDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "HOME" ) ) );
m_executableFileName = QCoreApplication::applicationName().toLower();
m_configFileName = m_executableFileName + ".conf";
m_executableFilePath = m_binaryDirectory.absolutePath() + "/" + m_executableFileName;
m_configFilePath = m_binaryDirectory.absolutePath() + "/" + m_configFileName;
if ( m_tempDirectory.absolutePath().isEmpty() || ( m_tempDirectory.path() == "." ) ) if ( m_tempDirectory.absolutePath().isEmpty() || ( m_tempDirectory.path() == "." ) )
{ {
m_tempDirectory = _PATH_TMP; m_tempDirectory = _PATH_TMP;
@ -163,7 +168,8 @@ bool Paths::updatePaths()
m_configDirectory = m_prefixDirectory.absolutePath() + "/etc"; m_configDirectory = m_prefixDirectory.absolutePath() + "/etc";
m_cacheDirectory = m_prefixDirectory.absolutePath() + "/var"; m_cacheDirectory = m_prefixDirectory.absolutePath() + "/var";
m_dataDirectory = m_prefixDirectory.absolutePath() + "/share"; m_dataDirectory = m_prefixDirectory.absolutePath() + "/share";
m_configFilePath = QFile( m_configDirectory.absolutePath() + "/" + PROJECT_NAME_LOWER + ".conf" ); m_configFilePath = QFile( m_configDirectory.absolutePath() + "/" +
QCoreApplication::applicationName() + ".conf" );
} }
if ( m_prefixDirectory.absolutePath().startsWith( "/opt" ) || if ( m_prefixDirectory.absolutePath().startsWith( "/opt" ) ||
@ -172,23 +178,29 @@ bool Paths::updatePaths()
QString dataDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_DATA_HOME" ) ) ); QString dataDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_DATA_HOME" ) ) );
if ( dataDirectory.isEmpty() ) if ( dataDirectory.isEmpty() )
{ {
dataDirectory = m_homeDirectory.absolutePath() + ".local/share/"; dataDirectory = m_homeDirectory.absolutePath() + ".local/share";
} }
m_dataDirectory = dataDirectory + ORGANIZATION_NAME_LOWER + "/" + PROJECT_NAME_LOWER; m_dataDirectory = dataDirectory + "/" +
QCoreApplication::organizationName().toLower() + "/" +
QCoreApplication::applicationName().toLower();
QString configDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_CONFIG_HOME" ) ) ); QString configDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_CONFIG_HOME" ) ) );
if ( configDirectory.isEmpty() ) if ( configDirectory.isEmpty() )
{ {
configDirectory = m_homeDirectory.absolutePath() + ".config/"; configDirectory = m_homeDirectory.absolutePath() + ".config";
} }
m_configDirectory = configDirectory + ORGANIZATION_NAME_LOWER + "/" + PROJECT_NAME_LOWER; m_configDirectory = configDirectory + "/" +
QCoreApplication::organizationName().toLower() + "/" +
QCoreApplication::applicationName().toLower();
QString cacheDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_CACHE_HOME" ) ) ); QString cacheDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_CACHE_HOME" ) ) );
if ( cacheDirectory.isEmpty() ) if ( cacheDirectory.isEmpty() )
{ {
cacheDirectory = m_homeDirectory.absolutePath() + ".cache/"; cacheDirectory = m_homeDirectory.absolutePath() + ".cache";
} }
m_cacheDirectory = cacheDirectory + ORGANIZATION_NAME_LOWER + "/" + PROJECT_NAME_LOWER; m_cacheDirectory = cacheDirectory + "/" +
QCoreApplication::organizationName().toLower() + "/" +
QCoreApplication::applicationName().toLower();
} }
return( true ); return( true );
@ -214,7 +226,8 @@ QString Paths::findConfigFile( const QString& defaultConfigFile )
return( defaultConfigFile ); return( defaultConfigFile );
} }
auto fileName = QString::fromLocal8Bit( qgetenv( qPrintable( PROJECT_NAME_UPPER "_CONFIG" ) ) ); auto fileName = QString::fromLocal8Bit( qgetenv( QCoreApplication::applicationName()
.toUpper().toUtf8() + "_CONFIG" ) );
if ( QFileInfo( fileName ).isReadable() ) if ( QFileInfo( fileName ).isReadable() )
{ {
m_configFilePath = fileName; m_configFilePath = fileName;

View File

@ -12,7 +12,7 @@ Logger::Logger( std::string name ) :
m_logger( nullptr ), m_logger( nullptr ),
m_name( std::move( name ) ), m_name( std::move( name ) ),
m_outputPattern( "[%H:%M:%S %z] [%n] [%^---%L---%$] %v" ), m_outputPattern( "[%H:%M:%S %z] [%n] [%^---%L---%$] %v" ),
m_baseFileName( fmt::format( "{}_{}", PROJECT_NAME, "st" ) ), m_baseFileName( "default" ),
m_maxRotatingFileSize( rotatingFileSize ), m_maxRotatingFileSize( rotatingFileSize ),
m_maxRotatingFilesCount( 3 ), m_maxRotatingFilesCount( 3 ),
m_outputLevel( spdlog::level::trace ), m_outputLevel( spdlog::level::trace ),