~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/logging.cc

  • Committer: Brian Aker
  • Date: 2009-04-27 14:36:40 UTC
  • Revision ID: brian@gaz-20090427143640-f6zjmtt9vm55qgm2
Patch on show processlist from  davi@apache.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Mark Atwood
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
1
20
#include <drizzled/server_includes.h>
2
21
#include <drizzled/logging.h>
3
 
 
4
 
int logging_initializer(st_plugin_int *plugin)
5
 
{
6
 
  logging_t *p;
7
 
 
8
 
  fprintf(stderr, "MRA %s plugin:%s dl:%s\n",
9
 
          __func__, plugin->name.str, plugin->plugin_dl->dl.str);
10
 
 
11
 
  p= (logging_t *) malloc(sizeof(logging_t));
12
 
  if (p == NULL) return 1;
13
 
  memset(p, 0, sizeof(logging_t));
14
 
 
15
 
  plugin->data= (void *)p;
16
 
 
17
 
  if (plugin->plugin->init)
18
 
  {
19
 
    if (plugin->plugin->init((void *)p))
20
 
    {
21
 
      sql_print_error("Logging plugin '%s' init function returned error.",
22
 
                      plugin->name.str);
23
 
      goto err;
24
 
    }
25
 
  }
26
 
  return 0;
27
 
 
28
 
err:
29
 
  free(p);
30
 
  return 1;
31
 
}
32
 
 
33
 
int logging_finalizer(st_plugin_int *plugin)
34
 
35
 
  logging_t *p = (logging_t *) plugin->data;
36
 
 
37
 
  fprintf(stderr, "MRA %s plugin:%s dl:%s\n",
38
 
          __func__, plugin->name.str, plugin->plugin_dl->dl.str);
39
 
 
40
 
  if (plugin->plugin->deinit)
41
 
  {
42
 
    if (plugin->plugin->deinit((void *)p))
43
 
    {
44
 
      sql_print_error("Logging plugin '%s' deinit function returned error.",
45
 
                      plugin->name.str);
46
 
    }
47
 
  }
48
 
 
49
 
  if (p) free(p);
50
 
 
51
 
  return 0;
52
 
}
53
 
 
54
 
static bool logging_pre_iterate (THD *thd, plugin_ref plugin,
55
 
                                 void *stuff __attribute__ ((__unused__)))
56
 
{
57
 
  logging_t *l= plugin_data(plugin, logging_t *);
58
 
 
59
 
  if (l && l->logging_pre)
60
 
  {
61
 
    if (l->logging_pre(thd))
62
 
      return true;
63
 
  }
64
 
  return false;
65
 
}
66
 
 
67
 
void logging_pre_do (THD *thd)
68
 
{
69
 
  if (plugin_foreach(thd, logging_pre_iterate, DRIZZLE_LOGGER_PLUGIN, NULL))
70
 
  {
71
 
    sql_print_error("Logging plugin pre had an error.");
72
 
  }
73
 
  return;
74
 
}
75
 
 
76
 
static bool logging_post_iterate (THD *thd, plugin_ref plugin, 
77
 
                                  void *stuff __attribute__ ((__unused__)))
78
 
{
79
 
  logging_t *l= plugin_data(plugin, logging_t *);
80
 
 
81
 
  if (l && l->logging_post)
82
 
  {
83
 
    if (l->logging_post(thd))
84
 
      return true;
85
 
  }
86
 
  return false;
87
 
}
88
 
 
89
 
void logging_post_do (THD *thd)
90
 
{
91
 
  if (plugin_foreach(thd, logging_post_iterate, DRIZZLE_LOGGER_PLUGIN, NULL))
92
 
  {
93
 
    sql_print_error("Logging plugin post had an error.");
94
 
  }
95
 
  return;
 
22
#include <drizzled/gettext.h>
 
23
#include "drizzled/plugin_registry.h"
 
24
 
 
25
#include <vector>
 
26
 
 
27
using namespace std;
 
28
 
 
29
static vector<Logging_handler *> all_loggers;
 
30
 
 
31
void add_logger(Logging_handler *handler)
 
32
{
 
33
  if (handler != NULL)
 
34
    all_loggers.push_back(handler);
 
35
}
 
36
 
 
37
void remove_logger(Logging_handler *handler)
 
38
{
 
39
  if (handler != NULL)
 
40
    all_loggers.erase(find(all_loggers.begin(), all_loggers.end(), handler));
 
41
}
 
42
 
 
43
 
 
44
class LoggingPreIterate : public unary_function<Logging_handler *, bool>
 
45
{
 
46
  Session *session;
 
47
public:
 
48
  LoggingPreIterate(Session *session_arg) :
 
49
    unary_function<Logging_handler *, bool>(),
 
50
    session(session_arg) {}
 
51
 
 
52
  inline result_type operator()(argument_type handler)
 
53
  {
 
54
    if (handler->pre(session))
 
55
    {
 
56
      /* TRANSLATORS: The leading word "logging" is the name
 
57
         of the plugin api, and so should not be translated. */
 
58
      errmsg_printf(ERRMSG_LVL_ERROR,
 
59
                    _("logging '%s' pre() failed"),
 
60
                    handler->getName().c_str());
 
61
      return true;
 
62
    }
 
63
    return false;
 
64
  }
 
65
};
 
66
 
 
67
 
 
68
class LoggingPostIterate : public unary_function<Logging_handler *, bool>
 
69
{
 
70
  Session *session;
 
71
public:
 
72
  LoggingPostIterate(Session *session_arg) :
 
73
    unary_function<Logging_handler *, bool>(),
 
74
    session(session_arg) {}
 
75
 
 
76
  /* This gets called once for each loaded logging plugin */
 
77
  inline result_type operator()(argument_type handler)
 
78
  {
 
79
    if (handler->post(session))
 
80
    {
 
81
      /* TRANSLATORS: The leading word "logging" is the name
 
82
         of the plugin api, and so should not be translated. */
 
83
      errmsg_printf(ERRMSG_LVL_ERROR,
 
84
                    _("logging '%s' post() failed"),
 
85
                    handler->getName().c_str());
 
86
      return true;
 
87
    }
 
88
    return false;
 
89
  }
 
90
};
 
91
 
 
92
 
 
93
/* This is the logging_pre_do entry point.
 
94
   This gets called by the rest of the Drizzle server code */
 
95
bool logging_pre_do (Session *session)
 
96
{
 
97
  /* Use find_if instead of foreach so that we can collect return codes */
 
98
  vector<Logging_handler *>::iterator iter=
 
99
    find_if(all_loggers.begin(), all_loggers.end(),
 
100
            LoggingPreIterate(session)); 
 
101
  /* If iter is == end() here, that means that all of the plugins returned
 
102
   * false, which in this case means they all succeeded. Since we want to 
 
103
   * return false on success, we return the value of the two being != 
 
104
   */
 
105
  return iter != all_loggers.end();
 
106
}
 
107
 
 
108
/* This is the logging_post_do entry point.
 
109
   This gets called by the rest of the Drizzle server code */
 
110
bool logging_post_do (Session *session)
 
111
{
 
112
  /* Use find_if instead of foreach so that we can collect return codes */
 
113
  vector<Logging_handler *>::iterator iter=
 
114
    find_if(all_loggers.begin(), all_loggers.end(),
 
115
            LoggingPreIterate(session)); 
 
116
  /* If iter is == end() here, that means that all of the plugins returned
 
117
   * false, which in this case means they all succeeded. Since we want to 
 
118
   * return false on success, we return the value of the two being != 
 
119
   */
 
120
  return iter != all_loggers.end();
96
121
}