summaryrefslogtreecommitdiffstats
path: root/utils/mirror_vfd/mirror_remote.c
blob: 81a3625ef3f24ea5ea174d715ff2563a792881bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* Common operations for "remote" processes for the Mirror VFD.
 *
 * Jacob Smith, 2020-03-06
 */

#include "mirror_remote.h"

#ifdef H5_HAVE_MIRROR_VFD


/* ---------------------------------------------------------------------------
 * Function:    mirror_log
 *
 * Purpose:     Write message to the logging stream/file.
 *              If logging info pointer is NULL, uses logging defaults.
 * ----------------------------------------------------------------------------
 */
void
mirror_log(struct mirror_log_info *info,
           unsigned int            level,
           const char             *format,
           ...)
{
    FILE *stream = MIRROR_LOG_DEFAULT_STREAM;
    unsigned int verbosity = MIRROR_LOG_DEFAULT_VERBOSITY;
    hbool_t      custom = FALSE;

    if (info != NULL && info->magic == MIRROR_LOG_INFO_MAGIC) {
        stream = info->stream;
        verbosity = info->verbosity;
        custom = TRUE;
    }

    if (level == V_NONE) {
        return;
    }
    else
    if (level <= verbosity) {
        if (custom == TRUE && info->prefix[0] != '\0') {
            HDfprintf(stream, "%s", info->prefix);
        }

        switch (level) {
        case (V_ERR) :
            HDfprintf(stream, "ERROR ");
            break;
        case (V_WARN) :
            HDfprintf(stream, "WARNING ");
            break;
        default:
            break;
        }

        if (format != NULL) {
            va_list args;
            HDva_start(args, format);
            HDvfprintf(stream, format, args);
            HDva_end(args);
        }

        HDfprintf(stream, "\n");
        HDfflush(stream);
    } /* end if sufficiently verbose to print */
} /* end mirror_log() */


/* ---------------------------------------------------------------------------
 * Function:    session_log_bytes
 *
 * Purpose:     "Pretty-print" raw binary data to logging stream/file.
 *              If info pointer is NULL, uses logging defaults.
 * ----------------------------------------------------------------------------
 */
void
mirror_log_bytes(struct mirror_log_info *info,
                 unsigned int            level,
                 size_t                  n_bytes,
                 const unsigned char    *buf)
{
    FILE *stream = MIRROR_LOG_DEFAULT_STREAM;
    unsigned int verbosity = MIRROR_LOG_DEFAULT_VERBOSITY;

    if (buf == NULL) {
        return;
    }

    if (info != NULL && info->magic == MIRROR_LOG_INFO_MAGIC) {
        stream = info->stream;
        verbosity = info->verbosity;
    }

    if (level <= verbosity) {
        size_t bytes_written = 0;
        const unsigned char *b = NULL;

        /* print whole lines */
        while ((n_bytes - bytes_written) >= 32) {
            b = buf + bytes_written; /* point to region in buffer */
            HDfprintf(stream,
                    "%04zX  %02X%02X%02X%02X %02X%02X%02X%02X" \
                          " %02X%02X%02X%02X %02X%02X%02X%02X" \
                          " %02X%02X%02X%02X %02X%02X%02X%02X" \
                          " %02X%02X%02X%02X %02X%02X%02X%02X\n",
                    bytes_written,
                    b[0], b[1], b[2], b[3],   b[4], b[5], b[6], b[7],
                    b[8], b[9], b[10],b[11],  b[12],b[13],b[14],b[15],
                    b[16],b[17],b[18],b[19],  b[20],b[21],b[22],b[23],
                    b[24],b[25],b[26],b[27],  b[28],b[29],b[30],b[31]);
            bytes_written += 32;
        }

        /* start partial line */
        if (n_bytes > bytes_written) {
            HDfprintf(stream, "%04zX ", bytes_written);
        }

        /* partial line blocks */
        while ((n_bytes - bytes_written) >= 4) {
            HDfprintf(stream, " %02X%02X%02X%02X",
                    buf[bytes_written],   buf[bytes_written+1],
                    buf[bytes_written+2], buf[bytes_written+3]);
            bytes_written += 4;
        }

        /* block separator before partial block */
        if (n_bytes > bytes_written) {
            HDfprintf(stream, " ");
        }

        /* partial block individual bytes */
        while (n_bytes > bytes_written) {
            HDfprintf(stream, "%02X", buf[bytes_written++]);
        }

        /* end partial line */
        HDfprintf(stream, "\n");
    } /* end if suitably verbose to log */
} /* end mirror_log_bytes() */


/* ---------------------------------------------------------------------------
 * Function:    mirror_log_init
 *
 * Purpose:     Prepare a loginfo_t structure for use.
 *
 * Return:      Success: Pointer to newly-ceated info.
 *              Failure: NULL. Either unable to allocate or cannot open file.
 * ----------------------------------------------------------------------------
 */
loginfo_t *
mirror_log_init(char *path, char *prefix, unsigned int verbosity)
{
    loginfo_t *info = NULL;

    info = (loginfo_t *)HDmalloc(sizeof(loginfo_t));
    if (info != NULL) {
        info->magic = MIRROR_LOG_INFO_MAGIC;
        info->verbosity = verbosity;
        info->stream = MIRROR_LOG_DEFAULT_STREAM;
        info->prefix[0] = '\0';

        if (prefix && *prefix) {
            HDstrncpy(info->prefix, prefix, MIRROR_LOG_PREFIX_MAX);
        }

        if (path && *path) {
            FILE *f = NULL;
            f = HDfopen(path, "w");
            if (NULL == f) {
                HDfprintf(MIRROR_LOG_DEFAULT_STREAM,
                        "WARN custom logging path could not be opened: %s\n",
                        path);
                info->magic += 1;
                HDfree(info);
            }
            else {
                info->stream = f;
            }
        }

    } /* end if able to allocate */

    return info;
} /* end mirror_log_init() */


/* ---------------------------------------------------------------------------
 * Function:    mirror_log_term
 *
 * Purpose:     Shut down and clean up a loginfo_t structure.
 *
 * Return:      Success: SUCCEED. Resources released.
 *              Failure: FAIL.    Indeterminite state.
 * ----------------------------------------------------------------------------
 */
herr_t
mirror_log_term(loginfo_t *info)
{
    if (info == NULL || info->magic != MIRROR_LOG_INFO_MAGIC) {
        return FAIL;
    }
    if (info->stream != stderr || info->stream != stdout) {
        if (HDfclose(info->stream) < 0) {
            return FAIL;
        }
    }
    info->magic += 1;
    HDfree(info);
    return SUCCEED;
} /* end mirror_log_term() */

#endif /* H5_HAVE_MIRROR_VFD */