~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/logging.cc

  • Committer: Brian Aker
  • Date: 2009-04-07 20:09:30 UTC
  • mfrom: (971.1.17 mordred)
  • Revision ID: brian@gaz-20090407200930-27jkul7lkwkjs2to
Merge from Monty

Show diffs side-by-side

added added

removed removed

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