syslog.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. // Has to be defined before including due to legacy Unices
  27. #define SYSLOG_NAMES 1
  28. #include <AK/String.h>
  29. #include <AK/StringBuilder.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <syslog.h>
  33. #include <unistd.h>
  34. // This implementation doesn't talk to a syslog server. Any options related to
  35. // that are no-ops.
  36. extern "C" {
  37. // For implementation simplicity, we actually only use the re-entrant version
  38. // of each function, and the version that isn't just redirects with a static
  39. // struct to share.
  40. static struct syslog_data global_log_data = {
  41. .ident = nullptr,
  42. .logopt = 0,
  43. .facility = LOG_USER,
  44. .maskpri = LOG_UPTO(LOG_DEBUG)
  45. };
  46. // Used when ident is null, since syslog traditionally prints the program's
  47. // own name; the process name will always be the same unless we exec.
  48. static char program_name_buffer[256];
  49. static bool program_name_set = false;
  50. // Convenience function for initialization and checking what string to use
  51. // for the program name.
  52. static const char* get_syslog_ident(struct syslog_data* data)
  53. {
  54. if (!program_name_set && data->ident == nullptr)
  55. program_name_set = get_process_name(program_name_buffer, sizeof(program_name_buffer)) >= 0;
  56. if (data->ident != nullptr)
  57. return data->ident;
  58. else if (program_name_set)
  59. return program_name_buffer;
  60. ASSERT_NOT_REACHED();
  61. }
  62. void openlog_r(const char* ident, int logopt, int facility, struct syslog_data* data)
  63. {
  64. data->ident = ident;
  65. data->logopt = logopt;
  66. data->facility = facility;
  67. // default value
  68. data->maskpri = LOG_UPTO(LOG_DEBUG);
  69. // would be where we connect to a daemon
  70. }
  71. void openlog(const char* ident, int logopt, int facility)
  72. {
  73. openlog_r(ident, logopt, facility, &global_log_data);
  74. }
  75. void closelog_r(struct syslog_data* data)
  76. {
  77. // would be where we disconnect from a daemon
  78. // restore defaults
  79. data->ident = nullptr;
  80. data->logopt = 0;
  81. data->facility = LOG_USER;
  82. data->maskpri = LOG_UPTO(LOG_DEBUG);
  83. }
  84. void closelog(void)
  85. {
  86. closelog_r(&global_log_data);
  87. }
  88. int setlogmask_r(int maskpri, struct syslog_data* data)
  89. {
  90. // Remember, this takes the input of LOG_MASK/LOG_UPTO
  91. int old_maskpri = data->maskpri;
  92. data->maskpri = maskpri;
  93. return old_maskpri;
  94. }
  95. int setlogmask(int maskpri)
  96. {
  97. return setlogmask_r(maskpri, &global_log_data);
  98. }
  99. void syslog_r(int priority, struct syslog_data* data, const char* message, ...)
  100. {
  101. va_list ap;
  102. va_start(ap, message);
  103. vsyslog_r(priority, data, message, ap);
  104. va_end(ap);
  105. }
  106. void syslog(int priority, const char* message, ...)
  107. {
  108. va_list ap;
  109. va_start(ap, message);
  110. vsyslog_r(priority, &global_log_data, message, ap);
  111. va_end(ap);
  112. }
  113. void vsyslog_r(int priority, struct syslog_data* data, const char* message, va_list args)
  114. {
  115. StringBuilder combined;
  116. int real_priority = LOG_PRI(priority);
  117. // Lots of parens, but it just extracts the priority from combo and masks.
  118. if (!(data->maskpri & LOG_MASK(real_priority)))
  119. return;
  120. // Some metadata would be consumed by a syslog daemon, if we had one.
  121. if (data->logopt & LOG_PID)
  122. combined.appendf("%s[%d]: ", get_syslog_ident(data), getpid());
  123. else
  124. combined.appendf("%s: ", get_syslog_ident(data));
  125. combined.appendvf(message, args);
  126. String combined_string = combined.build();
  127. if (data->logopt & LOG_CONS)
  128. dbgputstr(combined_string.characters(), combined_string.length());
  129. if (data->logopt & LOG_PERROR)
  130. fputs(combined_string.characters(), stderr);
  131. }
  132. void vsyslog(int priority, const char* message, va_list args)
  133. {
  134. vsyslog_r(priority, &global_log_data, message, args);
  135. }
  136. }