~drizzle-trunk/drizzle/development

499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
499.2.10 by Mark Atwood
add editor format hints, and other useful metadata comments
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
3
 *
1010 by Brian Aker
Replacing Sun employee copyright headers (aka... anything done by a Sun
4
 *  Copyright (C) 2008 Sun Microsystems
499.2.10 by Mark Atwood
add editor format hints, and other useful metadata comments
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
383.6.1 by Mark Atwood
add pluggable logging
20
#include <drizzled/server_includes.h>
21
#include <drizzled/logging.h>
549 by Monty Taylor
Took gettext.h out of header files.
22
#include <drizzled/gettext.h>
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
23
#include "drizzled/plugin_registry.h"
24
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
25
#include <vector>
26
27
using namespace std;
28
968.2.34 by Monty Taylor
Made all_logging static - not that it matters since it's gonna become a member variable one day.
29
static vector<Logging_handler *> all_loggers;
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
30
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
31
void add_logger(Logging_handler *handler)
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
32
{
971.1.74 by Monty Taylor
Added a null trap.
33
  if (handler != NULL)
34
    all_loggers.push_back(handler);
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
35
}
36
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
37
void remove_logger(Logging_handler *handler)
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
38
{
971.1.74 by Monty Taylor
Added a null trap.
39
  if (handler != NULL)
40
    all_loggers.erase(find(all_loggers.begin(), all_loggers.end(), handler));
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
41
}
383.6.1 by Mark Atwood
add pluggable logging
42
958.1.1 by Monty Taylor
Made logging plugin class based.
43
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
44
class LoggingPreIterate : public unary_function<Logging_handler *, bool>
383.6.1 by Mark Atwood
add pluggable logging
45
{
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
46
  Session *session;
47
public:
48
  LoggingPreIterate(Session *session_arg) :
49
    unary_function<Logging_handler *, bool>(),
50
    session(session_arg) {}
383.6.1 by Mark Atwood
add pluggable logging
51
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
52
  inline result_type operator()(argument_type handler)
383.6.1 by Mark Atwood
add pluggable logging
53
  {
958.1.1 by Monty Taylor
Made logging plugin class based.
54
    if (handler->pre(session))
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
55
    {
56
      /* TRANSLATORS: The leading word "logging" is the name
57
         of the plugin api, and so should not be translated. */
958.1.1 by Monty Taylor
Made logging plugin class based.
58
      errmsg_printf(ERRMSG_LVL_ERROR,
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
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
968.2.34 by Monty Taylor
Made all_logging static - not that it matters since it's gonna become a member variable one day.
76
  /* This gets called once for each loaded logging plugin */
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
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
383.6.1 by Mark Atwood
add pluggable logging
92
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
93
/* This is the logging_pre_do entry point.
94
   This gets called by the rest of the Drizzle server code */
520.1.22 by Brian Aker
Second pass of thd cleanup
95
bool logging_pre_do (Session *session)
383.6.1 by Mark Atwood
add pluggable logging
96
{
968.2.34 by Monty Taylor
Made all_logging static - not that it matters since it's gonna become a member variable one day.
97
  /* Use find_if instead of foreach so that we can collect return codes */
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
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.
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
109
   This gets called by the rest of the Drizzle server code */
520.1.22 by Brian Aker
Second pass of thd cleanup
110
bool logging_post_do (Session *session)
383.6.1 by Mark Atwood
add pluggable logging
111
{
968.2.34 by Monty Taylor
Made all_logging static - not that it matters since it's gonna become a member variable one day.
112
  /* Use find_if instead of foreach so that we can collect return codes */
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
113
  vector<Logging_handler *>::iterator iter=
114
    find_if(all_loggers.begin(), all_loggers.end(),
1039.4.1 by Mark Atwood
fix stuppid logging bug
115
            LoggingPostIterate(session)); 
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
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();
383.6.1 by Mark Atwood
add pluggable logging
121
}