summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclaiff <claiff@mail.ru>2022-09-23 16:16:56 +0300
committerclaiff <claiff@mail.ru>2022-09-23 16:16:56 +0300
commitb8c1b47e32215cfa9f0483144ee643867ffc67c7 (patch)
tree5a37659a3df70b30eaa1f66d5b3b2c7a60bf75aa
parentee8c175e9eae4faccdf913578fb30d09c200204f (diff)
downloadobmc-sila-smtp-feature/logger.tar.xz
add phosphor loggerfeature/logger
-rw-r--r--CMakeLists.txt7
-rw-r--r--src/checker/errors/settings/port_number.cpp11
-rw-r--r--src/file/mail.cpp8
-rw-r--r--src/file/settings.cpp82
-rw-r--r--src/logger/journal.cpp2
-rw-r--r--src/logger/journal.hpp2
-rw-r--r--src/logger/logger_set.cpp4
-rw-r--r--src/logger/logger_set.hpp2
-rw-r--r--src/logger/phosphor.cpp12
-rw-r--r--src/logger/phosphor.hpp2
-rw-r--r--src/logger/types/ilogger.hpp5
-rw-r--r--src/message/sender.cpp8
12 files changed, 78 insertions, 67 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9fe6741..ca5d5b2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -5,7 +5,7 @@ project(smtp LANGUAGES CXX)
cmake_policy(SET CMP0054 NEW)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
-set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -fno-rtti")
@@ -23,7 +23,7 @@ option (
option (
ADD_PHOSPHOR_LOGGING
"All logging write to webui"
- ON
+ OFF
)
if(NOT ${YOCTO_DEPENDENCIES})
@@ -170,7 +170,8 @@ set(SRC_FILES
${CHECKER_DIR}/errors/types/isettings_error.hpp
)
-if( ${ADD_PHOSPHOR_LOGGING} )
+if( ${ADD_PHOSPHOR_LOGGING} STREQUAL "ON")
+ message("hello")
set(PHOSPHOR_LOGGING_FILES
src/logger/phosphor.cpp
src/logger/phosphor.hpp
diff --git a/src/checker/errors/settings/port_number.cpp b/src/checker/errors/settings/port_number.cpp
index b02eec4..73469cb 100644
--- a/src/checker/errors/settings/port_number.cpp
+++ b/src/checker/errors/settings/port_number.cpp
@@ -1,7 +1,7 @@
#include <stdexcept>
#include "port_number.hpp"
-#include "management/logger.hpp"
+#include "logger/logger_set.hpp"
namespace smtp::checker::errors::settings
{
@@ -10,12 +10,13 @@ namespace smtp::checker::errors::settings
static const std::string PORT_FIELD = "port";
static constexpr int MIN_PORT_NUMBER = 0;
static constexpr int MAX_PORT_NUMBER = 65535;
+ static const std::string METHOD_NAME = "Check settings";
//TODO общее использование полей
auto find = line.find( PORT_FIELD );
if( find == line.end() )
{
- manage::Logger::LogError( "Port doesn't found" );
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Port doesn't found" );
return false;
}
auto host_as_string = find->second;
@@ -30,17 +31,17 @@ namespace smtp::checker::errors::settings
}
catch( std::invalid_argument const& ex )
{
- manage::Logger::LogError( "Port doesn't entered by numbers" );
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Port doesn't entered by numbers" );
return false;
}
catch( std::out_of_range const& ex )
{
- manage::Logger::LogError( "Port out of range" );
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Port out of range" );
return false;
}
catch( ... )
{
- manage::Logger::LogError( "Port doesn't entered by numbers" );
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Port doesn't entered by numbers" );
return false;
}
return host_as_int >= MIN_PORT_NUMBER && host_as_int <= MAX_PORT_NUMBER;
diff --git a/src/file/mail.cpp b/src/file/mail.cpp
index cf5fe26..2a2231b 100644
--- a/src/file/mail.cpp
+++ b/src/file/mail.cpp
@@ -13,10 +13,12 @@ namespace smtp::file
manage::MailsSet Mail::Read() const
{
+ static const std::string METHOD_NAME = "Read mails";
+
std::ifstream mail_file{ mPathFile, std::fstream::in };
if ( !mail_file.is_open() )
{
- manage::Logger::LogError( "Unable to open file to read " + mPathFile );
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Unable to open file to read " + mPathFile );
return {};
}
std::string line{};
@@ -35,10 +37,12 @@ namespace smtp::file
bool Mail::Write( manage::MailsSet const& data ) const
{
+ static const std::string METHOD_NAME = "Write mails";
+
std::ofstream mail_file{ mPathFile, std::fstream::out | std::fstream::trunc };
if ( !mail_file.is_open() )
{
- manage::Logger::LogError( "Unable to open file to write " + mPathFile );
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Unable to open file to write " + mPathFile );
return false;
}
for( const auto& mail : data )
diff --git a/src/file/settings.cpp b/src/file/settings.cpp
index 83f8322..70f3cf7 100644
--- a/src/file/settings.cpp
+++ b/src/file/settings.cpp
@@ -8,51 +8,53 @@ namespace smtp::file
//
//Constructors
//
- Settings::Settings( std::string const& path_file, checker::RegistratorSettings const& registrator_errors )
- : mPathFile( path_file )
- , mRegistratorErrors( registrator_errors )
- {
+ Settings::Settings( std::string const& path_file, checker::RegistratorSettings const& registrator_errors )
+ : mPathFile( path_file )
+ , mRegistratorErrors( registrator_errors )
+ {
- }
+ }
//
//Public methods
//
- manage::SettingsFields Settings::Read() const
- {
- auto parsed_store = GetParsedStore();
- return converter::Full{}.Convert( parsed_store );
- }
+ manage::SettingsFields Settings::Read() const
+ {
+ auto parsed_store = GetParsedStore();
+ return converter::Full{}.Convert( parsed_store );
+ }
- bool Settings::Write( manage::SettingsFields const& settings_fields ) const
- {
- auto parsed_data = converter::Full{}.Convert( settings_fields );
+ bool Settings::Write( manage::SettingsFields const& settings_fields ) const
+ {
+ auto parsed_data = converter::Full{}.Convert( settings_fields );
return mRegistratorErrors.Check( parsed_data ) && SetParsedData( parsed_data );
- }
+ }
//
//Private methods
//
manage::SettingsFileDataType Settings::GetParsedStore() const
- {
- std::ifstream settings_file{ mPathFile, std::fstream::in };
- if ( !settings_file.is_open() )
- {
- manage::Logger::LogError( "Unable to open file to read " + mPathFile );
- return {};
- }
+ {
+ static const std::string METHOD_NAME = "Read settings";
+
+ std::ifstream settings_file{mPathFile, std::fstream::in};
+ if( !settings_file.is_open())
+ {
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Unable to open file to read " + mPathFile );
+ return {};
+ }
auto result = GetDataFromFile( settings_file );
settings_file.close();
- return mRegistratorErrors.Check(result) ? result : manage::SettingsFileDataType{};
- }
+ return mRegistratorErrors.Check( result ) ? result : manage::SettingsFileDataType{};
+ }
manage::SettingsFileDataType Settings::GetDataFromFile( std::ifstream& settings_file ) const
{
std::string line{};
manage::SettingsFileDataType result;
- while ( std::getline( settings_file, line ) )
+ while( std::getline( settings_file, line ))
{
auto parsed_data = parser::Settings{}.Parse( line );
result.insert( parsed_data );
@@ -61,21 +63,23 @@ namespace smtp::file
}
bool Settings::SetParsedData( manage::SettingsFileDataType const& parsed_data ) const
- {
- std::ofstream settings_file{ mPathFile, std::fstream::out | std::fstream::trunc };
- if ( !settings_file.is_open() )
- {
- manage::Logger::LogError( "Unable to open file to write " + mPathFile );
- return false;
- }
- for( const auto& data : parsed_data )
- {
- auto line = BuildParam( data );
- settings_file << line << "\n";
- }
- settings_file.close();
- return true;
- }
+ {
+ static const std::string METHOD_NAME = "Write settings";
+
+ std::ofstream settings_file{mPathFile, std::fstream::out | std::fstream::trunc};
+ if( !settings_file.is_open())
+ {
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Unable to open file to write " + mPathFile );
+ return false;
+ }
+ for( const auto& data: parsed_data )
+ {
+ auto line = BuildParam( data );
+ settings_file << line << "\n";
+ }
+ settings_file.close();
+ return true;
+ }
std::string Settings::BuildParam( std::pair < std::string, std::string > const& data ) const
{
diff --git a/src/logger/journal.cpp b/src/logger/journal.cpp
index 001e87d..ba002b0 100644
--- a/src/logger/journal.cpp
+++ b/src/logger/journal.cpp
@@ -4,7 +4,7 @@
namespace smtp::logger
{
- void Journal::LogError( std::string const& message ) const
+ void Journal::LogError( std::string const& method, std::string const& message ) const
{
std::cout << message << std::endl;
}
diff --git a/src/logger/journal.hpp b/src/logger/journal.hpp
index 1b06b8c..0f1e6bb 100644
--- a/src/logger/journal.hpp
+++ b/src/logger/journal.hpp
@@ -11,7 +11,7 @@ namespace smtp::logger
Journal() = default;
~Journal() override = default;
- void LogError( std::string const& message ) const override;
+ void LogError( std::string const& method, std::string const& message ) const override;
};
}
diff --git a/src/logger/logger_set.cpp b/src/logger/logger_set.cpp
index 6ea6d58..d5d45a0 100644
--- a/src/logger/logger_set.cpp
+++ b/src/logger/logger_set.cpp
@@ -33,11 +33,11 @@ namespace smtp::logger
mInstance.reset( new LoggerSet( loggers ));
}
- void LoggerSet::LogError( std::string const& message )
+ void LoggerSet::LogError( std::string const& method, std::string const& message )
{
for( const auto& logger: mLogger )
{
- logger->LogError( message );
+ logger->LogError( method, message );
}
}
diff --git a/src/logger/logger_set.hpp b/src/logger/logger_set.hpp
index fb74e56..e482042 100644
--- a/src/logger/logger_set.hpp
+++ b/src/logger/logger_set.hpp
@@ -19,7 +19,7 @@ namespace smtp::logger
LoggerSet( const LoggerSet& ) = delete;
LoggerSet& operator=( LoggerSet& ) = delete;
- void LogError( std::string const& message );
+ void LogError( std::string const& method, std::string const& message );
static std::shared_ptr < LoggerSet > GetInstance();
};
}
diff --git a/src/logger/phosphor.cpp b/src/logger/phosphor.cpp
index d3c81b0..f3575c4 100644
--- a/src/logger/phosphor.cpp
+++ b/src/logger/phosphor.cpp
@@ -1,15 +1,13 @@
-#include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/elog.hpp>
-#include <xyz/openbmc_project/Common/error.hpp>
+#include <phosphor-logging/lg2.hpp>
#include "phosphor.hpp"
namespace smtp::logger
{
- void Phosphor::LogError( std::string const& message ) const
+ void Phosphor::LogError( std::string const& method, std::string const& message ) const
{
- using InternalFailure =
- sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
- phosphor::logging::commit<InternalFailure>(message);
+ auto join_message = method + ',' + message;
+ const auto* msg = "OpenBMC.0.1.SMTPError";
+ lg2::error("SMTP error", "REDFISH_MESSAGE_ID", msg, "REDFISH_MESSAGE_ARGS", join_message);
}
} \ No newline at end of file
diff --git a/src/logger/phosphor.hpp b/src/logger/phosphor.hpp
index dd536b5..f40ddf2 100644
--- a/src/logger/phosphor.hpp
+++ b/src/logger/phosphor.hpp
@@ -11,6 +11,6 @@ namespace smtp::logger
Phosphor() = default;
~Phosphor() override = default;
- void LogError( std::string const& message ) const override;
+ void LogError( std::string const& method, std::string const& message ) const override;
};
}
diff --git a/src/logger/types/ilogger.hpp b/src/logger/types/ilogger.hpp
index 9f88668..5dce1d5 100644
--- a/src/logger/types/ilogger.hpp
+++ b/src/logger/types/ilogger.hpp
@@ -9,7 +9,8 @@ namespace smtp::logger::types
public:
virtual ~ILogger() = default;
- virtual void LogError( std::string const& message ) const = 0;
+ virtual void LogError( std::string const& method, std::string const& message ) const = 0;
};
- using ILoggerPtr = std::shared_ptr<ILogger>;
+
+ using ILoggerPtr = std::shared_ptr < ILogger >;
} \ No newline at end of file
diff --git a/src/message/sender.cpp b/src/message/sender.cpp
index 5845628..85c5d5d 100644
--- a/src/message/sender.cpp
+++ b/src/message/sender.cpp
@@ -29,6 +29,8 @@ namespace smtp::message
bool Sender::Send( std::string const& mail_from, std::string const& subject, std::string const& text )
{
+ static const std::string METHOD_NAME = "Send message";
+
CURLcode result = CURLE_OK;
curl_slist* recipients = nullptr;
WriteThis upload_ctx{};
@@ -38,14 +40,14 @@ namespace smtp::message
mSettingsStorage.ReloadSettings();
if( !InitCurl( curl, upload_ctx, mail_from ))
{
- logger::LoggerSet::GetInstance()->LogError( "Error to initializate curl" );
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Error to initializate message service" );
return false;
}
//TODO раздать mail_to другим методам
auto mail_to = mMailTo.GetMailToSend();
if( mail_to.empty())
{
- logger::LoggerSet::GetInstance()->LogError( "We haven't any mail to" );
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "We haven't any mail to" );
return false;
}
FillRecipients( curl, recipients );
@@ -56,7 +58,7 @@ namespace smtp::message
if( result != CURLE_OK )
{
std::string message = "Error to send messge: " + std::string( curl_easy_strerror( result ));
- logger::LoggerSet::GetInstance()->LogError( message );
+ logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, message );
return false;
}
//TODO сделать деинициализацию через регистратор деинициализаторов