17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#include <drizzled/plugin/logging.h>
20
#include <drizzled/server_includes.h>
21
#include <drizzled/plugin/logging_handler.h>
22
22
#include <drizzled/gettext.h>
23
23
#include <drizzled/session.h>
48
40
static ulong sysvar_logging_syslog_threshold_big_resultset= 0;
49
41
static ulong sysvar_logging_syslog_threshold_big_examined= 0;
43
static int syslog_facility= -1;
44
static int syslog_priority= -1;
51
46
/* stolen from mysys/my_getsystime
52
47
until the Session has a good utime "now" we can use
53
48
will have to use this instead */
55
51
static uint64_t get_microtime()
57
53
#if defined(HAVE_GETHRTIME)
69
class Logging_syslog: public drizzled::plugin::Logging
65
class Logging_syslog: public Logging_handler
78
: drizzled::plugin::Logging("Logging_syslog"),
79
syslog_facility(-1), syslog_priority(-1)
82
for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
84
if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
86
syslog_facility= facilitynames[ndx].c_val;
90
if (syslog_facility == -1)
92
errmsg_printf(ERRMSG_LVL_WARN,
93
_("syslog facility \"%s\" not known, using \"local0\""),
94
sysvar_logging_syslog_facility);
95
syslog_facility= LOG_LOCAL0;
98
for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
100
if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
102
syslog_priority= prioritynames[ndx].c_val;
106
if (syslog_priority == -1)
108
errmsg_printf(ERRMSG_LVL_WARN,
109
_("syslog priority \"%s\" not known, using \"info\""),
110
sysvar_logging_syslog_priority);
111
syslog_priority= LOG_INFO;
114
openlog(sysvar_logging_syslog_ident,
115
LOG_PID, syslog_facility);
69
Logging_syslog() : Logging_handler("Logging_syslog") {}
123
71
virtual bool post (Session *session)
132
80
if (session->examined_row_count < sysvar_logging_syslog_threshold_big_examined)
83
/* TODO, looks like connect_utime isnt being set in the session
84
object. We could store the time this plugin was loaded, but that
85
would just be a dumb workaround. */
135
86
/* TODO, the session object should have a "utime command completed"
136
87
inside itself, so be more accurate, and so this doesnt have to
137
88
keep calling current_utime, which can be slow */
144
95
/* to avoid trying to printf %s something that is potentially NULL */
146
const char *dbs= session->db.empty() ? "" : session->db.c_str();
97
const char *dbs= (session->db) ? session->db : "";
100
dbl= session->db_length;
148
const char *qys= (! session->getQueryString().empty()) ? session->getQueryString().c_str() : "";
102
const char *qys= (session->query) ? session->query : "";
151
qyl= session->getQueryLength();
105
qyl= session->query_length;
153
107
syslog(syslog_priority,
154
108
"thread_id=%ld query_id=%ld"
156
110
" query=\"%.*s\""
157
111
" command=\"%.*s\""
158
112
" t_connect=%lld t_start=%lld t_lock=%lld"
159
" rows_sent=%ld rows_examined=%ld"
160
" tmp_table=%ld total_warn_count=%ld\n",
113
" rows_sent=%ld rows_examined=%ld\n",
161
114
(unsigned long) session->thread_id,
162
(unsigned long) session->getQueryId(),
163
(int)session->db.length(), dbs,
115
(unsigned long) session->query_id,
165
118
(int) command_name[session->command].length,
166
119
command_name[session->command].str,
167
(unsigned long long) (t_mark - session->getConnectMicroseconds()),
168
(unsigned long long) (t_mark - session->start_utime),
169
(unsigned long long) (t_mark - session->utime_after_lock),
170
(unsigned long) session->sent_row_count,
171
(unsigned long) session->examined_row_count,
172
(unsigned long) session->tmp_table,
173
(unsigned long) session->total_warn_count);
120
(unsigned long long) (t_mark - session->connect_utime),
121
(unsigned long long) (t_mark - session->start_utime),
122
(unsigned long long) (t_mark - session->utime_after_lock),
123
(unsigned long) session->sent_row_count,
124
(unsigned long) session->examined_row_count);
127
syslog(syslog_priority,
128
"thread_id=%ld query_id=%ld"
132
" t_connect=%lld t_start=%lld t_lock=%lld"
133
" rows_sent=%ld rows_examined=%ld\n",
134
(unsigned long) session->thread_id,
135
(unsigned long) session->query_id,
136
session->db_length, session->db,
137
// dont need to quote the query, because syslog does it itself
138
session->query_length, session->query,
139
(int) command_name[session->command].length,
140
command_name[session->command].str,
141
(unsigned long long) (t_mark - session->connect_utime),
142
(unsigned long long) (t_mark - session->start_utime),
143
(unsigned long long) (t_mark - session->utime_after_lock),
144
(unsigned long) session->sent_row_count,
145
(unsigned long) session->examined_row_count);
179
153
static Logging_syslog *handler= NULL;
181
static int logging_syslog_plugin_init(drizzled::plugin::Context &context)
155
static int logging_syslog_plugin_init(PluginRegistry ®istry)
158
for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
160
if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
162
syslog_facility= facilitynames[ndx].c_val;
166
if (syslog_facility == -1)
168
errmsg_printf(ERRMSG_LVL_WARN,
169
_("syslog facility \"%s\" not known, using \"local0\""),
170
sysvar_logging_syslog_facility);
171
syslog_facility= LOG_LOCAL0;
175
for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
177
if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
179
syslog_priority= prioritynames[ndx].c_val;
183
if (syslog_priority == -1)
185
errmsg_printf(ERRMSG_LVL_WARN,
186
_("syslog priority \"%s\" not known, using \"info\""),
187
sysvar_logging_syslog_priority);
188
syslog_priority= LOG_INFO;
191
openlog(sysvar_logging_syslog_ident,
192
LOG_PID, syslog_facility);
183
194
handler= new Logging_syslog();
184
context.add(handler);
195
registry.add(handler);
200
static int logging_syslog_plugin_deinit(PluginRegistry ®istry)
202
registry.remove(handler);
272
DRIZZLE_DECLARE_PLUGIN
291
drizzle_declare_plugin(logging_syslog)
275
293
"logging_syslog",
277
295
"Mark Atwood <mark@fallenpegasus.com>",
278
296
N_("Log to syslog"),
279
297
PLUGIN_LICENSE_GPL,
280
298
logging_syslog_plugin_init,
299
logging_syslog_plugin_deinit,
300
NULL, /* status variables */
281
301
logging_syslog_system_variables,
284
DRIZZLE_DECLARE_PLUGIN_END;
304
drizzle_declare_plugin_end;