sd-journal.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #ifndef foosdjournalhfoo
  2. #define foosdjournalhfoo
  3. /***
  4. This file is part of systemd.
  5. Copyright 2011 Lennart Poettering
  6. systemd is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation; either version 2.1 of the License, or
  9. (at your option) any later version.
  10. systemd is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with systemd; If not, see <http://www.gnu.org/licenses/>.
  16. ***/
  17. #include <inttypes.h>
  18. #include <stdarg.h>
  19. #include <sys/types.h>
  20. #include <sys/uio.h>
  21. #include <syslog.h>
  22. #include "sd-id128.h"
  23. #include "_sd-common.h"
  24. /* Journal APIs. See sd-journal(3) for more information. */
  25. _SD_BEGIN_DECLARATIONS;
  26. /* Write to daemon */
  27. int sd_journal_print(int priority, const char *format, ...) _sd_printf_(2, 3);
  28. int sd_journal_printv(int priority, const char *format, va_list ap) _sd_printf_(2, 0);
  29. int sd_journal_send(const char *format, ...) _sd_printf_(1, 0) _sd_sentinel_;
  30. int sd_journal_sendv(const struct iovec *iov, int n);
  31. int sd_journal_perror(const char *message);
  32. /* Used by the macros below. You probably don't want to call this directly. */
  33. int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) _sd_printf_(5, 6);
  34. int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) _sd_printf_(5, 0);
  35. int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) _sd_printf_(4, 0) _sd_sentinel_;
  36. int sd_journal_sendv_with_location(const char *file, const char *line, const char *func, const struct iovec *iov, int n);
  37. int sd_journal_perror_with_location(const char *file, const char *line, const char *func, const char *message);
  38. /* implicitly add code location to messages sent, if this is enabled */
  39. #ifndef SD_JOURNAL_SUPPRESS_LOCATION
  40. #define sd_journal_print(priority, ...) sd_journal_print_with_location(priority, "CODE_FILE=" __FILE__, "CODE_LINE=" _SD_STRINGIFY(__LINE__), __func__, __VA_ARGS__)
  41. #define sd_journal_printv(priority, format, ap) sd_journal_printv_with_location(priority, "CODE_FILE=" __FILE__, "CODE_LINE=" _SD_STRINGIFY(__LINE__), __func__, format, ap)
  42. #define sd_journal_send(...) sd_journal_send_with_location("CODE_FILE=" __FILE__, "CODE_LINE=" _SD_STRINGIFY(__LINE__), __func__, __VA_ARGS__)
  43. #define sd_journal_sendv(iovec, n) sd_journal_sendv_with_location("CODE_FILE=" __FILE__, "CODE_LINE=" _SD_STRINGIFY(__LINE__), __func__, iovec, n)
  44. #define sd_journal_perror(message) sd_journal_perror_with_location("CODE_FILE=" __FILE__, "CODE_LINE=" _SD_STRINGIFY(__LINE__), __func__, message)
  45. #endif
  46. int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix);
  47. /* Browse journal stream */
  48. typedef struct sd_journal sd_journal;
  49. /* Open flags */
  50. enum {
  51. SD_JOURNAL_LOCAL_ONLY = 1 << 0,
  52. SD_JOURNAL_RUNTIME_ONLY = 1 << 1,
  53. SD_JOURNAL_SYSTEM = 1 << 2,
  54. SD_JOURNAL_CURRENT_USER = 1 << 3,
  55. SD_JOURNAL_OS_ROOT = 1 << 4,
  56. SD_JOURNAL_SYSTEM_ONLY = SD_JOURNAL_SYSTEM /* deprecated name */
  57. };
  58. /* Wakeup event types */
  59. enum {
  60. SD_JOURNAL_NOP,
  61. SD_JOURNAL_APPEND,
  62. SD_JOURNAL_INVALIDATE
  63. };
  64. int sd_journal_open(sd_journal **ret, int flags);
  65. int sd_journal_open_directory(sd_journal **ret, const char *path, int flags);
  66. int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags);
  67. int sd_journal_open_files(sd_journal **ret, const char **paths, int flags);
  68. int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags);
  69. int sd_journal_open_container(sd_journal **ret, const char *machine, int flags); /* deprecated */
  70. void sd_journal_close(sd_journal *j);
  71. int sd_journal_previous(sd_journal *j);
  72. int sd_journal_next(sd_journal *j);
  73. int sd_journal_previous_skip(sd_journal *j, uint64_t skip);
  74. int sd_journal_next_skip(sd_journal *j, uint64_t skip);
  75. int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret);
  76. int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id);
  77. int sd_journal_set_data_threshold(sd_journal *j, size_t sz);
  78. int sd_journal_get_data_threshold(sd_journal *j, size_t *sz);
  79. int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *l);
  80. int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *l);
  81. void sd_journal_restart_data(sd_journal *j);
  82. int sd_journal_add_match(sd_journal *j, const void *data, size_t size);
  83. int sd_journal_add_disjunction(sd_journal *j);
  84. int sd_journal_add_conjunction(sd_journal *j);
  85. void sd_journal_flush_matches(sd_journal *j);
  86. int sd_journal_seek_head(sd_journal *j);
  87. int sd_journal_seek_tail(sd_journal *j);
  88. int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec);
  89. int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec);
  90. int sd_journal_seek_cursor(sd_journal *j, const char *cursor);
  91. int sd_journal_get_cursor(sd_journal *j, char **cursor);
  92. int sd_journal_test_cursor(sd_journal *j, const char *cursor);
  93. int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to);
  94. int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, const sd_id128_t boot_id, uint64_t *from, uint64_t *to);
  95. int sd_journal_get_usage(sd_journal *j, uint64_t *bytes);
  96. int sd_journal_query_unique(sd_journal *j, const char *field);
  97. int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l);
  98. void sd_journal_restart_unique(sd_journal *j);
  99. int sd_journal_enumerate_fields(sd_journal *j, const char **field);
  100. void sd_journal_restart_fields(sd_journal *j);
  101. int sd_journal_get_fd(sd_journal *j);
  102. int sd_journal_get_events(sd_journal *j);
  103. int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec);
  104. int sd_journal_process(sd_journal *j);
  105. int sd_journal_wait(sd_journal *j, uint64_t timeout_usec);
  106. int sd_journal_reliable_fd(sd_journal *j);
  107. int sd_journal_get_catalog(sd_journal *j, char **text);
  108. int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **text);
  109. int sd_journal_has_runtime_files(sd_journal *j);
  110. int sd_journal_has_persistent_files(sd_journal *j);
  111. /* The inverse condition avoids ambiguity of dangling 'else' after the macro */
  112. #define SD_JOURNAL_FOREACH(j) \
  113. if (sd_journal_seek_head(j) < 0) { } \
  114. else while (sd_journal_next(j) > 0)
  115. /* The inverse condition avoids ambiguity of dangling 'else' after the macro */
  116. #define SD_JOURNAL_FOREACH_BACKWARDS(j) \
  117. if (sd_journal_seek_tail(j) < 0) { } \
  118. else while (sd_journal_previous(j) > 0)
  119. /* Iterate through the data fields of the current journal entry */
  120. #define SD_JOURNAL_FOREACH_DATA(j, data, l) \
  121. for (sd_journal_restart_data(j); sd_journal_enumerate_data((j), &(data), &(l)) > 0; )
  122. /* Iterate through the all known values of a specific field */
  123. #define SD_JOURNAL_FOREACH_UNIQUE(j, data, l) \
  124. for (sd_journal_restart_unique(j); sd_journal_enumerate_unique((j), &(data), &(l)) > 0; )
  125. /* Iterate through all known field names */
  126. #define SD_JOURNAL_FOREACH_FIELD(j, field) \
  127. for (sd_journal_restart_fields(j); sd_journal_enumerate_fields((j), &(field)) > 0; )
  128. _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_journal, sd_journal_close);
  129. _SD_END_DECLARATIONS;
  130. #endif