BenchLogger.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2012 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef BenchLogger_DEFINED
  8. #define BenchLogger_DEFINED
  9. #include <stdio.h>
  10. #include "include/core/SkString.h"
  11. #include "include/core/SkTypes.h"
  12. class SkFILEWStream;
  13. /**
  14. * Class that allows logging to a file while simultaneously logging to stdout/stderr.
  15. */
  16. class BenchLogger {
  17. public:
  18. BenchLogger();
  19. /**
  20. * Not virtual, since this class is not intended to be subclassed.
  21. */
  22. ~BenchLogger();
  23. /**
  24. * Specify a file to write progress logs to. Unless this is called with a valid file path,
  25. * BenchLogger will only write to stdout/stderr.
  26. */
  27. bool SetLogFile(const char file[]);
  28. /**
  29. * Log an error to stderr, taking a C style string as input.
  30. */
  31. void logError(const char msg[]) { this->nativeLogError(msg); }
  32. /**
  33. * Log an error to stderr, taking an SkString as input.
  34. */
  35. void logError(const SkString& str) { this->nativeLogError(str.c_str()); }
  36. /**
  37. * Log the progress of the bench tool to both stdout and the log file specified by SetLogFile,
  38. * if any, taking a C style string as input.
  39. */
  40. void logProgress(const char msg[]) {
  41. this->nativeLogProgress(msg);
  42. this->fileWrite(msg, strlen(msg));
  43. }
  44. /**
  45. * Log the progress of the bench tool to both stdout and the log file specified by SetLogFile,
  46. * if any, taking an SkString as input.
  47. */
  48. void logProgress(const SkString& str) {
  49. this->nativeLogProgress(str.c_str());
  50. this->fileWrite(str.c_str(), str.size());
  51. }
  52. private:
  53. #ifdef SK_BUILD_FOR_ANDROID
  54. void nativeLogError(const char msg[]) { SkDebugf("%s", msg); }
  55. #else
  56. void nativeLogError(const char msg[]) { fprintf(stderr, "%s", msg); }
  57. #endif
  58. void nativeLogProgress(const char msg[]) { SkDebugf("%s", msg); }
  59. void fileWrite(const char msg[], size_t size);
  60. SkFILEWStream* fFileStream;
  61. };
  62. #endif // BenchLogger_DEFINED