jpulog.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2018, Chips&Media
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #if defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WIN32) || defined(__MINGW32__)
  27. #include <windows.h>
  28. #endif
  29. #include "../jpuapi/jpuconfig.h"
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdarg.h>
  33. #include "jpulog.h"
  34. #if defined(linux) || defined(__linux) || defined(ANDROID)
  35. #include <time.h>
  36. #include <sys/time.h>
  37. #include <termios.h>
  38. #include <unistd.h> // for read()
  39. static struct termios initial_settings, new_settings;
  40. static int peek_character = -1;
  41. #endif
  42. #define ANSI_COLOR_ERR "\x1b[31m" // RED
  43. #define ANSI_COLOR_TRACE "\x1b[32m" // GREEN
  44. #define ANSI_COLOR_WARN "\x1b[33m" // YELLOW
  45. #define ANSI_COLOR_BLUE "\x1b[34m" // BLUE
  46. #define ANSI_COLOR_INFO ""
  47. // For future
  48. #define ANSI_COLOR_MAGENTA "\x1b[35m" // MAGENTA
  49. #define ANSI_COLOR_CYAN "\x1b[36m" // CYAN
  50. #define ANSI_COLOR_RESET "\x1b[0m" // RESET
  51. static int log_colors[MAX_LOG_LEVEL] = {
  52. 0,
  53. TERM_COLOR_R|TERM_COLOR_G|TERM_COLOR_B|TERM_COLOR_BRIGHT, //INFO
  54. TERM_COLOR_R|TERM_COLOR_B|TERM_COLOR_BRIGHT, //WARN
  55. TERM_COLOR_R|TERM_COLOR_BRIGHT, // ERR
  56. TERM_COLOR_R|TERM_COLOR_G|TERM_COLOR_B //TRACE
  57. };
  58. static unsigned log_decor = LOG_HAS_TIME | LOG_HAS_FILE | LOG_HAS_MICRO_SEC |
  59. LOG_HAS_NEWLINE |
  60. LOG_HAS_SPACE | LOG_HAS_COLOR;
  61. static int max_log_level = ERR;
  62. static FILE *fpLog = NULL;
  63. #ifdef WIN32
  64. static void term_restore_color();
  65. static void term_set_color(int color);
  66. #endif
  67. BOOL InitLog(const char* path)
  68. {
  69. if (path != NULL) {
  70. if ((fpLog=fopen(path, "w")) == NULL) {
  71. printf("Failed to open log file(%s)\n", path);
  72. return FALSE;
  73. }
  74. }
  75. return TRUE;
  76. }
  77. void DeInitLog()
  78. {
  79. if (fpLog != NULL) {
  80. fclose(fpLog);//lint !e482
  81. fpLog = NULL;
  82. }
  83. }
  84. void SetMaxLogLevel(int level)
  85. {
  86. max_log_level = level;
  87. }
  88. int GetMaxLogLevel()
  89. {
  90. return max_log_level;
  91. }
  92. void SetLogColor(int level, int color)
  93. {
  94. log_colors[level] = color;
  95. }
  96. int GetLogColor(int level)
  97. {
  98. return log_colors[level];
  99. }
  100. void SetLogDecor(int decor)
  101. {
  102. log_decor = decor;
  103. }
  104. int GetLogDecor()
  105. {
  106. return log_decor;
  107. }
  108. void LogMsg(int level, const char *format, ...)
  109. {
  110. va_list ptr;
  111. char logBuf[MAX_PRINT_LENGTH] = {0};
  112. char* prefix = "";
  113. char* postfix= "";
  114. if (level > max_log_level)
  115. return;
  116. #if defined(SUPPORT_SW_UART) || defined(SUPPORT_SW_UART_V2)
  117. pthread_mutex_lock(&s_log_mutex);
  118. #endif
  119. if ((log_decor & LOG_HAS_COLOR)) {
  120. postfix = ANSI_COLOR_RESET;
  121. switch (level) {
  122. case INFO: prefix = ANSI_COLOR_INFO; break;
  123. case ERR: prefix = ANSI_COLOR_ERR "[ERROR]"; break;
  124. case TRACE: prefix = ANSI_COLOR_TRACE; break;
  125. case WARN: prefix = ANSI_COLOR_WARN"[WARN ]"; break;
  126. default: prefix = ""; break;
  127. }
  128. }
  129. va_start( ptr, format );
  130. #if defined(WIN32) || defined(__MINGW32__)
  131. _vsnprintf( logBuf, MAX_PRINT_LENGTH, format, ptr );
  132. #else
  133. vsnprintf( logBuf, MAX_PRINT_LENGTH, format, ptr );
  134. #endif
  135. va_end(ptr);
  136. #ifdef WIN32
  137. if (log_decor & LOG_HAS_COLOR)
  138. term_set_color(log_colors[level]);
  139. #endif
  140. #ifdef ANDROID
  141. if (level == ERR)
  142. {
  143. #ifdef LOGE
  144. LOGE("%s", logBuf);
  145. #endif
  146. #ifdef ALOGE
  147. ALOGE("%s", logBuf);
  148. #endif
  149. }
  150. else
  151. {
  152. #ifdef LOGI
  153. LOGI("%s", logBuf);
  154. #endif
  155. #ifdef ALOGI
  156. ALOGI("%s", logBuf);
  157. #endif
  158. }
  159. puts(logBuf);
  160. #else
  161. fputs(prefix, stderr);
  162. fputs(postfix, stderr);
  163. fprintf(stderr, logBuf);
  164. //fputs(logBuf, stderr);
  165. #endif
  166. #ifdef SUPPORT_LOG_SAVE
  167. if ((log_decor & LOG_HAS_FILE) && fpLog)
  168. {
  169. fwrite(logBuf, strlen(logBuf), 1,fpLog);
  170. fflush(fpLog);
  171. }
  172. #endif
  173. #ifdef WIN32
  174. if (log_decor & LOG_HAS_COLOR)
  175. term_restore_color();
  176. #endif
  177. }//lint !e438
  178. #ifdef WIN32
  179. static void term_set_color(int color)
  180. {
  181. unsigned short attr = 0;
  182. if (color & TERM_COLOR_R)
  183. attr |= FOREGROUND_RED;
  184. if (color & TERM_COLOR_G)
  185. attr |= FOREGROUND_GREEN;
  186. if (color & TERM_COLOR_B)
  187. attr |= FOREGROUND_BLUE;
  188. if (color & TERM_COLOR_BRIGHT)
  189. attr |= FOREGROUND_INTENSITY;
  190. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attr);
  191. }
  192. static void term_restore_color(void)
  193. {
  194. term_set_color(log_colors[4]);
  195. }
  196. #endif
  197. static double timer_frequency_;
  198. #if defined(_MSC_VER)
  199. static LARGE_INTEGER initial_;
  200. static LARGE_INTEGER frequency_;
  201. static LARGE_INTEGER counter_;
  202. #endif
  203. #if defined(linux) || defined(__linux) || defined(ANDROID)
  204. struct timeval tv_start;
  205. struct timeval tv_end;
  206. #endif
  207. #if defined(linux) || defined(__linux) || defined(ANDROID)
  208. #else
  209. void timer_init()
  210. {
  211. #if defined(_MSC_VER)
  212. if (QueryPerformanceFrequency(&frequency_))
  213. {
  214. //printf("High:%d, Quad:%d, Low:%d\n", frequency_.HighPart, frequency_.QuadPart, frequency_.LowPart);
  215. timer_frequency_ = (double)((long double)((long)(frequency_.HighPart) >> 32) + frequency_.LowPart);//lint !e572
  216. }
  217. else
  218. printf("QueryPerformanceFrequency returned FAIL\n");
  219. #endif
  220. }
  221. #endif
  222. void timer_start()
  223. {
  224. #if defined(linux) || defined(__linux) || defined(ANDROID)
  225. #else
  226. timer_init();
  227. #endif
  228. #if defined(linux) || defined(__linux) || defined(ANDROID)
  229. gettimeofday(&tv_start, NULL);
  230. #else
  231. #if defined(_MSC_VER)
  232. if (timer_frequency_ == 0)
  233. return;
  234. QueryPerformanceCounter(&initial_);
  235. #endif
  236. #endif
  237. }
  238. void timer_stop()
  239. {
  240. #if defined(linux) || defined(__linux) || defined(ANDROID)
  241. gettimeofday(&tv_end, NULL);
  242. #else
  243. #if defined(_MSC_VER)
  244. if (timer_frequency_ == 0)
  245. return;
  246. QueryPerformanceCounter(&counter_);
  247. #endif
  248. #endif
  249. }
  250. double timer_elapsed_ms()
  251. {
  252. double ms;
  253. ms = timer_elapsed_us()/1000.0;
  254. return ms;
  255. }
  256. double timer_elapsed_us()
  257. {
  258. double elapsed = 0;
  259. #if defined(linux) || defined(__linux) || defined(ANDROID)
  260. double start_us = 0;
  261. double end_us = 0;
  262. end_us = tv_end.tv_sec*1000*1000 + tv_end.tv_usec;
  263. start_us = tv_start.tv_sec*1000*1000 + tv_start.tv_usec;
  264. elapsed = end_us - start_us;
  265. #else
  266. #if defined(_MSC_VER)
  267. if (timer_frequency_ == 0)
  268. return 0;
  269. elapsed = (double)((long double)(counter_.QuadPart - initial_.QuadPart) / (long double)frequency_.QuadPart);
  270. #endif
  271. #endif
  272. return elapsed;
  273. }
  274. int timer_is_valid()
  275. {
  276. return timer_frequency_ != 0;
  277. }
  278. double timer_frequency()
  279. {
  280. return timer_frequency_;
  281. }
  282. #if defined(linux) || defined(__linux) || defined(ANDROID)
  283. void init_keyboard()
  284. {
  285. tcgetattr(0,&initial_settings);
  286. new_settings = initial_settings;
  287. new_settings.c_lflag &= ~ICANON;
  288. new_settings.c_lflag &= ~ECHO;
  289. new_settings.c_cc[VMIN] = 1;
  290. new_settings.c_cc[VTIME] = 0;
  291. tcsetattr(0, TCSANOW, &new_settings);
  292. peek_character = -1;
  293. }
  294. void close_keyboard()
  295. {
  296. tcsetattr(0, TCSANOW, &initial_settings);
  297. fflush(stdout);
  298. }
  299. int kbhit()
  300. {
  301. unsigned char ch;
  302. int nread;
  303. if (peek_character != -1) return 1;
  304. new_settings.c_cc[VMIN]=0;
  305. tcsetattr(0, TCSANOW, &new_settings);
  306. nread = read(0,&ch,1);
  307. new_settings.c_cc[VMIN]=1;
  308. tcsetattr(0, TCSANOW, &new_settings);
  309. if(nread == 1)
  310. {
  311. peek_character = (int)ch;
  312. return 1;
  313. }
  314. return 0;
  315. }
  316. int getch()
  317. {
  318. int val;
  319. char ch;
  320. if(peek_character != -1)
  321. {
  322. val = peek_character;
  323. peek_character = -1;
  324. return val;
  325. }
  326. read(0,&ch,1);
  327. return ch;
  328. }
  329. #endif //#if defined(linux) || defined(__linux) || defined(ANDROID)