Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new axmol log system based on fmtlib #1732

Merged
merged 19 commits into from
Mar 7, 2024
Merged

Implement new axmol log system based on fmtlib #1732

merged 19 commits into from
Mar 7, 2024

Conversation

halx99
Copy link
Collaborator

@halx99 halx99 commented Mar 5, 2024

Describe your changes

Add new macros fmtlib style format args

AXLOGD("hello {} value={}", "world",  29.0);
  • The deprecated API ax::log was removed, could replaced by ax::print
  • AXLOGD: like AXLOG when _AX_DEBUG=1
  • AXLOGI: INFO
  • AXLOGW: WARN
  • AXLOGE: ERR
  • AXLOGX: always output like invoke ax::log or ax::print
  • Add API: ax::setLogLevel to control log level at runtime
  • Add API: ax::setLogFmtFlag to enable add log prefix, i.e. ax::setLogFmtFlag(ax::LogFmtFlag::Full) will add prefix as: D/[2024-03-06 02:20:52.019][PID:83f0][TID:b0e4]`
  • Add API: ax::setLogOutput to redirect log to anywhere, i.e. write to file
  • Colorful log support Logging colorful support #1735
  • output log message to stderr if level is LogLevel::Error Logging colorful support #1735
// add to AppleDelegate.cpp

#include "yasio/singleton.hpp"

USING_NS_AX;

class SampleLogOutput : public ILogOutput
{
public:
    SampleLogOutput()
    {
        auto fu   = FileUtils::getInstance();
        _filePath = fu->getWritablePath() + "game.log";
        _fs       = fu->openFileStream(_filePath, ax::IFileStream::Mode::APPEND);

        ax::setLogFmtFlag(ax::LogFmtFlag::Full);

        AXLOGW("further log message will write to {}", _filePath);

        ax::setLogOutput(this);
    }

    ~SampleLogOutput() override
    {
        ax::setLogOutput(nullptr);
        _fs->close();
    }
    void write(std::string&& message, LogLevel) override
    {
        if (_fs->size() > _threshold)
            _fs->resize(0);
        _fs->write(message.c_str(), static_cast<int>(message.size()));
    }

private:
    std::string _filePath;
    std::unique_ptr<IFileStream> _fs;
    int64_t _threshold{1024 * 1024 * 10};  // 10MB
};

bool AppDelegate::applicationDidFinishLaunching()
{
   // Initialize LogOutput
    yasio::singleton<SampleLogOutput>::instance();

   /// ...
}

Issue ticket number and link

Checklist before requesting a review

For each PR

  • Add Copyright if it missed:
    - "Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md)."

  • I have performed a self-review of my code.

    Optional:

    • I have checked readme and add important infos to this PR.
    • I have added/adapted some tests too.

For core/new feature PR

  • I have checked readme and add important infos to this PR.
  • I have added thorough tests.

@halx99 halx99 added the enhancement New feature or request label Mar 5, 2024
@halx99 halx99 added this to the 2.1.3 milestone Mar 5, 2024
@halx99 halx99 marked this pull request as draft March 5, 2024 18:21
@halx99 halx99 marked this pull request as ready for review March 5, 2024 20:18
@halx99 halx99 changed the title Implement new log macros based on fmtlib Implement new axmol log system based on fmtlib Mar 6, 2024
@halx99 halx99 merged commit 5d2c42e into dev Mar 7, 2024
15 checks passed
@halx99 halx99 deleted the new-log-stubs branch March 7, 2024 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant