~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/error_message.cc

  • Committer: Marisa Plumb
  • Date: 2010-12-04 02:38:29 UTC
  • mto: This revision was merged to the branch mainline in revision 1984.
  • Revision ID: marisa.plumb@gmail.com-20101204023829-2khzxh30wxi256db
updates to a few sql docs 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
 
20
#include "config.h"
 
21
#include "drizzled/plugin/error_message.h"
21
22
 
22
 
#include <drizzled/error.h>
23
 
#include <drizzled/gettext.h>
24
 
#include <drizzled/plugin/error_message.h>
 
23
#include "drizzled/gettext.h"
25
24
 
26
25
#include <cstdio>
27
26
#include <algorithm>
28
27
#include <vector>
29
28
 
 
29
using namespace std;
 
30
 
30
31
namespace drizzled
31
32
{
32
33
 
33
 
std::vector<plugin::ErrorMessage *> all_errmsg_handler;
 
34
vector<plugin::ErrorMessage *> all_errmsg_handler;
 
35
bool errmsg_has= false;
 
36
 
34
37
 
35
38
bool plugin::ErrorMessage::addPlugin(plugin::ErrorMessage *handler)
36
39
{
37
40
  all_errmsg_handler.push_back(handler);
 
41
  errmsg_has= true;
38
42
  return false;
39
43
}
40
44
 
41
 
void plugin::ErrorMessage::removePlugin(plugin::ErrorMessage *)
 
45
void plugin::ErrorMessage::removePlugin(plugin::ErrorMessage *handler)
42
46
{
43
 
  all_errmsg_handler.clear();
 
47
  all_errmsg_handler.erase(find(all_errmsg_handler.begin(),
 
48
                                all_errmsg_handler.end(), handler));
44
49
}
45
50
 
46
51
 
47
 
class Print : public std::unary_function<plugin::ErrorMessage *, bool>
 
52
class Print : public unary_function<plugin::ErrorMessage *, bool>
48
53
{
49
 
  error::level_t priority;
 
54
  Session *session;
 
55
  int priority;
50
56
  const char *format;
51
57
  va_list ap;
52
 
 
53
58
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
 
  }
 
59
  Print(Session *session_arg, int priority_arg,
 
60
        const char *format_arg, va_list ap_arg)
 
61
    : unary_function<plugin::ErrorMessage *, bool>(), session(session_arg),
 
62
      priority(priority_arg), format(format_arg)
 
63
    {
 
64
      va_copy(ap, ap_arg);
 
65
    }
61
66
 
62
67
  ~Print()  { va_end(ap); }
63
68
 
65
70
  {
66
71
    va_list handler_ap;
67
72
    va_copy(handler_ap, ap);
68
 
    if (handler->errmsg(priority, format, handler_ap))
 
73
    if (handler->errmsg(session, priority, format, handler_ap))
69
74
    {
70
75
      /* we're doing the errmsg plugin api,
71
76
         so we can't trust the errmsg api to emit our error messages
84
89
}; 
85
90
 
86
91
 
87
 
bool plugin::ErrorMessage::vprintf(error::level_t priority, char const *format, va_list ap)
 
92
bool plugin::ErrorMessage::vprintf(Session *session, int priority,
 
93
                                   char const *format, va_list ap)
88
94
{
89
 
  if (not (priority >= error::verbosity()))
90
 
    return false;
91
95
 
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())
 
96
  /* check to see if any errmsg plugin has been loaded
 
97
     if not, just fall back to emitting the message to stderr */
 
98
  if (!errmsg_has)
97
99
  {
98
100
    /* if it turns out that the vfprintf doesnt do one single write
99
101
       (single writes are atomic), then this needs to be rewritten to
100
102
       vsprintf into a char buffer, and then write() that char buffer
101
103
       to stderr */
102
 
      vfprintf(stderr, format, ap);
103
 
      fputc('\n', stderr);
 
104
    vfprintf(stderr, format, ap);
104
105
    return false;
105
106
  }
106
107
 
107
108
  /* 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
 
 
 
109
  vector<plugin::ErrorMessage *>::iterator iter=
 
110
    find_if(all_errmsg_handler.begin(), all_errmsg_handler.end(),
 
111
            Print(session, priority, format, ap)); 
112
112
  /* If iter is == end() here, that means that all of the plugins returned
113
113
   * false, which in this case means they all succeeded. Since we want to 
114
114
   * return false on success, we return the value of the two being !=