~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/logging.cc

  • Committer: Monty Taylor
  • Date: 2008-10-16 09:12:23 UTC
  • mto: (511.1.6 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016091223-17ngih0qu9vssjs3
We pass -Wunused-macros now!

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 Sun Microsystems
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
 
 
20
 
#include "config.h"
21
 
#include "drizzled/plugin/logging.h"
22
 
#include "drizzled/gettext.h"
23
 
#include "drizzled/errmsg_print.h"
24
 
 
25
 
#include <vector>
26
 
#include <algorithm>
27
 
 
28
 
class Session;
29
 
 
30
 
using namespace std;
31
 
 
32
 
namespace drizzled
33
 
{
34
 
 
35
 
vector<plugin::Logging *> all_loggers;
36
 
 
37
 
 
38
 
bool plugin::Logging::addPlugin(plugin::Logging *handler)
39
 
{
40
 
  if (handler != NULL)
41
 
    all_loggers.push_back(handler);
42
 
  return false;
43
 
}
44
 
 
45
 
void plugin::Logging::removePlugin(plugin::Logging *handler)
46
 
{
47
 
  if (handler != NULL)
48
 
    all_loggers.erase(find(all_loggers.begin(), all_loggers.end(), handler));
49
 
}
50
 
 
51
 
 
52
 
class PreIterate : public unary_function<plugin::Logging *, bool>
53
 
{
54
 
  Session *session;
55
 
public:
56
 
  PreIterate(Session *session_arg) :
57
 
    unary_function<plugin::Logging *, bool>(),
58
 
    session(session_arg) {}
59
 
 
60
 
  inline result_type operator()(argument_type handler)
61
 
  {
62
 
    if (handler->pre(session))
63
 
    {
64
 
      /* TRANSLATORS: The leading word "logging" is the name
65
 
         of the plugin api, and so should not be translated. */
66
 
      errmsg_printf(ERRMSG_LVL_ERROR,
67
 
                    _("logging '%s' pre() failed"),
68
 
                    handler->getName().c_str());
69
 
      return true;
70
 
    }
71
 
    return false;
72
 
  }
73
 
};
74
 
 
75
 
 
76
 
class PostIterate : public unary_function<plugin::Logging *, bool>
77
 
{
78
 
  Session *session;
79
 
public:
80
 
  PostIterate(Session *session_arg) :
81
 
    unary_function<plugin::Logging *, bool>(),
82
 
    session(session_arg) {}
83
 
 
84
 
  /* This gets called once for each loaded logging plugin */
85
 
  inline result_type operator()(argument_type handler)
86
 
  {
87
 
    if (handler->post(session))
88
 
    {
89
 
      /* TRANSLATORS: The leading word "logging" is the name
90
 
         of the plugin api, and so should not be translated. */
91
 
      errmsg_printf(ERRMSG_LVL_ERROR,
92
 
                    _("logging '%s' post() failed"),
93
 
                    handler->getName().c_str());
94
 
      return true;
95
 
    }
96
 
    return false;
97
 
  }
98
 
};
99
 
 
100
 
class PostEndIterate : public unary_function<plugin::Logging *, bool>
101
 
{
102
 
  Session *session;
103
 
public:
104
 
  PostEndIterate(Session *session_arg) :
105
 
    unary_function<plugin::Logging *, bool>(),
106
 
    session(session_arg) {}
107
 
 
108
 
  /* This gets called once for each loaded logging plugin */
109
 
  inline result_type operator()(argument_type handler)
110
 
  {
111
 
    if (handler->postEnd(session))
112
 
    {
113
 
      /* TRANSLATORS: The leading word "logging" is the name
114
 
         of the plugin api, and so should not be translated. */
115
 
      errmsg_printf(ERRMSG_LVL_ERROR,
116
 
                    _("logging '%s' postEnd() failed"),
117
 
                    handler->getName().c_str());
118
 
      return true;
119
 
    }
120
 
    return false;
121
 
  }
122
 
};
123
 
 
124
 
/* This is the Logging::preDo entry point.
125
 
   This gets called by the rest of the Drizzle server code */
126
 
bool plugin::Logging::preDo(Session *session)
127
 
{
128
 
  /* Use find_if instead of foreach so that we can collect return codes */
129
 
  vector<plugin::Logging *>::iterator iter=
130
 
    find_if(all_loggers.begin(), all_loggers.end(),
131
 
            PreIterate(session)); 
132
 
  /* If iter is == end() here, that means that all of the plugins returned
133
 
   * false, which in this case means they all succeeded. Since we want to 
134
 
   * return false on success, we return the value of the two being != 
135
 
   */
136
 
  return iter != all_loggers.end();
137
 
}
138
 
 
139
 
/* This is the Logging::postDo entry point.
140
 
   This gets called by the rest of the Drizzle server code */
141
 
bool plugin::Logging::postDo(Session *session)
142
 
{
143
 
  /* Use find_if instead of foreach so that we can collect return codes */
144
 
  vector<plugin::Logging *>::iterator iter=
145
 
    find_if(all_loggers.begin(), all_loggers.end(),
146
 
            PostIterate(session)); 
147
 
  /* If iter is == end() here, that means that all of the plugins returned
148
 
   * false, which in this case means they all succeeded. Since we want to 
149
 
   * return false on success, we return the value of the two being != 
150
 
   */
151
 
  return iter != all_loggers.end();
152
 
}
153
 
 
154
 
/* This gets called in the session destructor */
155
 
bool plugin::Logging::postEndDo(Session *session)
156
 
{
157
 
  /* Use find_if instead of foreach so that we can collect return codes */
158
 
  vector<plugin::Logging *>::iterator iter=
159
 
    find_if(all_loggers.begin(), all_loggers.end(),
160
 
            PostEndIterate(session));
161
 
  /* If iter is == end() here, that means that all of the plugins returned
162
 
   * false, which in this case means they all succeeded. Since we want to
163
 
   * return false on success, we return the value of the two being !=
164
 
   */
165
 
  return iter != all_loggers.end();
166
 
}
167
 
 
168
 
} /* namespace drizzled */
 
1
#include <drizzled/server_includes.h>
 
2
#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;
 
96
}