~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/error_message.cc

  • Committer: Mark Atwood
  • Date: 2008-10-03 01:39:40 UTC
  • mto: This revision was merged to the branch mainline in revision 437.
  • Revision ID: mark@fallenpegasus.com-20081003013940-mvefjo725dltz41h
rename logging_noop to logging_query

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, Inc.
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
 
 
22
 
#include <drizzled/error.h>
23
 
#include <drizzled/gettext.h>
24
 
#include <drizzled/plugin/error_message.h>
25
 
 
26
 
#include <cstdio>
27
 
#include <algorithm>
28
 
#include <vector>
29
 
 
30
 
namespace drizzled
31
 
{
32
 
 
33
 
std::vector<plugin::ErrorMessage *> all_errmsg_handler;
34
 
 
35
 
bool plugin::ErrorMessage::addPlugin(plugin::ErrorMessage *handler)
36
 
{
37
 
  all_errmsg_handler.push_back(handler);
38
 
  return false;
39
 
}
40
 
 
41
 
void plugin::ErrorMessage::removePlugin(plugin::ErrorMessage *)
42
 
{
43
 
  all_errmsg_handler.clear();
44
 
}
45
 
 
46
 
 
47
 
class Print : public std::unary_function<plugin::ErrorMessage *, bool>
48
 
{
49
 
  error::level_t priority;
50
 
  const char *format;
51
 
  va_list ap;
52
 
 
53
 
public:
54
 
  Print(error::level_t priority_arg,
55
 
        const char *format_arg, va_list ap_arg) : 
56
 
    std::unary_function<plugin::ErrorMessage *, bool>(),
57
 
    priority(priority_arg), format(format_arg)
58
 
  {
59
 
    va_copy(ap, ap_arg);
60
 
  }
61
 
 
62
 
  ~Print()  { va_end(ap); }
63
 
 
64
 
  inline result_type operator()(argument_type handler)
65
 
  {
66
 
    va_list handler_ap;
67
 
    va_copy(handler_ap, ap);
68
 
    if (handler->errmsg(priority, format, handler_ap))
69
 
    {
70
 
      /* we're doing the errmsg plugin api,
71
 
         so we can't trust the errmsg api to emit our error messages
72
 
         so we will emit error messages to stderr */
73
 
      /* TRANSLATORS: The leading word "errmsg" is the name
74
 
         of the plugin api, and so should not be translated. */
75
 
      fprintf(stderr,
76
 
              _("errmsg plugin '%s' errmsg() failed"),
77
 
              handler->getName().c_str());
78
 
      va_end(handler_ap);
79
 
      return true;
80
 
    }
81
 
    va_end(handler_ap);
82
 
    return false;
83
 
  }
84
 
}; 
85
 
 
86
 
 
87
 
bool plugin::ErrorMessage::vprintf(error::level_t priority, char const *format, va_list ap)
88
 
{
89
 
  if (not (priority >= error::verbosity()))
90
 
    return false;
91
 
 
92
 
  /* 
93
 
    Check to see if any errmsg plugin has been loaded
94
 
    if not, just fall back to emitting the message to stderr.
95
 
  */
96
 
  if (not all_errmsg_handler.size())
97
 
  {
98
 
    /* if it turns out that the vfprintf doesnt do one single write
99
 
       (single writes are atomic), then this needs to be rewritten to
100
 
       vsprintf into a char buffer, and then write() that char buffer
101
 
       to stderr */
102
 
      vfprintf(stderr, format, ap);
103
 
      fputc('\n', stderr);
104
 
    return false;
105
 
  }
106
 
 
107
 
  /* Use find_if instead of foreach so that we can collect return codes */
108
 
  std::vector<plugin::ErrorMessage *>::iterator iter=
109
 
    std::find_if(all_errmsg_handler.begin(), all_errmsg_handler.end(),
110
 
                 Print(priority, format, ap)); 
111
 
 
112
 
  /* If iter is == end() here, that means that all of the plugins returned
113
 
   * false, which in this case means they all succeeded. Since we want to 
114
 
   * return false on success, we return the value of the two being != 
115
 
   */
116
 
  return iter != all_errmsg_handler.end();
117
 
}
118
 
 
119
 
} /* namespace drizzled */