Skip to content

Peach Exceptions - #1

Open
joren-dev wants to merge 16 commits into
masterfrom
exceptions
Open

Peach Exceptions#1
joren-dev wants to merge 16 commits into
masterfrom
exceptions

Conversation

@joren-dev

@joren-dev joren-dev commented Nov 19, 2020

Copy link
Copy Markdown
Owner

Overview

Changes:

  • Introduces exception structure

Usage

#include <fstream>
#include <iostream>
#include <peach_library/exceptions/exception.hpp>

namespace peach
{
    enum class ExceptionTypes
    {
        kOutOfLine
    };
}

int main()
{

    peach::PrefixManager::set_default_prefix("log");

    peach::Prefix<peach::ExceptionTypes::kOutOfLine>::get().set_prefix("out_of_line");

    try
    {
        throw peach::PeachException<peach::ExceptionTypes::kOutOfLine>(peach::SL, "Something on", 23, "faulted");
    }
    catch (const peach::PeachException<peach::ExceptionTypes::kOutOfLine>& ec)
    {
        std::ofstream obj("error_log.txt");
        std::ostringstream ss;

        obj << ec;
        ss << ec;

        ec.print_to_console();
        std::cout << '\n' << ss.str();
    }

    return 0;
}

Output

// File output:
[out_of_line][03:27:30] at (line:file) 23 : C:\...\main.cpp ->
		{ asd 23 as  }
// Console output: 
out_of_line( " [out_of_line][03:27:30] at (line:file) 23 : C:\...\main.cpp ->
                { asd 23 as  } " )

[out_of_line][03:27:30] at (line:file) 23 : C:\...\main.cpp ->
                { asd 23 as  }

Note that paths here above are hidden with the three dots

Any changes on files besides the ones directly connected to your feature/change (emtpy if none):

  • none

Additional Notes:

  • none

Tests:

  • Test feature thorougly
  • Compiles w/ clang 11
  • Formatted according to the .clang-format

Extra

  • Add explanation/documentation on wiki/readme page

@joren-dev
joren-dev requested a review from Nerlant November 19, 2020 01:03
@joren-dev joren-dev self-assigned this Nov 19, 2020
@joren-dev
joren-dev marked this pull request as draft November 19, 2020 01:06
@joren-dev
joren-dev marked this pull request as ready for review November 19, 2020 16:31
Comment thread pe_exceptions/core/pe_base_exception.hpp Outdated
Comment thread pe_exceptions/core/pe_base_exception.hpp Outdated
Comment thread pe_exceptions/core/pe_base_exception.hpp Outdated
Comment thread pe_exceptions/core/pe_normal_exception.cpp Outdated
Comment thread pe_exceptions/core/pe_normal_exception.hpp Outdated
Comment thread pe_exceptions/utils/pe_exception_safety.hpp Outdated
Comment thread pe_exceptions/utils/pe_exception_safety.hpp Outdated
Comment thread pe_exceptions/utils/pe_exception_safety.hpp Outdated
Comment thread pe_exceptions/core/pe_base_exception.hpp Outdated
Comment thread pe_exceptions/core/pe_normal_exception.cpp Outdated
Comment thread pe_exceptions/core/pe_normal_exception.hpp Outdated
Comment thread pe_exceptions/core/pe_normal_exception.hpp Outdated
Comment thread pe_exceptions/pe_exception.hpp Outdated
Comment thread pe_exceptions/utils/pe_exception_safety.hpp Outdated
Comment thread pe_exceptions/core/pe_base_exception.hpp Outdated
@joren-dev
joren-dev requested a review from Nerlant February 10, 2022 02:25
Comment thread CMakeSettings.json
"ctestCommandArgs": "",
"inheritEnvironments": [ "clang_cl_x64_x64" ],
"variables": []
"inheritEnvironments": [ "msvc_x64_x64" ]

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im pretty sure these files should be hidden?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to either remove this file from git or make is obvious that it is a IDE dependent file.


namespace peach
{
enum class ExceptionTypes;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the ::detail part was removed in this file, this code can now be moved into the same scope as the rest

std::forward< Tys >( args )... ) }

