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

Standardize logging utility #552

Merged
merged 2 commits into from
Feb 4, 2025
Merged

Conversation

ChinYikMing
Copy link
Collaborator

@ChinYikMing ChinYikMing commented Feb 3, 2025

Previously, messages were printed using both printf and fprintf,
causing the information to be mixed between stdout and stderr. To
address this, the log.[ch] was integrated to standardize the logging.
The log.[ch]'s API are encapsulated one more layer with prefix 'rv',
and included in src/common.h.

The logging API uses color to differentiate messages at different
levels, ensuring that logging all information to the same stdout stream
does not cause confusion. The color feature is controlled by
ENABLE_LOG_COLOR and is enabled by default.

Note that the logging stdout stream is registered during
rv_remap_stdstream() as the new stdout stream could be remapped at
there.

Since the newly introduced logging APIs generate logs during runtime,
the check recipe must filter out this log information before validation.

Additionally, refine the check recipe into a new target, check-test,
which serves as a template to enhance readability.

Summary by Bito

This PR implements a unified logging system that replaces scattered printf/fprintf calls with a standardized API supporting multiple severity levels and color-coded output. The system is integrated across the RISC-V emulator core, device emulation, and system calls, with the build system updated to handle filtered logging output and improved test recipes.

Unit tests added: False

Estimated effort to review (1-5, lower is better): 5

Copy link

bito-code-review bot commented Feb 3, 2025

Code Review Agent Run #571ed1

Actionable Suggestions - 9
  • src/riscv.c - 1
  • src/syscall.c - 1
  • src/syscall_sdl.c - 2
  • src/log.c - 1
    • Consider larger buffer for time format · Line 55-55
  • src/log.h - 1
    • Incorrect function reference in macro definition · Line 61-61
  • Makefile - 3
Review Details
  • Files reviewed - 15 · Commit Range: a841b40..d8912c4
    • Makefile
    • src/common.h
    • src/devices/uart.c
    • src/devices/virtio-blk.c
    • src/emulate.c
    • src/feature.h
    • src/log.c
    • src/log.h
    • src/main.c
    • src/riscv.c
    • src/riscv.h
    • src/syscall.c
    • src/syscall_sdl.c
    • src/system.c
    • src/t2c.c
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • Fb Infer (Static Code Analysis) - ✖︎ Failed

AI Code Review powered by Bito Logo

@ChinYikMing ChinYikMing changed the title Feat/logging api Standardize logging utility Feb 3, 2025
Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

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

Benchmarks

Benchmark suite Current: 85701c4 Previous: 9e07773 Ratio
Dhrystone 1307 Average DMIPS over 10 runs 1330 Average DMIPS over 10 runs 1.02
Coremark 969.939 Average iterations/sec over 10 runs 962.043 Average iterations/sec over 10 runs 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Copy link

bito-code-review bot commented Feb 3, 2025

Changelist by Bito

This pull request implements the following key changes.

Key Change Files Impacted
Feature Improvement - Standardized Logging System Implementation

log.h - Added new logging header with severity levels and color support

log.c - Implemented core logging functionality with color-coded output

common.h - Integrated logging utility headers

riscv.h - Added logging support and configuration

riscv.c - Replaced printf/fprintf with standardized logging calls

uart.c - Updated error messages to use new logging system

virtio-blk.c - Converted error reporting to use logging API

syscall.c - Migrated system messages to logging framework

syscall_sdl.c - Updated SDL-related messages to use logging system

system.c - Converted MMIO error messages to logging API

t2c.c - Updated error handling to use logging framework

emulate.c - Integrated logging system for emulator messages

main.c - Added logging initialization and usage

Testing - Test Infrastructure Enhancement

Makefile - Added log filtering and improved test recipe readability with check-test template

src/riscv.c Outdated Show resolved Hide resolved
src/syscall.c Outdated Show resolved Hide resolved
src/syscall_sdl.c Outdated Show resolved Hide resolved
src/syscall_sdl.c Outdated Show resolved Hide resolved
src/log.c Show resolved Hide resolved
src/log.h Outdated Show resolved Hide resolved
Makefile Show resolved Hide resolved
Makefile Show resolved Hide resolved
Makefile Show resolved Hide resolved
Copy link

bito-code-review bot commented Feb 3, 2025

Code Review Agent Run #608943

Actionable Suggestions - 7
  • src/syscall_sdl.c - 2
  • src/main.c - 1
  • src/riscv.c - 2
  • src/log.c - 2
Additional Suggestions - 1
  • Makefile - 1
    • Consider adding detailed test failure output · Line 388-388
Review Details
  • Files reviewed - 15 · Commit Range: 7c9d75e..517c23d
    • Makefile
    • src/common.h
    • src/devices/uart.c
    • src/devices/virtio-blk.c
    • src/emulate.c
    • src/feature.h
    • src/log.c
    • src/log.h
    • src/main.c
    • src/riscv.c
    • src/riscv.h
    • src/syscall.c
    • src/syscall_sdl.c
    • src/system.c
    • src/t2c.c
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • Fb Infer (Static Code Analysis) - ✖︎ Failed

AI Code Review powered by Bito Logo

src/main.c Outdated Show resolved Hide resolved
src/riscv.c Show resolved Hide resolved
src/riscv.c Outdated Show resolved Hide resolved
src/log.c Outdated Show resolved Hide resolved
src/log.c Outdated Show resolved Hide resolved
Copy link

bito-code-review bot commented Feb 3, 2025

Code Review Agent Run #d67d08

Actionable Suggestions - 6
  • src/syscall_sdl.c - 2
  • Makefile - 1
    • Consider adding SHA1SUM command error handling · Line 367-370
  • src/main.c - 2
  • src/t2c.c - 1
    • Consider consistent error handling pattern · Line 309-309
Additional Suggestions - 4
  • src/log.h - 2
    • Consider adding LOG_NONE level for disabling · Line 43-50
    • Consider const qualifier for time field · Line 34-34
  • src/syscall_sdl.c - 1
    • Consider consistent error handling approach · Line 257-265
  • src/devices/uart.c - 1
    • Consider null check for strerror result · Line 50-50
Review Details
  • Files reviewed - 15 · Commit Range: 517e17b..da1fa29
    • Makefile
    • src/common.h
    • src/devices/uart.c
    • src/devices/virtio-blk.c
    • src/emulate.c
    • src/feature.h
    • src/log.c
    • src/log.h
    • src/main.c
    • src/riscv.c
    • src/riscv.h
    • src/syscall.c
    • src/syscall_sdl.c
    • src/system.c
    • src/t2c.c
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • Fb Infer (Static Code Analysis) - ✖︎ Failed

AI Code Review powered by Bito Logo

@ChinYikMing ChinYikMing force-pushed the feat/logging-api branch 2 times, most recently from 3c36054 to 10ca95d Compare February 3, 2025 16:53
src/main.c Outdated Show resolved Hide resolved
src/t2c.c Outdated Show resolved Hide resolved
Copy link

bito-code-review bot commented Feb 3, 2025

Code Review Agent Run #343259

Actionable Suggestions - 4
  • src/syscall_sdl.c - 1
    • Consider adding error handling for unknown request · Line 1034-1034
  • Makefile - 2
  • src/log.c - 1
    • Consider explicit initialization of time field · Line 150-155
Additional Suggestions - 5
  • Makefile - 3
  • src/system.c - 1
    • Consider adding more context to error · Line 98-98
  • src/log.h - 1
    • Consider adding semicolon after enum definition · Line 43-50
Review Details
  • Files reviewed - 15 · Commit Range: c2a3185..10ca95d
    • Makefile
    • src/common.h
    • src/devices/uart.c
    • src/devices/virtio-blk.c
    • src/emulate.c
    • src/feature.h
    • src/log.c
    • src/log.h
    • src/main.c
    • src/riscv.c
    • src/riscv.h
    • src/syscall.c
    • src/syscall_sdl.c
    • src/system.c
    • src/t2c.c
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • Fb Infer (Static Code Analysis) - ✖︎ Failed

AI Code Review powered by Bito Logo

Makefile Show resolved Hide resolved
Makefile Show resolved Hide resolved
Makefile Show resolved Hide resolved
Copy link

bito-code-review bot commented Feb 3, 2025

Code Review Agent Run #e5b0db

Actionable Suggestions - 2
  • src/syscall_sdl.c - 1
  • Makefile - 1
    • Consider adding detailed test failure output · Line 375-378
Additional Suggestions - 4
  • src/system.c - 1
    • Consider adding address to MMIO error · Line 98-98
  • src/log.h - 1
    • Consider consolidating duplicate logging macros · Line 53-58
  • src/main.c - 1
    • Consider consistent log levels for lifecycle events · Line 278-278
  • src/devices/virtio-blk.c - 1
    • Consider consolidating duplicate error messages · Line 411-417
Review Details
  • Files reviewed - 15 · Commit Range: 934c21d..e759f8e
    • Makefile
    • src/common.h
    • src/devices/uart.c
    • src/devices/virtio-blk.c
    • src/emulate.c
    • src/feature.h
    • src/log.c
    • src/log.h
    • src/main.c
    • src/riscv.c
    • src/riscv.h
    • src/syscall.c
    • src/syscall_sdl.c
    • src/system.c
    • src/t2c.c
  • Files skipped - 1
    • README.md - Reason: Filter setting
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • Fb Infer (Static Code Analysis) - ✖︎ Failed

AI Code Review powered by Bito Logo

Makefile Show resolved Hide resolved
src/log.c Outdated Show resolved Hide resolved
src/log.c Outdated Show resolved Hide resolved
src/main.c Outdated
"RV32I[MAFC] Emulator which loads an ELF file to execute.\n"
"Usage: %s [options] [filename] [arguments]\n"
"Options:\n"
rv_log_info(
Copy link
Contributor

Choose a reason for hiding this comment

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

This message should always appear, so that INFO is not sufficient.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This message should always appear, so that INFO is not sufficient.

Use TRACE instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Use TRACE instead.

How about ERROR instead? While dumping the usage, the program exits with non-zero code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Use TRACE instead.

How about ERROR instead? While dumping the usage, the program exits with non-zero code.

I chose wrong level (TRACE) which is the lowest log level. I originally planned to choose highest log level (FATAL), but ERROR works as well, so I'll go with that.

Makefile Outdated Show resolved Hide resolved
Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

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

Since the logging feature is quite useful, we can promote the functions as public APIs. That is, include log.h in the header riscv.h.

Copy link

bito-code-review bot commented Feb 4, 2025

Code Review Agent Run #648fc0

Actionable Suggestions - 7
  • src/syscall_sdl.c - 1
    • Consider adding error handling for invalid request · Line 1034-1034
  • src/log.c - 2
  • Makefile - 1
  • src/main.c - 2
  • src/devices/virtio-blk.c - 1
    • Consider consolidating duplicate error messages · Line 411-417
Review Details
  • Files reviewed - 15 · Commit Range: 57c74da..fee21ca
    • Makefile
    • src/common.h
    • src/devices/uart.c
    • src/devices/virtio-blk.c
    • src/emulate.c
    • src/feature.h
    • src/log.c
    • src/log.h
    • src/main.c
    • src/riscv.c
    • src/riscv.h
    • src/syscall.c
    • src/syscall_sdl.c
    • src/system.c
    • src/t2c.c
  • Files skipped - 1
    • README.md - Reason: Filter setting
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • Fb Infer (Static Code Analysis) - ✖︎ Failed

AI Code Review powered by Bito Logo

@ChinYikMing ChinYikMing force-pushed the feat/logging-api branch 2 times, most recently from 45839c7 to 9e07773 Compare February 4, 2025 05:59
src/log.c Show resolved Hide resolved
src/log.c Show resolved Hide resolved
Makefile Show resolved Hide resolved
src/main.c Outdated Show resolved Hide resolved
src/main.c Show resolved Hide resolved
src/devices/virtio-blk.c Outdated Show resolved Hide resolved
Copy link

bito-code-review bot commented Feb 4, 2025

Code Review Agent Run #d970f8

Actionable Suggestions - 4
  • src/syscall_sdl.c - 1
    • Consider adding error handling for invalid request · Line 1034-1034
  • src/main.c - 2
  • src/riscv.c - 1
Additional Suggestions - 1
  • src/log.h - 1
    • Consider reducing macro code duplication · Line 53-58
Review Details
  • Files reviewed - 15 · Commit Range: 4667fc3..9e07773
    • Makefile
    • src/common.h
    • src/devices/uart.c
    • src/devices/virtio-blk.c
    • src/emulate.c
    • src/feature.h
    • src/log.c
    • src/log.h
    • src/main.c
    • src/riscv.c
    • src/riscv.h
    • src/syscall.c
    • src/syscall_sdl.c
    • src/system.c
    • src/t2c.c
  • Files skipped - 1
    • README.md - Reason: Filter setting
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • Fb Infer (Static Code Analysis) - ✖︎ Failed

AI Code Review powered by Bito Logo

src/main.c Outdated Show resolved Hide resolved
src/riscv.c Outdated Show resolved Hide resolved
Previously, messages were printed using both printf and fprintf,
causing the information to be mixed between stdout and stderr. To
address this, the log.[ch] was integrated to standardize the logging.
The log.[ch]'s API are encapsulated one more layer with prefix 'rv',
and included in src/common.h.

The logging API uses color to differentiate messages at different
levels, ensuring that logging all information to the same stdout stream
does not cause confusion. The color feature is controlled by
ENABLE_LOG_COLOR and is enabled by default.

Note that the logging stdout stream is registered during
rv_remap_stdstream() as the new stdout stream could be remapped at
there.

Related: sysprog21#310
Since the newly introduced logging APIs generate logs during runtime,
the check recipe must filter out this log information before validation.

Additionally, refine the check recipe into a new target, check-test,
which serves as a template to enhance readability.
Copy link

bito-code-review bot commented Feb 4, 2025

Code Review Agent Run #d90a76

Actionable Suggestions - 1
  • src/syscall_sdl.c - 1
Additional Suggestions - 4
  • src/riscv.c - 1
  • src/system.c - 1
    • Consider enhancing MMIO error message details · Line 98-98
  • src/log.h - 1
    • Consider wrapping macros in do-while · Line 53-58
  • src/feature.h - 1
    • Consider more descriptive feature flag name · Line 111-114
Review Details
  • Files reviewed - 15 · Commit Range: 2e87a75..85701c4
    • Makefile
    • src/common.h
    • src/devices/uart.c
    • src/devices/virtio-blk.c
    • src/emulate.c
    • src/feature.h
    • src/log.c
    • src/log.h
    • src/main.c
    • src/riscv.c
    • src/riscv.h
    • src/syscall.c
    • src/syscall_sdl.c
    • src/system.c
    • src/t2c.c
  • Files skipped - 1
    • README.md - Reason: Filter setting
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • Fb Infer (Static Code Analysis) - ✖︎ Failed

AI Code Review powered by Bito Logo

@jserv jserv merged commit 487149f into sysprog21:master Feb 4, 2025
7 of 9 checks passed
@jserv
Copy link
Contributor

jserv commented Feb 4, 2025

Thank @ChinYikMing for contributing!

@ChinYikMing ChinYikMing deleted the feat/logging-api branch February 4, 2025 08:57
@jserv jserv added this to the release-2025.1 milestone Feb 4, 2025
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