Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.2 KiB

  1. /*
  2. * logs system time and a text string to a log buffer
  3. */
  4. #include "windows.h"
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include "sumserve.h"
  8. #include "errlog.h"
  9. #include "server.h"
  10. /*
  11. * users HLOG handle is a pointer to one of these structures
  12. *
  13. * core is the section we send to him on request.
  14. */
  15. struct error_log {
  16. CRITICAL_SECTION critsec;
  17. struct corelog core;
  18. };
  19. /* create an empty log */
  20. HLOG Log_Create(void)
  21. {
  22. HLOG hlog;
  23. hlog = GlobalLock(GlobalAlloc(GHND, sizeof(struct error_log)));
  24. if (hlog == NULL) {
  25. return(NULL);
  26. }
  27. InitializeCriticalSection(&hlog->critsec);
  28. hlog->core.lcode = LRESPONSE;
  29. hlog->core.bWrapped = FALSE;
  30. hlog->core.dwRevCount = 1;
  31. hlog->core.length = 0;
  32. return(hlog);
  33. }
  34. /* delete a log */
  35. VOID Log_Delete(HLOG hlog)
  36. {
  37. DeleteCriticalSection(&hlog->critsec);
  38. GlobalFree(GlobalHandle(hlog));
  39. }
  40. /*
  41. * private function to delete the first log item in order to
  42. * make space. Critsec already held
  43. */
  44. VOID Log_DeleteFirstItem(HLOG hlog)
  45. {
  46. int length;
  47. PBYTE pData;
  48. /* note that we have lost data */
  49. hlog->core.bWrapped = TRUE;
  50. if (hlog->core.length <= 0) {
  51. return;
  52. }
  53. pData = hlog->core.Data;
  54. /*
  55. * we need to erase one entry - that is, one FILETIME struct,
  56. * plus a null-terminated string (including the null).
  57. */
  58. length = sizeof(FILETIME) + lstrlen (pData + sizeof(FILETIME)) + 1;
  59. MoveMemory(pData, pData + length, hlog->core.length - length);
  60. hlog->core.length -= length;
  61. }
  62. /* write a previous formatted string and a time to the log */
  63. VOID Log_WriteData(HLOG hlog, LPFILETIME ptime, LPSTR pstr)
  64. {
  65. int length;
  66. LPBYTE pData;
  67. EnterCriticalSection(&hlog->critsec);
  68. /* every change changes the revision number */
  69. hlog->core.dwRevCount++;
  70. /*
  71. * we will insert the string plus null plus a filetime struct
  72. */
  73. length = lstrlen(pstr) + 1 + sizeof(FILETIME);
  74. /*
  75. * make space in log for this item by deleting earlier items
  76. */
  77. while ( (int)(sizeof(hlog->core.Data) - hlog->core.length) < length) {
  78. Log_DeleteFirstItem(hlog);
  79. }
  80. pData = &hlog->core.Data[hlog->core.length];
  81. /*
  82. * first part of the item is the time as a FILETIME struct
  83. */
  84. * (FILETIME UNALIGNED *)pData = *ptime;
  85. pData += sizeof(FILETIME);
  86. /* followed by the ansi string */
  87. lstrcpy(pData, pstr);
  88. pData[lstrlen(pstr)] = '\0';
  89. /* update current log length */
  90. hlog->core.length += length;
  91. LeaveCriticalSection(&hlog->critsec);
  92. }
  93. /* send a log to a named-pipe client */
  94. VOID Log_Send(HANDLE hpipe, HLOG hlog)
  95. {
  96. ss_sendblock(hpipe, (PSTR) &hlog->core, sizeof(hlog->core));
  97. }
  98. VOID
  99. Log_Write(HLOG hlog, char * szFormat, ...)
  100. {
  101. char buf[512];
  102. va_list va;
  103. FILETIME ft;
  104. SYSTEMTIME systime;
  105. va_start(va, szFormat);
  106. wvsprintfA(buf, szFormat, va);
  107. va_end(va);
  108. dprintf1((buf));
  109. GetSystemTime(&systime);
  110. SystemTimeToFileTime(&systime, &ft);
  111. Log_WriteData(hlog, &ft, buf);
  112. }