wsyslog.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * This file modified from sources for imap4 for use
  3. * in PHP 3
  4. */
  5. /*
  6. * Program: Unix compatibility routines
  7. *
  8. * Author: Mark Crispin
  9. * Networks and Distributed Computing
  10. * Computing & Communications
  11. * University of Washington
  12. * Administration Building, AG-44
  13. * Seattle, WA 98195
  14. * Internet: MRC@CAC.Washington.EDU
  15. *
  16. * Date: 14 September 1996
  17. * Last Edited: 22 October 1996
  18. *
  19. * Copyright 1996 by the University of Washington
  20. *
  21. * Permission to use, copy, modify, and distribute this software and its
  22. * documentation for any purpose and without fee is hereby granted, provided
  23. * that the above copyright notice appears in all copies and that both the
  24. * above copyright notice and this permission notice appear in supporting
  25. * documentation, and that the name of the University of Washington not be
  26. * used in advertising or publicity pertaining to distribution of the software
  27. * without specific, written prior permission. This software is made available
  28. * "as is", and
  29. * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  30. * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  31. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  32. * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  33. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  34. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  35. * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  36. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  37. *
  38. */
  39. /* DEDICATION
  40. * This file is dedicated to my dog, Unix, also known as Yun-chan and
  41. * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast. Unix
  42. * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
  43. * a two-month bout with cirrhosis of the liver.
  44. *
  45. * He was a dear friend, and I miss him terribly.
  46. *
  47. * Lift a leg, Yunie. Luv ya forever!!!!
  48. */
  49. #include "php.h" /*php specific */
  50. #include "syslog.h"
  51. #include <stdio.h>
  52. #include <fcntl.h>
  53. #include <process.h>
  54. #include "php_win32_globals.h"
  55. #include "wsyslog.h"
  56. #include "codepage.h"
  57. void closelog(void)
  58. {
  59. if (INVALID_HANDLE_VALUE != PW32G(log_source)) {
  60. DeregisterEventSource(PW32G(log_source));
  61. PW32G(log_source) = INVALID_HANDLE_VALUE;
  62. }
  63. if (PW32G(log_header)) {
  64. free(PW32G(log_header));
  65. PW32G(log_header) = NULL;
  66. }
  67. }
  68. /* Emulator for BSD syslog() routine
  69. * Accepts: priority
  70. * message
  71. * parameters
  72. */
  73. void syslog(int priority, const char *message, ...)
  74. {
  75. va_list args;
  76. va_start(args, message); /* initialize vararg mechanism */
  77. vsyslog(priority, message, args);
  78. va_end(args);
  79. }
  80. void vsyslog(int priority, const char *message, va_list args)
  81. {
  82. LPTSTR strs[2];
  83. unsigned short etype;
  84. char *tmp = NULL;
  85. DWORD evid;
  86. wchar_t *strsw[2];
  87. /* default event source */
  88. if (INVALID_HANDLE_VALUE == PW32G(log_source))
  89. openlog("php", LOG_PID, LOG_SYSLOG);
  90. switch (priority) { /* translate UNIX type into NT type */
  91. case LOG_ALERT:
  92. etype = EVENTLOG_ERROR_TYPE;
  93. evid = PHP_SYSLOG_ERROR_TYPE;
  94. break;
  95. case LOG_INFO:
  96. etype = EVENTLOG_INFORMATION_TYPE;
  97. evid = PHP_SYSLOG_INFO_TYPE;
  98. break;
  99. default:
  100. etype = EVENTLOG_WARNING_TYPE;
  101. evid = PHP_SYSLOG_WARNING_TYPE;
  102. }
  103. vspprintf(&tmp, 0, message, args); /* build message */
  104. strsw[0] = php_win32_cp_any_to_w(PW32G(log_header));
  105. strsw[1] = php_win32_cp_any_to_w(tmp);
  106. /* report the event */
  107. if (strsw[0] && strsw[1]) {
  108. ReportEventW(PW32G(log_source), etype, (unsigned short) priority, evid, NULL, 2, 0, strsw, NULL);
  109. free(strsw[0]);
  110. free(strsw[1]);
  111. efree(tmp);
  112. return;
  113. }
  114. free(strsw[0]);
  115. free(strsw[1]);
  116. strs[0] = PW32G(log_header); /* write header */
  117. strs[1] = tmp; /* then the message */
  118. ReportEventA(PW32G(log_source), etype, (unsigned short) priority, evid, NULL, 2, 0, strs, NULL);
  119. efree(tmp);
  120. }
  121. /* Emulator for BSD openlog() routine
  122. * Accepts: identity
  123. * options
  124. * facility
  125. */
  126. void openlog(const char *ident, int logopt, int facility)
  127. {
  128. size_t header_len;
  129. closelog();
  130. PW32G(log_source) = RegisterEventSource(NULL, "PHP-" PHP_VERSION);
  131. header_len = strlen(ident) + 2 + 11;
  132. PW32G(log_header) = malloc(header_len*sizeof(char));
  133. sprintf_s(PW32G(log_header), header_len, (logopt & LOG_PID) ? "%s[%d]" : "%s", ident, getpid());
  134. }