goToMain / libosdp

Implementation of IEC 60839-11-5 OSDP (Open Supervised Device Protocol); provides a C library with support for C++, Rust and Python3
https://libosdp.sidcha.dev
Apache License 2.0
138 stars 71 forks source link

External Logger port support #80

Closed bryanvalletta closed 2 years ago

bryanvalletta commented 2 years ago

We're integrating this library into our embedded application, and it would be nice to be able to use our currently implemented logging (just console) mechanism to output libosdp messages out-of-box. So offering a way to configure the library to use a logger port would be very useful.

Thanks!

sidcha commented 2 years ago

Hey, this is already supported :). Have a look at osdp_logger_init which takes osdp_log_puts_fn_t (doc). Let me know if that doesn't fit your use case for some reason.

More context: This is a very common requirement for embedded libraries to pipe logs though an existing logging infrastructure. Since not all logging infrastructures are made alike, some of the features that LibOSDP uses may not be available in tiny embedded devices. For this reason, there is a logger module in utils that accepts a file or function pointer to write out the log messages post all formatting. By default, it puts it out to stdout; on embedded systems that have stdout redirected to UART with some form of putchar redirection, things just work out of the box.

bryanvalletta commented 2 years ago

I think our specific issue is that we use a logging framework that's actually very similar to what is built into libOSDP (we actually run into build issues when compiling - multiple function definitions). So if there is a way to define a port for all the logging macros that would be useful for our use case.

sidcha commented 2 years ago

Ah, I see. Which project/environment are you building LibOSDP in? Maybe the namespace "logger_" in utils/logger.c is too generic. Can you provide the build logs so I can see what sort of conflicts these are? If it is a known popular project/environment, I can try to refactor names in LibOSDP so they don't conflict there.

Alternatively, you can come up with a solution that works for you and send a PR here so I can review your changes. :)

bryanvalletta commented 2 years ago

We're building as a part of a C application using CMake and ARM GCC.

And maybe I will do that haha, I have already forked and made some changes to get them to play nicely, so I can just clean that up a bit!

Also while I have you and unrelated, is there API docs I can reference for the python implementation (specifically for the ControlPanel)?

sidcha commented 2 years ago

Unfortunately no. I haven't gotten around to documenting it yet. The python sample cp_app.py should give you an idea of how to use the module. You can also have a look at the list of methods here and each of those methods have a doc string right above it which may provide some insights.

Just an FYI, there two kind/levels of Python support right now (honestly I don't know which is better, at different times either looks better than the other). Inside python/ there is a python-c-bindings module that exposes the raw C API over python with some (un)marshalling of C structures to python dicts. Here, you should find 1-1 python calls for the C methods. After this was developed, for ease of testing with pytest, I wrote some wrappers on top of this module that does some more device lifecycle management; this can be found at pytest/testlib/. Feel free to choose the one that best fits your need/taste.

bryanvalletta commented 2 years ago

This is all great info thank you! I will also close this issue and take a stab at implementing the logger framework port as well.