syslog.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. // Has to be defined before including due to legacy Unices
  7. #define SYSLOG_NAMES 1
  8. #include <AK/DeprecatedString.h>
  9. #include <AK/StringBuilder.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <syslog.h>
  13. #include <unistd.h>
  14. // This implementation doesn't talk to a syslog server. Any options related to
  15. // that are no-ops.
  16. extern "C" {
  17. // For implementation simplicity, we actually only use the re-entrant version
  18. // of each function, and the version that isn't just redirects with a static
  19. // struct to share.
  20. static struct syslog_data global_log_data = {
  21. .ident = nullptr,
  22. .logopt = 0,
  23. .facility = LOG_USER,
  24. .maskpri = LOG_UPTO(LOG_DEBUG)
  25. };
  26. // Used when ident is null, since syslog traditionally prints the program's
  27. // own name; the process name will always be the same unless we exec.
  28. static char program_name_buffer[256];
  29. static bool program_name_set = false;
  30. // Convenience function for initialization and checking what string to use
  31. // for the program name.
  32. static char const* get_syslog_ident(struct syslog_data* data)
  33. {
  34. if (!program_name_set && data->ident == nullptr)
  35. program_name_set = get_process_name(program_name_buffer, sizeof(program_name_buffer)) >= 0;
  36. if (data->ident != nullptr)
  37. return data->ident;
  38. else if (program_name_set)
  39. return program_name_buffer;
  40. VERIFY_NOT_REACHED();
  41. }
  42. void openlog_r(char const* ident, int logopt, int facility, struct syslog_data* data)
  43. {
  44. data->ident = ident;
  45. data->logopt = logopt;
  46. data->facility = facility;
  47. // default value
  48. data->maskpri = LOG_UPTO(LOG_DEBUG);
  49. // would be where we connect to a daemon
  50. }
  51. void openlog(char const* ident, int logopt, int facility)
  52. {
  53. openlog_r(ident, logopt, facility, &global_log_data);
  54. }
  55. void closelog_r(struct syslog_data* data)
  56. {
  57. // would be where we disconnect from a daemon
  58. // restore defaults
  59. data->ident = nullptr;
  60. data->logopt = 0;
  61. data->facility = LOG_USER;
  62. data->maskpri = LOG_UPTO(LOG_DEBUG);
  63. }
  64. void closelog(void)
  65. {
  66. closelog_r(&global_log_data);
  67. }
  68. int setlogmask_r(int maskpri, struct syslog_data* data)
  69. {
  70. // Remember, this takes the input of LOG_MASK/LOG_UPTO
  71. int old_maskpri = data->maskpri;
  72. data->maskpri = maskpri;
  73. return old_maskpri;
  74. }
  75. int setlogmask(int maskpri)
  76. {
  77. return setlogmask_r(maskpri, &global_log_data);
  78. }
  79. void syslog_r(int priority, struct syslog_data* data, char const* message, ...)
  80. {
  81. va_list ap;
  82. va_start(ap, message);
  83. vsyslog_r(priority, data, message, ap);
  84. va_end(ap);
  85. }
  86. void syslog(int priority, char const* message, ...)
  87. {
  88. va_list ap;
  89. va_start(ap, message);
  90. vsyslog_r(priority, &global_log_data, message, ap);
  91. va_end(ap);
  92. }
  93. void vsyslog_r(int priority, struct syslog_data* data, char const* message, va_list args)
  94. {
  95. StringBuilder combined;
  96. int real_priority = LOG_PRI(priority);
  97. // Lots of parens, but it just extracts the priority from combo and masks.
  98. if (!(data->maskpri & LOG_MASK(real_priority)))
  99. return;
  100. // Some metadata would be consumed by a syslog daemon, if we had one.
  101. if (data->logopt & LOG_PID)
  102. combined.appendff("{}[{}]: ", get_syslog_ident(data), getpid());
  103. else
  104. combined.appendff("{}: ", get_syslog_ident(data));
  105. combined.appendvf(message, args);
  106. auto combined_string = combined.to_deprecated_string();
  107. if (data->logopt & LOG_CONS)
  108. dbgputstr(combined_string.characters(), combined_string.length());
  109. if (data->logopt & LOG_PERROR)
  110. fputs(combined_string.characters(), stderr);
  111. }
  112. void vsyslog(int priority, char const* message, va_list args)
  113. {
  114. vsyslog_r(priority, &global_log_data, message, args);
  115. }
  116. }