{
m_err_msg = this->what( );

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure this is legal?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should be fine, as we do not overwrite what() and at this point the base std::runtime_error is fully constructed.
You should be able to omit the this->:

Suggested change
m_err_msg = this->what( );
m_err_msg = what( );

Comment thread exceptions/include/peach_library/exceptions/exception.hpp
Comment on lines +58 to +63
void print_to_console( ) const noexcept
{
// TODO: possibly determine if compiled application has a console available!
std::cerr << m_prefix << R"(( " )" << m_err_msg.substr( 0, m_err_msg.size( ) - 2 )
<< R"( " ))" << '\n';
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would actually want to get rid of this, they can already get this precise message in a string, the user can just std::cout or print however the user seems fit, saves us a lot of trouble ensuring the constraints for this method

ss << " }\n\n";
const auto put_time = std::put_time( &current_time, "[%T]" );

if ( line_num == constants::kNoSourceLocation && file_name.empty( ) )

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not the most elegant way, any alternatives?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnt it enough to check for constants::kNoSourceLocation?

Also you could split the passing into the stringstream into multiple lines to avoid duplicate code in the if and else body.

@joren-dev joren-dev Feb 12, 2022

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its enough to check for constants::kNoSourceLocation but in case value of constants::kNoSourceLocation(-1) is passed but the file name isnt empty, then it should just assume it has the std::source_location data dont you recon?

The only time we dont want to use it is when we pass (constants::kNoSourceLocation, "") to format_error, any other case def isnt using that ctor - might as well be waterproof

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the duplicate code, theres no clean way to really do what you suggest - as both versions differ in quite some points somewhere in the middle of the stringstream. Do you have a suggestion on how to do it cleanly?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like this should work:

    ss << '[' << prefix << ']' << put_time;

    if ( line_num == constants::kNoSourceLocation && file_name.empty( ) )
    {
      ss << " -> { ";
    } else
    {
      ss << '[' << prefix << ']' << put_time << " at (line:file) " << line_num << " : " << file_name
         << " ->\n\t\t{ ";
    }

    ( ( ss << std::forward< Tys >( args ) << ' ' ), ... );
    ss << " }\n\n";

Comment thread .gitignore
.vs/
out/
/out
/build

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe reorder this to look nicer and remove the duplicate build folder

Comment thread CMakeSettings.json
"ctestCommandArgs": "",
"inheritEnvironments": [ "clang_cl_x64_x64" ],
"variables": []
"inheritEnvironments": [ "msvc_x64_x64" ]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to either remove this file from git or make is obvious that it is a IDE dependent file.

}
// clang-format on

inline static std::string m_default_prefix = { "LOG" };

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be protected

// Short info:
// * Provides general way to set default prefix
// Link docu:
class PrefixManager

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating instances of this does not really make any sense. So either the constructor should be protected (for inheritance to work), or we replace the class with a namespace that stores the default prefix (preferred by me).

}
// clang-format on

std::string m_prefix;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be private.

Comment thread exceptions/include/peach_library/exceptions/exception.hpp
// * The method what() isnt templated in the base class, so we cannot override what()
// * First ctor handles the case when std::source_location information is wanted, second does it without
// Link docu:
template< ExceptionTypes Val > class PeachException : virtual public detail::BaseException

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no need to use virtual inheritance here.

ss << " }\n\n";
const auto put_time = std::put_time( &current_time, "[%T]" );

if ( line_num == constants::kNoSourceLocation && file_name.empty( ) )

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnt it enough to check for constants::kNoSourceLocation?

Also you could split the passing into the stringstream into multiple lines to avoid duplicate code in the if and else body.

Comment on lines +20 to +22
const time_t t { std::time( nullptr ) };
tm current_time { };
localtime_s( &current_time, &t );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC localtime_s does not exist on Linux. So when ensuring Linux compatibility we will need to check for the platform we compile for. Therefore i would vote to create a function that returns the time in which we can do the platform dependent stuff .

ss << " }\n\n";
}

return std::string { ss.str( ) };

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the wrapping into the std::string really necessary? When the return type is std::string, it should be moved/copied anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants