Doxygen Book
nnstreamer_log.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-only */
11 #ifdef __ANDROID__
12 #ifndef _NO_EXECINFO_
13 #define _NO_EXECINFO_
14 #endif
15 #endif
16 
17 #ifndef _NO_EXECINFO_
18 /* Android does not have execinfo.h. It has unwind.h instead. */
19 #include <execinfo.h>
20 #endif
21 
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <glib.h>
26 
27 #include "nnstreamer_log.h"
28 
34 char *
36 {
37  char *retstr = NULL;
38 #ifndef _NO_EXECINFO_
39  void *array[20];
40  char **strings;
41  int size, i, len;
42  int strsize = 0, strcursor = 0;
43 
44  size = backtrace (array, 20);
45  strings = backtrace_symbols (array, size);
46  if (strings != NULL) {
47  for (i = 0; i < size; i++)
48  strsize += strlen (strings[i]);
49 
50  retstr = malloc (sizeof (char) * (strsize + 1));
51  if (retstr) {
52  for (i = 0; i < size; i++) {
53  len = strlen (strings[i]);
54  memcpy (retstr + strcursor, strings[i], len);
55  strcursor += len;
56  }
57 
58  retstr[strsize] = '\0';
59  }
60 
61  free (strings);
62  }
63 #else
64  retstr = strdup ("Android-nnstreamer does not support backtrace.\n");
65 #endif
66 
67  return retstr;
68 }
69 
70 #define _NNSTREAMER_ERROR_LENGTH (4096U)
71 static char errmsg[_NNSTREAMER_ERROR_LENGTH] = { 0 };
72 
73 static int errmsg_reported = 0;
74 G_LOCK_DEFINE_STATIC (errlock);
75 
80 const char *
82 {
83  G_LOCK (errlock);
84  if (errmsg_reported || errmsg[0] == '\0') {
85  G_UNLOCK (errlock);
86  return NULL;
87  }
88  G_UNLOCK (errlock);
89 
90  errmsg_reported = 1;
91  return errmsg;
92 }
93 
97 __attribute__((__format__ (__printf__, 1, 2)))
98  void _nnstreamer_error_write (const char *fmt, ...)
99 {
104  va_list arg_ptr;
105  G_LOCK (errlock);
106 
107  va_start (arg_ptr, fmt);
108  vsnprintf (errmsg, _NNSTREAMER_ERROR_LENGTH, fmt, arg_ptr);
109  va_end (arg_ptr);
110 
111  errmsg_reported = 0;
112 
113  G_UNLOCK (errlock);
114 }
115 
119 void
121 {
122  G_LOCK (errlock);
123 
124  errmsg[0] = '\0';
125 
126  G_UNLOCK (errlock);
127 }
errmsg
static char errmsg[_NNSTREAMER_ERROR_LENGTH]
Definition: nnstreamer_log.c:71
nnstreamer_log.h
Internal log util for NNStreamer plugins and native APIs.
__attribute__
__attribute__((__format__(__printf__, 1, 2)))
overwrites the error message buffer with the new message.
Definition: nnstreamer_log.c:97
_nnstreamer_error
const char * _nnstreamer_error(void)
return the last internal error string and clean it.
Definition: nnstreamer_log.c:81
_nnstreamer_error_clean
void _nnstreamer_error_clean(void)
cleans up the error message buffer.
Definition: nnstreamer_log.c:120
_backtrace_to_string
char * _backtrace_to_string(void)
stack trace as a string for error messages
Definition: nnstreamer_log.c:35
errmsg_reported
static int errmsg_reported
Definition: nnstreamer_log.c:73
_NNSTREAMER_ERROR_LENGTH
#define _NNSTREAMER_ERROR_LENGTH
Definition: nnstreamer_log.c:70
_nnstreamer_error_write
void _nnstreamer_error_write(const char *fmt,...)
overwrites the error message buffer with the new message.
G_LOCK_DEFINE_STATIC
G_LOCK_DEFINE_STATIC(errlock)