~zisis00/drizzle/ldap_policy

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

  • Committer: Zisis Sialveras
  • Date: 2012-08-09 18:18:22 UTC
  • mfrom: (2553.1.25 workspace)
  • Revision ID: zisis00@gmail.com-20120809181822-9wobhdfdx411tu5w
Some changes in ldap_policy

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
#include <boost/scoped_array.hpp>
23
23
 
 
24
#include <drizzled/item.h>
24
25
#include <drizzled/plugin.h>
25
26
#include <drizzled/plugin/logging.h>
26
27
#include <drizzled/gettext.h>
40
41
#include <cerrno>
41
42
#include <memory>
42
43
 
 
44
using namespace drizzled;
 
45
 
43
46
namespace drizzle_plugin {
 
47
namespace logging_gearman {
44
48
 
45
49
namespace po= boost::program_options;
46
50
 
 
51
bool updateHost(Session *, set_var*);
 
52
bool updateFunction(Session *, set_var*);
47
53
/* TODO make this dynamic as needed */
48
54
static const int MAX_MSG_LEN= 32*1024;
49
55
 
159
165
  public drizzled::plugin::Logging
160
166
{
161
167
 
162
 
  const std::string _host;
163
 
  const std::string _function;
 
168
  std::string sysvar_host;
 
169
  std::string sysvar_function;
164
170
 
165
171
  int _gearman_client_ok;
166
172
  gearman_client_st _gearman_client;
173
179
  LoggingGearman(const std::string &host,
174
180
                 const std::string &function) :
175
181
    drizzled::plugin::Logging("gearman_query_log"),
176
 
    _host(host),
177
 
    _function(function),
 
182
    sysvar_host(host),
 
183
    sysvar_function(function),
178
184
    _gearman_client_ok(0),
179
185
    _gearman_client()
180
186
  {
269
275
    char job_handle[GEARMAN_JOB_HANDLE_SIZE];
270
276
  
271
277
    (void) gearman_client_do_background(&_gearman_client,
272
 
                                        _function.c_str(),
 
278
                                        sysvar_function.c_str(),
273
279
                                        NULL,
274
280
                                        (void *) msgbuf.get(),
275
281
                                        (size_t) msgbuf_len,
277
283
  
278
284
    return false;
279
285
  }
 
286
  
 
287
  /**
 
288
   * This function changes the current gearman host to the parameter passed to the function.
 
289
   *
 
290
   * @return True on success, False on error.
 
291
   */
 
292
  bool setHost(std::string &new_host)
 
293
  {
 
294
    gearman_return_t tmp_ret;
 
295
    
 
296
    /*
 
297
      New server is added to the list of servers using gearman_client_add_server.
 
298
      If the call does not result in error, then all the servers are removed from the list and
 
299
      new server only is added. This is done to ensure that a bad server url does not replace
 
300
      the existing server url.
 
301
      TODO Create a new instance of gearman_client_st and add the new server in it. If success, release the
 
302
      old gearman_client_st and use new instance of gearman_client_st everywhere.
 
303
    */
 
304
    tmp_ret= gearman_client_add_server(&_gearman_client,
 
305
                                   new_host.c_str(), 0);
 
306
    if (tmp_ret != GEARMAN_SUCCESS)
 
307
    {
 
308
      drizzled::errmsg_printf(drizzled::error::ERROR, _("fail gearman_client_add_server(): %s"),
 
309
                              gearman_client_error(&_gearman_client));
 
310
      return false;
 
311
    }
 
312
 
 
313
    gearman_client_remove_servers(&_gearman_client);
 
314
    gearman_client_add_server(&_gearman_client, new_host.c_str(), 0);
 
315
    sysvar_host= new_host;
 
316
    return true;
 
317
  }
 
318
 
 
319
  /**
 
320
   * This function changes the gearman function with the new one.
 
321
   *
 
322
   * @return True on success (as we dont have to do anything except updating the function string, this always return true.)
 
323
   */
 
324
  bool setFunction(std::string &new_function)
 
325
  {
 
326
    sysvar_function= new_function;
 
327
    return true;
 
328
  }
 
329
  
 
330
  /**
 
331
   * Getter for host
 
332
   *
 
333
   * @return sysvar_host
 
334
   */
 
335
  std::string& getHost()
 
336
  {
 
337
    return sysvar_host;
 
338
  }
 
339
  
 
340
  /**
 
341
   * Getter for function
 
342
   *
 
343
   * @return sysvar_function
 
344
   */
 
345
  std::string& getFunction()
 
346
  {
 
347
    return sysvar_function;
 
348
  }
280
349
};
281
350
 
282
351
static LoggingGearman *handler= NULL;
283
352
 
284
 
static int logging_gearman_plugin_init(drizzled::module::Context &context)
 
353
/**
 
354
 * This function is called when the value of gearman host is updated dynamically from the drizzle server
 
355
 *
 
356
 * @return False on success, True on error
 
357
 */
 
358
bool updateHost(Session *, set_var* var)
 
359
{
 
360
  if (not var->value->str_value.empty())
 
361
  {
 
362
    std::string newHost(var->value->str_value.data());
 
363
    if (handler->setHost(newHost))
 
364
      return false; //success
 
365
    else
 
366
      return true; // error
 
367
  }
 
368
  errmsg_printf(error::ERROR, _("logging_gearman_host cannot be NULL"));
 
369
  return true; // error
 
370
}
 
371
 
 
372
/**
 
373
 * This function is called when the value of gearman function is updated dynamically
 
374
 *
 
375
 * @return False on error, True on success
 
376
 */
 
377
bool updateFunction(Session *, set_var* var)
 
378
{
 
379
  if (not var->value->str_value.empty())
 
380
  {
 
381
    std::string newFunction(var->value->str_value.data());
 
382
    if (handler->setFunction(newFunction))
 
383
      return false; //success
 
384
    else
 
385
      return true; // error
 
386
  }
 
387
  errmsg_printf(error::ERROR, _("logging_gearman_function cannot be NULL"));
 
388
  return true; // error
 
389
}
 
390
 
 
391
 
 
392
static int init(drizzled::module::Context &context)
285
393
{
286
394
  const drizzled::module::option_map &vm= context.getOptions();
287
395
 
288
396
  handler= new LoggingGearman(vm["host"].as<std::string>(),
289
397
                              vm["function"].as<std::string>());
290
398
  context.add(handler);
291
 
  context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
292
 
  context.registerVariable(new drizzled::sys_var_const_string_val("function", vm["function"].as<std::string>()));
 
399
  context.registerVariable(new sys_var_std_string("host", handler->getHost(), NULL, &updateHost));
 
400
  context.registerVariable(new sys_var_std_string("function", handler->getFunction(), NULL, &updateFunction));
293
401
 
294
402
  return 0;
295
403
}
304
412
          _("Gearman Function to send logging to"));
305
413
}
306
414
 
 
415
} /* namespace logging_gearman */
307
416
} /* namespace drizzle_plugin */
308
417
 
309
418
DRIZZLE_DECLARE_PLUGIN
314
423
  "Mark Atwood",
315
424
  N_("Logs queries to a Gearman server"),
316
425
  drizzled::PLUGIN_LICENSE_GPL,
317
 
  drizzle_plugin::logging_gearman_plugin_init,
 
426
  drizzle_plugin::logging_gearman::init,
318
427
  NULL,
319
 
  drizzle_plugin::init_options
 
428
  drizzle_plugin::logging_gearman::init_options
320
429
}
321
430
DRIZZLE_DECLARE_PLUGIN_END;