diff --git a/include/audit_handler.h b/include/audit_handler.h index 9b718a4..5e26213 100644 --- a/include/audit_handler.h +++ b/include/audit_handler.h @@ -554,7 +554,7 @@ class Audit_file_handler: public Audit_io_handler public: Audit_file_handler() : - m_sync_period(0), m_log_file(NULL), m_sync_counter(0) + m_sync_period(0), m_log_file(NULL), m_sync_counter(0), m_bufsize(0) { m_io_type = "file"; } @@ -571,6 +571,11 @@ public: */ unsigned int m_sync_period; + /** + * The buf size used by the file stream. 0 = use default, negative or 1 = no buffering + */ + long m_bufsize; + /** * Write function we pass to formatter */ diff --git a/src/audit_handler.cc b/src/audit_handler.cc index 11b447e..7e58931 100644 --- a/src/audit_handler.cc +++ b/src/audit_handler.cc @@ -21,6 +21,7 @@ #include "audit_handler.h" //for definition of sockaddr_un #include +#include #include "static_assert.h" //utility macro to log also with a date as a prefix @@ -198,7 +199,7 @@ int Audit_file_handler::open(const char * io_dest, bool log_errors) { char format_name[FN_REFLEN]; fn_format(format_name, io_dest, "", "", MY_UNPACK_FILENAME); - m_log_file = my_fopen(format_name, O_RDWR | O_APPEND, MYF(0)); + m_log_file = my_fopen(format_name, O_WRONLY | O_APPEND| O_CREAT, MYF(0)); if (!m_log_file) { if(log_errors) @@ -209,6 +210,31 @@ int Audit_file_handler::open(const char * io_dest, bool log_errors) } return -1; } + ssize_t bufsize = BUFSIZ; + int res =0; + //0 -> use default, 1 or negative -> disabled + if(m_bufsize > 1) + { + bufsize = m_bufsize; + } + if(1 == m_bufsize || m_bufsize < 0) + { + //disabled + res = setvbuf(m_log_file, NULL, _IONBF, 0); + } + else + { + res = setvbuf(m_log_file, NULL, _IOFBF, bufsize); + + } + if(res) + { + sql_print_error( + "%s unable to set bufzie [%zd (%ld)] for file %s: %s.", + AUDIT_LOG_PREFIX, bufsize, m_bufsize, m_io_dest, strerror(errno)); + } + sql_print_information("%s bufsize for file [%s]: %zd. Value of json_file_bufsize: %ld.", AUDIT_LOG_PREFIX, m_io_dest, + __fbufsize(m_log_file), m_bufsize); return 0; } diff --git a/src/audit_plugin.cc b/src/audit_plugin.cc index abb0a9a..ef07326 100644 --- a/src/audit_plugin.cc +++ b/src/audit_plugin.cc @@ -1591,6 +1591,10 @@ static MYSQL_SYSVAR_STR(json_log_file, json_file_handler.m_io_dest, PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC, "AUDIT plugin json log file name", NULL, NULL, "mysql-audit.json"); +static MYSQL_SYSVAR_LONG(json_file_bufsize, json_file_handler.m_bufsize, + PLUGIN_VAR_RQCMDARG, + "AUDIT plugin json log file buffer size. Buffer size in bytes (lager size may improve performance). 0 = use default size, 1 = no buffering. If changed during runtime need to perform a flush for the new value to take affect.", + NULL, NULL, 0, 1, 262144, 0); static MYSQL_SYSVAR_UINT(json_file_sync, json_file_handler.m_sync_period, PLUGIN_VAR_RQCMDARG, @@ -1713,6 +1717,7 @@ static struct st_mysql_sys_var* audit_system_variables[] = MYSQL_SYSVAR(header_msg), MYSQL_SYSVAR(force_record_logins), MYSQL_SYSVAR(json_log_file), + MYSQL_SYSVAR(json_file_bufsize), MYSQL_SYSVAR(json_file_sync), MYSQL_SYSVAR(json_file_retry), MYSQL_SYSVAR(json_socket_retry),