~drizzle-trunk/drizzle/development

1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
22
23
#include <stdarg.h>
24
#include <limits.h>
25
#include <sys/types.h>
26
#include <sys/stat.h>
27
#include <fcntl.h>
28
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
29
#include <boost/date_time.hpp>
30
31
#include <drizzled/gettext.h>
32
#include <drizzled/session.h>
2269.1.4 by Olaf van der Spek
Session Times
33
#include <drizzled/session/times.h>
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
34
#include <drizzled/sql_parse.h>
2239.1.6 by Olaf van der Spek
Refactor includes
35
#include <drizzled/plugin.h>
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
36
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
37
#include "logging.h"
38
#include "wrap.h"
39
2269.1.4 by Olaf van der Spek
Session Times
40
namespace drizzle_plugin {
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
41
42
logging::Syslog::Syslog(const std::string &facility,
43
                        const std::string &priority,
44
                        uint64_t threshold_slow,
45
                        uint64_t threshold_big_resultset,
46
                        uint64_t threshold_big_examined) :
47
  drizzled::plugin::Logging("Syslog Logging"),
48
  _facility(WrapSyslog::getFacilityByName(facility.c_str())),
49
  _priority(WrapSyslog::getPriorityByName(priority.c_str())),
50
  _threshold_slow(threshold_slow),
51
  _threshold_big_resultset(threshold_big_resultset),
52
  _threshold_big_examined(threshold_big_examined)
53
{
54
  if (_facility < 0)
55
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
56
    drizzled::errmsg_printf(drizzled::error::WARN,
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
57
                            _("syslog facility \"%s\" not known, using \"local0\""),
58
                            facility.c_str());
59
    _facility= WrapSyslog::getFacilityByName("local0");
60
  }
61
62
  if (_priority < 0)
63
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
64
    drizzled::errmsg_printf(drizzled::error::WARN,
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
65
                            _("syslog priority \"%s\" not known, using \"info\""),
66
                            priority.c_str());
67
    _priority= WrapSyslog::getPriorityByName("info");
68
  }
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
69
}
70
71
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
72
bool logging::Syslog::post(drizzled::Session *session)
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
73
{
74
  assert(session != NULL);
75
76
  // return if query was not too small
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
77
  if (session->sent_row_count < _threshold_big_resultset)
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
78
    return false;
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
79
  if (session->examined_row_count < _threshold_big_examined)
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
80
    return false;
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
81
2040.4.3 by Brian Aker
Scale down the calls to universal_time().
82
  /*
83
    TODO, the session object should have a "utime command completed"
84
    inside itself, so be more accurate, and so this doesnt have to
85
    keep calling current_utime, which can be slow.
86
  */
2269.1.5 by Olaf van der Spek
Session Times
87
  uint64_t t_mark= session->times.getCurrentTimestamp(false);
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
88
89
  // return if query was not too slow
2269.1.6 by Olaf van der Spek
Session Times
90
  if (session->times.getElapsedTime() < _threshold_slow)
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
91
    return false;
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
92
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
93
  drizzled::Session::QueryString query_string(session->getQueryString());
2269.1.7 by Olaf van der Spek
Use util::string::ptr
94
  drizzled::util::string::ptr schema(session->schema());
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
95
96
  WrapSyslog::singleton()
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
97
    .log(_facility, _priority,
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
98
         "thread_id=%ld query_id=%ld"
99
         " db=\"%.*s\""
100
         " query=\"%.*s\""
101
         " command=\"%.*s\""
102
         " t_connect=%lld t_start=%lld t_lock=%lld"
103
         " rows_sent=%ld rows_examined=%ld"
104
         " tmp_table=%ld total_warn_count=%ld\n",
105
         (unsigned long) session->thread_id,
106
         (unsigned long) session->getQueryId(),
1976.5.2 by Brian Aker
This resolves the issue where one thread may be looking at schema while
107
         (int) schema->size(),
108
         schema->empty() ? "" : schema->c_str(),
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
109
         (int) query_string->length(),
1921.4.13 by Brian Aker
Fix issue where session info might not be correct.
110
         query_string->empty() ? "" : query_string->c_str(),
2119.4.1 by Monty Taylor
Turns on -fvisibility=hidden by default. Symbols intended to be used by
111
         (int) drizzled::getCommandName(session->command).size(),
112
         drizzled::getCommandName(session->command).c_str(),
2269.1.6 by Olaf van der Spek
Session Times
113
         (unsigned long long) (t_mark - session->times.getConnectMicroseconds()),
114
         (unsigned long long) (session->times.getElapsedTime()),
2269.1.4 by Olaf van der Spek
Session Times
115
         (unsigned long long) (t_mark - session->times.utime_after_lock),
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
116
         (unsigned long) session->sent_row_count,
117
         (unsigned long) session->examined_row_count,
118
         (unsigned long) session->tmp_table,
119
         (unsigned long) session->total_warn_count);
2227.4.3 by Olaf van der Spek
Remove unnecessary statement.h include
120
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
121
    return false;
122
}
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
123
124
} /* namespsace drizzle_plugin */