-
Notifications
You must be signed in to change notification settings - Fork 466
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
Add C++ plugin support and example class random-choice-generator()
#4484
Conversation
Build FAILURE |
c8d8689
to
7683897
Compare
typedef struct RandomChoiceGeneratorSourceDriver_ RandomChoiceGeneratorSourceDriver; | ||
|
||
class RandomChoiceGeneratorCpp | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could derive this from the source driver directly.
Maybe we would have an issue with the destructor side (as we need to be able to free this from the c side as the ref count gets to zero).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I came to the conclusion that there would be problems on both ctor and dtor side if we derived directly, but it was a busy day, so I will think this through again tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
g++ and gcc pack structs differently.
I haven't checked the standard yet, but inheriting a C struct from C++ is not straight-forward as far as I remember.
Virtual functions will be a problem too (especially the destructor).
macOS tries to include the
The only solution I have seen is to rename the |
e694d72
to
9c9ccb0
Compare
9c9ccb0
to
bee82a8
Compare
Build FAILURE |
bee82a8
to
49e5669
Compare
LGTM so far, exciting 👍 |
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
49e5669
to
9d40a55
Compare
|
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
9d40a55
to
48930ad
Compare
random-choice-generator()
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
48930ad
to
2f4ff11
Compare
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
2f4ff11
to
86f3793
Compare
Added cmake support. New commits:
Changes were amended to random-choice-generator: add C++ example class:
|
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
86f3793
to
2e5cf42
Compare
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
2e5cf42
to
c12d0b7
Compare
g++ drops errors for these. Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
It conflicts with the C++ syntax. Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
It conflicts with the C++ syntax. Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
It conflicts with the C++ syntax. Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
C++ compat helper headers: "compat/cpp-start.h" and "compat/cpp-end.h" Starting your C++ header with #include "compat/cpp-start.h" and ending it with #include "compat/cpp-end.h" helps you with two things: - You will be able to include C headers. - C files will be able to include your C++ header. Limitations: - Obviously you can only write code in your C++ header which is syntactically correct in a C code, so you cannot define classes, etc. Do these in either your .cpp file or in a separate .hpp file, which does not get included in your C code. - As we are not preparing each C header to be includable in C++ code, but we are trying to include them with these helpers on the call site, we cannot be sure that any additional C header include will work. This adds a level of uncertainty to developing a plugin in C++. If you find a C header that cannot be imported to C++ with these helpers, please try to fix those headers first. If it is not possible, extend this file with a workaround so others do not run into that issue again. Best practices: - Minimize the C++ code in your plugin, if you can write and build/link something as C code, do so, for example boilerplates: parser.{c,h} and plugin.c. This minimizes the chance of including an incompatible C header. - Build/link the C++ code as C++ separately and link to that from your C lib. Adding -lstdc++ to LIBADD is neccessary in this case as it is automatically added while linking the C++ object itself, but not when linking to the C++ object from a C lib. - In your C++ code it is not possible to derive from a C class in the usual C way by adding a super field and filling its free_fn, because we do our own reference counting and freeing logic and we cannot rely on the casting trick of struct packing. You can create a struct in the usual C inheritance way and have a pointer to your C++ class in it and you can store a pointer to this struct in your C++ class as super, so you can access its fields. You can set the necessary virtual functions with wrapper functions of your real C++ functions in the ctor of the C style "class" == struct. You can see an example usage of the C++ plugin support at: modules/examples/sources/random-choice-generator Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
c12d0b7
to
3e1f032
Compare
Makefile changes: - Since version 4.3.0, there is required to use pcre2 instead of pcre Reference: syslog-ng/syslog-ng#4537 - Disable c++ support by default to avoid picking libstdcpp dependency Reference: syslog-ng/syslog-ng#4484 Config changes: - Bump version in config file Signed-off-by: Josef Schlehofer <pepe.schlehofer@gmail.com>
Makefile changes: - Since version 4.3.0, there is required to use pcre2 instead of pcre Reference: syslog-ng/syslog-ng#4537 - Disable c++ support by default to avoid picking libstdcpp dependency Reference: syslog-ng/syslog-ng#4484 Config changes: - Bump version in config file Signed-off-by: Josef Schlehofer <pepe.schlehofer@gmail.com>
Makefile changes: - Since version 4.3.0, there is required to use pcre2 instead of pcre Reference: syslog-ng/syslog-ng#4537 - Disable c++ support by default to avoid picking libstdcpp dependency Reference: syslog-ng/syslog-ng#4484 Config changes: - Bump version in config file Signed-off-by: Josef Schlehofer <pepe.schlehofer@gmail.com> (cherry picked from commit c43599b)
Makefile changes: - Since version 4.3.0, there is required to use pcre2 instead of pcre Reference: syslog-ng/syslog-ng#4537 - Disable c++ support by default to avoid picking libstdcpp dependency Reference: syslog-ng/syslog-ng#4484 Config changes: - Bump version in config file Signed-off-by: Josef Schlehofer <pepe.schlehofer@gmail.com> (cherry picked from commit c43599b)
Makefile changes: - Since version 4.3.0, there is required to use pcre2 instead of pcre Reference: syslog-ng/syslog-ng#4537 - Disable c++ support by default to avoid picking libstdcpp dependency Reference: syslog-ng/syslog-ng#4484 Config changes: - Bump version in config file Signed-off-by: Josef Schlehofer <pepe.schlehofer@gmail.com> (cherry picked from commit c43599b)
Makefile changes: - Since version 4.3.0, there is required to use pcre2 instead of pcre Reference: syslog-ng/syslog-ng#4537 - Disable c++ support by default to avoid picking libstdcpp dependency Reference: syslog-ng/syslog-ng#4484 Config changes: - Bump version in config file Signed-off-by: Josef Schlehofer <pepe.schlehofer@gmail.com>
This PR adds support for creating C++ plugins
Internal API breaking changes
lib/templates
: Public fieldLogTemplate::template
has been renamed toLogTemplate::template_str
.lib/logmsg
: Public fieldLogMessage::protected
has been renamed toLogMessage::write_protected
.C++ helper headers:
compat/cpp-start.h
andcompat/cpp-end.h
Starting your C++ header with
#include "compat/cpp-start.h"
and ending it with#include "compat/cpp-end.h"
helps you with two things:Limitations
.cpp
file or in a separate.hpp
file, which does not get included in your C code.If you find a C header that cannot be imported to C++ with these helpers, please try to fix those headers first. If it is not possible, extend this file with a workaround so others do not run into that issue again.
C++ plugin best practices
parser.{c,h}
andplugin.c
. This minimizes the chance of including an incompatible C header.-lstdc++
toLIBADD
is neccessary in this case as it is automatically added while linking the C++ object itself, but not when linking to the C++ object from a C lib.super
field and filling itsfree_fn
, because we do our own reference counting and freeing logic and we cannot rely on the casting trick of struct packing. You can create a struct in the usual C inheritance way and have a pointer to your C++ class in it and you can store a pointer to this struct in your C++ class assuper
, so you can access its fields. You can set the necessary virtual functions with wrapper functions of your real C++ functions in the ctor of the C style "class" == struct.You can see an example usage of the C++ plugin support at:
modules/examples/sources/random-choice-generator
.Build
By default C++ plugins are built if a C++ compiler is available. It is possible to disable it manually:
../configure --disable-cpp
cmake -DENABLE_CPP=Off ..
On MacOS, the C++ support is disabled. See: #4484 (comment)
Example plugin:
random-choice-generator()
It is added to demonstrate the C++ plugin support.
It chooses a random value from the values set in
choices()
and generates a message from it.Minimal config:
Signed-off-by: Attila Szakacs attila.szakacs@axoflow.com