Skip to content

Commit 69a4398

Browse files
authored
Merge pull request #64 from Autonomy-Logic/fix/plugin-centralized-logging
Add centralized plugin logging for Python and native plugins
2 parents 822c2ac + dac94b9 commit 69a4398

10 files changed

Lines changed: 881 additions & 335 deletions

File tree

core/src/drivers/plugin_driver.h

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#ifndef PLUGIN_DRIVER_H
22
#define PLUGIN_DRIVER_H
33

4-
#include "../lib/iec_types.h"
54
#include "../plc_app/plcapp_manager.h"
65
#include "plugin_config.h"
6+
#include "plugin_types.h"
77
#include "python_plugin_bridge.h"
8-
#include <pthread.h>
98

109
// Maximum number of plugins
1110
#define MAX_PLUGINS 16
@@ -23,12 +22,6 @@ typedef void (*plugin_cycle_start_func_t)(void);
2322
typedef void (*plugin_cycle_end_func_t)(void);
2423
typedef void (*plugin_cleanup_func_t)(void);
2524

26-
// Logging function pointer types
27-
typedef void (*plugin_log_info_func_t)(const char *fmt, ...);
28-
typedef void (*plugin_log_debug_func_t)(const char *fmt, ...);
29-
typedef void (*plugin_log_warn_func_t)(const char *fmt, ...);
30-
typedef void (*plugin_log_error_func_t)(const char *fmt, ...);
31-
3225
typedef struct
3326
{
3427
void *handle; // Handle to the loaded shared library
@@ -40,42 +33,6 @@ typedef struct
4033
plugin_cleanup_func_t cleanup;
4134
} plugin_funct_bundle_t;
4235

