Log.c 15 KB

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