1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
4
* Copyright (C) 2008 Sun Microsystems, Inc.
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
26
26
#include <algorithm>
34
vector<plugin::ErrorMessage *> all_errmsg_handler;
35
bool errmsg_has= false;
32
std::vector<plugin::ErrorMessage *> all_errmsg_handler;
38
34
bool plugin::ErrorMessage::addPlugin(plugin::ErrorMessage *handler)
40
36
all_errmsg_handler.push_back(handler);
45
void plugin::ErrorMessage::removePlugin(plugin::ErrorMessage *handler)
40
void plugin::ErrorMessage::removePlugin(plugin::ErrorMessage *)
47
all_errmsg_handler.erase(find(all_errmsg_handler.begin(),
48
all_errmsg_handler.end(), handler));
42
all_errmsg_handler.clear();
52
class Print : public unary_function<plugin::ErrorMessage *, bool>
46
class Print : public std::unary_function<plugin::ErrorMessage *, bool>
48
error::level_t priority;
56
49
const char *format;
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)
53
Print(error::level_t priority_arg,
54
const char *format_arg, va_list ap_arg) :
55
std::unary_function<plugin::ErrorMessage *, bool>(),
56
priority(priority_arg), format(format_arg)
67
61
~Print() { va_end(ap); }
71
65
va_list handler_ap;
72
66
va_copy(handler_ap, ap);
73
if (handler->errmsg(session, priority, format, handler_ap))
67
if (handler->errmsg(priority, format, handler_ap))
75
69
/* we're doing the errmsg plugin api,
76
70
so we can't trust the errmsg api to emit our error messages
92
bool plugin::ErrorMessage::vprintf(Session *session, int priority,
93
char const *format, va_list ap)
86
bool plugin::ErrorMessage::vprintf(error::level_t priority, char const *format, va_list ap)
96
/* check to see if any errmsg plugin has been loaded
97
if not, just fall back to emitting the message to stderr */
90
Check to see if any errmsg plugin has been loaded
91
if not, just fall back to emitting the message to stderr.
93
if (not all_errmsg_handler.size())
100
95
/* if it turns out that the vfprintf doesnt do one single write
101
96
(single writes are atomic), then this needs to be rewritten to
102
97
vsprintf into a char buffer, and then write() that char buffer
104
99
vfprintf(stderr, format, ap);
108
104
/* Use find_if instead of foreach so that we can collect return codes */
109
vector<plugin::ErrorMessage *>::iterator iter=
110
find_if(all_errmsg_handler.begin(), all_errmsg_handler.end(),
111
Print(session, priority, format, ap));
105
std::vector<plugin::ErrorMessage *>::iterator iter=
106
std::find_if(all_errmsg_handler.begin(), all_errmsg_handler.end(),
107
Print(priority, format, ap));
112
109
/* If iter is == end() here, that means that all of the plugins returned
113
110
* false, which in this case means they all succeeded. Since we want to
114
111
* return false on success, we return the value of the two being !=