Source code of Windows XP (NT5)
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.

109 lines
2.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 2000
  6. //
  7. // File: print.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #pragma hdrstop
  12. #include <stdio.h>
  13. #include "print.h"
  14. CPrint::CPrint(
  15. BOOL bVerbose,
  16. LPCWSTR pszLogFile // optional. Default is NULL.
  17. ) : m_bVerbose(bVerbose),
  18. m_pfLog(NULL)
  19. {
  20. if (NULL != pszLogFile)
  21. {
  22. //
  23. // Open the log file.
  24. //
  25. m_pfLog = _wfopen(pszLogFile, L"w");
  26. if (NULL == m_pfLog)
  27. {
  28. fwprintf(stderr, L"Unable to open log file \"%s\"\n", pszLogFile);
  29. fprintf(stderr, _strerror("Reason: "));
  30. fwprintf(stderr, L"Output will go to stderr\n");
  31. }
  32. }
  33. if (NULL == m_pfLog)
  34. {
  35. //
  36. // If no log file is specified or if we failed to open the specified
  37. // log file, default to stderr.
  38. //
  39. m_pfLog = stderr;
  40. }
  41. }
  42. CPrint::~CPrint(
  43. void
  44. )
  45. {
  46. if (NULL != m_pfLog && stderr != m_pfLog)
  47. {
  48. fclose(m_pfLog);
  49. }
  50. }
  51. //
  52. // Some simple printing functions that handle verbose vs. non-verbose mode.
  53. // The output is directed at whatever stream m_pfLog refers to.
  54. // If the user specified a log file on the command line, and that file
  55. // could be opened, all output goes to that file. Otherwise, it
  56. // defaults to stderr.
  57. //
  58. void
  59. CPrint::_Print(
  60. LPCWSTR pszFmt,
  61. va_list args
  62. ) const
  63. {
  64. TraceAssert(NULL != m_pfLog);
  65. TraceAssert(NULL != pszFmt);
  66. vfwprintf(m_pfLog, pszFmt, args);
  67. }
  68. //
  69. // Output regardless of "verbose" mode.
  70. //
  71. void
  72. CPrint::PrintAlways(
  73. LPCWSTR pszFmt,
  74. ...
  75. ) const
  76. {
  77. va_list args;
  78. va_start(args, pszFmt);
  79. _Print(pszFmt, args);
  80. va_end(args);
  81. }
  82. //
  83. // Output only in "verbose" mode.
  84. // Verbose is turned on with the -v cmd line switch.
  85. //
  86. void
  87. CPrint::PrintVerbose(
  88. LPCWSTR pszFmt,
  89. ...
  90. ) const
  91. {
  92. if (m_bVerbose)
  93. {
  94. va_list args;
  95. va_start(args, pszFmt);
  96. _Print(pszFmt, args);
  97. va_end(args);
  98. }
  99. }