log.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/benchmarks/log.cpp
  3. // Purpose: Log-related benchmarks
  4. // Author: Vadim Zeitlin
  5. // Created: 2012-01-21
  6. // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #include "bench.h"
  10. #include "wx/log.h"
  11. // This class is used to check that the arguments of log functions are not
  12. // evaluated.
  13. struct NotCreated
  14. {
  15. NotCreated() { wxAbort(); }
  16. const char* AsStr() const { return "unreachable"; }
  17. };
  18. // Temporarily change the log level to the given one.
  19. class LogLevelSetter
  20. {
  21. public:
  22. LogLevelSetter(wxLogLevel levelNew)
  23. : m_levelOld(wxLog::GetLogLevel())
  24. {
  25. wxLog::SetLogLevel(levelNew);
  26. }
  27. ~LogLevelSetter()
  28. {
  29. wxLog::SetLogLevel(m_levelOld);
  30. }
  31. private:
  32. const wxLogLevel m_levelOld;
  33. wxDECLARE_NO_COPY_CLASS(LogLevelSetter);
  34. };
  35. BENCHMARK_FUNC(LogDebugDisabled)
  36. {
  37. LogLevelSetter level(wxLOG_Info);
  38. wxLogDebug("Ignored debug message: %s", NotCreated().AsStr());
  39. return true;
  40. }
  41. BENCHMARK_FUNC(LogTraceDisabled)
  42. {
  43. LogLevelSetter level(wxLOG_Info);
  44. wxLogTrace("", NotCreated().AsStr());
  45. return true;
  46. }
  47. BENCHMARK_FUNC(LogTraceActive)
  48. {
  49. static bool s_added = false;
  50. if ( !s_added )
  51. {
  52. s_added = true;
  53. wxLog::AddTraceMask("logbench");
  54. }
  55. // Remove the actual logging overhead by simply throwing away the log
  56. // messages.
  57. class NulLog : public wxLog
  58. {
  59. public:
  60. NulLog()
  61. : m_logOld(wxLog::SetActiveTarget(this))
  62. {
  63. }
  64. virtual ~NulLog()
  65. {
  66. wxLog::SetActiveTarget(m_logOld);
  67. }
  68. protected:
  69. virtual void DoLogRecord(wxLogLevel,
  70. const wxString&,
  71. const wxLogRecordInfo&)
  72. {
  73. }
  74. wxLog* m_logOld;
  75. };
  76. NulLog nulLog;
  77. wxLogTrace("logbench", "Trace message");
  78. return true;
  79. }
  80. BENCHMARK_FUNC(LogTraceInactive)
  81. {
  82. wxLogTrace("bloordyblop", "Trace message");
  83. return true;
  84. }