~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 *  Copyright (C) 2010 Mark Atwood
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include <cstdarg>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <boost/date_time.hpp>

#include <drizzled/gettext.h>
#include <drizzled/session.h>
#include <drizzled/session/times.h>
#include <drizzled/sql_parse.h>
#include <drizzled/plugin.h>
#include <drizzled/error.h>

#include "logging.h"
#include "wrap.h"

namespace drizzle_plugin {

logging::Syslog::Syslog(const std::string &facility,
                        uint64_t threshold_slow,
                        uint64_t threshold_big_resultset,
                        uint64_t threshold_big_examined) :
  drizzled::plugin::Logging("Syslog Logging"),
  _facility(WrapSyslog::getFacilityByName(facility.c_str())),
  _threshold_slow(threshold_slow),
  _threshold_big_resultset(threshold_big_resultset),
  _threshold_big_examined(threshold_big_examined)
{
  if (_facility < 0)
  {
    drizzled::errmsg_printf(drizzled::error::WARN,
                            _("syslog facility \"%s\" not known, using \"local0\""),
                            facility.c_str());
    _facility= WrapSyslog::getFacilityByName("local0");
  }
}


bool logging::Syslog::post(drizzled::Session *session)
{
  assert(session != NULL);

  // return if query was not too small
  if (session->sent_row_count < _threshold_big_resultset)
  {
    return false;
  }

  if (session->examined_row_count < _threshold_big_examined)
  {
    return false;
  }

  /*
    TODO, the session object should have a "utime command completed"
    inside itself, so be more accurate, and so this doesnt have to
    keep calling current_utime, which can be slow.
  */
  uint64_t t_mark= session->times.getCurrentTimestamp(false);

  // return if query was not too slow
  if (session->times.getElapsedTime() < _threshold_slow)
  {
    return false;
  }

  drizzled::Session::QueryString query_string(session->getQueryString());
  drizzled::util::string::ptr schema(session->schema());

  WrapSyslog::singleton()
    .log(_facility, drizzled::error::INFO,
         "thread_id=%ld query_id=%ld"
         " db=\"%.*s\""
         " query=\"%.*s\""
         " command=\"%.*s\""
         " t_connect=%lld t_start=%lld t_lock=%lld"
         " rows_sent=%ld rows_examined=%ld"
         " tmp_table=%ld total_warn_count=%ld\n",
         (unsigned long) session->thread_id,
         (unsigned long) session->getQueryId(),
         (int) schema->size(),
         schema->empty() ? "" : schema->c_str(),
         (int) query_string->length(),
         query_string->empty() ? "" : query_string->c_str(),
         (int) drizzled::getCommandName(session->command).size(),
         drizzled::getCommandName(session->command).c_str(),
         (unsigned long long) (t_mark - session->times.getConnectMicroseconds()),
         (unsigned long long) (session->times.getElapsedTime()),
         (unsigned long long) (t_mark - session->times.utime_after_lock),
         (unsigned long) session->sent_row_count,
         (unsigned long) session->examined_row_count,
         (unsigned long) session->tmp_table,
         (unsigned long) session->total_warn_count);

    return false;
}

} /* namespsace drizzle_plugin */