2020-08-24 13:38:27 +08:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
|
|
|
* Copyright(c) 2020 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* Author: Karol Trzcinski <karolx.trzcinski@linux.intel.com>
|
|
|
|
*/
|
|
|
|
|
2020-08-31 20:15:35 +08:00
|
|
|
#include "convert.h"
|
2020-08-24 14:08:44 +08:00
|
|
|
#include <ctype.h>
|
2020-08-24 13:38:27 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-12-01 20:32:37 +08:00
|
|
|
char *log_vasprintf(const char *format, va_list args)
|
2020-08-24 13:38:27 +08:00
|
|
|
{
|
|
|
|
va_list args_copy;
|
|
|
|
int size;
|
|
|
|
char localbuf[1];
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
va_copy(args_copy, args);
|
|
|
|
size = vsnprintf(localbuf, 1, format, args_copy);
|
|
|
|
va_end(args_copy);
|
|
|
|
|
|
|
|
result = malloc(size + 1);
|
|
|
|
if (result)
|
|
|
|
vsnprintf(result, size + 1, format, args);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-12-01 20:32:37 +08:00
|
|
|
char *log_asprintf(const char *format, ...)
|
2020-08-24 13:38:27 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
va_start(args, format);
|
2021-12-01 20:32:37 +08:00
|
|
|
result = log_vasprintf(format, args);
|
2020-08-24 13:38:27 +08:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-19 23:57:34 +08:00
|
|
|
/** Prints 1. once to stderr. 2. a second time to the global out_fd if
|
|
|
|
* out_fd is neither stderr nor stdout (because the -o option was used).
|
|
|
|
*/
|
2020-08-31 20:15:35 +08:00
|
|
|
void log_err(const char *fmt, ...)
|
2020-08-24 13:38:27 +08:00
|
|
|
{
|
2020-10-19 23:32:48 +08:00
|
|
|
FILE *out_fd = global_config ? global_config->out_fd : NULL;
|
2020-08-24 13:38:27 +08:00
|
|
|
static const char prefix[] = "error: ";
|
|
|
|
ssize_t needed_size;
|
|
|
|
va_list args, args_alloc;
|
|
|
|
char *buff;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
2021-04-19 23:57:34 +08:00
|
|
|
/* Print into buff first, then outputs buff twice */
|
2020-08-24 13:38:27 +08:00
|
|
|
va_copy(args_alloc, args);
|
|
|
|
needed_size = vsnprintf(NULL, 0, fmt, args_alloc) + 1;
|
|
|
|
buff = malloc(needed_size);
|
|
|
|
va_end(args_alloc);
|
|
|
|
|
|
|
|
if (buff) {
|
|
|
|
vsprintf(buff, fmt, args);
|
|
|
|
fprintf(stderr, "%s%s", prefix, buff);
|
|
|
|
|
|
|
|
/* take care about out_fd validity and duplicated logging */
|
|
|
|
if (out_fd && out_fd != stderr && out_fd != stdout) {
|
|
|
|
fprintf(out_fd, "%s%s", prefix, buff);
|
|
|
|
fflush(out_fd);
|
|
|
|
}
|
|
|
|
free(buff);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s", prefix);
|
|
|
|
vfprintf(stderr, fmt, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
2020-08-24 14:08:44 +08:00
|
|
|
|
|
|
|
/* trim whitespaces from string begin */
|
|
|
|
char *ltrim(char *s)
|
|
|
|
{
|
2022-02-03 01:35:45 +08:00
|
|
|
while (isspace((int)*s))
|
2020-08-24 14:08:44 +08:00
|
|
|
s++;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* trim whitespaces from string end */
|
|
|
|
char *rtrim(char *s)
|
|
|
|
{
|
|
|
|
char *ptr = s + strlen(s) - 1;
|
|
|
|
|
2022-02-03 01:35:45 +08:00
|
|
|
while (ptr >= s && isspace((int)*ptr)) {
|
2020-08-24 14:08:44 +08:00
|
|
|
*ptr = '\0';
|
|
|
|
--ptr;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* trim whitespaces from string begin and end*/
|
|
|
|
char *trim(char *s)
|
|
|
|
{
|
|
|
|
return ltrim(rtrim(s));
|
|
|
|
}
|