-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherror_handler.c
More file actions
86 lines (77 loc) · 2.34 KB
/
error_handler.c
File metadata and controls
86 lines (77 loc) · 2.34 KB
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
// Pithesiser - a software synthesiser for Raspberry Pi
// Copyright (C) 2015 Nicholas Tuckett
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
* error_handler.c
*
* Created on: 26 Dec 2013
* Author: ntuckett
*/
#include "error_handler.h"
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <libconfig.h>
#include "logging.h"
#define MAX_ERROR_MSG_LEN 1024
#define MAX_ERROR_STACK 16
static int error_stack[MAX_ERROR_STACK];
static const char* error_msg_stack[MAX_ERROR_STACK];
static int error_stack_head = 0;
void push_last_error()
{
if (error_stack_head < MAX_ERROR_STACK)
{
error_msg_stack[error_stack_head] = NULL;
error_stack[error_stack_head++] = errno;
}
}
void push_custom_error(const char* message)
{
if (error_stack_head < MAX_ERROR_STACK)
{
error_msg_stack[error_stack_head] = message;
error_stack[error_stack_head++] = 0;
}
}
void pop_error_report(const char* user_message)
{
if (error_stack_head > 0)
{
char error_msg[MAX_ERROR_MSG_LEN];
error_stack_head--;
if (error_stack[error_stack_head] != 0)
{
strerror_r(error_stack[error_stack_head], error_msg, MAX_ERROR_MSG_LEN);
LOG_ERROR("%s: %s", user_message, error_msg);
}
else
{
LOG_ERROR("%s: %s", user_message, error_msg_stack[error_stack_head]);
}
}
}
#define MAX_MESSAGE_BUFFER_LEN 4096
void setting_error_report(config_setting_t *setting, const char* message, ...)
{
char message_buffer[MAX_MESSAGE_BUFFER_LEN];
va_list arg_list;
va_start(arg_list, message);
vsnprintf(message_buffer, MAX_MESSAGE_BUFFER_LEN, message, arg_list);
va_end(arg_list);
message_buffer[MAX_MESSAGE_BUFFER_LEN - 1] = 0;
LOG_ERROR("%s at line %d in %s", message_buffer, config_setting_source_line(setting), config_setting_source_file(setting));
}