43-
// Runtime buffer access structure for plugins
44-
typedef struct
45-
{
46-
// Buffer pointers
47-
IEC_BOOL *(*bool_input)[8];
48-
IEC_BOOL *(*bool_output)[8];
49-
IEC_BYTE **byte_input;
50-
IEC_BYTE **byte_output;
51-
IEC_UINT **int_input;
52-
IEC_UINT **int_output;
53-
IEC_UDINT **dint_input;
54-
IEC_UDINT **dint_output;
55-
IEC_ULINT **lint_input;
56-
IEC_ULINT **lint_output;
57-
IEC_UINT **int_memory;
58-
IEC_UDINT **dint_memory;
59-
IEC_ULINT **lint_memory;
60-
IEC_BOOL *(*bool_memory)[8];
61-
62-
// Mutex functions
63-
int (*mutex_take)(pthread_mutex_t *mutex);
64-
int (*mutex_give)(pthread_mutex_t *mutex);
65-
pthread_mutex_t *buffer_mutex;
66-
char plugin_specific_config_file_path[256];
67-
68-
// Buffer size information
69-
int buffer_size;
70-
int bits_per_buffer;
71-
72-
// Logging functions
73-
plugin_log_info_func_t log_info;
74-
plugin_log_debug_func_t log_debug;
75-
plugin_log_warn_func_t log_warn;
76-
plugin_log_error_func_t log_error;
77-
} plugin_runtime_args_t;
78-
7936
// Plugin instance structure
8037
typedef struct plugin_instance_s
8138
{

core/src/drivers/plugin_types.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @file plugin_types.h
3+
* @brief Common type definitions for OpenPLC plugins
4+
*
5+
* This header defines the essential types and structures shared between
6+
* the plugin driver system and native plugins. It provides:
7+
* - Logging function pointer types
8+
* - The plugin_runtime_args_t structure for runtime buffer access
9+
*
10+
* Both Python and native plugins receive a pointer to plugin_runtime_args_t
11+
* during initialization, giving them access to PLC I/O buffers, mutex
12+
* functions, and centralized logging.
13+
*/
14+
15+
#ifndef PLUGIN_TYPES_H
16+
#define PLUGIN_TYPES_H
17+
18+
#include "../lib/iec_types.h"
19+
#include <pthread.h>
20+
21+
/**
22+
* @brief Logging function pointer types
23+
*
24+
* These function pointers are provided to plugins for routing log messages
25+
* through the central OpenPLC logging system. Messages logged through these
26+
* functions will appear in the OpenPLC Editor's log viewer.
27+
*/
28+
typedef void (*plugin_log_info_func_t)(const char *fmt, ...);
29+
typedef void (*plugin_log_debug_func_t)(const char *fmt, ...);
30+
typedef void (*plugin_log_warn_func_t)(const char *fmt, ...);
31+
typedef void (*plugin_log_error_func_t)(const char *fmt, ...);
32+
33+
/**
34+
* @brief Runtime buffer access structure for plugins
35+
*
36+
* This structure is passed to plugins during initialization, providing
37+
* access to:
38+
* - PLC I/O buffers (bool, byte, int, dint, lint for inputs/outputs/memory)
39+
* - Mutex functions for thread-safe buffer access
40+
* - Plugin-specific configuration file path
41+
* - Buffer size information
42+
* - Centralized logging functions
43+
*
44+
* Plugins should use mutex_take/mutex_give when accessing buffers to ensure
45+
* thread safety with the PLC scan cycle.
46+
*/
47+
typedef struct
48+
{
49+
/* Buffer pointers */
50+
IEC_BOOL *(*bool_input)[8];
51+
IEC_BOOL *(*bool_output)[8];
52+
IEC_BYTE **byte_input;
53+
IEC_BYTE **byte_output;
54+
IEC_UINT **int_input;
55+
IEC_UINT **int_output;
56+
IEC_UDINT **dint_input;
57+
IEC_UDINT **dint_output;
58+
IEC_ULINT **lint_input;
59+
IEC_ULINT **lint_output;
60+
IEC_UINT **int_memory;
61+
IEC_UDINT **dint_memory;
62+
IEC_ULINT **lint_memory;
63+
IEC_BOOL *(*bool_memory)[8];
64+
65+
/* Mutex functions for thread-safe buffer access */
66+
int (*mutex_take)(pthread_mutex_t *mutex);
67+
int (*mutex_give)(pthread_mutex_t *mutex);
68+
pthread_mutex_t *buffer_mutex;
69+
70+
/* Plugin configuration */
71+
char plugin_specific_config_file_path[256];
72+
73+
/* Buffer size information */
74+
int buffer_size;
75+
int bits_per_buffer;
76+
77+
/* Logging functions - route messages through central logging system */
78+
plugin_log_info_func_t log_info;
79+
plugin_log_debug_func_t log_debug;
80+
plugin_log_warn_func_t log_warn;
81+
plugin_log_error_func_t log_error;
82+
} plugin_runtime_args_t;
83+
84+
#endif /* PLUGIN_TYPES_H */

core/src/drivers/plugins/native/examples/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# Makefile for test plugin
22

33
CC = gcc
4-
CFLAGS = -fPIC -shared -I../../../../lib -I../../../../drivers
4+
CFLAGS = -fPIC -shared -I../../../../lib -I../../../../drivers -I..
55
LDFLAGS = -lpthread
66

77
TARGET = test_plugin.so
88
SOURCE = test_plugin.c
9+
PLUGIN_LOGGER = ../plugin_logger.c
910

1011
LOADER_TARGET = test_plugin_loader
1112
LOADER_SOURCE = test_plugin_loader.c
1213

1314
all: $(TARGET) $(LOADER_TARGET)
1415

15-
$(TARGET): $(SOURCE)
16-
$(CC) $(CFLAGS) $(LDFLAGS) -o $(TARGET) $(SOURCE)
16+
$(TARGET): $(SOURCE) $(PLUGIN_LOGGER)
17+
$(CC) $(CFLAGS) $(LDFLAGS) -o $(TARGET) $(SOURCE) $(PLUGIN_LOGGER)
1718

1819
$(LOADER_TARGET): $(LOADER_SOURCE)
1920
$(CC) -o $(LOADER_TARGET) $(LOADER_SOURCE) -ldl -lpthread

0 commit comments

Comments
 (0)