]> sigrok.org Git - libsigrok.git/blame - error.c
Doxygen: Initial groups and topic short descriptions.
[libsigrok.git] / error.c
CommitLineData
b5a8e848
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "libsigrok.h"
22
7b870c38
UH
23/**
24 * @defgroup grp_error Error handling
25 *
26 * Error handling in libsigrok.
27 *
28 * @{
29 */
30
b5a8e848
UH
31/**
32 * Return a human-readable error string for the given libsigrok error code.
33 *
34 * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
35 * @return A const string containing a short, human-readable (English)
36 * description of the error, such as "memory allocation error".
37 * The string must NOT be free'd by the caller!
38 * @see sr_strerror_name
39 */
40SR_API const char *sr_strerror(int error_code)
41{
42 const char *str;
43
44 /*
45 * Note: All defined SR_* error macros from libsigrok.h should have
46 * an entry in this function, as well as in sr_strerror_name().
47 */
48
49 switch (error_code) {
50 case SR_OK:
51 str = "no error";
52 break;
53 case SR_ERR:
54 str = "generic/unspecified error";
55 break;
56 case SR_ERR_MALLOC:
57 str = "memory allocation error";
58 break;
59 case SR_ERR_BUG:
60 str = "internal error";
61 break;
62 case SR_ERR_SAMPLERATE:
63 str = "invalid samplerate";
64 break;
65 default:
66 str = "unknown error";
67 break;
68 }
69
70 return str;
71}
72
73/**
74 * Return the "name" string of the given libsigrok error code.
75 *
76 * For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC",
77 * the name of the SR_OK code is "SR_OK", and so on.
78 *
79 * This function can be used for various purposes where the "name" string of
80 * a libsigrok error code is useful.
81 *
82 * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
83 * @return A const string containing the "name" of the error code as string.
84 * The string must NOT be free'd by the caller!
85 * @see sr_strerror
86 */
87SR_API const char *sr_strerror_name(int error_code)
88{
89 const char *str;
90
91 /*
92 * Note: All defined SR_* error macros from libsigrok.h should have
93 * an entry in this function, as well as in sr_strerror().
94 */
95
96 switch (error_code) {
97 case SR_OK:
98 str = "SR_OK";
99 break;
100 case SR_ERR:
101 str = "SR_ERR";
102 break;
103 case SR_ERR_MALLOC:
104 str = "SR_ERR_MALLOC";
105 break;
106 case SR_ERR_BUG:
107 str = "SR_ERR_BUG";
108 break;
109 case SR_ERR_SAMPLERATE:
110 str = "SR_ERR_SAMPLERATE";
111 break;
112 default:
113 str = "unknown error code";
114 break;
115 }
116
117 return str;
118}
7b870c38
UH
119
120/** @} */