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

constexpr LEVELS for easier custom level usage #483

Merged
merged 1 commit into from
May 4, 2023

Conversation

SzGaa
Copy link
Contributor

@SzGaa SzGaa commented Mar 10, 2023

Usage of C++20's new constexpr functionalities for easier usage of custom LEVELS!

Modified:

  • With C++20 LEVELS's constructor can have a constexpr specifier
  • const LEVELS can be replaced to be constexpr's instead

PULL REQUEST DESCRIPTION

Easy, but not too readable solution.

#if __cplusplus >= 202002L

This functionality provides an easier usage of custom log levels as value can be used within switch statements.
No explicit constant is required to store & work with custom LEVEL's value.

		//  TRACE is between DBUG and INFO
	constexpr LEVELS TRACE { (DBUG.value + INFO.value) / 2, "TRACE"};

	void ReceiveLogMessage( g3::LogMessageMover logEntry)
	{
		switch ( logEntry.get()._level)
		{
			case my_namespace::TRACE.value:
			...
			case g3::kWarningValue:
			...
		}

Warning:
On the example above 'DBUG' is used as 'CHANGE_G3LOG_DEBUG_TO_DBUG' cmake option was set.

Testing

  • This new/modified code was covered by unit tests.

  • (insight) Was all tests written using TDD (Test Driven Development) style?

  • The CI (Windows, Linux, OSX) are working without issues.

  • Was new functionality documented?

  • The testing steps 1 - 2 below were followed

step 1

mkdir build; cd build; cmake -DADD_G3LOG_UNIT_TEST=ON ..

// linux/osx alternative, simply run: ./scripts/buildAndRunTests.sh

step 2: use one of these alternatives to run tests:

  • Cross-Platform: ctest
  • or ctest -V for verbose output
  • Linux: make test

Usage of C++20's new constexpr functionalities for easier usage of custom LEVELS!

Modified:
* With C++20 LEVELS's constructor can have a constexpr specifier
* const LEVELS can be replaced to be constexpr's instead
@KjellKod
Copy link
Owner

Very cool.

Could you add something to DOC: g3log-with-sinks that shows how the usage would improve with C++20?

I.e. the current example shows this

  FG_Color GetColor(const LEVELS level) const {
     if (level.value == WARNING.value) { return YELLOW; }
     if (level.value == DEBUG.value) { return GREEN; }
     if (g3::internal::wasFatal(level)) { return RED; }

     return WHITE;
  }

@KjellKod
Copy link
Owner

and maybe to here also? DOC: custom logging levels

@SzGaa
Copy link
Contributor Author

SzGaa commented Mar 11, 2023

Yes, I'll have a look on the docs too.

@KjellKod
Copy link
Owner

@SzGaa I am planning to do a minor release update when the next change comes in. It would be great to get this in, please look into doing the tiny doc upgrade we discussed earlier.

@KjellKod
Copy link
Owner

KjellKod commented May 4, 2023

I'll go ahead and merge this and hopefully the docs will be updated by you or me before we forget about it.

@KjellKod KjellKod merged commit cc0fb7c into KjellKod:master May 4, 2023
@KjellKod
Copy link
Owner

This will be reverted. Ref #496 (review)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants