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

Improve logging #46

Merged
merged 1 commit into from
Nov 8, 2024
Merged

Improve logging #46

merged 1 commit into from
Nov 8, 2024

Conversation

nirs
Copy link
Member

@nirs nirs commented Sep 16, 2024

This does not fix all the issues mentioned in #43 but prepare the infrastructure. With these changes adding timestamps for all logs is a trivial change.

  • Log everything to stderr - this has many advantages: much easier to
    understand what happened before errors, much easier to understand the
    daemon state, since stderr is line buffered, so events (e.g. start,
    stop, connect, disconnect) are logged immediately. Easier to get logs
    from users.
  • Replace fprintf and printf with logging macros (ERRORF, ERROR, INFO)
  • Replace perror with ERRORN, showing the same output an error log.
  • Use prefix for all logs ("DEBUG|", "INFO |", "ERROR|") to make the
    meaning of the message clear, and make all log message
    aligned nicely.
  • Remove unneeded log for vmnet_return_t when we get VMNET_SUCCESS
  • Replace print_vmnet_status() with vmnet_strerror() returning readable
    error name. Callers use ERRORF() to log the vmnet function name,
    return value and error name.
  • Log socket length when it is too long
  • Log the signal name and number
  • Don't use perror() for logging errors from function snot setting errno
  • Don't log same error twice (once in a function, second in the caller)

Example log with this change

# tail -f /var/log/socket_vmnet/stderr 
INFO | Initializing vmnet.framework (mode 1001)
INFO | * vmnet_subnet_mask: 255.255.255.0
INFO | * vmnet_mtu: 1500
INFO | * vmnet_end_address: 192.168.105.254
INFO | * vmnet_start_address: 192.168.105.1
INFO | * vmnet_interface_id: 3924D245-6997-472D-A263-97E2A27FF173
INFO | * vmnet_max_packet_size: 1514
INFO | * vmnet_nat66_prefix: fd9b:5a14:ba57:e3d3::
INFO | * vmnet_mac_address: 6a:d6:97:42:f0:20
INFO | Accepted a connection (fd 5)
INFO | Accepted a connection (fd 6)
INFO | Accepted a connection (fd 7)
INFO | Connection closed by peer (fd 7)
INFO | Closing a connection (fd 7)
INFO | Connection closed by peer (fd 6)
INFO | Closing a connection (fd 6)
INFO | Connection closed by peer (fd 5)
INFO | Closing a connection (fd 5)

@nirs nirs marked this pull request as draft September 16, 2024 17:03
#define INFOF(fmt, ...) fprintf(stderr, "INFO | " fmt "\n", __VA_ARGS__)
#define ERROR(msg) fprintf(stderr, "ERROR| " msg "\n")
#define ERRORF(fmt, ...) fprintf(stderr, "ERROR| " fmt "\n", __VA_ARGS__)
#define ERRORN(name) ERRORF(name ": %s", strerror(errno))
Copy link
Member Author

Choose a reason for hiding this comment

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

This should move to log.h so we can split the to code to multiple files and use consistent logging.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

See man 3 os_log for the usage

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I'll try this.

But I think we should keep the logging macros so it is easy to change the logging backend later.

Copy link
Member

Choose a reason for hiding this comment

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

Do you plan to update this?

Copy link
Member Author

Choose a reason for hiding this comment

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

What I have here was good enough for testing and fixing various issue, and I did not have time to check this direction so far.

I can update with with the version I run locally on top of current master, and we can add an issue to consider os_log.

We anyway need to add timestamps, using os_log will avoid this work.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added an issue to consider using os_log: #64

main.c Show resolved Hide resolved
@AkihiroSuda AkihiroSuda added this to the v1.1.5 milestone Sep 17, 2024
main.c Outdated Show resolved Hide resolved
@nirs nirs force-pushed the logging branch 2 times, most recently from d8500a8 to f28945a Compare September 17, 2024 20:21
main.c Show resolved Hide resolved
main.c Show resolved Hide resolved
main.c Show resolved Hide resolved
main.c Show resolved Hide resolved
main.c Outdated Show resolved Hide resolved
main.c Outdated Show resolved Hide resolved
main.c Outdated Show resolved Hide resolved
- Log everything to stderr - this has many advantages: much easier to
  understand what happened before errors, much easier to understand the
  daemon state, since stderr is line buffered, so events (e.g. start,
  stop, connect, disconnect) are logged immediately. Easier to get logs
  from users.
- Replace fprintf and printf with logging macros (ERRORF, ERROR, INFO)
- Replace perror with ERRORN, showing the same output an error log.
- Use prefix for all logs ("DEBUG|", "INFO |", "ERROR|") to make the
  meaning of the message clear, and make all log message
  aligned nicely.
- Remove unneeded log for vmnet_return_t when we get VMNET_SUCCESS
- Replace print_vmnet_status() with vmnet_strerror() returning readable
  error name. Callers use ERRORF() to log the vmnet function name,
  return value and error name.
- Log socket length when it is too long
- Log the signal name and number
- Don't use perror() for logging errors from function snot setting errno
- Don't log same error twice (once in a function, second in the caller)

Signed-off-by: Nir Soffer <[email protected]>
@nirs nirs marked this pull request as ready for review November 7, 2024 22:59
@nirs nirs changed the title WIP: Improve logging Improve logging Nov 7, 2024
@nirs nirs requested a review from AkihiroSuda November 7, 2024 23:10
@nirs nirs mentioned this pull request Nov 8, 2024
Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Thanks

@AkihiroSuda AkihiroSuda merged commit b981095 into lima-vm:master Nov 8, 2024
4 checks passed
@nirs nirs deleted the logging branch November 8, 2024 14:25
@nirs
Copy link
Member Author

nirs commented Nov 8, 2024

Opened #70 to update docs/launchd configuration.

@nirs nirs mentioned this pull request Dec 1, 2024
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