Skip to content

Commit

Permalink
Migrated from NSLog to OSLog
Browse files Browse the repository at this point in the history
  • Loading branch information
newmarcel committed Jun 15, 2023
1 parent 375d5b1 commit c5a49e7
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 47 deletions.
19 changes: 10 additions & 9 deletions KeepingYouAwake/KYADefines.h
Expand Up @@ -6,17 +6,18 @@
// Copyright © 2017 Marcel Dierkes. All rights reserved.
//

#ifndef KYA_DEFINES_H
#define KYA_DEFINES_H
#pragma once

#import <Foundation/Foundation.h>
#import <os/log.h>

// Auto Type
#define Auto const __auto_type
#define AutoVar __auto_type
#define AutoWeak __weak __auto_type const

#if DEBUG
#define KYALog(_args...) NSLog(_args)
#else
#define KYALog(_args...)
#endif

#endif /* KYA_DEFINES_H */
// Logging
NS_INLINE os_log_t KYALogCreateWithCategory(const char *category)
{
return os_log_create("info.marcel-dierkes.KeepingYouAwake", category);
}
Expand Up @@ -8,6 +8,7 @@

#import <KYAActivationDurations/KYAActivationDuration.h>
#import "KYADefines.h"
#import "KYAActivationDurationsLog.h"
#include <chrono>

NSTimeInterval const KYAActivationDurationIndefinite = 0.0f;
Expand Down Expand Up @@ -56,21 +57,21 @@ - (instancetype)initWithHours:(NSInteger)hours minutes:(NSInteger)minutes second
auto minutesValue = std::chrono::minutes { minutes };
if(minutesValue > 1h)
{
KYALog(@"Attempted to add a duration with a minutes component value greater than an hour.");
os_log_fault(KYAActivationDurationsLog(), "Attempted to add a duration with a minutes component value greater than an hour.");
return nil;
}

auto secondsValue = std::chrono::seconds { seconds };
if(secondsValue > 1min)
{
KYALog(@"Attempted to add a duration with a seconds component value greater than a minute.");
os_log_fault(KYAActivationDurationsLog(), "Attempted to add a duration with a seconds component value greater than a minute.");
return nil;
}

std::chrono::seconds totalValue = hoursValue + minutesValue + secondsValue;
if(totalValue == 0s)
{
KYALog(@"Attempted to add a 0 duration.");
os_log_fault(KYAActivationDurationsLog(), "Attempted to add a 0 duration.");
return nil;
}

Expand Down
Expand Up @@ -9,6 +9,7 @@
#import <KYAActivationDurations/KYAActivationDurationsController.h>
#import <KYAActivationDurations/NSUserDefaults+KYADefaultTimeInterval.h>
#import "KYADefines.h"
#import "KYAActivationDurationsLog.h"

#define KYA_USES_SIMPLE_USER_DEFAULTS_VALUES 1

Expand Down Expand Up @@ -224,7 +225,7 @@ - (void)loadFromUserDefaults
error:&error];
if(error != nil)
{
KYALog(@"Failed to unarchive durations %@", error.userInfo);
os_log_error(KYAActivationDurationsLog(), "Failed to unarchive durations %{public}@", error.userInfo);
}
}
#endif
Expand All @@ -241,7 +242,7 @@ - (void)loadFromUserDefaults
#else
self.storedActivationDurations = [NSMutableArray arrayWithArray:loadedDurations];
#endif
KYALog(@"Loaded durations from user defaults: %@", loadedDurations);
os_log(KYAActivationDurationsLog(), "Loaded durations from user defaults: %{public}@", loadedDurations);
}
}

Expand All @@ -264,15 +265,15 @@ - (void)saveToUserDefaults
error:&error];
if(error != nil)
{
KYALog(@"Failed to archive durations %@", error.userInfo);
os_log(KYAActivationDurationsLog(), "Failed to archive durations %{public}@", error.userInfo);
return;
}
[self.userDefaults setObject:data forKey:KYADefaultsKeyDurations];
#endif

[self.userDefaults synchronize];

KYALog(@"Saved durations to user defaults: %@", self.storedActivationDurations);
os_log(KYAActivationDurationsLog(), "Saved durations to user defaults: %{public}@", self.storedActivationDurations);
}

@end
@@ -0,0 +1,16 @@
//
// KYAActivationDurationsLog.h
// KYAActivationDurations
//
// Created by Marcel Dierkes on 14.05.23.
//

#import <Foundation/Foundation.h>
#import <os/log.h>

NS_ASSUME_NONNULL_BEGIN

/// Returns the default log for the ActivationDurations category.
FOUNDATION_EXPORT os_log_t KYAActivationDurationsLog(void);

NS_ASSUME_NONNULL_END
@@ -0,0 +1,19 @@
//
// KYAActivationDurationsLog.m
// KYAActivationDurations
//
// Created by Marcel Dierkes on 14.05.23.
//

#import "KYAActivationDurationsLog.h"
#import "KYADefines.h"

