Log.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2020 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. * Ian Craggs - updates for the async client
  16. * Ian Craggs - fix for bug #427028
  17. *******************************************************************************/
  18. /**
  19. * @file
  20. * \brief Logging and tracing module
  21. *
  22. *
  23. */
  24. #include "Log.h"
  25. #include "MQTTPacket.h"
  26. #include "MQTTProtocol.h"
  27. #include "MQTTProtocolClient.h"
  28. #include "Messages.h"
  29. #include "LinkedList.h"
  30. #include "StackTrace.h"
  31. #include "Thread.h"
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <stdarg.h>
  35. #include <time.h>
  36. #include <string.h>
  37. #if !defined(_WIN32) && !defined(_WIN64)
  38. #include <syslog.h>
  39. #include <sys/stat.h>
  40. #define GETTIMEOFDAY 1
  41. #else
  42. #define snprintf _snprintf
  43. #endif
  44. #if defined(GETTIMEOFDAY)
  45. #include <sys/time.h>
  46. #else
  47. #include <sys/timeb.h>
  48. #endif
  49. #if !defined(_WIN32) && !defined(_WIN64)
  50. /**
  51. * _unlink mapping for linux
  52. */
  53. #define _unlink unlink
  54. #endif
  55. #if !defined(min)
  56. #define min(A,B) ( (A) < (B) ? (A):(B))
  57. #endif
  58. trace_settings_type trace_settings =
  59. {
  60. #if defined(HIGH_PERFORMANCE)
  61. LOG_ERROR,
  62. #else
  63. TRACE_MINIMUM,
  64. #endif
  65. 400,
  66. INVALID_LEVEL
  67. };
  68. #define MAX_FUNCTION_NAME_LENGTH 256
  69. typedef struct
  70. {
  71. #if defined(GETTIMEOFDAY)
  72. struct timeval ts;
  73. #else
  74. struct timeb ts;
  75. #endif
  76. int sametime_count;
  77. int number;
  78. int thread_id;
  79. int depth;
  80. char name[MAX_FUNCTION_NAME_LENGTH + 1];
  81. int line;
  82. int has_rc;
  83. int rc;
  84. enum LOG_LEVELS level;
  85. } traceEntry;
  86. static int start_index = -1,
  87. next_index = 0;
  88. static traceEntry* trace_queue = NULL;
  89. static int trace_queue_size = 0;
  90. static FILE* trace_destination = NULL; /**< flag to indicate if trace is to be sent to a stream */
  91. static char* trace_destination_name = NULL; /**< the name of the trace file */
  92. static char* trace_destination_backup_name = NULL; /**< the name of the backup trace file */
  93. static int lines_written = 0; /**< number of lines written to the current output file */
  94. static int max_lines_per_file = 1000; /**< maximum number of lines to write to one trace file */
  95. static enum LOG_LEVELS trace_output_level = INVALID_LEVEL;
  96. static Log_traceCallback* trace_callback = NULL;
  97. static traceEntry* Log_pretrace(void);
  98. static char* Log_formatTraceEntry(traceEntry* cur_entry);
  99. static void Log_output(enum LOG_LEVELS log_level, const char *msg);
  100. static void Log_posttrace(enum LOG_LEVELS log_level, traceEntry* cur_entry);
  101. static void Log_trace(enum LOG_LEVELS log_level, const char *buf);
  102. #if 0
  103. static FILE* Log_destToFile(const char *dest);
  104. static int Log_compareEntries(const char *entry1, const char *entry2);
  105. #endif
  106. static int sametime_count = 0;
  107. #if defined(GETTIMEOFDAY)
  108. struct timeval now_ts, last_ts;
  109. #else
  110. struct timeb now_ts, last_ts;
  111. #endif
  112. static char msg_buf[512];
  113. #if defined(_WIN32) || defined(_WIN64)
  114. mutex_type log_mutex;
  115. #else
  116. static pthread_mutex_t log_mutex_store = PTHREAD_MUTEX_INITIALIZER;
  117. static mutex_type log_mutex = &log_mutex_store;
  118. #endif
  119. int Log_initialize(Log_nameValue* info)
  120. {
  121. int rc = SOCKET_ERROR;
  122. char* envval = NULL;
  123. #if !defined(_WIN32) && !defined(_WIN64)
  124. struct stat buf;
  125. #endif
  126. if ((trace_queue = malloc(sizeof(traceEntry) * trace_settings.max_trace_entries)) == NULL)
  127. goto exit;
  128. trace_queue_size = trace_settings.max_trace_entries;
  129. if ((envval = getenv("MQTT_C_CLIENT_TRACE")) != NULL && strlen(envval) > 0)
  130. {
  131. if (strcmp(envval, "ON") == 0 || (trace_destination = fopen(envval, "w")) == NULL)
  132. trace_destination = stdout;
  133. else
  134. {
  135. size_t namelen = 0;
  136. if ((trace_destination_name = malloc(strlen(envval) + 1)) == NULL)
  137. {
  138. free(trace_queue);
  139. goto exit;
  140. }
  141. strcpy(trace_destination_name, envval);
  142. namelen = strlen(envval) + 3;
  143. if ((trace_destination_backup_name = malloc(namelen)) == NULL)
  144. {
  145. free(trace_queue);
  146. free(trace_destination_name);
  147. goto exit;
  148. }
  149. if (snprintf(trace_destination_backup_name, namelen, "%s.0", trace_destination_name) >= namelen)
  150. trace_destination_backup_name[namelen-1] = '\0';
  151. }
  152. }
  153. if ((envval = getenv("MQTT_C_CLIENT_TRACE_MAX_LINES")) != NULL && strlen(envval) > 0)
  154. {
  155. max_lines_per_file = atoi(envval);
  156. if (max_lines_per_file <= 0)
  157. max_lines_per_file = 1000;
  158. }
  159. if ((envval = getenv("MQTT_C_CLIENT_TRACE_LEVEL")) != NULL && strlen(envval) > 0)
  160. {
  161. if (strcmp(envval, "MAXIMUM") == 0 || strcmp(envval, "TRACE_MAXIMUM") == 0)
  162. trace_settings.trace_level = TRACE_MAXIMUM;
  163. else if (strcmp(envval, "MEDIUM") == 0 || strcmp(envval, "TRACE_MEDIUM") == 0)
  164. trace_settings.trace_level = TRACE_MEDIUM;
  165. else if (strcmp(envval, "MINIMUM") == 0 || strcmp(envval, "TRACE_MINIMUM") == 0)
  166. trace_settings.trace_level = TRACE_MINIMUM;
  167. else if (strcmp(envval, "PROTOCOL") == 0 || strcmp(envval, "TRACE_PROTOCOL") == 0)
  168. trace_output_level = TRACE_PROTOCOL;
  169. else if (strcmp(envval, "ERROR") == 0 || strcmp(envval, "TRACE_ERROR") == 0)
  170. trace_output_level = LOG_ERROR;
  171. }
  172. Log_output(TRACE_MINIMUM, "=========================================================");
  173. Log_output(TRACE_MINIMUM, " Trace Output");
  174. if (info)
  175. {
  176. while (info->name)
  177. {
  178. snprintf(msg_buf, sizeof(msg_buf), "%s: %s", info->name, info->value);
  179. Log_output(TRACE_MINIMUM, msg_buf);
  180. info++;
  181. }
  182. }
  183. #if !defined(_WIN32) && !defined(_WIN64)
  184. if (stat("/proc/version", &buf) != -1)
  185. {
  186. FILE* vfile;
  187. if ((vfile = fopen("/proc/version", "r")) != NULL)
  188. {
  189. int len;
  190. strcpy(msg_buf, "/proc/version: ");
  191. len = strlen(msg_buf);
  192. if (fgets(&msg_buf[len], sizeof(msg_buf) - len, vfile))
  193. Log_output(TRACE_MINIMUM, msg_buf);
  194. fclose(vfile);
  195. }
  196. }
  197. #endif
  198. Log_output(TRACE_MINIMUM, "=========================================================");
  199. exit:
  200. return rc;
  201. }
  202. void Log_setTraceCallback(Log_traceCallback* callback)
  203. {
  204. trace_callback = callback;
  205. }
  206. void Log_setTraceLevel(enum LOG_LEVELS level)
  207. {
  208. if (level <= LOG_ERROR) /* the lowest we can go is LOG_ERROR */
  209. trace_settings.trace_level = level;
  210. trace_output_level = level;
  211. }
  212. void Log_terminate(void)
  213. {
  214. free(trace_queue);
  215. trace_queue = NULL;
  216. trace_queue_size = 0;
  217. if (trace_destination)
  218. {
  219. if (trace_destination != stdout)
  220. fclose(trace_destination);
  221. trace_destination = NULL;
  222. }
  223. if (trace_destination_name) {
  224. free(trace_destination_name);
  225. trace_destination_name = NULL;
  226. }
  227. if (trace_destination_backup_name) {
  228. free(trace_destination_backup_name);
  229. trace_destination_backup_name = NULL;
  230. }
  231. start_index = -1;
  232. next_index = 0;
  233. trace_output_level = INVALID_LEVEL;
  234. sametime_count = 0;
  235. }
  236. static traceEntry* Log_pretrace(void)
  237. {
  238. traceEntry *cur_entry = NULL;
  239. /* calling ftime/gettimeofday seems to be comparatively expensive, so we need to limit its use */
  240. if (++sametime_count % 20 == 0)
  241. {
  242. #if defined(GETTIMEOFDAY)
  243. gettimeofday(&now_ts, NULL);
  244. if (now_ts.tv_sec != last_ts.tv_sec || now_ts.tv_usec != last_ts.tv_usec)
  245. #else
  246. ftime(&now_ts);
  247. if (now_ts.time != last_ts.time || now_ts.millitm != last_ts.millitm)
  248. #endif
  249. {
  250. sametime_count = 0;
  251. last_ts = now_ts;
  252. }
  253. }
  254. if (trace_queue_size != trace_settings.max_trace_entries)
  255. {
  256. traceEntry* new_trace_queue = malloc(sizeof(traceEntry) * trace_settings.max_trace_entries);
  257. if (new_trace_queue == NULL)
  258. goto exit;
  259. memcpy(new_trace_queue, trace_queue, min(trace_queue_size, trace_settings.max_trace_entries) * sizeof(traceEntry));
  260. free(trace_queue);
  261. trace_queue = new_trace_queue;
  262. trace_queue_size = trace_settings.max_trace_entries;
  263. if (start_index > trace_settings.max_trace_entries + 1 ||
  264. next_index > trace_settings.max_trace_entries + 1)
  265. {
  266. start_index = -1;
  267. next_index = 0;
  268. }
  269. }
  270. /* add to trace buffer */
  271. cur_entry = &trace_queue[next_index];
  272. if (next_index == start_index) /* means the buffer is full */
  273. {
  274. if (++start_index == trace_settings.max_trace_entries)
  275. start_index = 0;
  276. } else if (start_index == -1)
  277. start_index = 0;
  278. if (++next_index == trace_settings.max_trace_entries)
  279. next_index = 0;
  280. exit:
  281. return cur_entry;
  282. }
  283. static char* Log_formatTraceEntry(traceEntry* cur_entry)
  284. {
  285. struct tm *timeinfo;
  286. int buf_pos = 31;
  287. #if defined(GETTIMEOFDAY)
  288. timeinfo = localtime((time_t *)&cur_entry->ts.tv_sec);
  289. #else
  290. timeinfo = localtime(&cur_entry->ts.time);
  291. #endif
  292. strftime(&msg_buf[7], 80, "%Y%m%d %H%M%S ", timeinfo);
  293. #if defined(GETTIMEOFDAY)
  294. snprintf(&msg_buf[22], sizeof(msg_buf)-22, ".%.3lu ", cur_entry->ts.tv_usec / 1000L);
  295. #else
  296. snprintf(&msg_buf[22], sizeof(msg_buf)-22, ".%.3hu ", cur_entry->ts.millitm);
  297. #endif
  298. buf_pos = 27;
  299. snprintf(msg_buf, sizeof(msg_buf), "(%.4d)", cur_entry->sametime_count);
  300. msg_buf[6] = ' ';
  301. if (cur_entry->has_rc == 2)
  302. strncpy(&msg_buf[buf_pos], cur_entry->name, sizeof(msg_buf)-buf_pos);
  303. else
  304. {
  305. const char *format = Messages_get(cur_entry->number, cur_entry->level);
  306. if (cur_entry->has_rc == 1)
  307. snprintf(&msg_buf[buf_pos], sizeof(msg_buf)-buf_pos, format, cur_entry->thread_id,
  308. cur_entry->depth, "", cur_entry->depth, cur_entry->name, cur_entry->line, cur_entry->rc);
  309. else
  310. snprintf(&msg_buf[buf_pos], sizeof(msg_buf)-buf_pos, format, cur_entry->thread_id,
  311. cur_entry->depth, "", cur_entry->depth, cur_entry->name, cur_entry->line);
  312. }
  313. return msg_buf;
  314. }
  315. static void Log_output(enum LOG_LEVELS log_level, const char *msg)
  316. {
  317. if (trace_destination)
  318. {
  319. fprintf(trace_destination, "%s\n", msg);
  320. if (trace_destination != stdout && ++lines_written >= max_lines_per_file)
  321. {
  322. fclose(trace_destination);
  323. _unlink(trace_destination_backup_name); /* remove any old backup trace file */
  324. rename(trace_destination_name, trace_destination_backup_name); /* rename recently closed to backup */
  325. trace_destination = fopen(trace_destination_name, "w"); /* open new trace file */
  326. if (trace_destination == NULL)
  327. trace_destination = stdout;
  328. lines_written = 0;
  329. }
  330. else
  331. fflush(trace_destination);
  332. }
  333. if (trace_callback)
  334. (*trace_callback)(log_level, msg);
  335. }
  336. static void Log_posttrace(enum LOG_LEVELS log_level, traceEntry* cur_entry)
  337. {
  338. if (((trace_output_level == -1) ? log_level >= trace_settings.trace_level : log_level >= trace_output_level))
  339. {
  340. char* msg = NULL;
  341. if (trace_destination || trace_callback)
  342. msg = &Log_formatTraceEntry(cur_entry)[7];
  343. Log_output(log_level, msg);
  344. }
  345. }
  346. static void Log_trace(enum LOG_LEVELS log_level, const char *buf)
  347. {
  348. traceEntry *cur_entry = NULL;
  349. if (trace_queue == NULL)
  350. return;
  351. cur_entry = Log_pretrace();
  352. memcpy(&(cur_entry->ts), &now_ts, sizeof(now_ts));
  353. cur_entry->sametime_count = sametime_count;
  354. cur_entry->has_rc = 2;
  355. strncpy(cur_entry->name, buf, sizeof(cur_entry->name));
  356. cur_entry->name[MAX_FUNCTION_NAME_LENGTH] = '\0';
  357. Log_posttrace(log_level, cur_entry);
  358. }
  359. /**
  360. * Log a message. If possible, all messages should be indexed by message number, and
  361. * the use of the format string should be minimized or negated altogether. If format is
  362. * provided, the message number is only used as a message label.
  363. * @param log_level the log level of the message
  364. * @param msgno the id of the message to use if the format string is NULL
  365. * @param aFormat the printf format string to be used if the message id does not exist
  366. * @param ... the printf inserts
  367. */
  368. void Log(enum LOG_LEVELS log_level, int msgno, const char *format, ...)
  369. {
  370. if (log_level >= trace_settings.trace_level)
  371. {
  372. const char *temp = NULL;
  373. va_list args;
  374. /* we're using a static character buffer, so we need to make sure only one thread uses it at a time */
  375. Thread_lock_mutex(log_mutex);
  376. if (format == NULL && (temp = Messages_get(msgno, log_level)) != NULL)
  377. format = temp;
  378. va_start(args, format);
  379. vsnprintf(msg_buf, sizeof(msg_buf), format, args);
  380. Log_trace(log_level, msg_buf);
  381. va_end(args);
  382. Thread_unlock_mutex(log_mutex);
  383. }
  384. }
  385. /**
  386. * The reason for this function is to make trace logging as fast as possible so that the
  387. * function exit/entry history can be captured by default without unduly impacting
  388. * performance. Therefore it must do as little as possible.
  389. * @param log_level the log level of the message
  390. * @param msgno the id of the message to use if the format string is NULL
  391. * @param aFormat the printf format string to be used if the message id does not exist
  392. * @param ... the printf inserts
  393. */
  394. void Log_stackTrace(enum LOG_LEVELS log_level, int msgno, int thread_id, int current_depth, const char* name, int line, int* rc)
  395. {
  396. traceEntry *cur_entry = NULL;
  397. if (trace_queue == NULL)
  398. return;
  399. if (log_level < trace_settings.trace_level)
  400. return;
  401. Thread_lock_mutex(log_mutex);
  402. cur_entry = Log_pretrace();
  403. memcpy(&(cur_entry->ts), &now_ts, sizeof(now_ts));
  404. cur_entry->sametime_count = sametime_count;
  405. cur_entry->number = msgno;
  406. cur_entry->thread_id = thread_id;
  407. cur_entry->depth = current_depth;
  408. strcpy(cur_entry->name, name);
  409. cur_entry->level = log_level;
  410. cur_entry->line = line;
  411. if (rc == NULL)
  412. cur_entry->has_rc = 0;
  413. else
  414. {
  415. cur_entry->has_rc = 1;
  416. cur_entry->rc = *rc;
  417. }
  418. Log_posttrace(log_level, cur_entry);
  419. Thread_unlock_mutex(log_mutex);
  420. }
  421. #if 0
  422. static FILE* Log_destToFile(const char *dest)
  423. {
  424. FILE* file = NULL;
  425. if (strcmp(dest, "stdout") == 0)
  426. file = stdout;
  427. else if (strcmp(dest, "stderr") == 0)
  428. file = stderr;
  429. else
  430. {
  431. if (strstr(dest, "FFDC"))
  432. file = fopen(dest, "ab");
  433. else
  434. file = fopen(dest, "wb");
  435. }
  436. return file;
  437. }
  438. static int Log_compareEntries(const char *entry1, const char *entry2)
  439. {
  440. int comp = strncmp(&entry1[7], &entry2[7], 19);
  441. /* if timestamps are equal, use the sequence numbers */
  442. if (comp == 0)
  443. comp = strncmp(&entry1[1], &entry2[1], 4);
  444. return comp;
  445. }
  446. /**
  447. * Write the contents of the stored trace to a stream
  448. * @param dest string which contains a file name or the special strings stdout or stderr
  449. */
  450. int Log_dumpTrace(char* dest)
  451. {
  452. FILE* file = NULL;
  453. ListElement* cur_trace_entry = NULL;
  454. const int msgstart = 7;
  455. int rc = -1;
  456. int trace_queue_index = 0;
  457. if ((file = Log_destToFile(dest)) == NULL)
  458. {
  459. Log(LOG_ERROR, 9, NULL, "trace", dest, "trace entries");
  460. goto exit;
  461. }
  462. fprintf(file, "=========== Start of trace dump ==========\n");
  463. /* Interleave the log and trace entries together appropriately */
  464. ListNextElement(trace_buffer, &cur_trace_entry);
  465. trace_queue_index = start_index;
  466. if (trace_queue_index == -1)
  467. trace_queue_index = next_index;
  468. else
  469. {
  470. Log_formatTraceEntry(&trace_queue[trace_queue_index++]);
  471. if (trace_queue_index == trace_settings.max_trace_entries)
  472. trace_queue_index = 0;
  473. }
  474. while (cur_trace_entry || trace_queue_index != next_index)
  475. {
  476. if (cur_trace_entry && trace_queue_index != -1)
  477. { /* compare these timestamps */
  478. if (Log_compareEntries((char*)cur_trace_entry->content, msg_buf) > 0)
  479. cur_trace_entry = NULL;
  480. }
  481. if (cur_trace_entry)
  482. {
  483. fprintf(file, "%s\n", &((char*)(cur_trace_entry->content))[msgstart]);
  484. ListNextElement(trace_buffer, &cur_trace_entry);
  485. }
  486. else
  487. {
  488. fprintf(file, "%s\n", &msg_buf[7]);
  489. if (trace_queue_index != next_index)
  490. {
  491. Log_formatTraceEntry(&trace_queue[trace_queue_index++]);
  492. if (trace_queue_index == trace_settings.max_trace_entries)
  493. trace_queue_index = 0;
  494. }
  495. }
  496. }
  497. fprintf(file, "========== End of trace dump ==========\n\n");
  498. if (file != stdout && file != stderr && file != NULL)
  499. fclose(file);
  500. rc = 0;
  501. exit:
  502. return rc;
  503. }
  504. #endif