DDAbstractDatabaseLogger.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2016, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. // Disable legacy macros
  16. #ifndef DD_LEGACY_MACROS
  17. #define DD_LEGACY_MACROS 0
  18. #endif
  19. #import "DDLog.h"
  20. /**
  21. * This class provides an abstract implementation of a database logger.
  22. *
  23. * That is, it provides the base implementation for a database logger to build atop of.
  24. * All that is needed for a concrete database logger is to extend this class
  25. * and override the methods in the implementation file that are prefixed with "db_".
  26. **/
  27. @interface DDAbstractDatabaseLogger : DDAbstractLogger {
  28. @protected
  29. NSUInteger _saveThreshold;
  30. NSTimeInterval _saveInterval;
  31. NSTimeInterval _maxAge;
  32. NSTimeInterval _deleteInterval;
  33. BOOL _deleteOnEverySave;
  34. BOOL _saveTimerSuspended;
  35. NSUInteger _unsavedCount;
  36. dispatch_time_t _unsavedTime;
  37. dispatch_source_t _saveTimer;
  38. dispatch_time_t _lastDeleteTime;
  39. dispatch_source_t _deleteTimer;
  40. }
  41. /**
  42. * Specifies how often to save the data to disk.
  43. * Since saving is an expensive operation (disk io) it is not done after every log statement.
  44. * These properties allow you to configure how/when the logger saves to disk.
  45. *
  46. * A save is done when either (whichever happens first):
  47. *
  48. * - The number of unsaved log entries reaches saveThreshold
  49. * - The amount of time since the oldest unsaved log entry was created reaches saveInterval
  50. *
  51. * You can optionally disable the saveThreshold by setting it to zero.
  52. * If you disable the saveThreshold you are entirely dependent on the saveInterval.
  53. *
  54. * You can optionally disable the saveInterval by setting it to zero (or a negative value).
  55. * If you disable the saveInterval you are entirely dependent on the saveThreshold.
  56. *
  57. * It's not wise to disable both saveThreshold and saveInterval.
  58. *
  59. * The default saveThreshold is 500.
  60. * The default saveInterval is 60 seconds.
  61. **/
  62. @property (assign, readwrite) NSUInteger saveThreshold;
  63. /**
  64. * See the description for the `saveThreshold` property
  65. */
  66. @property (assign, readwrite) NSTimeInterval saveInterval;
  67. /**
  68. * It is likely you don't want the log entries to persist forever.
  69. * Doing so would allow the database to grow infinitely large over time.
  70. *
  71. * The maxAge property provides a way to specify how old a log statement can get
  72. * before it should get deleted from the database.
  73. *
  74. * The deleteInterval specifies how often to sweep for old log entries.
  75. * Since deleting is an expensive operation (disk io) is is done on a fixed interval.
  76. *
  77. * An alternative to the deleteInterval is the deleteOnEverySave option.
  78. * This specifies that old log entries should be deleted during every save operation.
  79. *
  80. * You can optionally disable the maxAge by setting it to zero (or a negative value).
  81. * If you disable the maxAge then old log statements are not deleted.
  82. *
  83. * You can optionally disable the deleteInterval by setting it to zero (or a negative value).
  84. *
  85. * If you disable both deleteInterval and deleteOnEverySave then old log statements are not deleted.
  86. *
  87. * It's not wise to enable both deleteInterval and deleteOnEverySave.
  88. *
  89. * The default maxAge is 7 days.
  90. * The default deleteInterval is 5 minutes.
  91. * The default deleteOnEverySave is NO.
  92. **/
  93. @property (assign, readwrite) NSTimeInterval maxAge;
  94. /**
  95. * See the description for the `maxAge` property
  96. */
  97. @property (assign, readwrite) NSTimeInterval deleteInterval;
  98. /**
  99. * See the description for the `maxAge` property
  100. */
  101. @property (assign, readwrite) BOOL deleteOnEverySave;
  102. /**
  103. * Forces a save of any pending log entries (flushes log entries to disk).
  104. **/
  105. - (void)savePendingLogEntries;
  106. /**
  107. * Removes any log entries that are older than maxAge.
  108. **/
  109. - (void)deleteOldLogEntries;
  110. @end