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

Adds LOG_*_ONCE macros #41783

Closed

Conversation

RafaelLaya
Copy link
Contributor

@RafaelLaya RafaelLaya commented Jan 13, 2022

Sometimes it can be useful to execute a log statement only a fixed number of times. For instance, because logging it would produce a lot of spam and it's a fact that will stay constant during the whole runtime. This change adds macros to silence a single log statement after a given number of executions.

Signed-off-by: Rafael Laya rafaellaya@fb.com

@github-actions github-actions bot added the area: API Changes to public APIs label Jan 13, 2022
Copy link
Contributor

@nordic-krch nordic-krch left a comment

Choose a reason for hiding this comment

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

Please add test to tests/subsys/logging/log_api. If macro for limited number of executions is added to sys/util add also test there.

* @param ... A string optionally containing printk valid conversion specifier,
* followed by as many values as specifiers.
*/
#define LOG_ERR_ONCE(...) Z_LOG_ONCE(LOG_LEVEL_ERR, __VA_ARGS__)
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess that it can be helpful in certain debugging scenarios. What about making it more generic and converting to LOG_ERR_N(n, ...)? LOG_ERR_N(1, ...) would be LOG_ERR_ONCE equivalent.

I would do something like:

#define DO_N(n, code) \
  static int _do_n_cnt_##__LINE__; \
  if (_do_n_cnt_##__LINE__ < n) { \ 
    _do_n_cnt_##__LINE__++; \
    code; \
  }

In fact macro like that is not logging specific. Belongs better to sys/util_macro.h

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great suggestion!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi again @nordic-krch, I've made some local changes and about to make tests. However, I might be missing how to run the tests properly. For instance:

  1. I go to zephyr/tests/subsys/logging/log_api/src/test.inc
  2. As an attempt to break test_log_various_messages(), I replace the LOG_DBG() in line 125 by some garbage like LOG_DBG("garbage");
  3. Save
  4. Then from zephyr root I run ./scripts/twister -T tests/subsys/logging/log_api
  5. I see the tests passing even though I tried to break one

Did I miss something about running the tests?

$ ./scripts/twister -T tests/subsys/logging/log_api
ZEPHYR_BASE unset, using "/home/rafael/zephyrproject/zephyr"
Renaming output directory to /home/rafael/zephyrproject/zephyr/twister-out.49
INFO    - Zephyr version: v2.7.99-3242-gecb9b1d3b757
INFO    - JOBS: 8
INFO    - Using 'zephyr' toolchain.
INFO    - Selecting default platforms per test case
INFO    - Building initial testcase list...
INFO    - 18 test scenarios (666 configurations) selected, 280 configurations discarded due to filters.
INFO    - Adding tasks to the queue...
INFO    - Added initial list of jobs to queue
INFO    - Total complete:  386/ 386  100%  skipped:  388, failed:    0
INFO    - 278 of 278 test configurations passed (100.00%), 0 failed, 388 skipped with 0 warnings in 2366.77 seconds
INFO    - In total 278 test cases were executed, 388 skipped on 22 out of total 418 platforms (5.26%)
INFO    - 278 test configurations executed on platforms, 0 test configurations were only built.
INFO    - Saving reports...
INFO    - Writing xunit report /home/rafael/zephyrproject/zephyr/twister-out/twister.xml...
INFO    - Writing xunit report /home/rafael/zephyrproject/zephyr/twister-out/twister_report.xml...
INFO    - Run completed

Copy link
Contributor

Choose a reason for hiding this comment

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

you're right. It got broken when new ztest got introduced. You can try with fix: #41872.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! that fix helped

@@ -357,9 +357,21 @@ static inline char z_log_minimal_level_to_char(int level)
} \
} while (false)

