~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/syslog/logging.cc

  • Committer: kalebral at gmail
  • Date: 2010-12-04 04:58:08 UTC
  • mto: (1971.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1972.
  • Revision ID: kalebral@gmail.com-20101204045808-acto22oxfg43m02e
a few more updates of files that did not have license or had incorrect license structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2009 Sun Microsystems
5
5
 *  Copyright (C) 2010 Mark Atwood
6
6
 *
7
7
 *  This program is free software; you can redistribute it and/or modify
19
19
 */
20
20
 
21
21
#include "config.h"
 
22
#include <boost/date_time.hpp>
 
23
#include <drizzled/gettext.h>
 
24
#include <drizzled/session.h>
22
25
 
23
26
#include <stdarg.h>
24
27
#include <limits.h>
26
29
#include <sys/stat.h>
27
30
#include <fcntl.h>
28
31
 
29
 
#include <boost/date_time.hpp>
30
 
 
31
 
#include <drizzled/gettext.h>
32
 
#include <drizzled/session.h>
33
 
 
34
32
#include "logging.h"
35
33
#include "wrap.h"
36
34
 
37
 
namespace drizzle_plugin
38
 
{
39
 
 
40
 
logging::Syslog::Syslog(const std::string &facility,
41
 
                        const std::string &priority,
42
 
                        uint64_t threshold_slow,
43
 
                        uint64_t threshold_big_resultset,
44
 
                        uint64_t threshold_big_examined) :
45
 
  drizzled::plugin::Logging("Syslog Logging"),
46
 
  _facility(WrapSyslog::getFacilityByName(facility.c_str())),
47
 
  _priority(WrapSyslog::getPriorityByName(priority.c_str())),
48
 
  _threshold_slow(threshold_slow),
49
 
  _threshold_big_resultset(threshold_big_resultset),
50
 
  _threshold_big_examined(threshold_big_examined)
51
 
{
52
 
  if (_facility < 0)
53
 
  {
54
 
    drizzled::errmsg_printf(drizzled::error::WARN,
55
 
                            _("syslog facility \"%s\" not known, using \"local0\""),
56
 
                            facility.c_str());
57
 
    _facility= WrapSyslog::getFacilityByName("local0");
58
 
  }
59
 
 
60
 
  if (_priority < 0)
61
 
  {
62
 
    drizzled::errmsg_printf(drizzled::error::WARN,
63
 
                            _("syslog priority \"%s\" not known, using \"info\""),
64
 
                            priority.c_str());
65
 
    _priority= WrapSyslog::getPriorityByName("info");
66
 
  }
 
35
using namespace drizzled;
 
36
 
 
37
Logging_syslog::Logging_syslog()
 
38
  : drizzled::plugin::Logging("Logging_syslog")
 
39
{
 
40
  syslog_facility= WrapSyslog::getFacilityByName(syslog_module::sysvar_facility);
 
41
  if (syslog_facility < 0)
 
42
  {
 
43
    errmsg_printf(ERRMSG_LVL_WARN,
 
44
                  _("syslog facility \"%s\" not known, using \"local0\""),
 
45
                  syslog_module::sysvar_facility);
 
46
    syslog_facility= WrapSyslog::getFacilityByName("local0");
 
47
  }
 
48
 
 
49
  syslog_priority= WrapSyslog::getPriorityByName(syslog_module::sysvar_logging_priority);
 
50
  if (syslog_priority < 0)
 
51
  {
 
52
    errmsg_printf(ERRMSG_LVL_WARN,
 
53
                  _("syslog priority \"%s\" not known, using \"info\""),
 
54
                  syslog_module::sysvar_logging_priority);
 
55
    syslog_priority= WrapSyslog::getPriorityByName("info");
 
56
  }
 
57
 
 
58
  WrapSyslog::singleton().openlog(syslog_module::sysvar_ident);
67
59
}
68
60
 
69
61
 
70
 
bool logging::Syslog::post(drizzled::Session *session)
 
62
bool Logging_syslog::post (Session *session)
71
63
{
72
64
  assert(session != NULL);
73
65
 
 
66
  if (syslog_module::sysvar_logging_enable == false)
 
67
    return false;
 
68
  
74
69
  // return if query was not too small
75
 
  if (session->sent_row_count < _threshold_big_resultset)
76
 
    return false;
77
 
  if (session->examined_row_count < _threshold_big_examined)
78
 
    return false;
79
 
  
80
 
  /*
81
 
    TODO, the session object should have a "utime command completed"
82
 
    inside itself, so be more accurate, and so this doesnt have to
83
 
    keep calling current_utime, which can be slow.
84
 
  */
85
 
  uint64_t t_mark= session->getCurrentTimestamp(false);
 
70
  if (session->sent_row_count < syslog_module::sysvar_logging_threshold_big_resultset)
 
71
    return false;
 
72
  if (session->examined_row_count < syslog_module::sysvar_logging_threshold_big_examined)
 
73
    return false;
 
74
  
 
75
  /* TODO, the session object should have a "utime command completed"
 
76
     inside itself, so be more accurate, and so this doesnt have to
 
77
     keep calling current_utime, which can be slow */
 
78
  
 
79
  boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
 
80
  boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
 
81
  uint64_t t_mark= (mytime-epoch).total_microseconds();
86
82
 
87
83
  // return if query was not too slow
88
 
  if (session->getElapsedTime() < _threshold_slow)
 
84
  if ((t_mark - session->start_utime) < syslog_module::sysvar_logging_threshold_slow)
89
85
    return false;
90
86
  
91
 
  drizzled::Session::QueryString query_string(session->getQueryString());
92
 
  drizzled::util::string::const_shared_ptr schema(session->schema());
 
87
  Session::QueryString query_string(session->getQueryString());
93
88
 
94
89
  WrapSyslog::singleton()
95
 
    .log(_facility, _priority,
 
90
    .log(syslog_facility, syslog_priority,
96
91
         "thread_id=%ld query_id=%ld"
97
92
         " db=\"%.*s\""
98
93
         " query=\"%.*s\""
102
97
         " tmp_table=%ld total_warn_count=%ld\n",
103
98
         (unsigned long) session->thread_id,
104
99
         (unsigned long) session->getQueryId(),
105
 
         (int) schema->size(),
106
 
         schema->empty() ? "" : schema->c_str(),
 
100
         (int) session->getSchema().length(),
 
101
         session->getSchema().empty() ? "" : session->getSchema().c_str(),
107
102
         (int) query_string->length(), 
108
103
         query_string->empty() ? "" : query_string->c_str(),
109
 
         (int) drizzled::getCommandName(session->command).size(),
110
 
         drizzled::getCommandName(session->command).c_str(),
 
104
         (int) command_name[session->command].length,
 
105
         command_name[session->command].str,
111
106
         (unsigned long long) (t_mark - session->getConnectMicroseconds()),
112
 
         (unsigned long long) (session->getElapsedTime()),
 
107
         (unsigned long long) (t_mark - session->start_utime),
113
108
         (unsigned long long) (t_mark - session->utime_after_lock),
114
109
         (unsigned long) session->sent_row_count,
115
110
         (unsigned long) session->examined_row_count,
118
113
  
119
114
    return false;
120
115
}
121
 
 
122
 
} /* namespsace drizzle_plugin */