Messages.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2013 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 v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  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. *******************************************************************************/
  16. /**
  17. * @file
  18. * \brief Trace messages
  19. *
  20. */
  21. #include "Messages.h"
  22. #include "Log.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "Heap.h"
  27. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  28. #define max_msg_len 120
  29. static const char *protocol_message_list[] =
  30. {
  31. "%d %s -> CONNECT version %d clean: %d (%d)", /* 0, was 131, 68 and 69 */
  32. "%d %s <- CONNACK rc: %d", /* 1, was 132 */
  33. "%d %s -> CONNACK rc: %d (%d)", /* 2, was 138 */
  34. "%d %s <- PINGREQ", /* 3, was 35 */
  35. "%d %s -> PINGRESP (%d)", /* 4 */
  36. "%d %s <- DISCONNECT", /* 5 */
  37. "%d %s <- SUBSCRIBE msgid: %d", /* 6, was 39 */
  38. "%d %s -> SUBACK msgid: %d (%d)", /* 7, was 40 */
  39. "%d %s <- UNSUBSCRIBE msgid: %d", /* 8, was 41 */
  40. "%d %s -> UNSUBACK msgid: %d (%d)", /* 9 */
  41. "%d %s -> PUBLISH msgid: %d qos: %d retained: %d (%d) payload: %.*s", /* 10, was 42 */
  42. "%d %s <- PUBLISH msgid: %d qos: %d retained: %d payload: %.*s", /* 11, was 46 */
  43. "%d %s -> PUBACK msgid: %d (%d)", /* 12, was 47 */
  44. "%d %s -> PUBREC msgid: %d (%d)", /* 13, was 48 */
  45. "%d %s <- PUBACK msgid: %d", /* 14, was 49 */
  46. "%d %s <- PUBREC msgid: %d", /* 15, was 53 */
  47. "%d %s -> PUBREL msgid: %d (%d)", /* 16, was 57 */
  48. "%d %s <- PUBREL msgid %d", /* 17, was 58 */
  49. "%d %s -> PUBCOMP msgid %d (%d)", /* 18, was 62 */
  50. "%d %s <- PUBCOMP msgid:%d", /* 19, was 63 */
  51. "%d %s -> PINGREQ (%d)", /* 20, was 137 */
  52. "%d %s <- PINGRESP", /* 21, was 70 */
  53. "%d %s -> SUBSCRIBE msgid: %d (%d)", /* 22, was 72 */
  54. "%d %s <- SUBACK msgid: %d", /* 23, was 73 */
  55. "%d %s <- UNSUBACK msgid: %d", /* 24, was 74 */
  56. "%d %s -> UNSUBSCRIBE msgid: %d (%d)", /* 25, was 106 */
  57. "%d %s <- CONNECT", /* 26 */
  58. "%d %s -> PUBLISH qos: 0 retained: %d (%d)", /* 27 */
  59. "%d %s -> DISCONNECT (%d)", /* 28 */
  60. "Socket error for client identifier %s, socket %d, peer address %s; ending connection", /* 29 */
  61. };
  62. static const char *trace_message_list[] =
  63. {
  64. "Failed to remove client from bstate->clients", /* 0 */
  65. "Removed client %s from bstate->clients, socket %d", /* 1 */
  66. "Packet_Factory: unhandled packet type %d", /* 2 */
  67. "Packet %s received from client %s for message identifier %d, but no record of that message identifier found", /* 3 */
  68. "Packet %s received from client %s for message identifier %d, but message is wrong QoS, %d", /* 4 */
  69. "Packet %s received from client %s for message identifier %d, but message is in wrong state", /* 5 */
  70. "%s received from client %s for message id %d - removing publication", /* 6 */
  71. "Trying %s again for client %s, socket %d, message identifier %d", /* 7 */
  72. "", /* 8 */
  73. "(%lu) %*s(%d)> %s:%d", /* 9 */
  74. "(%lu) %*s(%d)< %s:%d", /* 10 */
  75. "(%lu) %*s(%d)< %s:%d (%d)", /* 11 */
  76. "Storing unsent QoS 0 message", /* 12 */
  77. };
  78. /**
  79. * Get a log message by its index
  80. * @param index the integer index
  81. * @param log_level the log level, used to determine which message list to use
  82. * @return the message format string
  83. */
  84. const char* Messages_get(int index, enum LOG_LEVELS log_level)
  85. {
  86. const char *msg = NULL;
  87. if (log_level == TRACE_PROTOCOL)
  88. msg = (index >= 0 && index < ARRAY_SIZE(protocol_message_list)) ? protocol_message_list[index] : NULL;
  89. else
  90. msg = (index >= 0 && index < ARRAY_SIZE(trace_message_list)) ? trace_message_list[index] : NULL;
  91. return msg;
  92. }