#define Z_LOG_DO_ACTION_ONCE(code) \
({ \
static _bool done_once = false; \
Copy link
Contributor

Choose a reason for hiding this comment

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

why _bool?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry that must be a typo

Copy link
Member

Choose a reason for hiding this comment

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

Might consider changing the name to something like log_done_once so it will be less confusing when someone is going through the symbol table.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Indeed it was a confusing name. I moved to sys_utils (since it's not log-specific) and renamed as DO_FIRST_N so that it's more clear that it just silences a piece of code after a given n. Does that sound better?

@nashif nashif requested a review from dcpleung January 13, 2022 18:53
@github-actions github-actions bot added the area: Tests Issues related to a particular existing or missing test label Jan 19, 2022
@RafaelLaya
Copy link
Contributor Author

RafaelLaya commented Jan 19, 2022

  • Re-named Z_LOG_DO_ACTION_ONCE to DO_FIRST_N (DO_FIRST_N(1, ...)) is equivalent to Z_LOG_DO_ACTION_ONCE) and moved to sys/util_macro.h
  • Added DO_FIRST_N tests to tests/unit/util/test.inc
  • Re-named LOG_*_ONCE to LOG_*_N and added parameter n
  • Added tests to tests/subsys/logging/log_api/src/test.inc

@RafaelLaya
Copy link
Contributor Author

Fixed tests with CONFIG_LOG_FUNC_NAME_PREFIX_DBG=y and CONFIG_SAMPLE_MODULE_LOG_LEVEL_DBG=y

@RafaelLaya
Copy link
Contributor Author

Split original into two commits

Copy link
Contributor

@nordic-krch nordic-krch left a comment

Choose a reason for hiding this comment

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

Codewise looks good. Please remove [] from commit messages. Also split commits into testing part and feature part.

@@ -39,6 +39,17 @@ extern "C" {
*/
#define LOG_ERR(...) Z_LOG(LOG_LEVEL_ERR, __VA_ARGS__)

/**
* @brief Writes an ERROR level message to the log, but only the first @p n times
Copy link
Contributor

Choose a reason for hiding this comment

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

English is not my primary language but in zephyr we usual don't use third person verb.
Write an ERROR ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I copied that portion of the docstring from the other macros and it was already in third person. However this reminds me to add the "@param n" documentation to the docstring. I'll do that + the split tomorrow

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, you're right. This header was one of my first contributions to zephyr and i did that mistake.

@RafaelLaya RafaelLaya force-pushed the add_log_once_macros branch 3 times, most recently from 6b26563 to f9d6a73 Compare January 20, 2022 06:06
@RafaelLaya
Copy link
Contributor Author

RafaelLaya commented Jan 20, 2022

Split into: 5 commits

  1. add macro for silencing code after N executions
  2. add tests to macro that silences code after N executions
  3. Fix verbs in log.h for consistency with the rest of the code-base
  4. Implement macros for logs with limited execution
  5. Add tests for logging macros that have limited execution

@nordic-krch
Copy link
Contributor

Commits with test changes should start with tests: .

@RafaelLaya
Copy link
Contributor Author

  • Re-named commits that add tests so that they start with tests:
  • Fixed missing parameters in doxygen documentation
  • Fixed white-space in DO_FIRST_N()
  • Added Signed-off-by in the body of the commits

Hopefully that fixes the CI failures!

@RafaelLaya RafaelLaya force-pushed the add_log_once_macros branch 2 times, most recently from 3dab952 to a1c436e Compare January 21, 2022 05:51
@RafaelLaya
Copy link
Contributor Author

  • Fixed the doxygen which was the single last CI test that was failing. Ran the Document Generation engine locally and it ran successfully so should be fine now.

@RafaelLaya
Copy link
Contributor Author

  • Fixed the author of the commit so it matches the signed-off-by line

nordic-krch
nordic-krch previously approved these changes Jan 24, 2022
Copy link
Contributor

@nordic-krch nordic-krch left a comment

Choose a reason for hiding this comment

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

Looks good, please fix the sign-off email address.

@RafaelLaya
Copy link
Contributor Author

Should be fixed now! I learned about gitlint so I checked locally that there are no errors

@nordic-krch
Copy link
Contributor

Still not fixed. You are pushing fromRafael Laya <rafaellaya@fb.com> and sign off with Rafael Laya Alfonzo <rafaellaya@fb.com>

@RafaelLaya
Copy link
Contributor Author

RafaelLaya commented Jan 26, 2022

I see, in that case I've removed the Alfonzo part from the signed-off-by line. Hopefully it works this time!

@nordic-krch
Copy link
Contributor

CI is green but it won't be merged until v3.0.0 comes out as there is a feature freeze phase now.

dcpleung
dcpleung previously approved these changes Jan 26, 2022
@RafaelLaya
Copy link
Contributor Author

Great! thanks for the patience during my first contribution to Zephyr :)

@nordic-krch
Copy link
Contributor

Great! thanks for the patience during my first contribution to Zephyr :)

No problem, I hope there will be more :)

@RafaelLaya
Copy link
Contributor Author

Resolved merge conflict

Sometimes you might want to execute a piece of code and then silence it.
This macro allows you to do so.

Signed-off-by: Rafael Laya <rafaellaya@fb.com>
This add tests to the macro DO_FIRST_N() which is used to silence a
piece of code after it has executed N times.

Signed-off-by: Rafael Laya <rafaellaya@fb.com>
…base

Zephyr does not use 3rd person verb in docstrings. This fixes the verbs
for log.h.

Signed-off-by: Rafael Laya <rafaellaya@fb.com>
Adds macros to silence a log after N executions, using the macro
DO_FIRST_N()

Signed-off-by: Rafael Laya <rafaellaya@fb.com>
This adds tests to the logging macros that silence after N executions.

Signed-off-by: Rafael Laya <rafaellaya@fb.com>
@nordic-krch
Copy link
Contributor

@RafaelLaya please resolve conflict. We could merge it soon.

@carlescufi
Copy link
Member

@RafaelLaya please rebase

@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label May 21, 2022
@github-actions github-actions bot closed this Jun 4, 2022
@bbilas
Copy link
Collaborator

bbilas commented Nov 18, 2022

@RafaelLaya would you mind re-opening that MR and preparing it to be merged? That's a super helpful feature that I want to see in the upstream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: Logging area: Tests Issues related to a particular existing or missing test Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants