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.

123 lines
2.5 KiB

  1. /*
  2. This is the class that is used for printing output to a log file
  3. or to standard output.
  4. */
  5. #include <nt.h>
  6. #include <ntrtl.h>
  7. #include <nturtl.h>
  8. #include <windows.h>
  9. #include <assert.h>
  10. #include "output.hpp"
  11. #include "strsafe.h"
  12. FILE *SymOutput::Open(LPTSTR szFileName)
  13. {
  14. //
  15. // If we assigned szFileName as filename, we do not need to copy it to itself.
  16. //
  17. if ( (szFileName != NULL) && (_tcscmp(szFileName, filename) != 0) ) {
  18. StringCbCopy(filename, sizeof(filename), szFileName);
  19. }
  20. //
  21. // If we assigned the NULL value, we point fh back to stdout.
  22. // Otherwise, we fopen this file
  23. //
  24. if (szFileName == NULL) {
  25. fh = stdout;
  26. } else if ( ( fh = fopen(filename, "a+t") ) == NULL ) {
  27. this->printf( "Standard output redirect failed.");
  28. return NULL;
  29. }
  30. return fh;
  31. }
  32. void SymOutput::Close(void)
  33. {
  34. //
  35. // Close File Handle, if it is not NULL or stdout.
  36. //
  37. if ((fh != NULL) && (fh != stdout)) {
  38. fflush(fh);
  39. fclose(fh);
  40. }
  41. }
  42. void SymOutput::FreeFileName(void)
  43. {
  44. if ( _tcscmp( filename, _T("") ) != 0 ) {
  45. StringCbCopy( filename, sizeof(filename), _T("") );
  46. }
  47. }
  48. FILE *SymOutput::SetFileName(LPTSTR szFileName)
  49. {
  50. //
  51. // We set the new file name, only if we specified the /d option.
  52. // Otherwise, the result goes to standard out
  53. //
  54. this->Close();
  55. this->FreeFileName();
  56. return this->Open(szFileName);
  57. }
  58. int SymOutput::stdprintf(const char *format, ...)
  59. {
  60. va_list ap;
  61. int r;
  62. //
  63. // If we previous use printf, print '\n' if fh goes to standard out
  64. //
  65. if ( ( ( sw & 2 ) == 0 ) && ( stdout == fh ) ) {
  66. r = _tprintf("\n");
  67. }
  68. sw = 2;
  69. va_start(ap, format);
  70. r = _vtprintf(format, ap);
  71. va_end(ap);
  72. return r;
  73. }
  74. int SymOutput::printf(const char *format, ...)
  75. {
  76. va_list ap;
  77. int r;
  78. // If we previous use stdprintf, print '\n' if fh goes to standard out
  79. if ( ( ( sw & 1 ) == 0 ) && ( stdout == fh ) ) {
  80. r = _ftprintf(fh, "\n");
  81. }
  82. sw = 1;
  83. va_start(ap, format);
  84. r = _vftprintf(fh, format, ap);
  85. if (fh != stdout) {
  86. fflush(fh);
  87. }
  88. va_end(ap);
  89. return r;
  90. }
  91. SymOutput::SymOutput()
  92. {
  93. fh = stdout;
  94. StringCbCopy( filename, sizeof(filename), _T("") );
  95. sw = 3;
  96. }
  97. SymOutput::~SymOutput()
  98. {
  99. this->Close();
  100. }