motion-logger 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. data/.gitignore +7 -0
  2. data/Gemfile +3 -0
  3. data/README.md +48 -0
  4. data/Rakefile +15 -0
  5. data/app/app_delegate.rb +5 -0
  6. data/lib/logger/log.rb +85 -0
  7. data/lib/logger/version.rb +5 -0
  8. data/lib/motion-logger.rb +14 -0
  9. data/motion-logger.gemspec +19 -0
  10. data/spec/log_spec.rb +41 -0
  11. data/vendor/Podfile.lock +6 -0
  12. data/vendor/Pods/CocoaLumberjack/.gitignore +24 -0
  13. data/vendor/Pods/CocoaLumberjack/.hgignore +6 -0
  14. data/vendor/Pods/CocoaLumberjack/CocoaLumberjack.podspec +18 -0
  15. data/vendor/Pods/CocoaLumberjack/LICENSE.txt +18 -0
  16. data/vendor/Pods/CocoaLumberjack/Lumberjack/DDASLLogger.h +41 -0
  17. data/vendor/Pods/CocoaLumberjack/Lumberjack/DDASLLogger.m +99 -0
  18. data/vendor/Pods/CocoaLumberjack/Lumberjack/DDAbstractDatabaseLogger.h +102 -0
  19. data/vendor/Pods/CocoaLumberjack/Lumberjack/DDAbstractDatabaseLogger.m +618 -0
  20. data/vendor/Pods/CocoaLumberjack/Lumberjack/DDFileLogger.h +334 -0
  21. data/vendor/Pods/CocoaLumberjack/Lumberjack/DDFileLogger.m +1346 -0
  22. data/vendor/Pods/CocoaLumberjack/Lumberjack/DDLog.h +498 -0
  23. data/vendor/Pods/CocoaLumberjack/Lumberjack/DDLog.m +979 -0
  24. data/vendor/Pods/CocoaLumberjack/Lumberjack/DDTTYLogger.h +49 -0
  25. data/vendor/Pods/CocoaLumberjack/Lumberjack/DDTTYLogger.m +186 -0
  26. data/vendor/Pods/CocoaLumberjack/README.markdown +37 -0
  27. data/vendor/Pods/Headers/CocoaLumberjack/DDASLLogger.h +41 -0
  28. data/vendor/Pods/Headers/CocoaLumberjack/DDAbstractDatabaseLogger.h +102 -0
  29. data/vendor/Pods/Headers/CocoaLumberjack/DDFileLogger.h +334 -0
  30. data/vendor/Pods/Headers/CocoaLumberjack/DDLog.h +498 -0
  31. data/vendor/Pods/Headers/CocoaLumberjack/DDTTYLogger.h +49 -0
  32. data/vendor/Pods/Pods-prefix.pch +3 -0
  33. data/vendor/Pods/Pods-resources.sh +15 -0
  34. data/vendor/Pods/Pods.bridgesupport +462 -0
  35. data/vendor/Pods/Pods.xcconfig +4 -0
  36. data/vendor/Pods/build-iPhoneSimulator/libPods.a +0 -0
  37. metadata +104 -0
@@ -0,0 +1,618 @@
1
+ #import "DDAbstractDatabaseLogger.h"
2
+ #import <math.h>
3
+
4
+ /**
5
+ * Welcome to Cocoa Lumberjack!
6
+ *
7
+ * The project page has a wealth of documentation if you have any questions.
8
+ * https://github.com/robbiehanson/CocoaLumberjack
9
+ *
10
+ * If you're new to the project you may wish to read the "Getting Started" wiki.
11
+ * https://github.com/robbiehanson/CocoaLumberjack/wiki/GettingStarted
12
+ **/
13
+
14
+ #if ! __has_feature(objc_arc)
15
+ #warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
16
+ #endif
17
+
18
+ @interface DDAbstractDatabaseLogger ()
19
+ - (void)destroySaveTimer;
20
+ - (void)destroyDeleteTimer;
21
+ @end
22
+
23
+ #pragma mark -
24
+
25
+ @implementation DDAbstractDatabaseLogger
26
+
27
+ - (id)init
28
+ {
29
+ if ((self = [super init]))
30
+ {
31
+ saveThreshold = 500;
32
+ saveInterval = 60; // 60 seconds
33
+ maxAge = (60 * 60 * 24 * 7); // 7 days
34
+ deleteInterval = (60 * 5); // 5 minutes
35
+ }
36
+ return self;
37
+ }
38
+
39
+ - (void)dealloc
40
+ {
41
+ [self destroySaveTimer];
42
+ [self destroyDeleteTimer];
43
+
44
+ }
45
+
46
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47
+ #pragma mark Override Me
48
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
49
+
50
+ - (BOOL)db_log:(DDLogMessage *)logMessage
51
+ {
52
+ // Override me and add your implementation.
53
+ //
54
+ // Return YES if an item was added to the buffer.
55
+ // Return NO if the logMessage was ignored.
56
+
57
+ return NO;
58
+ }
59
+
60
+ - (void)db_save
61
+ {
62
+ // Override me and add your implementation.
63
+ }
64
+
65
+ - (void)db_delete
66
+ {
67
+ // Override me and add your implementation.
68
+ }
69
+
70
+ - (void)db_saveAndDelete
71
+ {
72
+ // Override me and add your implementation.
73
+ }
74
+
75
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
76
+ #pragma mark Private API
77
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
78
+
79
+ - (void)performSaveAndSuspendSaveTimer
80
+ {
81
+ if (unsavedCount > 0)
82
+ {
83
+ if (deleteOnEverySave)
84
+ [self db_saveAndDelete];
85
+ else
86
+ [self db_save];
87
+ }
88
+
89
+ unsavedCount = 0;
90
+ unsavedTime = 0;
91
+
92
+ if (saveTimer && !saveTimerSuspended)
93
+ {
94
+ dispatch_suspend(saveTimer);
95
+ saveTimerSuspended = YES;
96
+ }
97
+ }
98
+
99
+ - (void)performDelete
100
+ {
101
+ if (maxAge > 0.0)
102
+ {
103
+ [self db_delete];
104
+
105
+ lastDeleteTime = dispatch_time(DISPATCH_TIME_NOW, 0);
106
+ }
107
+ }
108
+
109
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
110
+ #pragma mark Timers
111
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
112
+
113
+ - (void)destroySaveTimer
114
+ {
115
+ if (saveTimer)
116
+ {
117
+ dispatch_source_cancel(saveTimer);
118
+ dispatch_release(saveTimer);
119
+ saveTimer = NULL;
120
+ }
121
+ }
122
+
123
+ - (void)updateAndResumeSaveTimer
124
+ {
125
+ if ((saveTimer != NULL) && (saveInterval > 0.0) && (unsavedTime > 0.0))
126
+ {
127
+ uint64_t interval = saveInterval * NSEC_PER_SEC;
128
+ dispatch_time_t startTime = dispatch_time(unsavedTime, interval);
129
+
130
+ dispatch_source_set_timer(saveTimer, startTime, interval, 1.0);
131
+
132
+ if (saveTimerSuspended)
133
+ {
134
+ dispatch_resume(saveTimer);
135
+ saveTimerSuspended = NO;
136
+ }
137
+ }
138
+ }
139
+
140
+ - (void)createSuspendedSaveTimer
141
+ {
142
+ if ((saveTimer == NULL) && (saveInterval > 0.0))
143
+ {
144
+ saveTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, loggerQueue);
145
+
146
+ dispatch_source_set_event_handler(saveTimer, ^{ @autoreleasepool {
147
+
148
+ [self performSaveAndSuspendSaveTimer];
149
+
150
+ }});
151
+
152
+ saveTimerSuspended = YES;
153
+ }
154
+ }
155
+
156
+ - (void)destroyDeleteTimer
157
+ {
158
+ if (deleteTimer)
159
+ {
160
+ dispatch_source_cancel(deleteTimer);
161
+ dispatch_release(deleteTimer);
162
+ deleteTimer = NULL;
163
+ }
164
+ }
165
+
166
+ - (void)updateDeleteTimer
167
+ {
168
+ if ((deleteTimer != NULL) && (deleteInterval > 0.0) && (maxAge > 0.0))
169
+ {
170
+ uint64_t interval = deleteInterval * NSEC_PER_SEC;
171
+ dispatch_time_t startTime;
172
+
173
+ if (lastDeleteTime > 0)
174
+ startTime = dispatch_time(lastDeleteTime, interval);
175
+ else
176
+ startTime = dispatch_time(DISPATCH_TIME_NOW, interval);
177
+
178
+ dispatch_source_set_timer(deleteTimer, startTime, interval, 1.0);
179
+ }
180
+ }
181
+
182
+ - (void)createAndStartDeleteTimer
183
+ {
184
+ if ((deleteTimer == NULL) && (deleteInterval > 0.0) && (maxAge > 0.0))
185
+ {
186
+ deleteTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, loggerQueue);
187
+
188
+ dispatch_source_set_event_handler(deleteTimer, ^{ @autoreleasepool {
189
+
190
+ [self performDelete];
191
+
192
+ }});
193
+
194
+ [self updateDeleteTimer];
195
+
196
+ dispatch_resume(deleteTimer);
197
+ }
198
+ }
199
+
200
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
201
+ #pragma mark Configuration
202
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
203
+
204
+ - (NSUInteger)saveThreshold
205
+ {
206
+ if (dispatch_get_current_queue() == loggerQueue)
207
+ {
208
+ return saveThreshold;
209
+ }
210
+ else
211
+ {
212
+ __block NSUInteger result;
213
+
214
+ dispatch_sync(loggerQueue, ^{
215
+ result = saveThreshold;
216
+ });
217
+
218
+ return result;
219
+ }
220
+ }
221
+
222
+ - (void)setSaveThreshold:(NSUInteger)threshold
223
+ {
224
+ dispatch_block_t block = ^{
225
+
226
+ if (saveThreshold != threshold)
227
+ {
228
+ saveThreshold = threshold;
229
+
230
+ // Since the saveThreshold has changed,
231
+ // we check to see if the current unsavedCount has surpassed the new threshold.
232
+ //
233
+ // If it has, we immediately save the log.
234
+
235
+ if ((unsavedCount >= saveThreshold) && (saveThreshold > 0))
236
+ {
237
+ @autoreleasepool {
238
+
239
+ [self performSaveAndSuspendSaveTimer];
240
+
241
+ }
242
+ }
243
+ }
244
+ };
245
+
246
+ if (dispatch_get_current_queue() == loggerQueue)
247
+ block();
248
+ else
249
+ dispatch_async(loggerQueue, block);
250
+ }
251
+
252
+ - (NSTimeInterval)saveInterval
253
+ {
254
+ if (dispatch_get_current_queue() == loggerQueue)
255
+ {
256
+ return saveInterval;
257
+ }
258
+ else
259
+ {
260
+ __block NSTimeInterval result;
261
+
262
+ dispatch_sync(loggerQueue, ^{
263
+ result = saveInterval;
264
+ });
265
+
266
+ return result;
267
+ }
268
+ }
269
+
270
+ - (void)setSaveInterval:(NSTimeInterval)interval
271
+ {
272
+ dispatch_block_t block = ^{
273
+
274
+ // C99 recommended floating point comparison macro
275
+ // Read: isLessThanOrGreaterThan(floatA, floatB)
276
+
277
+ if (/* saveInterval != interval */ islessgreater(saveInterval, interval))
278
+ {
279
+ saveInterval = interval;
280
+
281
+ // There are several cases we need to handle here.
282
+ //
283
+ // 1. If the saveInterval was previously enabled and it just got disabled,
284
+ // then we need to stop the saveTimer. (And we might as well release it.)
285
+ //
286
+ // 2. If the saveInterval was previously disabled and it just got enabled,
287
+ // then we need to setup the saveTimer. (Plus we might need to do an immediate save.)
288
+ //
289
+ // 3. If the saveInterval increased, then we need to reset the timer so that it fires at the later date.
290
+ //
291
+ // 4. If the saveInterval decreased, then we need to reset the timer so that it fires at an earlier date.
292
+ // (Plus we might need to do an immediate save.)
293
+
294
+ if (saveInterval > 0.0)
295
+ {
296
+ @autoreleasepool
297
+ {
298
+ if (saveTimer == NULL)
299
+ {
300
+ // Handles #2
301
+ //
302
+ // Since the saveTimer uses the unsavedTime to calculate it's first fireDate,
303
+ // if a save is needed the timer will fire immediately.
304
+
305
+ [self createSuspendedSaveTimer];
306
+ [self updateAndResumeSaveTimer];
307
+ }
308
+ else
309
+ {
310
+ // Handles #3
311
+ // Handles #4
312
+ //
313
+ // Since the saveTimer uses the unsavedTime to calculate it's first fireDate,
314
+ // if a save is needed the timer will fire immediately.
315
+
316
+ [self updateAndResumeSaveTimer];
317
+ }
318
+ }
319
+ }
320
+ else if (saveTimer)
321
+ {
322
+ // Handles #1
323
+
324
+ [self destroySaveTimer];
325
+ }
326
+ }
327
+ };
328
+
329
+ if (dispatch_get_current_queue() == loggerQueue)
330
+ block();
331
+ else
332
+ dispatch_async(loggerQueue, block);
333
+ }
334
+
335
+ - (NSTimeInterval)maxAge
336
+ {
337
+ if (dispatch_get_current_queue() == loggerQueue)
338
+ {
339
+ return maxAge;
340
+ }
341
+ else
342
+ {
343
+ __block NSTimeInterval result;
344
+
345
+ dispatch_sync(loggerQueue, ^{
346
+ result = maxAge;
347
+ });
348
+
349
+ return result;
350
+ }
351
+ }
352
+
353
+ - (void)setMaxAge:(NSTimeInterval)interval
354
+ {
355
+ dispatch_block_t block = ^{
356
+
357
+ // C99 recommended floating point comparison macro
358
+ // Read: isLessThanOrGreaterThan(floatA, floatB)
359
+
360
+ if (/* maxAge != interval */ islessgreater(maxAge, interval))
361
+ {
362
+ NSTimeInterval oldMaxAge = maxAge;
363
+ NSTimeInterval newMaxAge = interval;
364
+
365
+ maxAge = interval;
366
+
367
+ // There are several cases we need to handle here.
368
+ //
369
+ // 1. If the maxAge was previously enabled and it just got disabled,
370
+ // then we need to stop the deleteTimer. (And we might as well release it.)
371
+ //
372
+ // 2. If the maxAge was previously disabled and it just got enabled,
373
+ // then we need to setup the deleteTimer. (Plus we might need to do an immediate delete.)
374
+ //
375
+ // 3. If the maxAge was increased,
376
+ // then we don't need to do anything.
377
+ //
378
+ // 4. If the maxAge was decreased,
379
+ // then we should do an immediate delete.
380
+
381
+ BOOL shouldDeleteNow = NO;
382
+
383
+ if (oldMaxAge > 0.0)
384
+ {
385
+ if (newMaxAge <= 0.0)
386
+ {
387
+ // Handles #1
388
+
389
+ [self destroyDeleteTimer];
390
+ }
391
+ else if (oldMaxAge > newMaxAge)
392
+ {
393
+ // Handles #4
394
+ shouldDeleteNow = YES;
395
+ }
396
+ }
397
+ else if (newMaxAge > 0.0)
398
+ {
399
+ // Handles #2
400
+ shouldDeleteNow = YES;
401
+ }
402
+
403
+ if (shouldDeleteNow)
404
+ {
405
+ @autoreleasepool
406
+ {
407
+ [self performDelete];
408
+
409
+ if (deleteTimer)
410
+ [self updateDeleteTimer];
411
+ else
412
+ [self createAndStartDeleteTimer];
413
+ }
414
+ }
415
+ }
416
+ };
417
+
418
+ if (dispatch_get_current_queue() == loggerQueue)
419
+ block();
420
+ else
421
+ dispatch_async(loggerQueue, block);
422
+ }
423
+
424
+ - (NSTimeInterval)deleteInterval
425
+ {
426
+ if (dispatch_get_current_queue() == loggerQueue)
427
+ {
428
+ return deleteInterval;
429
+ }
430
+ else
431
+ {
432
+ __block NSTimeInterval result;
433
+
434
+ dispatch_sync(loggerQueue, ^{
435
+ result = deleteInterval;
436
+ });
437
+
438
+ return result;
439
+ }
440
+ }
441
+
442
+ - (void)setDeleteInterval:(NSTimeInterval)interval
443
+ {
444
+ dispatch_block_t block = ^{
445
+
446
+ // C99 recommended floating point comparison macro
447
+ // Read: isLessThanOrGreaterThan(floatA, floatB)
448
+
449
+ if (/* deleteInterval != interval */ islessgreater(deleteInterval, interval))
450
+ {
451
+ deleteInterval = interval;
452
+
453
+ // There are several cases we need to handle here.
454
+ //
455
+ // 1. If the deleteInterval was previously enabled and it just got disabled,
456
+ // then we need to stop the deleteTimer. (And we might as well release it.)
457
+ //
458
+ // 2. If the deleteInterval was previously disabled and it just got enabled,
459
+ // then we need to setup the deleteTimer. (Plus we might need to do an immediate delete.)
460
+ //
461
+ // 3. If the deleteInterval increased, then we need to reset the timer so that it fires at the later date.
462
+ //
463
+ // 4. If the deleteInterval decreased, then we need to reset the timer so that it fires at an earlier date.
464
+ // (Plus we might need to do an immediate delete.)
465
+
466
+ if (deleteInterval > 0.0)
467
+ {
468
+ @autoreleasepool
469
+ {
470
+ if (deleteTimer == NULL)
471
+ {
472
+ // Handles #2
473
+ //
474
+ // Since the deleteTimer uses the lastDeleteTime to calculate it's first fireDate,
475
+ // if a delete is needed the timer will fire immediately.
476
+
477
+ [self createAndStartDeleteTimer];
478
+ }
479
+ else
480
+ {
481
+ // Handles #3
482
+ // Handles #4
483
+ //
484
+ // Since the deleteTimer uses the lastDeleteTime to calculate it's first fireDate,
485
+ // if a save is needed the timer will fire immediately.
486
+
487
+ [self updateDeleteTimer];
488
+ }
489
+ }
490
+ }
491
+ else if (deleteTimer)
492
+ {
493
+ // Handles #1
494
+
495
+ [self destroyDeleteTimer];
496
+ }
497
+ }
498
+ };
499
+
500
+ if (dispatch_get_current_queue() == loggerQueue)
501
+ block();
502
+ else
503
+ dispatch_async(loggerQueue, block);
504
+ }
505
+
506
+ - (BOOL)deleteOnEverySave
507
+ {
508
+ if (dispatch_get_current_queue() == loggerQueue)
509
+ {
510
+ return deleteOnEverySave;
511
+ }
512
+ else
513
+ {
514
+ __block BOOL result;
515
+
516
+ dispatch_sync(loggerQueue, ^{
517
+ result = deleteOnEverySave;
518
+ });
519
+
520
+ return result;
521
+ }
522
+ }
523
+
524
+ - (void)setDeleteOnEverySave:(BOOL)flag
525
+ {
526
+ dispatch_block_t block = ^{
527
+
528
+ deleteOnEverySave = flag;
529
+ };
530
+
531
+ if (dispatch_get_current_queue() == loggerQueue)
532
+ block();
533
+ else
534
+ dispatch_async(loggerQueue, block);
535
+ }
536
+
537
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
538
+ #pragma mark Public API
539
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
540
+
541
+ - (void)savePendingLogEntries
542
+ {
543
+ dispatch_block_t block = ^{ @autoreleasepool {
544
+
545
+ [self performSaveAndSuspendSaveTimer];
546
+ }};
547
+
548
+ if (dispatch_get_current_queue() == loggerQueue)
549
+ block();
550
+ else
551
+ dispatch_async(loggerQueue, block);
552
+ }
553
+
554
+ - (void)deleteOldLogEntries
555
+ {
556
+ dispatch_block_t block = ^{ @autoreleasepool {
557
+
558
+ [self performDelete];
559
+ }};
560
+
561
+ if (dispatch_get_current_queue() == loggerQueue)
562
+ block();
563
+ else
564
+ dispatch_async(loggerQueue, block);
565
+ }
566
+
567
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
568
+ #pragma mark DDLogger
569
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
570
+
571
+ - (void)didAddLogger
572
+ {
573
+ // If you override me be sure to invoke [super didAddLogger];
574
+
575
+ [self createSuspendedSaveTimer];
576
+
577
+ [self createAndStartDeleteTimer];
578
+ }
579
+
580
+ - (void)willRemoveLogger
581
+ {
582
+ // If you override me be sure to invoke [super willRemoveLogger];
583
+
584
+ [self performSaveAndSuspendSaveTimer];
585
+
586
+ [self destroySaveTimer];
587
+ [self destroyDeleteTimer];
588
+ }
589
+
590
+ - (void)logMessage:(DDLogMessage *)logMessage
591
+ {
592
+ if ([self db_log:logMessage])
593
+ {
594
+ BOOL firstUnsavedEntry = (++unsavedCount == 1);
595
+
596
+ if ((unsavedCount >= saveThreshold) && (saveThreshold > 0))
597
+ {
598
+ [self performSaveAndSuspendSaveTimer];
599
+ }
600
+ else if (firstUnsavedEntry)
601
+ {
602
+ unsavedTime = dispatch_time(DISPATCH_TIME_NOW, 0);
603
+ [self updateAndResumeSaveTimer];
604
+ }
605
+ }
606
+ }
607
+
608
+ - (void)flush
609
+ {
610
+ // This method is invoked by DDLog's flushLog method.
611
+ //
612
+ // It is called automatically when the application quits,
613
+ // or if the developer invokes DDLog's flushLog method prior to crashing or something.
614
+
615
+ [self performSaveAndSuspendSaveTimer];
616
+ }
617
+
618
+ @end