os_log_t KYAActivationDurationsLog()
{
static os_log_t log;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
log = KYALogCreateWithCategory("ActivationDurations");
});
return log;
}
Expand Up @@ -12,6 +12,7 @@
@interface KYAEventHandler ()
@property (nonatomic) dispatch_queue_t eventQueue;
@property (nonatomic) NSMapTable *eventTable;
@property (nonatomic) os_log_t log;
@end

@implementation KYAEventHandler
Expand All @@ -33,6 +34,7 @@ - (instancetype)init
{
self.eventQueue = dispatch_queue_create("info.marcel-dierkes.KeepingYouAwake.EventQueue", DISPATCH_QUEUE_SERIAL);
self.eventTable = [NSMapTable strongToStrongObjectsMapTable];
self.log = KYALogCreateWithCategory("ApplicationEvents");
}
return self;
}
Expand All @@ -57,7 +59,7 @@ - (void)handleEventForURL:(NSURL *)URL
KYAEventHandlerActionBlock actionBlock = [self.eventTable objectForKey:event.name];
if(actionBlock)
{
KYALog(@"Handling event:\n%@\nfor URL: %@", event, URL);
os_log(self.log, "Handling event:\n%{public}@\nfor URL: %{public}@", event, URL);
// Perform the action block on main queue, but in sync with the event queue
dispatch_sync(dispatch_get_main_queue(), ^{
actionBlock(event);
Expand Down
@@ -0,0 +1,16 @@
//
// KYAApplicationSupportLog.h
// KYAApplicationSupport
//
// Created by Marcel Dierkes on 14.05.23.
//

#import <Foundation/Foundation.h>
#import <os/log.h>

NS_ASSUME_NONNULL_BEGIN

/// Returns the default log for the ApplicationSupport category.
FOUNDATION_EXPORT os_log_t KYAApplicationSupportLog(void);

NS_ASSUME_NONNULL_END
@@ -0,0 +1,19 @@
//
// KYAApplicationSupportLog.m
// KYAApplicationSupport
//
// Created by Marcel Dierkes on 14.05.23.
//

#import "KYAApplicationSupportLog.h"
#import "KYADefines.h"

os_log_t KYAApplicationSupportLog()
{
static os_log_t log;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
log = KYALogCreateWithCategory("ApplicationSupport");
});
return log;
}
Expand Up @@ -9,6 +9,7 @@
#import <KYAApplicationSupport/NSApplication+KYALaunchAtLogin.h>
#import <ServiceManagement/ServiceManagement.h>
#import "KYADefines.h"
#import "KYAApplicationSupportLog.h"

@implementation NSApplication (KYALaunchAtLogin)

Expand Down Expand Up @@ -84,7 +85,7 @@ NS_INLINE void KYALaunchAtLoginAppServiceSetEnabled(BOOL enabled) API_AVAILABLE(

if(error != nil)
{
KYALog(@"Failed to register launch at login %@", error.userInfo);
os_log_error(KYAApplicationSupportLog(), "Failed to register launch at login %{public}@", error.userInfo);
}
}
else
Expand All @@ -94,7 +95,7 @@ NS_INLINE void KYALaunchAtLoginAppServiceSetEnabled(BOOL enabled) API_AVAILABLE(

if(error != nil)
{
KYALog(@"Failed to unregister launch at login %@", error.userInfo);
os_log_error(KYAApplicationSupportLog(), "Failed to unregister launch at login %{public}@", error.userInfo);
}
}
}
Expand Down Expand Up @@ -136,7 +137,7 @@ NS_INLINE BOOL KYALaunchAtLoginLoginItemIsEnabled()
Boolean success = SMLoginItemSetEnabled((__bridge CFStringRef)KYALauncherBundleIdentifier, (Boolean)enabled);
if(success == false)
{
KYALog(@"Failed to set login item to %@", @(enabled));
os_log_fault(KYAApplicationSupportLog(), "Failed to set login item to %{public}@", @(enabled));
}
return enabled;
}
Expand All @@ -150,7 +151,7 @@ NS_INLINE void KYALaunchAtLoginLoginItemSetEnabled(BOOL enabled)
}
else
{
KYALog(@"Failed to set login item to %@", @(enabled));
os_log_fault(KYAApplicationSupportLog(), "Failed to set login item to %{public}@", @(enabled));
}
}

Expand Down
Expand Up @@ -8,24 +8,25 @@
#import <XCTest/XCTest.h>
#import <KYAApplicationSupport/KYAApplicationSupport.h>
#import "../../Sources/KYAApplicationSupport/KYADefines.h"

