1 /*
2
3 Copyright (C) 2010 Alex Andreotti <alex.andreotti@gmail.com>
4
5 This file is part of chmc.
6
7 chmc is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 chmc is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with chmc. If not, see <http://www.gnu.org/licenses/>.
19
20 */
21 #include "err.h"
22
23 #include <stdarg.h>
24 #include <assert.h>
25
26 struct chmcErr
27 {
28 int code;
29 char msg[CHMC_ERRMAXLEN+1];
30 };
31
32 static struct chmcErr chmc_err = {
33 CHMC_NOERR,
34 '\0',
35 };
36
chmcerr_clean(void)37 void chmcerr_clean(void) {
38 chmc_err.code = CHMC_NOERR;
39 chmc_err.msg[0] = '\0';
40 }
41
chmcerr_code(void)42 int chmcerr_code(void) {
43 return chmc_err.code;
44 }
45
chmcerr_message(void)46 const char *chmcerr_message( void ) {
47 return chmc_err.msg;
48 }
49
chmcerr_set(int code,const char * fmt,...)50 void chmcerr_set(int code, const char *fmt, ...)
51 {
52 int len;
53 va_list ap;
54
55 chmc_err.code = code;
56
57 va_start(ap, fmt);
58
59 len = vsnprintf(chmc_err.msg, CHMC_ERRMAXLEN, fmt, ap);
60 if (len == CHMC_ERRMAXLEN)
61 chmc_err.msg[CHMC_ERRMAXLEN] = '\0';
62
63 assert(len <= CHMC_ERRMAXLEN);
64
65 va_end(ap);
66 }
67