21
21
#include "config.h"
23
#include <drizzled/gettext.h>
24
#include <drizzled/session.h>
23
26
#include <stdarg.h>
24
27
#include <limits.h>
25
29
#include <sys/types.h>
26
30
#include <sys/stat.h>
29
#include <boost/date_time.hpp>
31
#include <drizzled/gettext.h>
32
#include <drizzled/session.h>
34
33
#include "logging.h"
37
namespace drizzle_plugin
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)
54
drizzled::errmsg_printf(ERRMSG_LVL_WARN,
55
_("syslog facility \"%s\" not known, using \"local0\""),
57
_facility= WrapSyslog::getFacilityByName("local0");
62
drizzled::errmsg_printf(ERRMSG_LVL_WARN,
63
_("syslog priority \"%s\" not known, using \"info\""),
65
_priority= WrapSyslog::getPriorityByName("info");
70
bool logging::Syslog::post(drizzled::Session *session)
36
using namespace drizzled;
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 */
42
static uint64_t get_microtime()
44
#if defined(HAVE_GETHRTIME)
45
return gethrtime()/1000;
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;
56
Logging_syslog::Logging_syslog()
57
: drizzled::plugin::Logging("Logging_syslog")
59
syslog_facility= WrapSyslog::getFacilityByName(syslog_module::sysvar_facility);
60
if (syslog_facility < 0)
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");
68
syslog_priority= WrapSyslog::getPriorityByName(syslog_module::sysvar_logging_priority);
69
if (syslog_priority < 0)
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");
77
WrapSyslog::singleton().openlog(syslog_module::sysvar_ident);
81
bool Logging_syslog::post (Session *session)
72
83
assert(session != NULL);
85
if (syslog_module::sysvar_logging_enable == false)
74
88
// return if query was not too small
75
if (session->sent_row_count < _threshold_big_resultset)
89
if (session->sent_row_count < syslog_module::sysvar_logging_threshold_big_resultset)
77
if (session->examined_row_count < _threshold_big_examined)
91
if (session->examined_row_count < syslog_module::sysvar_logging_threshold_big_examined)
80
94
/* TODO, the session object should have a "utime command completed"
81
95
inside itself, so be more accurate, and so this doesnt have to
82
96
keep calling current_utime, which can be slow */
84
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
85
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
86
uint64_t t_mark= (mytime-epoch).total_microseconds();
98
uint64_t t_mark= get_microtime();
88
100
// return if query was not too slow
89
if ((t_mark - session->start_utime) < _threshold_slow)
101
if ((t_mark - session->start_utime) < syslog_module::sysvar_logging_threshold_slow)
92
drizzled::Session::QueryString query_string(session->getQueryString());
93
drizzled::util::string::const_shared_ptr schema(session->schema());
104
/* to avoid trying to printf %s something that is potentially NULL */
106
const char *dbs= session->db.empty() ? "" : session->db.c_str();
108
const char *qys= (! session->getQueryString().empty()) ? session->getQueryString().c_str() : "";
111
qyl= session->getQueryLength();
95
113
WrapSyslog::singleton()
96
.log(_facility, _priority,
114
.log(syslog_facility, syslog_priority,
97
115
"thread_id=%ld query_id=%ld"
103
121
" tmp_table=%ld total_warn_count=%ld\n",
104
122
(unsigned long) session->thread_id,
105
123
(unsigned long) session->getQueryId(),
106
(int) schema->size(),
107
schema->empty() ? "" : schema->c_str(),
108
(int) query_string->length(),
109
query_string->empty() ? "" : query_string->c_str(),
110
(int) drizzled::command_name[session->command].length,
111
drizzled::command_name[session->command].str,
124
(int) session->db.length(), dbs,
126
(int) command_name[session->command].length,
127
command_name[session->command].str,
112
128
(unsigned long long) (t_mark - session->getConnectMicroseconds()),
113
129
(unsigned long long) (t_mark - session->start_utime),
114
130
(unsigned long long) (t_mark - session->utime_after_lock),