~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_syslog/logging_syslog.cc

  • Committer: Monty Taylor
  • Date: 2009-04-14 19:16:51 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 994.
  • Revision ID: mordred@inaugust.com-20090414191651-ltbww6hpqks8k7qk
Clarified instructions in README.

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 Mark Atwood
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
21
 
#include <drizzled/plugin/logging.h>
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/plugin/logging_handler.h>
22
22
#include <drizzled/gettext.h>
23
23
#include <drizzled/session.h>
24
24
 
31
31
#endif
32
32
 
33
33
#include <stdarg.h>
34
 
#include <limits.h>
35
 
#include <sys/time.h>
36
 
#include <sys/types.h>
37
 
#include <sys/stat.h>
38
 
#include <fcntl.h>
39
 
 
40
 
 
41
 
using namespace drizzled;
42
34
 
43
35
static bool sysvar_logging_syslog_enable= false;
44
36
static char* sysvar_logging_syslog_ident= NULL;
48
40
static ulong sysvar_logging_syslog_threshold_big_resultset= 0;
49
41
static ulong sysvar_logging_syslog_threshold_big_examined= 0;
50
42
 
 
43
static int syslog_facility= -1;
 
44
static int syslog_priority= -1;
 
45
 
51
46
/* stolen from mysys/my_getsystime
52
47
   until the Session has a good utime "now" we can use
53
48
   will have to use this instead */
54
49
 
 
50
#include <sys/time.h>
55
51
static uint64_t get_microtime()
56
52
{
57
53
#if defined(HAVE_GETHRTIME)
66
62
#endif
67
63
}
68
64
 
69
 
class Logging_syslog: public drizzled::plugin::Logging
 
65
class Logging_syslog: public Logging_handler
70
66
{
71
 
 
72
 
  int syslog_facility;
73
 
  int syslog_priority;
74
 
 
75
67
public:
76
68
 
77
 
  Logging_syslog()
78
 
    : drizzled::plugin::Logging("Logging_syslog"),
79
 
      syslog_facility(-1), syslog_priority(-1)
80
 
  {
81
 
 
82
 
    for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
83
 
    {
84
 
      if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
85
 
      {
86
 
        syslog_facility= facilitynames[ndx].c_val;
87
 
        break;
88
 
      }
89
 
    }
90
 
    if (syslog_facility == -1)
91
 
    {
92
 
      errmsg_printf(ERRMSG_LVL_WARN,
93
 
                    _("syslog facility \"%s\" not known, using \"local0\""),
94
 
                    sysvar_logging_syslog_facility);
95
 
      syslog_facility= LOG_LOCAL0;
96
 
    }
97
 
 
98
 
    for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
99
 
    {
100
 
      if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
101
 
      {
102
 
        syslog_priority= prioritynames[ndx].c_val;
103
 
        break;
104
 
      }
105
 
    }
106
 
    if (syslog_priority == -1)
107
 
    {
108
 
      errmsg_printf(ERRMSG_LVL_WARN,
109
 
                    _("syslog priority \"%s\" not known, using \"info\""),
110
 
                    sysvar_logging_syslog_priority);
111
 
      syslog_priority= LOG_INFO;
112
 
    }
113
 
 
114
 
    openlog(sysvar_logging_syslog_ident,
115
 
            LOG_PID, syslog_facility);
116
 
  }
117
 
 
118
 
  ~Logging_syslog()
119
 
  {
120
 
    closelog();
121
 
  }
 
69
  Logging_syslog() : Logging_handler("Logging_syslog") {}
122
70
 
123
71
  virtual bool post (Session *session)
124
72
  {
132
80
    if (session->examined_row_count < sysvar_logging_syslog_threshold_big_examined)
133
81
      return false;
134
82
  
 
83
    /* TODO, looks like connect_utime isnt being set in the session
 
84
       object.  We could store the time this plugin was loaded, but that
 
85
       would just be a dumb workaround. */
135
86
    /* TODO, the session object should have a "utime command completed"
136
87
       inside itself, so be more accurate, and so this doesnt have to
137
88
       keep calling current_utime, which can be slow */
143
94
  
144
95
    /* to avoid trying to printf %s something that is potentially NULL */
145
96
  
146
 
    const char *dbs= session->db.empty() ? "" : session->db.c_str();
 
97
    const char *dbs= (session->db) ? session->db : "";
 
98
    int dbl= 0;
 
99
    if (dbs)
 
100
      dbl= session->db_length;
147
101
  
148
 
    const char *qys= (! session->getQueryString().empty()) ? session->getQueryString().c_str() : "";
 
102
    const char *qys= (session->query) ? session->query : "";
149
103
    int qyl= 0;
150
104
    if (qys)
151
 
      qyl= session->getQueryLength();
 
105
      qyl= session->query_length;
152
106
    
153
107
    syslog(syslog_priority,
154
108
           "thread_id=%ld query_id=%ld"
156
110
           " query=\"%.*s\""
157
111
           " command=\"%.*s\""
158
112
           " t_connect=%lld t_start=%lld t_lock=%lld"
159
 
           " rows_sent=%ld rows_examined=%ld"
160
 
           " tmp_table=%ld total_warn_count=%ld\n",
 
113
           " rows_sent=%ld rows_examined=%ld\n",
161
114
           (unsigned long) session->thread_id,
162
 
           (unsigned long) session->getQueryId(),
163
 
           (int)session->db.length(), dbs,
 
115
           (unsigned long) session->query_id,
 
116
           dbl, dbs,
164
117
           qyl, qys,
165
118
           (int) command_name[session->command].length,
166
119
           command_name[session->command].str,
167
 
           (unsigned long long) (t_mark - session->getConnectMicroseconds()),
168
 
           (unsigned long long) (t_mark - session->start_utime),
169
 
           (unsigned long long) (t_mark - session->utime_after_lock),
170
 
           (unsigned long) session->sent_row_count,
171
 
           (unsigned long) session->examined_row_count,
172
 
           (unsigned long) session->tmp_table,
173
 
           (unsigned long) session->total_warn_count);
 
120
           (unsigned long long) (t_mark - session->connect_utime),
 
121
           (unsigned long long) (t_mark - session->start_utime),
 
122
           (unsigned long long) (t_mark - session->utime_after_lock),
 
123
           (unsigned long) session->sent_row_count,
 
124
           (unsigned long) session->examined_row_count);
 
125
  
 
126
#if 0
 
127
    syslog(syslog_priority,
 
128
           "thread_id=%ld query_id=%ld"
 
129
           " db=\"%.*s\""
 
130
           " query=\".*%s\""
 
131
           " command=%.*s"
 
132
           " t_connect=%lld t_start=%lld t_lock=%lld"
 
133
           " rows_sent=%ld rows_examined=%ld\n",
 
134
           (unsigned long) session->thread_id,
 
135
           (unsigned long) session->query_id,
 
136
           session->db_length, session->db,
 
137
           // dont need to quote the query, because syslog does it itself
 
138
           session->query_length, session->query,
 
139
           (int) command_name[session->command].length,
 
140
           command_name[session->command].str,
 
141
           (unsigned long long) (t_mark - session->connect_utime),
 
142
           (unsigned long long) (t_mark - session->start_utime),
 
143
           (unsigned long long) (t_mark - session->utime_after_lock),
 
144
           (unsigned long) session->sent_row_count,
 
145
           (unsigned long) session->examined_row_count);
 
146
  
 
147
#endif
174
148
  
175
149
    return false;
176
150
  }
