Skip to content

Commit fd04840

Browse files
committed
enable set special device (stdout and stderr) to eventlog
1 parent 71e4fc6 commit fd04840

File tree

2 files changed

+126
-12
lines changed

2 files changed

+126
-12
lines changed

lib/MySQL_Logger.cpp

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,14 +1261,25 @@ void MySQL_Logger::audit_flush_log_unlocked() {
12611261
}
12621262

12631263
void MySQL_Logger::events_open_log_unlocked() {
1264-
events.log_file_id=events_find_next_id();
1265-
if (events.log_file_id!=0) {
1266-
events.log_file_id=events_find_next_id()+1;
1267-
} else {
1268-
events.log_file_id++;
1264+
// Check if this is a special device file like /dev/stdout or /dev/stderr
1265+
bool is_special_device = (strcmp(events.base_filename, "/dev/stdout") == 0 ||
1266+
strcmp(events.base_filename, "/dev/stderr") == 0);
1267+
1268+
if (!is_special_device) {
1269+
events.log_file_id=events_find_next_id();
1270+
if (events.log_file_id!=0) {
1271+
events.log_file_id=events_find_next_id()+1;
1272+
} else {
1273+
events.log_file_id++;
1274+
}
12691275
}
1276+
12701277
char *filen=NULL;
1271-
if (events.base_filename[0]=='/') { // absolute path
1278+
if (is_special_device) {
1279+
// For special device files, use the filename directly without rotation
1280+
filen=(char *)malloc(strlen(events.base_filename)+1);
1281+
strcpy(filen, events.base_filename);
1282+
} else if (events.base_filename[0]=='/') { // absolute path
12721283
filen=(char *)malloc(strlen(events.base_filename)+11);
12731284
sprintf(filen,"%s.%08d",events.base_filename,events.log_file_id);
12741285
} else { // relative path
@@ -1310,14 +1321,25 @@ void MySQL_Logger::events_open_log_unlocked() {
13101321
};
13111322

13121323
void MySQL_Logger::audit_open_log_unlocked() {
1313-
audit.log_file_id=audit_find_next_id();
1314-
if (audit.log_file_id!=0) {
1315-
audit.log_file_id=audit_find_next_id()+1;
1316-
} else {
1317-
audit.log_file_id++;
1324+
// Check if this is a special device file like /dev/stdout or /dev/stderr
1325+
bool is_special_device = (strcmp(audit.base_filename, "/dev/stdout") == 0 ||
1326+
strcmp(audit.base_filename, "/dev/stderr") == 0);
1327+
1328+
if (!is_special_device) {
1329+
audit.log_file_id=audit_find_next_id();
1330+
if (audit.log_file_id!=0) {
1331+
audit.log_file_id=audit_find_next_id()+1;
1332+
} else {
1333+
audit.log_file_id++;
1334+
}
13181335
}
1336+
13191337
char *filen=NULL;
1320-
if (audit.base_filename[0]=='/') { // absolute path
1338+
if (is_special_device) {
1339+
// For special device files, use the filename directly without rotation
1340+
filen=(char *)malloc(strlen(audit.base_filename)+1);
1341+
strcpy(filen, audit.base_filename);
1342+
} else if (audit.base_filename[0]=='/') { // absolute path
13211343
filen=(char *)malloc(strlen(audit.base_filename)+11);
13221344
sprintf(filen,"%s.%08d",audit.base_filename,audit.log_file_id);
13231345
} else { // relative path
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @file test_logger_special_devices-t.cpp
3+
* @brief Tests for MySQL_Logger special device file handling (/dev/stdout, /dev/stderr)
4+
* @details This test ensures that eventslog_filename and auditlog_filename can be set to
5+
* special device files like /dev/stdout and /dev/stderr without rotation errors.
6+
*/
7+
8+
#include <cstdlib>
9+
#include <cstdio>
10+
#include <cstring>
11+
#include <unistd.h>
12+
#include <string>
13+
#include <fstream>
14+
15+
#include "mysql.h"
16+
#include "mysqld_error.h"
17+
#include "proxysql.h"
18+
#include "cpp-dotenv/dotenv.h"
19+
20+
#include "tap.h"
21+
#include "command_line.h"
22+
#include "utils.h"
23+
24+
int main(int argc, char** argv) {
25+
CommandLine cl;
26+
27+
if (cl.getEnv()) {
28+
diag("Failed to get the required environmental variables.");
29+
return -1;
30+
}
31+
32+
MYSQL* proxysql_admin = mysql_init(NULL);
33+
34+
if (!mysql_real_connect(proxysql_admin, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) {
35+
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin));
36+
return -1;
37+
}
38+
39+
plan(6);
40+
41+
// Test 1: Set eventslog_filename to /dev/stdout (should not fail)
42+
{
43+
MYSQL_QUERY(proxysql_admin, "SET mysql-eventslog_filename='/dev/stdout'");
44+
MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME");
45+
ok(true, "Successfully set eventslog_filename to /dev/stdout");
46+
}
47+
48+
// Test 2: Set eventslog_filename to /dev/stderr (should not fail)
49+
{
50+
MYSQL_QUERY(proxysql_admin, "SET mysql-eventslog_filename='/dev/stderr'");
51+
MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME");
52+
ok(true, "Successfully set eventslog_filename to /dev/stderr");
53+
}
54+
55+
// Test 3: Set auditlog_filename to /dev/stdout (should not fail)
56+
{
57+
MYSQL_QUERY(proxysql_admin, "SET mysql-auditlog_filename='/dev/stdout'");
58+
MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME");
59+
ok(true, "Successfully set auditlog_filename to /dev/stdout");
60+
}
61+
62+
// Test 4: Set auditlog_filename to /dev/stderr (should not fail)
63+
{
64+
MYSQL_QUERY(proxysql_admin, "SET mysql-auditlog_filename='/dev/stderr'");
65+
MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME");
66+
ok(true, "Successfully set auditlog_filename to /dev/stderr");
67+
}
68+
69+
// Test 5: FLUSH LOGS with special device should work
70+
{
71+
MYSQL_QUERY(proxysql_admin, "SET mysql-eventslog_filename='/dev/stdout'");
72+
MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME");
73+
MYSQL_QUERY(proxysql_admin, "PROXYSQL FLUSH LOGS");
74+
ok(true, "FLUSH LOGS works with /dev/stdout");
75+
}
76+
77+
// Test 6: Verify we can switch back to regular file
78+
{
79+
MYSQL_QUERY(proxysql_admin, "SET mysql-eventslog_filename='/tmp/test_events.log'");
80+
MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME");
81+
ok(true, "Successfully switched back to regular file");
82+
}
83+
84+
// Cleanup
85+
MYSQL_QUERY(proxysql_admin, "SET mysql-eventslog_filename=''");
86+
MYSQL_QUERY(proxysql_admin, "SET mysql-auditlog_filename=''");
87+
MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME");
88+
89+
mysql_close(proxysql_admin);
90+
91+
return exit_status();
92+
}

0 commit comments

Comments
 (0)