Drizzled Public API Documentation

logging.cc

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *  Copyright (C) 2010 Mark Atwood
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; version 2 of the License.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <stdarg.h>
00024 #include <limits.h>
00025 #include <sys/types.h>
00026 #include <sys/stat.h>
00027 #include <fcntl.h>
00028 
00029 #include <boost/date_time.hpp>
00030 
00031 #include <drizzled/gettext.h>
00032 #include <drizzled/session.h>
00033 #include <drizzled/sql_parse.h>
00034 
00035 #include "logging.h"
00036 #include "wrap.h"
00037 
00038 namespace drizzle_plugin
00039 {
00040 
00041 logging::Syslog::Syslog(const std::string &facility,
00042                         const std::string &priority,
00043                         uint64_t threshold_slow,
00044                         uint64_t threshold_big_resultset,
00045                         uint64_t threshold_big_examined) :
00046   drizzled::plugin::Logging("Syslog Logging"),
00047   _facility(WrapSyslog::getFacilityByName(facility.c_str())),
00048   _priority(WrapSyslog::getPriorityByName(priority.c_str())),
00049   _threshold_slow(threshold_slow),
00050   _threshold_big_resultset(threshold_big_resultset),
00051   _threshold_big_examined(threshold_big_examined)
00052 {
00053   if (_facility < 0)
00054   {
00055     drizzled::errmsg_printf(drizzled::error::WARN,
00056                             _("syslog facility \"%s\" not known, using \"local0\""),
00057                             facility.c_str());
00058     _facility= WrapSyslog::getFacilityByName("local0");
00059   }
00060 
00061   if (_priority < 0)
00062   {
00063     drizzled::errmsg_printf(drizzled::error::WARN,
00064                             _("syslog priority \"%s\" not known, using \"info\""),
00065                             priority.c_str());
00066     _priority= WrapSyslog::getPriorityByName("info");
00067   }
00068 }
00069 
00070 
00071 bool logging::Syslog::post(drizzled::Session *session)
00072 {
00073   assert(session != NULL);
00074 
00075   // return if query was not too small
00076   if (session->sent_row_count < _threshold_big_resultset)
00077     return false;
00078   if (session->examined_row_count < _threshold_big_examined)
00079     return false;
00080 
00081   /*
00082     TODO, the session object should have a "utime command completed"
00083     inside itself, so be more accurate, and so this doesnt have to
00084     keep calling current_utime, which can be slow.
00085   */
00086   uint64_t t_mark= session->getCurrentTimestamp(false);
00087 
00088   // return if query was not too slow
00089   if (session->getElapsedTime() < _threshold_slow)
00090     return false;
00091 
00092   drizzled::Session::QueryString query_string(session->getQueryString());
00093   drizzled::util::string::const_shared_ptr schema(session->schema());
00094 
00095   WrapSyslog::singleton()
00096     .log(_facility, _priority,
00097          "thread_id=%ld query_id=%ld"
00098          " db=\"%.*s\""
00099          " query=\"%.*s\""
00100          " command=\"%.*s\""
00101          " t_connect=%lld t_start=%lld t_lock=%lld"
00102          " rows_sent=%ld rows_examined=%ld"
00103          " tmp_table=%ld total_warn_count=%ld\n",
00104          (unsigned long) session->thread_id,
00105          (unsigned long) session->getQueryId(),
00106          (int) schema->size(),
00107          schema->empty() ? "" : schema->c_str(),
00108          (int) query_string->length(),
00109          query_string->empty() ? "" : query_string->c_str(),
00110          (int) drizzled::getCommandName(session->command).size(),
00111          drizzled::getCommandName(session->command).c_str(),
00112          (unsigned long long) (t_mark - session->getConnectMicroseconds()),
00113          (unsigned long long) (session->getElapsedTime()),
00114          (unsigned long long) (t_mark - session->utime_after_lock),
00115          (unsigned long) session->sent_row_count,
00116          (unsigned long) session->examined_row_count,
00117          (unsigned long) session->tmp_table,
00118          (unsigned long) session->total_warn_count);
00119 
00120     return false;
00121 }
00122 
00123 } /* namespsace drizzle_plugin */