This source file includes following definitions.
- init
- finalize
- mylog
- sev2str
- write_local
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include "pmix_config.h"
22 #include "pmix_common.h"
23
24 #include <string.h>
25 #ifdef HAVE_TIME_H
26 #include <time.h>
27 #endif
28 #ifdef HAVE_SYS_TIME_H
29 #include <sys/time.h>
30 #endif
31 #ifdef HAVE_SYSLOG_H
32 #include <syslog.h>
33 #endif
34 #include <stdarg.h>
35
36 #include "src/util/argv.h"
37 #include "src/util/error.h"
38 #include "src/util/name_fns.h"
39 #include "src/util/show_help.h"
40 #include "src/mca/bfrops/bfrops.h"
41 #include "src/server/pmix_server_ops.h"
42
43 #include "src/mca/plog/base/base.h"
44 #include "plog_syslog.h"
45
46
47
48 static pmix_status_t init(void);
49 static void finalize(void);
50 static pmix_status_t mylog(const pmix_proc_t *source,
51 const pmix_info_t data[], size_t ndata,
52 const pmix_info_t directives[], size_t ndirs,
53 pmix_op_cbfunc_t cbfunc, void *cbdata);
54
55
56 pmix_plog_module_t pmix_plog_syslog_module = {
57 .name = "syslog",
58 .init = init,
59 .finalize = finalize,
60 .log = mylog
61 };
62
63
64 static pmix_status_t init(void)
65 {
66 int opts;
67 char *mychannels = "lsys,gsys,syslog,local_syslog,global_syslog";
68
69 pmix_plog_syslog_module.channels = pmix_argv_split(mychannels, ',');
70
71 opts = LOG_CONS | LOG_PID;
72 openlog("PMIx Log Report:", opts, LOG_USER);
73
74 return PMIX_SUCCESS;
75 }
76
77 static void finalize(void)
78 {
79 closelog();
80 pmix_argv_free(pmix_plog_syslog_module.channels);
81 }
82
83 static pmix_status_t write_local(const pmix_proc_t *source,
84 time_t timestamp,
85 int severity, char *msg,
86 const pmix_info_t *data, size_t ndata);
87
88
89 static pmix_status_t mylog(const pmix_proc_t *source,
90 const pmix_info_t data[], size_t ndata,
91 const pmix_info_t directives[], size_t ndirs,
92 pmix_op_cbfunc_t cbfunc, void *cbdata)
93 {
94 size_t n;
95 int pri = mca_plog_syslog_component.level;
96 pmix_status_t rc;
97 time_t timestamp = 0;
98
99
100 if (NULL == data || 0 == ndata) {
101 return PMIX_ERR_NOT_AVAILABLE;
102 }
103
104
105 if (NULL != directives) {
106 for (n=0; n < ndirs; n++) {
107 if (0 == strncmp(directives[n].key, PMIX_LOG_SYSLOG_PRI, PMIX_MAX_KEYLEN)) {
108 pri = directives[n].value.data.integer;
109 } else if (0 == strncmp(directives[n].key, PMIX_LOG_TIMESTAMP, PMIX_MAX_KEYLEN)) {
110 timestamp = directives[n].value.data.time;
111 }
112 }
113 }
114
115
116 for (n=0; n < ndata; n++) {
117 if (0 == strncmp(data[n].key, PMIX_LOG_SYSLOG, PMIX_MAX_KEYLEN)) {
118
119 rc = write_local(source, timestamp, pri, data[n].value.data.string, data, ndata);
120 if (PMIX_SUCCESS == rc) {
121
122 PMIX_INFO_OP_COMPLETED(&data[n]);
123 }
124 } else if (0 == strncmp(data[n].key, PMIX_LOG_LOCAL_SYSLOG, PMIX_MAX_KEYLEN)) {
125 rc = write_local(source, timestamp, pri, data[n].value.data.string, data, ndata);
126 if (PMIX_SUCCESS == rc) {
127
128 PMIX_INFO_OP_COMPLETED(&data[n]);
129 }
130 } else if (0 == strncmp(data[n].key, PMIX_LOG_GLOBAL_SYSLOG, PMIX_MAX_KEYLEN)) {
131
132 if (PMIX_PROC_IS_GATEWAY(pmix_globals.mypeer)) {
133 rc = write_local(source, timestamp, pri, data[n].value.data.string, data, ndata);
134 if (PMIX_SUCCESS == rc) {
135
136 PMIX_INFO_OP_COMPLETED(&data[n]);
137 }
138 }
139 }
140 }
141
142 return PMIX_SUCCESS;
143 }
144
145 static char* sev2str(int severity)
146 {
147 switch (severity) {
148 case LOG_EMERG:
149 return "EMERGENCY";
150 case LOG_ALERT:
151 return "ALERT";
152 case LOG_CRIT:
153 return "CRITICAL";
154 case LOG_ERR:
155 return "ERROR";
156 case LOG_WARNING:
157 return "WARNING";
158 case LOG_NOTICE:
159 return "NOTICE";
160 case LOG_INFO:
161 return "INFO";
162 case LOG_DEBUG:
163 return "DEBUG";
164 default:
165 return "UNKNOWN SEVERITY";
166 }
167 }
168
169 static pmix_status_t write_local(const pmix_proc_t *source,
170 time_t timestamp,
171 int severity, char *msg,
172 const pmix_info_t *data, size_t ndata)
173 {
174 char tod[48], *datastr, *tmp, *tmp2;
175 pmix_status_t rc;
176 size_t n;
177
178 pmix_output_verbose(5, pmix_plog_base_framework.framework_output,
179 "plog:syslog:mylog function called with severity %d", severity);
180
181 if (0 < timestamp) {
182
183 (void)ctime_r(×tamp, tod);
184
185 tod[strlen(tod)] = '\0';
186 }
187
188 if (NULL == data) {
189 syslog(severity, "%s [%s:%d]%s PROC %s:%d REPORTS: %s",
190 tod, pmix_globals.myid.nspace, pmix_globals.myid.rank,
191 sev2str(severity),
192 source->nspace, source->rank,
193 (NULL == msg) ? "<N/A>" : msg);
194 } else {
195
196
197 if (NULL == msg) {
198 datastr = strdup("\n");
199 } else {
200 if (0 > asprintf(&datastr, "%s", msg)) {
201 return PMIX_ERR_NOMEM;
202 }
203 }
204 for (n=0; n < ndata; n++) {
205 PMIX_BFROPS_PRINT(rc, pmix_globals.mypeer,
206 &tmp, "\t", (pmix_info_t*)&data[n], PMIX_INFO);
207 if (PMIX_SUCCESS != rc) {
208 free(datastr);
209 return rc;
210 }
211 if (0 > asprintf(&tmp2, "%s\n%s", datastr, tmp)) {
212 free(datastr);
213 return PMIX_ERR_NOMEM;
214 }
215 free(datastr);
216 free(tmp);
217 datastr = tmp2;
218 }
219
220 syslog(severity, "%s [%s:%d]%s PROC %s:%d REPORTS: %s",
221 tod, pmix_globals.myid.nspace, pmix_globals.myid.rank,
222 sev2str(severity), source->nspace, source->rank, datastr);
223 free(datastr);
224 }
225
226 return PMIX_SUCCESS;
227 }