~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

  • Committer: Monty Taylor
  • Date: 2010-11-25 01:53:19 UTC
  • mto: (1953.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1955.
  • Revision ID: mordred@inaugust.com-20101125015319-ia85msn25uemopgc
Re-enabled -Wformat and then cleaned up the carnage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include <cerrno>
37
37
#include <memory>
38
38
 
39
 
 
40
 
namespace drizzle_plugin
41
 
{
42
 
 
 
39
using namespace drizzled;
43
40
namespace po= boost::program_options;
44
41
 
45
42
/* TODO make this dynamic as needed */
46
43
static const int MAX_MSG_LEN= 32*1024;
47
44
 
 
45
static bool sysvar_logging_gearman_enable;
 
46
static char* sysvar_logging_gearman_host= NULL;
 
47
static char* sysvar_logging_gearman_function= NULL;
 
48
 
48
49
/* quote a string to be safe to include in a CSV line
49
50
   that means backslash quoting all commas, doublequotes, backslashes,
50
51
   and all the ASCII unprintable characters
153
154
  return dst;
154
155
}
155
156
 
156
 
class LoggingGearman :
157
 
  public drizzled::plugin::Logging
 
157
class LoggingGearman : public plugin::Logging
158
158
{
159
159
 
160
 
  const std::string _host;
161
 
  const std::string _function;
162
 
 
163
 
  int _gearman_client_ok;
164
 
  gearman_client_st _gearman_client;
165
 
 
166
 
  LoggingGearman();
167
 
  LoggingGearman(const LoggingGearman&);
 
160
  int gearman_client_ok;
 
161
  gearman_client_st gearman_client;
168
162
 
169
163
public:
170
164
 
171
 
  LoggingGearman(const std::string &host,
172
 
                 const std::string &function) :
173
 
    drizzled::plugin::Logging("LoggingGearman"),
174
 
    _host(host),
175
 
    _function(function),
176
 
    _gearman_client_ok(0),
177
 
    _gearman_client()
 
165
  LoggingGearman()
 
166
    : plugin::Logging("LoggingGearman"),
 
167
      gearman_client_ok(0)
178
168
  {
179
169
    gearman_return_t ret;
180
170
 
181
 
 
182
 
    if (gearman_client_create(&_gearman_client) == NULL)
 
171
    if (sysvar_logging_gearman_enable == false)
 
172
      return;
 
173
 
 
174
    if (sysvar_logging_gearman_host == NULL)
 
175
      return;
 
176
 
 
177
 
 
178
    if (gearman_client_create(&gearman_client) == NULL)
183
179
    {
184
180
      char errmsg[STRERROR_MAX];
185
181
      strerror_r(errno, errmsg, sizeof(errmsg));
186
 
      drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
187
 
                              errmsg);
 
182
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
 
183
                    errmsg);
188
184
      return;
189
185
    }
190
186
 
191
187
    /* TODO, be able to override the port */
192
188
    /* TODO, be able send to multiple servers */
193
 
    ret= gearman_client_add_server(&_gearman_client,
194
 
                                   host.c_str(), 0);
 
189
    ret= gearman_client_add_server(&gearman_client,
 
190
                                   sysvar_logging_gearman_host, 0);
195
191
    if (ret != GEARMAN_SUCCESS)
196
192
    {
197
 
      drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
198
 
                              gearman_client_error(&_gearman_client));
 
193
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
 
194
                    gearman_client_error(&gearman_client));
199
195
      return;
200
196
    }
201
197
 
202
 
    _gearman_client_ok= 1;
 
198
    gearman_client_ok= 1;
203
199
 
204
200
  }
205
201
 
206
202
  ~LoggingGearman()
207
203
  {
208
 
    if (_gearman_client_ok)
 
204
    if (gearman_client_ok)
209
205
    {
210
 
      gearman_client_free(&_gearman_client);
 
206
      gearman_client_free(&gearman_client);
211
207
    }
212
208
  }
213
209
 
214
 
  virtual bool post(drizzled::Session *session)
 
210
  virtual bool post(Session *session)
215
211
  {
216
212
    boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
217
213
    int msgbuf_len= 0;
222
218
       but that crashes the server, so for now, we just lie a little bit
223
219
    */
224
220
 
225
 
    if (not _gearman_client_ok)
 
221
    if (!gearman_client_ok)
226
222
        return false;
227
223
  
228
224
    /* TODO, the session object should have a "utime command completed"
238
234
    unsigned char qs[255];
239
235
  
240
236
    // to avoid trying to printf %s something that is potentially NULL
241
 
    drizzled::util::string::const_shared_ptr dbs(session->schema());
 
237
    const char *dbs= session->db.empty() ? "" : session->db.c_str();
242
238
  
243
239
    msgbuf_len=
244
240
      snprintf(msgbuf.get(), MAX_MSG_LEN,
249
245
               session->thread_id,
250
246
               session->getQueryId(),
251
247
               // dont need to quote the db name, always CSV safe
252
 
               (int)dbs->size(), dbs->c_str(),
 
248
               (int)session->getSchema().length(), dbs,
253
249
               // do need to quote the query
254
250
               quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
255
251
               // command_name is defined in drizzled/sql_parse.cc
256
252
               // dont need to quote the command name, always CSV safe
257
 
               (int)drizzled::command_name[session->command].length,
258
 
               drizzled::command_name[session->command].str,
 
253
               (int)command_name[session->command].length,
 
254
               command_name[session->command].str,
259
255
               // counters are at end, to make it easier to add more
260
256
               (t_mark - session->getConnectMicroseconds()),
261
257
               (t_mark - session->start_utime),
265
261
               session->tmp_table,
266
262
               session->total_warn_count,
267
263
               session->getServerId(),
268
 
               drizzled::glob_hostname
 
264
               glob_hostname
269
265
               );
270
266
  
271
267
    char job_handle[GEARMAN_JOB_HANDLE_SIZE];
272
268
  
273
 
    (void) gearman_client_do_background(&_gearman_client,
274
 
                                        _function.c_str(),
 
269
    (void) gearman_client_do_background(&gearman_client,
 
270
                                        sysvar_logging_gearman_function,
275
271
                                        NULL,
276
272
                                        (void *) msgbuf.get(),
277
273
                                        (size_t) msgbuf_len,
283
279
 
284
280
static LoggingGearman *handler= NULL;
285
281
 
286
 
static int logging_gearman_plugin_init(drizzled::module::Context &context)
 
282
static int logging_gearman_plugin_init(module::Context &context)
287
283
{
288
 
  const drizzled::module::option_map &vm= context.getOptions();
289
 
 
290
 
  handler= new LoggingGearman(vm["host"].as<std::string>(),
291
 
                              vm["function"].as<std::string>());
 
284
  handler= new LoggingGearman();
292
285
  context.add(handler);
293
 
  context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
294
 
  context.registerVariable(new drizzled::sys_var_const_string_val("function", vm["function"].as<std::string>()));
295
286
 
296
287
  return 0;
297
288
}
298
289
 
299
290
static void init_options(drizzled::module::option_context &context)
300
291
{
301
 
  context("host",
302
 
          po::value<std::string>()->default_value("localhost"),
303
 
          N_("Hostname for logging to a Gearman server"));
304
 
  context("function",
305
 
          po::value<std::string>()->default_value("drizzlelog"),
306
 
          N_("Gearman Function to send logging to"));
 
292
  context("enable",
 
293
          po::value<bool>(&sysvar_logging_gearman_enable)->default_value(false)->zero_tokens(),
 
294
          N_("Enable logging to a gearman server"));
307
295
}
308
296
 
309
 
} /* namespace drizzle_plugin */
 
297
static DRIZZLE_SYSVAR_BOOL(
 
298
                           enable,
 
299
                           sysvar_logging_gearman_enable,
 
300
                           PLUGIN_VAR_NOCMDARG,
 
301
                           N_("Enable logging to a gearman server"),
 
302
                           NULL, /* check func */
 
303
                           NULL, /* update func */
 
304
                           false /* default */);
 
305
 
 
306
static DRIZZLE_SYSVAR_STR(
 
307
                          host,
 
308
                          sysvar_logging_gearman_host,
 
309
                          PLUGIN_VAR_READONLY,
 
310
                          N_("Hostname for logging to a Gearman server"),
 
311
                          NULL, /* check func */
 
312
                          NULL, /* update func*/
 
313
                          "localhost" /* default */);
 
314
 
 
315
static DRIZZLE_SYSVAR_STR(
 
316
                          function,
 
317
                          sysvar_logging_gearman_function,
 
318
                          PLUGIN_VAR_READONLY,
 
319
                          N_("Gearman Function to send logging to"),
 
320
                          NULL, /* check func */
 
321
                          NULL, /* update func*/
 
322
                          "drizzlelog" /* default */);
 
323
 
 
324
static drizzle_sys_var* logging_gearman_system_variables[]= {
 
325
  DRIZZLE_SYSVAR(enable),
 
326
  DRIZZLE_SYSVAR(host),
 
327
  DRIZZLE_SYSVAR(function),
 
328
  NULL
 
329
};
310
330
 
311
331
DRIZZLE_DECLARE_PLUGIN
312
332
{
315
335
    "0.1",
316
336
    "Mark Atwood <mark@fallenpegasus.com>",
317
337
    N_("Log queries to a Gearman server"),
318
 
    drizzled::PLUGIN_LICENSE_GPL,
319
 
    drizzle_plugin::logging_gearman_plugin_init,
320
 
    NULL,
321
 
    drizzle_plugin::init_options
 
338
    PLUGIN_LICENSE_GPL,
 
339
    logging_gearman_plugin_init,
 
340
    logging_gearman_system_variables,
 
341
    init_options
322
342
}
323
343
DRIZZLE_DECLARE_PLUGIN_END;