Coder Social home page Coder Social logo

Comments (12)

andrewauclair avatar andrewauclair commented on June 13, 2024 2

That would be a breaking change to any existing calls to information that use those types. The only real option that comes to mind is to use static_cast<int>(__LINE__) instead of just __LINE__ in the logger macros.

from poco.

andrewauclair avatar andrewauclair commented on June 13, 2024 1

Are you running into this issue with /ZI in Debug? #2913 (comment)

from poco.

siren186 avatar siren186 commented on June 13, 2024

Are you running into this issue with /ZI in Debug? #2913 (comment)

Yes, you are right !
What about add some methods like

void information(const std::string& msg, const char* file, int line);
void information(const std::string& msg, const char* file, long int line); // new method to add

to fix the problem ?

from poco.

bas524 avatar bas524 commented on June 13, 2024

Hi!
What do you think about replacing method

void information(const std::string& msg, const char* file, int line);

with

template<typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
void information(const std::string& msg, const char* file, T line);

?
IMO this isn't breaking change

from poco.

aleks-f avatar aleks-f commented on June 13, 2024

@bas524 that looks like it should work

from poco.

bas524 avatar bas524 commented on June 13, 2024

@siren186, can you help me to set correct compiler options? I want to verify my changes in godbolt, but I can't get described behavour when __LINE__ is long
link with sample code

from poco.

andrewauclair avatar andrewauclair commented on June 13, 2024

IMO this isn't breaking change

Opinions aside, this is a breaking change. Using a template doesn't change the output. The same thing would happen with just void information(const std::string& msg, const char* file, long line). See this godbolt for a comparison: https://godbolt.org/z/v9hGsjd8s.

Any calls to the parameter pack version of information such as information("%s:%d", "127.0.0.1", 5000L) would be broken.

from poco.

bas524 avatar bas524 commented on June 13, 2024

@andrewauclair , I think that I found another good way to resolve the problem without templates
I fix type of __LINE__ with decltype and use this type for declaration

namespace Poco {

using LineNumber = decltype(__LINE__);

}

and then

void log(const std::string& text, Message::Priority prio, const char* file, LineNumber line);

from poco.

andrewauclair avatar andrewauclair commented on June 13, 2024

That would cause the same issue. Although it would only cause the issue for when the type of __LINE__ is long. I'm also not sure that would actually work if Poco was compiled without ZI but the application was.

The ultimate (but sadly still breaking) change would be to separate the log functions that takes file and line from the other log functions. I've wanted to add some optional C++20 logging with std::source_location for a while as well.

from poco.

siren186 avatar siren186 commented on June 13, 2024

@siren186, can you help me to set correct compiler options? I want to verify my changes in godbolt, but I can't get described behavour when __LINE__ is long link with sample code

It compiled with errors.

@andrewauclair , I think that I found another good way to resolve the problem without templates I fix type of __LINE__ with decltype and use this type for declaration

namespace Poco {

using LineNumber = decltype(__LINE__);

}

and then

void log(const std::string& text, Message::Priority prio, const char* file, LineNumber line);

It works well !!!

from poco.

matejk avatar matejk commented on June 13, 2024

@andrewauclair , I think that I found another good way to resolve the problem without templates I fix type of __LINE__ with decltype and use this type for declaration

namespace Poco {

using LineNumber = decltype(__LINE__);

}

and then

void log(const std::string& text, Message::Priority prio, const char* file, LineNumber line);

IMO this is the most elegant solution because the type for the line number is derived from the compiler/platform.

from poco.

andrewauclair avatar andrewauclair commented on June 13, 2024

Here's what I think would be a good compromise. A breaking change is required to properly account for long __LINE__ and the current logging functions that log file and line number interfere with the variadic template versions of the functions. It would be good to separate these into different functions. To make sure the breaking change is clear, we can mark the existing log function with file and line as deleted. Code then can be migrated to the new logWithLocation and the deleted version can be removed after a release or two. This would allow using the variadic template form with those arguments.

As a bonus, we can define a version of logWithVersion that uses the new std::source_location if the header is available. The actual check for this include would be a lot messier than this, but this is just an example. This can technically be done with the variadic template form as well using deduction guides to allow the proper calling of the function.

Godbolt for this code: https://godbolt.org/z/rM1T3d5qa

#include <iostream>
#include <string>
#include <format>

#if __has_include(<source_location>)
#include <source_location>
#endif

struct Logger {
    // delete the existing log function with file and line to make the breaking change obvious
    void log(const std::string& msg, const char* file, int line) = delete;

    // add a new logging function with a different name to prevent confusion with the variadic template version
    void logWithLocation(const std::string& msg, const char* file, decltype(__LINE__) line) {
        std::cout << file << ":" << line << ": " << msg << '\n';
    }

    // new log function with location using the C++20 std::source_location for information
#if __has_include(<source_location>)
    void logWithLocation(const std::string& msg, std::source_location location = std::source_location::current()) {
        std::cout << location.file_name() << '('
            << location.line() << ':'
            << location.column() << ") `"
            << location.function_name() << ": " << msg << '\n';
    }
#endif

    template<typename T, typename... Args>
    void log (const std::string& format, T t, Args... args) {
        std::cout << std::vformat(format, std::make_format_args(t, args...)) << '\n';
    }
};

int main()
{
    Logger logger;

    logger.logWithLocation("failed here", __FILE__, __LINE__);
    logger.logWithLocation("failed again");
    logger.log("{}:{}", "127.0.0.1", 5000L);
}
/app/example.cpp:38: failed here
/app/example.cpp(39:27) `int main(): failed again
127.0.0.1:5000

from poco.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.