drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
plugin/syslog/logging.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2009 Sun Microsystems, Inc.
5 * Copyright (C) 2010 Mark Atwood
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <config.h>
22
23#include <cstdarg>
24#include <limits.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28
29#include <boost/date_time.hpp>
30
31#include <drizzled/gettext.h>
32#include <drizzled/session.h>
33#include <drizzled/session/times.h>
34#include <drizzled/sql_parse.h>
35#include <drizzled/plugin.h>
36#include <drizzled/error.h>
37
38#include "logging.h"
39#include "wrap.h"
40
41namespace drizzle_plugin {
42namespace syslog {
43using namespace drizzled;
44
45extern bool sysvar_logging_enable;
46
47logging::Syslog::Syslog(const std::string &facility,
48 uint64_constraint threshold_slow,
49 uint64_constraint threshold_big_resultset,
50 uint64_constraint threshold_big_examined) :
51 drizzled::plugin::Logging("syslog_query_log"),
52 _facility(WrapSyslog::getFacilityByName(facility.c_str())),
53 sysvar_facility(facility),
54 _threshold_slow(threshold_slow),
55 _threshold_big_resultset(threshold_big_resultset),
56 _threshold_big_examined(threshold_big_examined)
57{
58 if (_facility < 0)
59 {
60 drizzled::errmsg_printf(drizzled::error::WARN,
61 _("syslog facility \"%s\" not known, using \"local0\""),
62 facility.c_str());
63 _facility= WrapSyslog::getFacilityByName("local0");
64 sysvar_facility= "local0";
65 }
66}
67
68
69bool logging::Syslog::post(drizzled::Session *session)
70{
71 assert(session != NULL);
72
73 // return if query was not too small
74 if (session->sent_row_count < _threshold_big_resultset)
75 {
76 return false;
77 }
78
79 if (session->examined_row_count < _threshold_big_examined)
80 {
81 return false;
82 }
83
84 // return if query logging is not enabled
85 if (sysvar_logging_enable == false)
86 return false;
87
88
89 /*
90 TODO, the session object should have a "utime command completed"
91 inside itself, so be more accurate, and so this doesnt have to
92 keep calling current_utime, which can be slow.
93 */
94 uint64_t t_mark= session->times.getCurrentTimestamp(false);
95
96 // return if query was not too slow
97 if (session->times.getElapsedTime() < _threshold_slow)
98 {
99 return false;
100 }
101
102 drizzled::Session::QueryString query_string(session->getQueryString());
103 drizzled::util::string::ptr schema(session->schema());
104
105 WrapSyslog::singleton()
106 .log(_facility, drizzled::error::INFO,
107 "thread_id=%ld query_id=%ld"
108 " db=\"%.*s\""
109 " query=\"%.*s\""
110 " command=\"%.*s\""
111 " t_connect=%lld t_start=%lld t_lock=%lld"
112 " rows_sent=%ld rows_examined=%ld"
113 " tmp_table=%ld total_warn_count=%ld\n",
114 (unsigned long) session->thread_id,
115 (unsigned long) session->getQueryId(),
116 (int) schema->size(),
117 schema->empty() ? "" : schema->c_str(),
118 (int) query_string->length(),
119 query_string->empty() ? "" : query_string->c_str(),
120 (int) drizzled::getCommandName(session->command).size(),
121 drizzled::getCommandName(session->command).c_str(),
122 (unsigned long long) (t_mark - session->times.getConnectMicroseconds()),
123 (unsigned long long) (session->times.getElapsedTime()),
124 (unsigned long long) (t_mark - session->times.utime_after_lock),
125 (unsigned long) session->sent_row_count,
126 (unsigned long) session->examined_row_count,
127 (unsigned long) session->tmp_table,
128 (unsigned long) session->total_warn_count);
129
130 return false;
131}
132
133bool logging::Syslog::setFacility(std::string new_facility)
134{
135 int tmp_facility= WrapSyslog::getFacilityByName(new_facility.c_str());
136 if(tmp_facility>0)
137 {
138 _facility= tmp_facility;
139 sysvar_facility= new_facility;
140 return true;
141 }
142 return false;
143}
144
145std::string& logging::Syslog::getFacility()
146{
147 return sysvar_facility;
148}
149
150} /* namespace syslog */
151} /* namespsace drizzle_plugin */
std::shared_ptr< const std::string > QueryString
Definition session.h:133