178
152
 
179
153
static Logging_syslog *handler= NULL;
180
154
 
181
 
static int logging_syslog_plugin_init(drizzled::plugin::Context &context)
 
155
static int logging_syslog_plugin_init(PluginRegistry &registry)
182
156
{
 
157
  syslog_facility= -1;
 
158
  for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
 
159
  {
 
160
    if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
 
161
    {
 
162
      syslog_facility= facilitynames[ndx].c_val;
 
163
      break;
 
164
    }
 
165
  }
 
166
  if (syslog_facility == -1)
 
167
  {
 
168
    errmsg_printf(ERRMSG_LVL_WARN,
 
169
                  _("syslog facility \"%s\" not known, using \"local0\""),
 
170
                  sysvar_logging_syslog_facility);
 
171
    syslog_facility= LOG_LOCAL0;
 
172
  }
 
173
 
 
174
  syslog_priority= -1;
 
175
  for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
 
176
  {
 
177
    if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
 
178
    {
 
179
      syslog_priority= prioritynames[ndx].c_val;
 
180
      break;
 
181
    }
 
182
  }
 
183
  if (syslog_priority == -1)
 
184
  {
 
185
    errmsg_printf(ERRMSG_LVL_WARN,
 
186
                  _("syslog priority \"%s\" not known, using \"info\""),
 
187
                  sysvar_logging_syslog_priority);
 
188
    syslog_priority= LOG_INFO;
 
189
  }
 
190
 
 
191
  openlog(sysvar_logging_syslog_ident,
 
192
          LOG_PID, syslog_facility);
 
193
 
183
194
  handler= new Logging_syslog();
184
 
  context.add(handler);
 
195
  registry.add(handler);
 
196
 
 
197
  return 0;
 
198
}
 
199
 
 
200
static int logging_syslog_plugin_deinit(PluginRegistry &registry)
 
201
{
 
202
  registry.remove(handler);
 
203
  delete handler;
185
204
 
186
205
  return 0;
187
206
}
190
209
  enable,
191
210
  sysvar_logging_syslog_enable,
192
211
  PLUGIN_VAR_NOCMDARG,
193
 
  N_("Enable logging to syslog"),
 
212
  N_("Enable logging"),
194
213
  NULL, /* check func */
195
214
  NULL, /* update func */
196
215
  false /* default */);
258
277
  ULONG_MAX, /* max */
259
278
  0 /* blksiz */);
260
279
 
261
 
static drizzle_sys_var* logging_syslog_system_variables[]= {
 
280
static struct st_mysql_sys_var* logging_syslog_system_variables[]= {
262
281
  DRIZZLE_SYSVAR(enable),
263
282
  DRIZZLE_SYSVAR(ident),
264
283
  DRIZZLE_SYSVAR(facility),
269
288
  NULL
270
289
};
271
290
 
272
 
DRIZZLE_DECLARE_PLUGIN
 
291
drizzle_declare_plugin(logging_syslog)
273
292
{
274
 
  DRIZZLE_VERSION_ID,
275
293
  "logging_syslog",
276
294
  "0.2",
277
295
  "Mark Atwood <mark@fallenpegasus.com>",
278
296
  N_("Log to syslog"),
279
297
  PLUGIN_LICENSE_GPL,
280
298
  logging_syslog_plugin_init,
 
299
  logging_syslog_plugin_deinit,
 
300
  NULL,   /* status variables */
281
301
  logging_syslog_system_variables,
282
302
  NULL
283
303
}
284
 
DRIZZLE_DECLARE_PLUGIN_END;
 
304
drizzle_declare_plugin_end;