#define KYA_GENERATE_BOOL_TEST(_short_getter_name, _property_name, _defaults_key) \
- (void)testProperty_##_property_name \
{ \
Auto defaults = self.defaults; \
Auto key = _defaults_key; \
\
defaults.kya_##_property_name = YES; \
XCTAssertTrue([defaults kya_##_short_getter_name]); \
XCTAssertTrue([defaults boolForKey:key]); \
\
defaults.kya_##_property_name = NO; \
XCTAssertFalse([defaults kya_##_short_getter_name]); \
XCTAssertFalse([defaults boolForKey:key]); \
\
[defaults setBool:YES forKey:key]; \
XCTAssertTrue([defaults kya_##_short_getter_name]); \
KYALog(@"Tested User Defaults Key '%@'.", key); \
#import "../../Sources/KYAApplicationSupport/KYAApplicationSupportLog.h"

#define KYA_GENERATE_BOOL_TEST(_short_getter_name, _property_name, _defaults_key) \
- (void)testProperty_##_property_name \
{ \
Auto defaults = self.defaults; \
Auto key = _defaults_key; \
\
defaults.kya_##_property_name = YES; \
XCTAssertTrue([defaults kya_##_short_getter_name]); \
XCTAssertTrue([defaults boolForKey:key]); \
\
defaults.kya_##_property_name = NO; \
XCTAssertFalse([defaults kya_##_short_getter_name]); \
XCTAssertFalse([defaults boolForKey:key]); \
\
[defaults setBool:YES forKey:key]; \
XCTAssertTrue([defaults kya_##_short_getter_name]); \
os_log(KYAApplicationSupportLog(), "Tested User Defaults Key '%{public}@'.", key); \
}

@interface KYAUserDefaultsKeysTests : XCTestCase
Expand Down
Expand Up @@ -19,6 +19,7 @@
@interface KYADevice ()
@property (nonatomic, readwrite) KYABatteryMonitor *batteryMonitor;
@property (nonatomic, readwrite) KYALowPowerModeMonitor *lowPowerModeMonitor;
@property (nonatomic) os_log_t log;
@end

@implementation KYADevice
Expand All @@ -42,6 +43,7 @@ - (instancetype)init
{
self.batteryMonitor = [KYABatteryMonitor new];
self.lowPowerModeMonitor = [KYALowPowerModeMonitor new];
self.log = KYALogCreateWithCategory("DeviceInfo");
}
return self;
}
Expand Down Expand Up @@ -79,7 +81,7 @@ - (void)setBatteryMonitoringEnabled:(BOOL)batteryMonitoringEnabled
batteryMonitor.capacityChangeHandler = nil;
}

KYALog(@"Battery Monitoring: %@", batteryMonitoringEnabled ? @"YES" : @"NO");
os_log(self.log, "Battery Monitoring: %{public}@", batteryMonitoringEnabled ? @"YES" : @"NO");
}

#pragma mark - Lower Power Mode
Expand Down Expand Up @@ -115,7 +117,7 @@ - (void)setLowPowerModeMonitoringEnabled:(BOOL)lowPowerModeMonitoringEnabled
lowPowerModeMonitor.lowPowerModeChangeHandler = nil;
}

KYALog(@"Low Power Mode Monitoring: %@", lowPowerModeMonitoringEnabled ? @"YES" : @"NO");
os_log(self.log, "Low Power Mode Monitoring: %{public}@", lowPowerModeMonitoringEnabled ? @"YES" : @"NO");
}

#pragma mark - Notifications
Expand All @@ -135,7 +137,7 @@ - (void)deviceParameterDidChange:(KYADeviceParameter)deviceParameter
userInfo:userInfo];
});

KYALog(@"Device parameter did change: %@, %@.", deviceParameter, userInfo);
os_log(self.log, "Device parameter did change: %{public}@, %{public}@.", deviceParameter, userInfo);
}

@end
Expand Up @@ -15,6 +15,7 @@ @interface KYASleepWakeTimer ()
@property (copy, nonatomic, readwrite) NSDate *fireDate;
@property (nonatomic, readwrite) NSTimeInterval scheduledTimeInterval;
@property (nonatomic) NSTask *caffeinateTask;
@property (nonatomic) os_log_t log;
@end

@implementation KYASleepWakeTimer
Expand All @@ -33,6 +34,8 @@ - (instancetype)init
usingBlock:^(NSNotification *note) {
[weakSelf invalidate];
}];

self.log = KYALogCreateWithCategory("SleepWakeTimer");
}
return self;
}
Expand All @@ -46,7 +49,7 @@ - (void)dealloc

- (void)scheduleWithTimeInterval:(NSTimeInterval)timeInterval completion:(KYASleepWakeTimerCompletionBlock)completion
{
KYALog(@"%@ will activate with time interval %@.", self, @(timeInterval));
os_log(self.log, "%{public}@ will activate with time interval %{public}@.", self, @(timeInterval));

Auto delegate = self.delegate;
if([self.delegate respondsToSelector:@selector(sleepWakeTimer:willActivateWithTimeInterval:)])
Expand Down Expand Up @@ -154,7 +157,7 @@ - (void)terminateWithForce:(BOOL)forcedTermination
}
});

KYALog(@"%@ did deactivate.", self);
os_log(self.log, "%{public}@ did deactivate.", self);
}

@end
Expand Up @@ -53,7 +53,8 @@ - (void)configureStatusItem
{
button.contentTintColor = NSColor.systemBlueColor;
}
KYALog(@"Blue status bar item color is enabled for DEBUG builds.");
Auto log = KYALogCreateWithCategory("StatusItemUI");
os_log_debug(log, "Blue status bar item color is enabled for DEBUG builds.");
#endif

self.systemStatusItem = statusItem;
Expand Down

0 comments on commit c5a49e7

Please sign in to comment.