helper.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /** @file helper.h
  3. * @brief Includes helper functions for the Logs Management project.
  4. */
  5. #ifndef HELPER_H_
  6. #define HELPER_H_
  7. #include "libnetdata/libnetdata.h"
  8. #include <assert.h>
  9. #define LOGS_MANAGEMENT_PLUGIN_STR "logs-management.plugin"
  10. #define LOGS_MANAG_STR_HELPER(x) #x
  11. #define LOGS_MANAG_STR(x) LOGS_MANAG_STR_HELPER(x)
  12. #ifndef m_assert
  13. #if defined(LOGS_MANAGEMENT_DEV_MODE)
  14. #define m_assert(expr, msg) assert(((void)(msg), (expr)))
  15. #else
  16. #define m_assert(expr, msg) do{} while(0)
  17. #endif // LOGS_MANAGEMENT_DEV_MODE
  18. #endif // m_assert
  19. /* Test if a timestamp is within a valid range
  20. * 1649175852000 equals Tuesday, 5 April 2022 16:24:12,
  21. * 2532788652000 equals Tuesday, 5 April 2050 16:24:12
  22. */
  23. #define TEST_MS_TIMESTAMP_VALID(x) (((x) > 1649175852000 && (x) < 2532788652000)? 1:0)
  24. #define TIMESTAMP_MS_STR_SIZE sizeof("1649175852000")
  25. #ifdef ENABLE_LOGSMANAGEMENT_TESTS
  26. #define UNIT_STATIC
  27. #else
  28. #define UNIT_STATIC static
  29. #endif // ENABLE_LOGSMANAGEMENT_TESTS
  30. #ifndef COMPILE_TIME_ASSERT // https://stackoverflow.com/questions/3385515/static-assert-in-c
  31. #define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(!!(COND))*2-1]
  32. // token pasting madness:
  33. #define COMPILE_TIME_ASSERT3(X,L) STATIC_ASSERT(X,static_assertion_at_line_##L)
  34. #define COMPILE_TIME_ASSERT2(X,L) COMPILE_TIME_ASSERT3(X,L)
  35. #define COMPILE_TIME_ASSERT(X) COMPILE_TIME_ASSERT2(X,__LINE__)
  36. #endif // COMPILE_TIME_ASSERT
  37. #if defined(NETDATA_INTERNAL_CHECKS) && defined(LOGS_MANAGEMENT_DEV_MODE)
  38. #define debug_log(args...) netdata_logger(NDLS_COLLECTORS, NDLP_DEBUG, __FILE__, __FUNCTION__, __LINE__, ##args)
  39. #else
  40. #define debug_log(fmt, args...) do {} while(0)
  41. #endif
  42. /**
  43. * @brief Extract file_basename from full file path
  44. * @param path String containing the full path.
  45. * @return Pointer to the file_basename string
  46. */
  47. static inline char *get_basename(const char *const path) {
  48. if(!path) return NULL;
  49. char *s = strrchr(path, '/');
  50. if (!s)
  51. return strdupz(path);
  52. else
  53. return strdupz(s + 1);
  54. }
  55. typedef enum {
  56. STR2XX_SUCCESS = 0,
  57. STR2XX_OVERFLOW,
  58. STR2XX_UNDERFLOW,
  59. STR2XX_INCONVERTIBLE
  60. } str2xx_errno;
  61. /* Convert string s to int out.
  62. * https://stackoverflow.com/questions/7021725/how-to-convert-a-string-to-integer-in-c
  63. *
  64. * @param[out] out The converted int. Cannot be NULL.
  65. * @param[in] s Input string to be converted.
  66. *
  67. * The format is the same as strtol,
  68. * except that the following are inconvertible:
  69. * - empty string
  70. * - leading whitespace
  71. * - any trailing characters that are not part of the number
  72. * Cannot be NULL.
  73. *
  74. * @param[in] base Base to interpret string in. Same range as strtol (2 to 36).
  75. * @return Indicates if the operation succeeded, or why it failed.
  76. */
  77. static inline str2xx_errno str2int(int *out, char *s, int base) {
  78. char *end;
  79. if (unlikely(s[0] == '\0' || isspace(s[0]))){
  80. // debug_log( "str2int error: STR2XX_INCONVERTIBLE 1");
  81. // m_assert(0, "str2int error: STR2XX_INCONVERTIBLE");
  82. return STR2XX_INCONVERTIBLE;
  83. }
  84. errno = 0;
  85. long l = strtol(s, &end, base);
  86. /* Both checks are needed because INT_MAX == LONG_MAX is possible. */
  87. if (unlikely(l > INT_MAX || (errno == ERANGE && l == LONG_MAX))){
  88. debug_log( "str2int error: STR2XX_OVERFLOW");
  89. // m_assert(0, "str2int error: STR2XX_OVERFLOW");
  90. return STR2XX_OVERFLOW;
  91. }
  92. if (unlikely(l < INT_MIN || (errno == ERANGE && l == LONG_MIN))){
  93. debug_log( "str2int error: STR2XX_UNDERFLOW");
  94. // m_assert(0, "str2int error: STR2XX_UNDERFLOW");
  95. return STR2XX_UNDERFLOW;
  96. }
  97. if (unlikely(*end != '\0')){
  98. debug_log( "str2int error: STR2XX_INCONVERTIBLE 2");
  99. // m_assert(0, "str2int error: STR2XX_INCONVERTIBLE 2");
  100. return STR2XX_INCONVERTIBLE;
  101. }
  102. *out = l;
  103. return STR2XX_SUCCESS;
  104. }
  105. static inline str2xx_errno str2float(float *out, char *s) {
  106. char *end;
  107. if (unlikely(s[0] == '\0' || isspace(s[0]))){
  108. // debug_log( "str2float error: STR2XX_INCONVERTIBLE 1\n");
  109. // m_assert(0, "str2float error: STR2XX_INCONVERTIBLE");
  110. return STR2XX_INCONVERTIBLE;
  111. }
  112. errno = 0;
  113. float f = strtof(s, &end);
  114. /* Both checks are needed because INT_MAX == LONG_MAX is possible. */
  115. if (unlikely((errno == ERANGE && f == HUGE_VALF))){
  116. debug_log( "str2float error: STR2XX_OVERFLOW\n");
  117. // m_assert(0, "str2float error: STR2XX_OVERFLOW");
  118. return STR2XX_OVERFLOW;
  119. }
  120. if (unlikely((errno == ERANGE && f == -HUGE_VALF))){
  121. debug_log( "str2float error: STR2XX_UNDERFLOW\n");
  122. // m_assert(0, "str2float error: STR2XX_UNDERFLOW");
  123. return STR2XX_UNDERFLOW;
  124. }
  125. if (unlikely((*end != '\0'))){
  126. debug_log( "str2float error: STR2XX_INCONVERTIBLE 2\n");
  127. // m_assert(0, "str2float error: STR2XX_INCONVERTIBLE");
  128. return STR2XX_INCONVERTIBLE;
  129. }
  130. *out = f;
  131. return STR2XX_SUCCESS;
  132. }
  133. /**
  134. * @brief Read last line of *filename, up to max_line_width characters.
  135. * @note This function should be used carefully as it is not the most
  136. * efficient one. But it is a quick-n-dirty way of reading the last line
  137. * of a file.
  138. * @param[in] filename File to be read.
  139. * @param[in] max_line_width Integer indicating the max line width to be read.
  140. * If a line is longer than that, it will be truncated. If zero or negative, a
  141. * default value will be used instead.
  142. * @return Pointer to a string holding the line that was read, or NULL if error.
  143. */
  144. static inline char *read_last_line(const char *filename, int max_line_width){
  145. uv_fs_t req;
  146. int64_t start_pos, end_pos;
  147. uv_file file_handle = -1;
  148. uv_buf_t uvBuf;
  149. char *buff = NULL;
  150. int rc, line_pos = -1, bytes_read;
  151. max_line_width = max_line_width > 0 ? max_line_width : 1024; // 1024 == default value
  152. rc = uv_fs_stat(NULL, &req, filename, NULL);
  153. end_pos = req.statbuf.st_size;
  154. uv_fs_req_cleanup(&req);
  155. if (unlikely(rc)) {
  156. collector_error("[%s]: uv_fs_stat() error: (%d) %s", filename, rc, uv_strerror(rc));
  157. m_assert(0, "uv_fs_stat() failed during read_last_line()");
  158. goto error;
  159. }
  160. if(end_pos == 0) goto error;
  161. start_pos = end_pos - max_line_width;
  162. if(start_pos < 0) start_pos = 0;
  163. rc = uv_fs_open(NULL, &req, filename, O_RDONLY, 0, NULL);
  164. uv_fs_req_cleanup(&req);
  165. if (unlikely(rc < 0)) {
  166. collector_error("[%s]: uv_fs_open() error: (%d) %s",filename, rc, uv_strerror(rc));
  167. m_assert(0, "uv_fs_open() failed during read_last_line()");
  168. goto error;
  169. }
  170. file_handle = rc;
  171. buff = callocz(1, (size_t) (end_pos - start_pos + 1) * sizeof(char));
  172. uvBuf = uv_buf_init(buff, (unsigned int) (end_pos - start_pos));
  173. rc = uv_fs_read(NULL, &req, file_handle, &uvBuf, 1, start_pos, NULL);
  174. uv_fs_req_cleanup(&req);
  175. if (unlikely(rc < 0)){
  176. collector_error("[%s]: uv_fs_read() error: (%d) %s", filename, rc, uv_strerror(rc));
  177. m_assert(0, "uv_fs_read() failed during read_last_line()");
  178. goto error;
  179. }
  180. bytes_read = rc;
  181. buff[bytes_read] = '\0';
  182. for(int i = bytes_read - 2; i >= 0; i--){ // -2 because -1 could be '\n'
  183. if (buff[i] == '\n'){
  184. line_pos = i;
  185. break;
  186. }
  187. }
  188. if(line_pos >= 0){
  189. char *line = callocz(1, (size_t) (bytes_read - line_pos) * sizeof(char));
  190. memcpy(line, &buff[line_pos + 1], (size_t) (bytes_read - line_pos));
  191. freez(buff);
  192. uv_fs_close(NULL, &req, file_handle, NULL);
  193. return line;
  194. }
  195. if(start_pos == 0){
  196. uv_fs_close(NULL, &req, file_handle, NULL);
  197. return buff;
  198. }
  199. error:
  200. if(buff) freez(buff);
  201. if(file_handle >= 0) uv_fs_close(NULL, &req, file_handle, NULL);
  202. return NULL;
  203. }
  204. static inline void memcpy_iscntrl_fix(char *dest, char *src, size_t num){
  205. while(num--){
  206. *dest++ = unlikely(!iscntrl(*src)) ? *src : ' ';
  207. src++;
  208. }
  209. }
  210. #endif // HELPER_H_