jpulog.c 8.4 KB

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