Skip to content

Commit

Permalink
add extended debug log docs
Browse files Browse the repository at this point in the history
  • Loading branch information
martinwag committed Mar 14, 2023
1 parent 72e3724 commit acacb7d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ The *modbus_strerror()* function is provided to translate libmodbus-specific
error codes into error message strings; for details refer to
[modbus_strerror](modbus_strerror.md).

## Extended debug

By default, the debug output created by enabling [modbus_set_debug](modbus_set_debug.md)
is written to stdout/stderr. With the following functions those can be redirected
to files or a callback.

- [modbus_set_out_user_data](modbus_set_out_user_data.md)
- [modbus_set_error_user_data](modbus_set_error_user_data.md)
- [modbus_set_trace_handler](modbus_set_trace_handler.md)

## Miscellaneous

To deviate from the Modbus standard, you can enable or disable quirks with:
Expand Down
32 changes: 32 additions & 0 deletions docs/modbus_set_error_user_data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# modbus_set_error_user_data

## Name

modbus_set_error_user_data - set file stream to write log output

## Synopsis

```c
void modbus_set_error_user_data(modbus_t *ctx, void* out_user_data);
```
## Description
The *modbus_set_error_user_data()* changes where log output is written
to when enabled with [modbus_set_debug](modbus_set_debug.md). Defaults
to stderr when not set.
## Example
```c
FILE *fp;
fp = fopen("error.txt", "w");
modbus_set_error_user_data(ctx, fp);
modbus_set_debug(ctx, 1)
```

## See also

- [modbus_set_out_user_data](modbus_set_out_user_data.md)
- [modbus_set_trace_handler](modbus_set_trace_handler.md)
32 changes: 32 additions & 0 deletions docs/modbus_set_out_user_data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# modbus_set_out_user_data

## Name

modbus_set_out_user_data - set file stream to write log output

## Synopsis

```c
void modbus_set_out_user_data(modbus_t *ctx, void* out_user_data);
```
## Description
The *modbus_set_out_user_data()* changes where log output is written
to when enabled with [modbus_set_debug](modbus_set_debug.md). Defaults
to stdout when not set.
## Example
```c
FILE *fp;
fp = fopen("output.txt", "w");
modbus_set_out_user_data(ctx, fp);
modbus_set_debug(ctx, 1)
```

## See also

- [modbus_set_error_user_data](modbus_set_error_user_data.md)
- [modbus_set_trace_handler](modbus_set_trace_handler.md)
44 changes: 44 additions & 0 deletions docs/modbus_set_trace_handler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# modbus_set_trace_handler

## Name

modbus_set_trace_handler - call method when log data is written

## Synopsis

```c
typedef int (*modbus_stream_handler_t)(void *user, const char *format, va_list ap);
void modbus_set_trace_handler(modbus_t *ctx, modbus_stream_handler_t handler);
```
## Description
The *modbus_set_trace_handler()* sets a callback. When log data is written, the
callback is called. A log message is finalized with a '\n' as last character.
Defaults to vfprintf when not set.
## Example
```c++
class Test {
public:
static int log_callback(void *user, const char *format, va_list ap)
{
auto *inst = reinterpret_cast<Test*>(user);
//call methods
}
void setup()
{
modbus_set_out_user_data(reinterpret_cast<void*>(this));
modbus_set_error_user_data(reinterpret_cast<void*>(this));
modbus_set_trace_handler(log_callback);
modbus_set_debug(true);
}
}
```

## See also

- [modbus_set_out_user_data](modbus_set_out_user_data.md)
- [modbus_set_error_user_data](modbus_set_error_user_data.md)

0 comments on commit acacb7d

Please sign in to comment.