aboutsummaryrefslogtreecommitdiff
path: root/lib/error_handler.c
blob: 292fa6fc95dc55ddf60e2f118f0410f8b1d1f9ff (plain) (blame)
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
#include "spm.h"

int spmerrno = 0;
static char spmerrbuf[255];
static char spmerrbuf_reason[255];

void spmerrno_cause(const char *reason) {
    char *buf = spmerrbuf_reason;
    sprintf(buf, " (%s)", reason);
    return;
}
/**
 *
 * @param code
 * @return
 */
char *spm_strerror(int code) {
    char *buf = spmerrbuf;
    int is_spm_error = SPM_ERR_CONFIRM(code);

    memset(buf, '\0', sizeof(spmerrbuf));
    if (is_spm_error == 0) {
        strcpy(buf, strerror(code));
    } else {
        strcpy(buf, SPM_ERR_STRING[SPM_ERR_INDEX(code)]);
    }

    if (strlen(spmerrbuf_reason)) {
        strcat(buf, spmerrbuf_reason);
    }
    return buf;
}

void spm_perror(const char *msg) {
    fprintf(stderr, "%s: %s\n", msg ? msg : "", spm_strerror(spmerrno));
}