28
28
#include < mysql/components/my_service.h>
29
29
#include < mysql/components/service_implementation.h>
30
30
#include < mysql/components/services/log_shared.h>
31
+ #if defined(MYSQL_DYNAMIC_PLUGIN)
32
+ #include < mysql/service_plugin_registry.h>
33
+ #endif
31
34
#include < stdarg.h>
35
+ #include < stdio.h>
32
36
33
- #include " my_compiler.h"
34
- #ifdef MYSQL_SERVER
37
+ #include < my_compiler.h>
38
+ #if defined( MYSQL_SERVER) && !defined(MYSQL_DYNAMIC_PLUGIN)
35
39
#include " sql/log.h"
36
40
#endif
37
41
38
-
39
42
/* *
40
43
Primitives for services to interact with the structured logger:
41
44
functions pertaining to log_line and log_item data
@@ -651,9 +654,9 @@ BEGIN_SERVICE_DEFINITION(log_builtins_syseventlog)
651
654
END_SERVICE_DEFINITION (log_builtins_syseventlog)
652
655
653
656
654
- # ifdef __cplusplus
657
+ #ifdef __cplusplus
655
658
656
- # if !defined(LOG_H)
659
+ #if !defined(LOG_H)
657
660
658
661
extern SERVICE_TYPE (log_builtins) *log_bi;
659
662
extern SERVICE_TYPE (log_builtins_string) *log_bs;
@@ -686,7 +689,9 @@ extern SERVICE_TYPE(log_builtins_string) *log_bs;
686
689
# define log_set_float log_item_set_float
687
690
# define log_set_lexstring log_item_set_lexstring
688
691
# define log_set_cstring log_item_set_cstring
689
- # endif
692
+ #endif // LOG_H
693
+
694
+ #ifndef DISABLE_ERROR_LOGGING
690
695
691
696
#define LogErr (severity, ecode, ...) LogEvent().prio(severity)\
692
697
.errcode(ecode)\
@@ -696,7 +701,43 @@ extern SERVICE_TYPE(log_builtins_string) *log_bs;
696
701
.function(__FUNCTION__)\
697
702
.lookup(ecode, ## __VA_ARGS__)
698
703
704
+ #define LogPluginErr (severity, ecode, ...) \
705
+ LogEvent ().prio(severity)\
706
+ .errcode(ecode)\
707
+ .subsys(LOG_SUBSYSTEM_TAG)\
708
+ .source_line(__LINE__)\
709
+ .source_file(MY_BASENAME)\
710
+ .function(__FUNCTION__)\
711
+ .lookup_quoted(ecode, " Plugin " LOG_SUBSYSTEM_TAG " reported" ,\
712
+ ## __VA_ARGS__)
713
+
714
+ #define LogPluginErrMsg (severity, ecode, ...) \
715
+ LogEvent ().prio(severity)\
716
+ .errcode(ecode)\
717
+ .subsys(LOG_SUBSYSTEM_TAG)\
718
+ .source_line(__LINE__)\
719
+ .source_file(MY_BASENAME)\
720
+ .function(__FUNCTION__)\
721
+ .message_quoted(" Plugin " LOG_SUBSYSTEM_TAG " reported" ,\
722
+ ## __VA_ARGS__)
723
+
724
+ #else
725
+
726
+ inline void dummy_log_message (longlong severity MY_ATTRIBUTE ((unused)),
727
+ longlong ecode MY_ATTRIBUTE((unused)),
728
+ ...)
729
+ {
730
+ return ;
731
+ }
732
+
733
+ #define LogErr (severity, ecode, ...) \
734
+ dummy_log_message (severity, ecode, ## __VA_ARGS__)
735
+ #define LogPluginErr (severity, ecode, ...) \
736
+ dummy_log_message (severity, ecode, ## __VA_ARGS__)
737
+ #define LogPluginErrMsg (severity, ecode, ...) \
738
+ dummy_log_message (severity, ecode, ## __VA_ARGS__)
699
739
740
+ #endif // DISABLE_ERROR_LOGGING
700
741
701
742
/* *
702
743
Modular logger: fluid API. Server-internal. Lets you use safe and
@@ -710,6 +751,7 @@ class LogEvent
710
751
private:
711
752
log_line *ll;
712
753
char *msg;
754
+ const char *msg_tag;
713
755
714
756
/* *
715
757
Set MySQL error-code if none has been set yet.
@@ -788,6 +830,7 @@ class LogEvent
788
830
}
789
831
else
790
832
msg= nullptr ;
833
+ msg_tag= nullptr ;
791
834
}
792
835
793
836
/* *
@@ -798,7 +841,7 @@ class LogEvent
798
841
799
842
@retval the LogEvent, for easy fluent-style chaining.
800
843
*/
801
- LogEvent &type (log_type val)
844
+ LogEvent &type (enum_log_type val)
802
845
{
803
846
log_set_int (log_line_item_set (this ->ll , LOG_ITEM_LOG_TYPE), val);
804
847
return *this ;
@@ -1111,6 +1154,30 @@ class LogEvent
1111
1154
LogEvent &message (const char *fmt, ...)
1112
1155
MY_ATTRIBUTE((format(printf, 2 , 3 )));
1113
1156
1157
+ /* *
1158
+ Fill in a format string by substituting the % with the given
1159
+ arguments and tag, then add the result as the event's message.
1160
+
1161
+ @param tag Tag to prefix to message.
1162
+ @param fmt message (treated as a printf-style format-string,
1163
+ so % substitution will happen)
1164
+ @param ... varargs to satisfy any % in the message
1165
+
1166
+ @retval the LogEvent, for easy fluent-style chaining.
1167
+ */
1168
+ LogEvent &message_quoted (const char *tag, const char *fmt, ...)
1169
+ MY_ATTRIBUTE((format(printf, 3 , 4 )))
1170
+ {
1171
+ msg_tag= tag;
1172
+
1173
+ va_list args;
1174
+ va_start (args, fmt);
1175
+ set_message (fmt, args);
1176
+ va_end (args);
1177
+
1178
+ return *this ;
1179
+ }
1180
+
1114
1181
/* *
1115
1182
Find an error message by its MySQL error code.
1116
1183
Substitute the % in that message with the given
@@ -1132,6 +1199,18 @@ class LogEvent
1132
1199
return *this ;
1133
1200
}
1134
1201
1202
+ LogEvent &lookup_quoted (longlong errcode, const char *tag, ...)
1203
+ {
1204
+ msg_tag= tag;
1205
+
1206
+ va_list args;
1207
+ va_start (args, tag);
1208
+ set_message_by_errcode (errcode, args);
1209
+ va_end (args);
1210
+
1211
+ return *this ;
1212
+ }
1213
+
1135
1214
/* *
1136
1215
Add a ad hoc integer value with the given key.
1137
1216
@@ -1217,6 +1296,12 @@ inline void LogEvent::set_message(const char *fmt, va_list ap)
1217
1296
{
1218
1297
if ((ll != nullptr ) && (msg != nullptr ))
1219
1298
{
1299
+ char buf[LOG_BUFF_MAX];
1300
+ if (msg_tag != nullptr )
1301
+ {
1302
+ snprintf (buf, LOG_BUFF_MAX - 1 , " %s: \' %s\' " , msg_tag, fmt);
1303
+ fmt= buf;
1304
+ }
1220
1305
size_t len= log_msg (msg, LOG_BUFF_MAX - 1 , fmt, ap);
1221
1306
log_set_lexstring (log_line_item_set (this ->ll , LOG_ITEM_LOG_MESSAGE),
1222
1307
msg, len);
@@ -1233,6 +1318,77 @@ inline LogEvent &LogEvent::message(const char *fmt, ...)
1233
1318
return *this ;
1234
1319
}
1235
1320
1236
- # endif
1321
+ // Methods initialize and de-initialize logging service for plugins.
1322
+ #if defined(MYSQL_DYNAMIC_PLUGIN)
1323
+
1324
+ /* *
1325
+ Method to de-initialize logging service in plugin.
1326
+
1327
+ param[in] reg_srv Pluin registry service.
1328
+ */
1329
+ inline void deinit_logging_service_for_plugin (SERVICE_TYPE(registry) **reg_srv)
1330
+ {
1331
+ if (log_bi)
1332
+ (*reg_srv)->release ((my_h_service)log_bi);
1333
+ if (log_bs)
1334
+ (*reg_srv)->release ((my_h_service)log_bs);
1335
+ mysql_plugin_registry_release (*reg_srv);
1336
+ log_bi= nullptr ;
1337
+ log_bs= nullptr ;
1338
+ *reg_srv= nullptr ;
1339
+ }
1340
+
1341
+ /* *
1342
+ Method to de-initialize logging service in plugin.
1343
+
1344
+ param[out] reg_srv Pluin registry service.
1345
+
1346
+ @retval false Success.
1347
+ @retval true Failed.
1348
+ */
1349
+ inline bool init_logging_service_for_plugin (SERVICE_TYPE(registry) **reg_srv)
1350
+ {
1351
+ my_h_service log_srv= nullptr ;
1352
+ my_h_service log_str_srv= nullptr ;
1353
+ *reg_srv= mysql_plugin_registry_acquire ();
1354
+ if (!(*reg_srv)->acquire (" log_builtins.mysql_server" , &log_srv) &&
1355
+ !(*reg_srv)->acquire (" log_builtins_string.mysql_server" , &log_str_srv))
1356
+ {
1357
+ log_bi= reinterpret_cast <SERVICE_TYPE (log_builtins) *>(log_srv);
1358
+ log_bs= reinterpret_cast <SERVICE_TYPE (log_builtins_string) *>(log_str_srv);
1359
+ }
1360
+ else
1361
+ {
1362
+ deinit_logging_service_for_plugin (reg_srv);
1363
+ return true ;
1364
+ }
1365
+ return false ;
1366
+ }
1367
+
1368
+ #elif defined(EXTRA_CODE_FOR_UNIT_TESTING)
1369
+
1370
+ /* *
1371
+ Method is used by unit tests.
1372
+
1373
+ param[in] reg_srv Pluin registry service.
1374
+ */
1375
+ inline bool init_logging_service_for_plugin (
1376
+ SERVICE_TYPE (registry) **reg_srv MY_ATTRIBUTE((unused)))
1377
+ {
1378
+ return false ;
1379
+ }
1380
+
1381
+ /* *
1382
+ Method is used by unit tests.
1383
+
1384
+ param[in] reg_srv Pluin registry service.
1385
+ */
1386
+ inline void deinit_logging_service_for_plugin (
1387
+ SERVICE_TYPE (registry) **reg_srv MY_ATTRIBUTE((unused)))
1388
+ { }
1389
+
1390
+ #endif // MYSQL_DYNAMIC_PLUGIN
1391
+
1392
+ #endif // __cplusplus
1237
1393
1238
1394
#endif
0 commit comments