Logger.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "stdafx.h"
  2. #include "Logger.h"
  3. Logger::Logger()
  4. {
  5. indentation = 0;
  6. isNewLine = false;
  7. }
  8. void Logger::Write(char const* format, ...)
  9. {
  10. // Write the string.
  11. va_list args;
  12. va_start(args, format);
  13. Write(format, args);
  14. va_end(args);
  15. }
  16. void Logger::WriteLine(char const* format, ...)
  17. {
  18. // Write the string.
  19. va_list args;
  20. va_start(args, format);
  21. Write(format, args);
  22. va_end(args);
  23. // Write the carriage return.
  24. printf("\n");
  25. isNewLine = true;
  26. }
  27. void Logger::Write(char const* format, va_list args)
  28. {
  29. // Indent if this is the first text on a new line.
  30. if (isNewLine)
  31. {
  32. isNewLine = false;
  33. for (int i = 0; i < indentation; i++)
  34. {
  35. printf(" ");
  36. }
  37. }
  38. // Write the string.
  39. vprintf(format, args);
  40. }
  41. void Logger::WriteBytes(_In_z_ char const* name, vector<uint8_t> const& bytes)
  42. {
  43. WriteLine("%s: %u bytes", name, bytes.size());
  44. Indent();
  45. for (size_t i = 0; i < bytes.size(); i++)
  46. {
  47. if (((i & 15) == 15) || (i == bytes.size() - 1))
  48. {
  49. WriteLine("%02X", bytes[i]);
  50. if (i >= 1024 && bytes.size() > 2048)
  51. {
  52. WriteLine("{snip: not bothering to print the remaining %d bytes}", bytes.size() - i);
  53. break;
  54. }
  55. }
  56. else
  57. {
  58. Write("%02X, ", bytes[i]);
  59. }
  60. }
  61. Unindent();
  62. }
  63. void Logger::WriteEnum(_In_z_ char const* name, int32_t value, _In_z_ _Deref_pre_z_ char const* const* enumValues)
  64. {
  65. if ((value >= 0) && (find(enumValues, enumValues + value, nullptr) == enumValues + value))
  66. {
  67. WriteLine("%s: %s", name, enumValues[value]);
  68. }
  69. else
  70. {
  71. WriteLine("%s: unknown enum value %d", name, value);
  72. }
  73. }