mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
core: use std::string in error_callback
No naked free(). Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
71f3189a31
commit
d05e289507
8 changed files with 40 additions and 74 deletions
|
@ -1,50 +1,44 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
#ifdef __clang__
|
||||
// Clang has a bug on zero-initialization of C structs.
|
||||
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
|
||||
#endif
|
||||
#include <stdarg.h>
|
||||
#include "errorhelper.h"
|
||||
#include "membuffer.h"
|
||||
#include "format.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#if !defined(Q_OS_ANDROID) && !defined(__ANDROID__)
|
||||
#define LOG_MSG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
|
||||
#define LOG_MSG(fmt, s) fprintf(stderr, fmt, s)
|
||||
#else
|
||||
#include <android/log.h>
|
||||
#define LOG_MSG(fmt, ...) __android_log_print(ANDROID_LOG_INFO, "Subsurface", fmt, ##__VA_ARGS__);
|
||||
#define LOG_MSG(fmt, s) __android_log_print(ANDROID_LOG_INFO, "Subsurface", fmt, s);
|
||||
#endif
|
||||
|
||||
#define VA_BUF(b, fmt) do { va_list args; va_start(args, fmt); put_vformat(b, fmt, args); va_end(args); } while (0)
|
||||
|
||||
int verbose;
|
||||
|
||||
void report_info(const char *fmt, ...)
|
||||
{
|
||||
membuffer buf;
|
||||
|
||||
VA_BUF(&buf, fmt);
|
||||
strip_mb(&buf);
|
||||
LOG_MSG("INFO: %s\n", mb_cstring(&buf));
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
std::string s = vformat_string_std(fmt, args);
|
||||
va_end(args);
|
||||
LOG_MSG("INFO: %s\n", s.c_str());
|
||||
}
|
||||
|
||||
static void (*error_cb)(char *) = NULL;
|
||||
static void (*error_cb)(std::string) = NULL;
|
||||
|
||||
int report_error(const char *fmt, ...)
|
||||
{
|
||||
membuffer buf;
|
||||
|
||||
VA_BUF(&buf, fmt);
|
||||
strip_mb(&buf);
|
||||
LOG_MSG("ERROR: %s\n", mb_cstring(&buf));
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
std::string s = vformat_string_std(fmt, args);
|
||||
va_end(args);
|
||||
LOG_MSG("ERROR: %s\n", s.c_str());
|
||||
|
||||
/* if there is no error callback registered, don't produce errors */
|
||||
if (!error_cb)
|
||||
return -1;
|
||||
error_cb(detach_cstring(&buf));
|
||||
if (error_cb)
|
||||
error_cb(std::move(s));
|
||||
return -1;
|
||||
}
|
||||
|
||||
void set_error_cb(void(*cb)(char *))
|
||||
void set_error_cb(void(*cb)(std::string))
|
||||
{
|
||||
error_cb = cb;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue