Skip to content
Navigation Menu
{{ message }}
forked from alsa-project/alsa-lib
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherror.c
More file actions
558 lines (504 loc) · 15.4 KB
/
Copy patherror.c
File metadata and controls
558 lines (504 loc) · 15.4 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
* \file error.c
* \brief Error code handling routines
* \author Jaroslav Kysela <perex@perex.cz>
* \date 1998-2001
*
* Error code handling routines.
*/
/*
* Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
*
* snd_strerror routine needs to be recoded for the locale support
*
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "local.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
static void snd_lib_error_default(const char *file, int line, const char *function, int errcode, const char *fmt, ...);
/**
* Array of error codes in US ASCII.
*/
static const char *snd_error_codes[] =
{
"Sound protocol is not compatible"
};
/**
* \brief Returns the message for an error code.
* \param errnum The error code number, which must be a system error code
* or an ALSA error code.
* \return The ASCII description of the given numeric error code.
*/
const char *snd_strerror(int errnum)
{
if (errnum < 0)
errnum = -errnum;
if (errnum < SND_ERROR_BEGIN)
return (const char *) strerror(errnum);
errnum -= SND_ERROR_BEGIN;
if ((unsigned int) errnum >= sizeof(snd_error_codes) / sizeof(const char *))
return "Unknown error";
return snd_error_codes[errnum];
}
#ifndef DOC_HIDDEN
#ifdef HAVE___THREAD
#define TLS_PFX __thread
#else
#define TLS_PFX /* NOP */
#endif
#endif
static TLS_PFX snd_lib_log_handler_t local_log = NULL;
static TLS_PFX snd_local_error_handler_t local_error = NULL;
/**
* \brief Install local log handler
* \param func The local log handler function
* \retval Previous local log handler function
*/
snd_lib_log_handler_t snd_lib_log_set_local(snd_lib_log_handler_t func)
{
snd_lib_log_handler_t old = local_log;
local_log = func;
return old;
}
/**
* Array of log priority level names.
*/
static const char *snd_log_prio_names[SND_LOG_LAST + 1] = {
[0] = NULL,
[SND_LOG_ERROR] = "error",
[SND_LOG_WARN] = "warning",
[SND_LOG_INFO] = "info",
[SND_LOG_DEBUG] = "debug",
[SND_LOG_TRACE] = "trace",
};
/**
* Array of interface names.
*/
static const char *snd_ilog_interface_names[SND_ILOG_LAST + 1] = {
[0] = NULL,
[SND_ILOG_CORE] = "core",
[SND_ILOG_CONFIG] = "config",
[SND_ILOG_CONTROL] = "control",
[SND_ILOG_HWDEP] = "hwdep",
[SND_ILOG_TIMER] = "timer",
[SND_ILOG_RAWMIDI] = "rawmidi",
[SND_ILOG_PCM] = "pcm",
[SND_ILOG_MIXER] = "mixer",
[SND_ILOG_SEQUENCER] = "sequencer",
[SND_ILOG_UCM] = "ucm",
[SND_ILOG_TOPOLOGY] = "topology",
[SND_ILOG_ASERVER] = "aserver",
[SND_ILOG_PCM_PARAMS] = "pcmpar",
};
/**
* \brief Function to convert log priority level to text.
* \param prio Priority value (SND_LOG_*).
* \return The textual representation of the priority level, or NULL if invalid.
*/
const char *snd_lib_log_priority(int prio)
{
if (prio >= 0 && prio <= SND_LOG_LAST)
return snd_log_prio_names[prio];
return NULL;
}
/**
* \brief Function to convert interface code to text.
* \param interface Interface (SND_ILOG_*).
* \return The textual representation of the interface code, or NULL if invalid.
*/
const char *snd_lib_log_interface(int interface)
{
if (interface >= 0 && interface <= SND_ILOG_LAST)
return snd_ilog_interface_names[interface];
return NULL;
}
/**
* \brief Structure to hold parsed debug configuration.
*/
static struct {
const char *configstr;
int global_level;
int interface_levels[SND_ILOG_LAST + 1];
int parsed;
} debug_config;
/**
* \brief Parse the LIBASOUND_DEBUG environment variable.
*
* Format: [<level>][,<interface1>:<level1>][,<interface2>:<level2>,...]
*
* Examples:
* "debug" - Set global level to debug
* "3" - Set global level to 3 (info)
* "info,pcm:debug" - Set global to info, pcm to debug
* "error,mixer:5,pcm:4" - Set global to error, mixer to 5 (trace), pcm to 4 (debug)
*/
static void parse_libasound_debug(const char *configstr)
{
const char *env;
char *str, *token, *saveptr;
int i;
if (debug_config.parsed && debug_config.configstr == configstr)
return;
debug_config.parsed = 1;
debug_config.global_level = 0;
debug_config.configstr = configstr;
for (i = 0; i <= SND_ILOG_LAST; i++)
debug_config.interface_levels[i] = 0;
if (configstr == NULL) {
env = getenv("LIBASOUND_DEBUG");
if (!env || !*env)
return;
} else {
env = configstr;
}
str = strdup(env);
if (!str)
return;
token = strtok_r(str, ",", &saveptr);
while (token) {
char *colon = strchr(token, ':');
if (colon) {
/* interface:level format */
*colon = '\0';
const char *interface_name = token;
const char *level_str = colon + 1;
int interface_num = -1;
int level = -1;
/* Try to find interface by name */
for (i = 1; i <= SND_ILOG_LAST; i++) {
if (snd_ilog_interface_names[i] &&
strcmp(snd_ilog_interface_names[i], interface_name) == 0) {
interface_num = i;
break;
}
}
/* If not found by name, try direct number */
if (interface_num < 0) {
char *endptr;
long val = strtol(interface_name, &endptr, 10);
if (*endptr == '\0' && val >= 0 && val <= SND_ILOG_LAST)
interface_num = val;
}
/* Parse level */
for (i = 1; i <= SND_LOG_LAST; i++) {
if (snd_log_prio_names[i] &&
strcmp(snd_log_prio_names[i], level_str) == 0) {
level = i;
break;
}
}
/* If not found by name, try direct number */
if (level < 0) {
char *endptr;
long val = strtol(level_str, &endptr, 10);
if (*endptr == '\0' && val >= 0 && val <= SND_LOG_LAST)
level = val;
}
/* Store the interface-specific level */
if (interface_num > 0 && level > 0)
debug_config.interface_levels[interface_num] = level;
} else {
/* Global level only */
int level = -1;
/* Try to find level by name */
for (i = 1; i <= SND_LOG_LAST; i++) {
if (snd_log_prio_names[i] &&
strcmp(snd_log_prio_names[i], token) == 0) {
level = i;
break;
}
}
/* If not found by name, try direct number */
if (level < 0) {
char *endptr;
long val = strtol(token, &endptr, 10);
if (*endptr == '\0' && val >= 0 && val <= SND_LOG_LAST)
level = val;
}
if (level > 0)
debug_config.global_level = level;
}
token = strtok_r(NULL, ",", &saveptr);
}
free(str);
}
/**
* \brief Check if a log message should be shown based on LIBASOUND_DEBUG.
* \param prio Priority value (SND_LOG_*).
* \param interface Interface (SND_ILOG_*).
* \param configstr Configuration string (usually LIBASOUND_DEBUG environment variable)
* \return 1 if the message should be shown, 0 otherwise.
*/
int snd_lib_log_filter(int prio, int interface, const char *configstr)
{
unsigned int level;
parse_libasound_debug(configstr);
if (interface > 0 && interface <= SND_ILOG_LAST && debug_config.interface_levels[interface] > 0) {
level = debug_config.interface_levels[interface];
} else {
level = debug_config.global_level;
}
if (level == 0)
level = SND_LOG_ERROR;
/* Show message if its priority is less than or equal to the configured level */
return prio <= (int)level;
}
static void snd_lib_error_vdefault(const char *file, int line, const char *function, int errcode, const char *fmt, va_list arg);
/**
* \brief The default log handler function.
* \param prio Priority value (SND_LOG_*).
* \param interface Interface (SND_ILOG_*).
* \param file The filename where the error was hit.
* \param line The line number.
* \param function The function name.
* \param errcode The error code.
* \param fmt The message (including the format characters).
* \param ... Optional arguments.
*
* If a local error function has been installed for the current thread by
* \ref snd_lib_log_set_local, it is called. Otherwise, prints the error
* message including location to \c stderr.
*/
static void snd_lib_vlog_default(int prio, int interface, const char *file, int line, const char *function, int errcode, const char *fmt, va_list arg)
{
const char *text1, *text2;
if (local_log) {
local_log(prio, interface, file, line, function, errcode, fmt, arg);
return;
}
if (local_error && prio == SND_LOG_ERROR) {
local_error(file, line, function, errcode, fmt, arg);
return;
}
if (snd_lib_error != snd_lib_error_default) {
if (prio == SND_LOG_ERROR)
snd_lib_error_vdefault(file, line, function, errcode, fmt, arg);
/* ignore other priorities - restore old behaviour */
return;
}
if (!snd_lib_log_filter(prio, interface, NULL))
return;
fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
text1 = snd_lib_log_priority(prio);
text2 = snd_lib_log_interface(interface);
if (text1 || text2)
fprintf(stderr, "[%s.%s] ", text1 ? text1 : "", text2 ? text2 : "");
vfprintf(stderr, fmt, arg);
if (errcode)
fprintf(stderr, ": %s", snd_strerror(errcode));
putc('\n', stderr);
}
/**
* \brief Root log handler function.
* \param prio Priority value (SND_LOG_*).
* \param interface Interface (SND_ILOG_*).
* \param file The filename where the error was hit.
* \param line The line number.
* \param function The function name.
* \param errcode The error code.
* \param fmt The message (including the format characters).
* \param ... Optional arguments.
*/
void snd_lib_log(int prio, int interface, const char *file, int line, const char *function, int errcode, const char *fmt, ...)
{
va_list arg;
va_start(arg, fmt);
snd_lib_vlog(prio, interface, file, line, function, errcode, fmt, arg);
va_end(arg);
}
/**
* \brief The check point function.
* \param interface Interface (SND_ILOG_*).
* \param file The filename where the error was hit.
* \param line The line number.
* \param function The function name.
* \param errcode The error code.
* \param fmt The message (including the format characters).
* \param ... Optional arguments.
*
* The error message is passed with error priority level to snd_lib_vlog handler.
*/
void snd_lib_check(int interface, const char *file, int line, const char *function, int errcode, const char *fmt, ...)
{
const char *verbose;
va_list arg;
va_start(arg, fmt);
verbose = getenv("LIBASOUND_DEBUG");
if (! verbose || ! *verbose)
goto finish;
snd_lib_vlog(SND_LOG_ERROR, interface, file, line, function, errcode, fmt, arg);
#ifdef ALSA_DEBUG_ASSERT
verbose = getenv("LIBASOUND_DEBUG_ASSERT");
if (verbose && *verbose)
assert(0);
#endif
finish:
va_end(arg);
}
/**
* \ingroup Error
* Pointer to the error handler function.
* For internal use only.
*/
snd_lib_log_handler_t snd_lib_vlog = snd_lib_vlog_default;
/**
* \brief Sets the log handler.
* \param handler The pointer to the new log handler function.
* \retval Previous log handler function
*
* This function sets a new log handler, or (if \c handler is \c NULL)
* the default one which prints the error messages to \c stderr.
*/
snd_lib_log_handler_t snd_lib_log_set_handler(snd_lib_log_handler_t handler)
{
snd_lib_log_handler_t old = snd_lib_vlog;
snd_lib_vlog = handler == NULL ? snd_lib_vlog_default : handler;
return old;
}
/**
* \brief Install local error handler
* \param func The local error handler function
* \retval Previous local error handler function
* \deprecated Since 1.2.15
*/
snd_local_error_handler_t snd_lib_error_set_local(snd_local_error_handler_t func)
{
snd_local_error_handler_t old = local_error;
local_error = func;
return old;
}
#ifndef DOC_HIDDEN
link_warning(snd_lib_error_set_local, "Warning: snd_lib_error_set_local is deprecated, use snd_lib_log_set_local");
#endif
/**
* \brief The default error handler function.
* \param file The filename where the error was hit.
* \param line The line number.
* \param function The function name.
* \param errcode The error code.
* \param fmt The message (including the format characters).
* \param ... Optional arguments.
* \deprecated Since 1.2.15
*
* Use snd_lib_vlog handler to print error message for anonymous interface.
*/
static void snd_lib_error_default(const char *file, int line, const char *function, int errcode, const char *fmt, ...)
{
va_list arg;
va_start(arg, fmt);
snd_lib_vlog(SND_LOG_ERROR, 0, file, line, function, errcode, fmt, arg);
va_end(arg);
}
/**
* \brief The default error handler function.
* \param file The filename where the error was hit.
* \param line The line number.
* \param function The function name.
* \param errcode The error code.
* \param fmt The message (including the format characters).
* \param arg Optional arguments.
* \deprecated Since 1.2.15
*
* Use snd_lib_vlog handler to print error message for anonymous interface.
*/
static void snd_lib_error_vdefault(const char *file, int line, const char *function, int errcode, const char *fmt, va_list arg)
{
char msg[512];
vsnprintf(msg, sizeof(msg), fmt, arg);
snd_lib_error(file, line, function, errcode, "%s", msg);
}
/**
* \ingroup Error
* \deprecated Since 1.2.15
* Pointer to the error handler function.
* For internal use only.
*/
snd_lib_error_handler_t snd_lib_error = snd_lib_error_default;
#ifndef DOC_HIDDEN
link_warning(snd_lib_error, "Warning: snd_lib_error is deprecated, use snd_log interface");
#endif
/**
* \brief Sets the error handler.
* \param handler The pointer to the new error handler function.
* \deprecated Since 1.2.15
*
* This function sets a new error handler, or (if \c handler is \c NULL)
* the default one which prints the error messages to \c stderr.
*/
int snd_lib_error_set_handler(snd_lib_error_handler_t handler)
{
snd_lib_error = handler == NULL ? snd_lib_error_default : handler;
return 0;
}
/**
* \brief Returns the ALSA sound library version in ASCII format
* \return The ASCII description of the used ALSA sound library.
*/
const char *snd_asoundlib_version(void)
{
return SND_LIB_VERSION_STR;
}
/**
* \brief Copy a C-string into a sized buffer
* \param dst Where to copy the string to
* \param src Where to copy the string from
* \param size Size of destination buffer
* \retval The source string length
*
* The result is always a valid NUL-terminated string that fits
* in the buffer (unless, of course, the buffer size is zero).
* It does not pad out the result like strncpy() does.
*/
size_t snd_strlcpy(char *dst, const char *src, size_t size)
{
size_t ret = strlen(src);
if (size) {
size_t len = ret >= size ? size - 1 : ret;
memcpy(dst, src, len);
dst[len] = '\0';
}
return ret;
}
/**
* \brief Append a C-string into a sized buffer
* \param dst Where to append the string to
* \param src Where to copy the string from
* \param size Size of destination buffer
* \retval The total string length (no trimming)
*
* The result is always a valid NUL-terminated string that fits
* in the buffer (unless, of course, the buffer size is zero).
* It does not pad out the result.
*/
size_t snd_strlcat(char *dst, const char *src, size_t size)
{
size_t dst_len = strlen(dst);
size_t len = strlen(src);
size_t ret = dst_len + len;
if (dst_len < size) {
dst += dst_len;
size -= dst_len;
if (len >= size)
len = size - 1;
memcpy(dst, src, len);
dst[len] = '\0';
}
return ret;
}
You can’t perform that action at this time.
