libxcoder  3.5.1
ni_log_logan.c
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (C) 2022 NETINT Technologies
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18  * SOFTWARE.
19  *
20  ******************************************************************************/
21 
22 /*!*****************************************************************************
23 * \file ni_log_logan.c
24 *
25 * \brief Exported logging routines definition
26 *
27 *******************************************************************************/
28 
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <time.h>
33 #if defined(__linux__) || defined(__APPLE__)
34 #include <sys/time.h>
35 #include <inttypes.h>
36 #endif
37 #include "ni_log_logan.h"
38 
39 #ifndef QUADRA
40 
41 static ni_log_level_t ni_log_level = NI_LOG_INFO;
42 static void (*ni_log_callback)(int, const char*, va_list) = ni_log_default_callback;
43 
44 #ifdef __ANDROID__
45 #include <android/log.h>
46 
47 static char ni_log_tag[128] = "libxcoder";
48 
49 #define ALOGV(fmt, ...) \
50  __android_log_vprint(ANDROID_LOG_VERBOSE, ni_log_tag, fmt, ##__VA_ARGS__)
51 #define ALOGD(fmt, ...) \
52  __android_log_vprint(ANDROID_LOG_DEBUG, ni_log_tag, fmt, ##__VA_ARGS__)
53 #define ALOGI(fmt, ...) \
54  __android_log_vprint(ANDROID_LOG_INFO, ni_log_tag, fmt, ##__VA_ARGS__)
55 #define ALOGW(fmt, ...) \
56  __android_log_vprint(ANDROID_LOG_WARN, ni_log_tag, fmt, ##__VA_ARGS__)
57 #define ALOGE(fmt, ...) \
58  __android_log_vprint(ANDROID_LOG_ERROR, ni_log_tag, fmt, ##__VA_ARGS__)
59 #endif
60 
61 
62 /*!******************************************************************************
63  * \brief Default ni_log() callback
64  *
65  * \param[in] level log level
66  * \param[in] fmt printf format specifier
67  * \param[in] vl variadric args list
68  *
69  * \return
70  *******************************************************************************/
71 void ni_log_default_callback(int level, const char* fmt, va_list vl)
72 {
73  if (level <= ni_log_level)
74  {
75 #ifndef __ANDROID__
76 #ifdef NI_LOG_TRACE_TIMESTAMPS
77  if (level == NI_LOG_TRACE)
78  {
79  struct timeval tv;
80  ni_logan_gettimeofday(&tv, NULL);
81  fprintf(stderr, "[%" PRIu64 "] ", tv.tv_sec * 1000000LL + tv.tv_usec);
82  }
83 #endif
84 #endif
85 
86 #ifdef __ANDROID__
87  if (level >= NI_LOG_DEBUG)
88  ALOGD(fmt, vl);
89  else if (level == NI_LOG_INFO)
90  ALOGI(fmt, vl);
91  else
92  ALOGE(fmt, vl);
93 #else
94  vfprintf(stderr, fmt, vl);
95 #endif
96  }
97 }
98 
99 /*!******************************************************************************
100  * \brief Set ni_log() callback
101  *
102  * \param[in] callback
103  *
104  * \return
105  *******************************************************************************/
106 void ni_log_set_callback(void (*log_callback)(int, const char*, va_list))
107 {
108  ni_log_callback = log_callback;
109 }
110 
111 /*!******************************************************************************
112  * \brief print log message using ni_log_callback
113  *
114  * \param[in] level log level
115  * \param[in] format printf format specifier
116  * \param[in] ... additional arguments
117  *
118  * \return
119  *******************************************************************************/
120 void ni_log(ni_log_level_t level, const char *fmt, ...)
121 {
122  va_list vl;
123  void (*log_callback)(int, const char*, va_list);
124 
125  va_start(vl, fmt);
126  if (log_callback = ni_log_callback)
127  log_callback(level, fmt, vl);
128  va_end(vl);
129 }
130 
131 /*!******************************************************************************
132  * \brief Set ni_log_level
133  *
134  * \param level log level
135  *
136  * \return
137  *******************************************************************************/
139 {
140  ni_log_level = level;
141 }
142 
143 /*!******************************************************************************
144  * \brief Get ni_log_level
145  *
146  * \return ni_log_level
147  *******************************************************************************/
149 {
150  return ni_log_level;
151 }
152 
153 /*!******************************************************************************
154  * \brief Convert ffmpeg log level integer to appropriate ni_log_level_t
155  *
156  * \param fflog_level integer representation of FFmpeg log level
157  *
158  * \return ni_log_level
159  *******************************************************************************/
161 {
162  ni_log_level_t converted_ni_log_level = NI_LOG_ERROR;
163  if (fflog_level >= -8)
164  {
165  converted_ni_log_level = NI_LOG_NONE;
166  }
167  if (fflog_level >= 8)
168  {
169  converted_ni_log_level = NI_LOG_FATAL;
170  }
171  if (fflog_level >= 16)
172  {
173  converted_ni_log_level = NI_LOG_ERROR;
174  }
175  if (fflog_level >= 32)
176  {
177  converted_ni_log_level = NI_LOG_INFO;
178  }
179  if (fflog_level >= 48)
180  {
181  converted_ni_log_level = NI_LOG_DEBUG;
182  }
183  if (fflog_level >= 56)
184  {
185  converted_ni_log_level = NI_LOG_TRACE;
186  }
187  return converted_ni_log_level;
188 }
189 
190 #ifdef __ANDROID__
191 /*!******************************************************************************
192  * \brief Set ni_log_tag
193  *
194  * \param log tag
195  *
196  * \return
197  *******************************************************************************/
198 void ni_log_set_log_tag(const char *log_tag)
199 {
200  strcpy(ni_log_tag, log_tag);
201  ni_log_tag[strlen(log_tag)] = '\0';
202 }
203 #endif
204 
205 #endif // #ifndef QUADRA
void ni_log_default_callback(int level, const char *fmt, va_list vl)
Default ni_log() callback.
Definition: ni_log_logan.c:71
ni_log_level_t ni_log_get_level(void)
Get ni_log_level.
Definition: ni_log_logan.c:148
ni_log_level_t ff_to_ni_log_level(int fflog_level)
Convert ffmpeg log level integer to appropriate ni_log_level_t.
Definition: ni_log_logan.c:160
void ni_log_set_callback(void(*log_callback)(int, const char *, va_list))
Set ni_log() callback.
Definition: ni_log_logan.c:106
void ni_log_set_level(ni_log_level_t level)
Set ni_log_level.
Definition: ni_log_logan.c:138
void ni_log(ni_log_level_t level, const char *fmt,...)
print log message using ni_log_callback
Definition: ni_log_logan.c:120
Exported logging routines definition.
ni_log_level_t
Definition: ni_log_logan.h:60
@ NI_LOG_NONE
Definition: ni_log_logan.h:62
@ NI_LOG_DEBUG
Definition: ni_log_logan.h:66
@ NI_LOG_TRACE
Definition: ni_log_logan.h:67
@ NI_LOG_FATAL
Definition: ni_log_logan.h:63
@ NI_LOG_ERROR
Definition: ni_log_logan.h:64
@ NI_LOG_INFO
Definition: ni_log_logan.h:65
int32_t ni_logan_gettimeofday(struct timeval *p_tp, void *p_tzp)
Definition: ni_util_logan.c:56