1 /*
2 Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "ndb_log.h"
26
27 // Use log.h until there is a plugin API which provides printing to error log
28 // without polluting the message with it's own hardcoded string and without
29 // need to pass in a MYSQL_PLUGIN pointer. Presumably 'my_plugin_log_service'
30 // can be extended with a my_log_message(level, prefix, message, ...) function
31 #include "log.h"
32
33
34 /*
35 Print message to MySQL Server's error log(s)
36
37 @param loglevel Selects the loglevel used when
38 printing the message to log.
39 @param prefix Prefix to be used in front of the message in
40 addition to "NDB" i.e "NDB <prefix>:"
41 @param[in] fmt printf-like format string
42 @param[in] ap Arguments
43
44 */
45
46 void
ndb_log_print(enum ndb_log_loglevel loglevel,const char * prefix,const char * fmt,va_list args)47 ndb_log_print(enum ndb_log_loglevel loglevel,
48 const char* prefix, const char* fmt, va_list args)
49 {
50 DBUG_ASSERT(fmt);
51
52 // Assemble the message
53 char msg_buf[512];
54 (void)my_vsnprintf(msg_buf, sizeof(msg_buf), fmt, args);
55
56 // Print message to MySQL error log
57 switch (loglevel)
58 {
59 case NDB_LOG_ERROR_LEVEL:
60 if (prefix)
61 sql_print_error("NDB %s: %s", prefix, msg_buf);
62 else
63 sql_print_error("NDB: %s", msg_buf);
64 break;
65 case NDB_LOG_WARNING_LEVEL:
66 if (prefix)
67 sql_print_warning("NDB %s: %s", prefix, msg_buf);
68 else
69 sql_print_warning("NDB: %s", msg_buf);
70 break;
71 case NDB_LOG_INFORMATION_LEVEL:
72 if (prefix)
73 sql_print_information("NDB %s: %s", prefix, msg_buf);
74 else
75 sql_print_information("NDB: %s", msg_buf);
76 break;
77 }
78 }
79
80
81 void
ndb_log_info(const char * fmt,...)82 ndb_log_info(const char* fmt, ...)
83 {
84 DBUG_ASSERT(fmt);
85
86 va_list args;
87 va_start(args, fmt);
88 ndb_log_print(NDB_LOG_INFORMATION_LEVEL, NULL, fmt, args);
89 va_end(args);
90 }
91
92
93 void
ndb_log_warning(const char * fmt,...)94 ndb_log_warning(const char* fmt, ...)
95 {
96 DBUG_ASSERT(fmt);
97
98 va_list args;
99 va_start(args, fmt);
100 ndb_log_print(NDB_LOG_WARNING_LEVEL, NULL, fmt, args);
101 va_end(args);
102 }
103
104
105 void
ndb_log_error(const char * fmt,...)106 ndb_log_error(const char* fmt, ...)
107 {
108 DBUG_ASSERT(fmt);
109
110 va_list args;
111 va_start(args, fmt);
112 ndb_log_print(NDB_LOG_ERROR_LEVEL, NULL, fmt, args);
113 va_end(args);
114 }
115
116
117 // the verbose level is currently controlled by "ndb_extra_logging"
118 extern ulong opt_ndb_extra_logging;
119
120 unsigned
ndb_log_get_verbose_level(void)121 ndb_log_get_verbose_level(void)
122 {
123 return opt_ndb_extra_logging;
124 }
125
126
127 void
ndb_log_verbose(unsigned verbose_level,const char * fmt,...)128 ndb_log_verbose(unsigned verbose_level, const char* fmt, ...)
129 {
130 DBUG_ASSERT(fmt);
131
132 // Print message only if verbose level is set high enough
133 if (ndb_log_get_verbose_level() < verbose_level)
134 return;
135
136 va_list args;
137 va_start(args, fmt);
138 ndb_log_print(NDB_LOG_INFORMATION_LEVEL, NULL, fmt, args);
139 va_end(args);
140 }
141
142