~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/syslog/logging.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

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
 
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
5
5
 *  Copyright (C) 2010 Mark Atwood
6
6
 *
7
7
 *  This program is free software; you can redistribute it and/or modify
20
20
 
21
21
#include "config.h"
22
22
 
23
 
#include <drizzled/gettext.h>
24
 
#include <drizzled/session.h>
25
 
 
26
23
#include <stdarg.h>
27
24
#include <limits.h>
28
 
#include <sys/time.h>
29
25
#include <sys/types.h>
30
26
#include <sys/stat.h>
31
27
#include <fcntl.h>
32
28
 
 
29
#include <boost/date_time.hpp>
 
30
 
 
31
#include <drizzled/gettext.h>
 
32
#include <drizzled/session.h>
 
33
 
33
34
#include "logging.h"
34
35
#include "wrap.h"
35
36
 
36
 
using namespace drizzled;
37
 
 
38
 
/* stolen from mysys/my_getsystime
39
 
   until the Session has a good utime "now" we can use
40
 
   will have to use this instead */
41
 
 
42
 
static uint64_t get_microtime()
43
 
{
44
 
#if defined(HAVE_GETHRTIME)
45
 
  return gethrtime()/1000;
46
 
#else
47
 
  uint64_t newtime;
48
 
  struct timeval t;
49
 
  /* loop is because gettimeofday may fail on some systems */
50
 
  while (gettimeofday(&t, NULL) != 0) {}
51
 
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
52
 
  return newtime;
53
 
#endif
54
 
}
55
 
 
56
 
Logging_syslog::Logging_syslog()
57
 
  : drizzled::plugin::Logging("Logging_syslog")
58
 
{
59
 
  syslog_facility= WrapSyslog::getFacilityByName(syslog_module::sysvar_facility);
60
 
  if (syslog_facility < 0)
61
 
  {
62
 
    errmsg_printf(ERRMSG_LVL_WARN,
63
 
                  _("syslog facility \"%s\" not known, using \"local0\""),
64
 
                  syslog_module::sysvar_facility);
65
 
    syslog_facility= WrapSyslog::getFacilityByName("local0");
66
 
  }
67
 
 
68
 
  syslog_priority= WrapSyslog::getPriorityByName(syslog_module::sysvar_logging_priority);
69
 
  if (syslog_priority < 0)
70
 
  {
71
 
    errmsg_printf(ERRMSG_LVL_WARN,
72
 
                  _("syslog priority \"%s\" not known, using \"info\""),
73
 
                  syslog_module::sysvar_logging_priority);
74
 
    syslog_priority= WrapSyslog::getPriorityByName("info");
75
 
  }
76
 
 
77
 
  WrapSyslog::singleton().openlog(syslog_module::sysvar_ident);
78
 
}
79
 
 
80
 
 
81
 
bool Logging_syslog::post (Session *session)
 
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
  }
 
67
}
 
68
 
 
69
 
 
70
bool logging::Syslog::post(drizzled::Session *session)
82
71
{
83
72
  assert(session != NULL);
84
73
 
85
 
  if (syslog_module::sysvar_logging_enable == false)
86
 
    return false;
87
 
  
88
74
  // return if query was not too small
89
 
  if (session->sent_row_count < syslog_module::sysvar_logging_threshold_big_resultset)
90
 
    return false;
91
 
  if (session->examined_row_count < syslog_module::sysvar_logging_threshold_big_examined)
92
 
    return false;
93
 
  
94
 
  /* TODO, the session object should have a "utime command completed"
95
 
     inside itself, so be more accurate, and so this doesnt have to
96
 
     keep calling current_utime, which can be slow */
97
 
  
98
 
  uint64_t t_mark= get_microtime();
 
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);
99
86
 
100
87
  // return if query was not too slow
101
 
  if ((t_mark - session->start_utime) < syslog_module::sysvar_logging_threshold_slow)
 
88
  if (session->getElapsedTime() < _threshold_slow)
102
89
    return false;
103
90
  
104
 
  /* to avoid trying to printf %s something that is potentially NULL */
105
 
  
106
 
  const char *dbs= session->db.empty() ? "" : session->db.c_str();
107
 
  
108
 
  const char *qys= (! session->getQueryString().empty()) ? session->getQueryString().c_str() : "";
109
 
  int qyl= 0;
110
 
  if (qys)
111
 
    qyl= session->getQueryLength();
 
91
  drizzled::Session::QueryString query_string(session->getQueryString());
 
92
  drizzled::util::string::const_shared_ptr schema(session->schema());
112
93
 
113
94
  WrapSyslog::singleton()
114
 
    .log(syslog_facility, syslog_priority,
 
95
    .log(_facility, _priority,
115
96
         "thread_id=%ld query_id=%ld"
116
97
         " db=\"%.*s\""
117
98
         " query=\"%.*s\""
121
102
         " tmp_table=%ld total_warn_count=%ld\n",
122
103
         (unsigned long) session->thread_id,
123
104
         (unsigned long) session->getQueryId(),
124
 
         (int) session->db.length(), dbs,
125
 
         qyl, qys,
126
 
         (int) command_name[session->command].length,
127
 
         command_name[session->command].str,
 
105
         (int) schema->size(),
 
106
         schema->empty() ? "" : schema->c_str(),
 
107
         (int) query_string->length(), 
 
108
         query_string->empty() ? "" : query_string->c_str(),
 
109
         (int) drizzled::getCommandName(session->command).size(),
 
110
         drizzled::getCommandName(session->command).c_str(),
128
111
         (unsigned long long) (t_mark - session->getConnectMicroseconds()),
129
 
         (unsigned long long) (t_mark - session->start_utime),
 
112
         (unsigned long long) (session->getElapsedTime()),
130
113
         (unsigned long long) (t_mark - session->utime_after_lock),
131
114
         (unsigned long) session->sent_row_count,
132
115
         (unsigned long) session->examined_row_count,
135
118
  
136
119
    return false;
137
120
}
 
121
 
 
122
} /* namespsace drizzle_plugin */