Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit 8e20b59

Browse files
committed
[Logging][Obj-C] Add external log writer interface.
* Adds NLWeaveLogWriter protocol for external clients to register a log writer to pass Weave logs to a custom logging component. * Adds NLWeaveLogging, with +[NLWeaveLogging setSharedLogWriter:], to manage processing Weave logs. * Updates WDM_LOG macros and nl::Weave::Logging::Log function to pass logs to the new NLWeaveLogging manager.
1 parent c02ab57 commit 8e20b59

File tree

4 files changed

+385
-25
lines changed

4 files changed

+385
-25
lines changed

src/device-manager/cocoa/NLLogging.h

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,53 @@
2222
*
2323
*/
2424

25+
#import <Foundation/Foundation.h>
26+
#import "NLWeaveLogging.h"
27+
28+
NS_ASSUME_NONNULL_BEGIN
29+
2530
#if DEBUG
26-
#define WDM_LOG_DEBUG NSLog
27-
#define WDM_LOG_ERROR NSLog
2831

29-
#define WDM_LOG_METHOD_SIG() ({ NSLog(@"[<%@: %p> %@]", NSStringFromClass([self class]), self, NSStringFromSelector(_cmd)); })
30-
#else
31-
// do nothing
32+
/** Name of the logging module for the Objective-C platform-specific component. */
33+
static NSString * const kNLWeaveDeviceManagerCocoaModuleName = @"DM-Cocoa";
34+
35+
/** Macros for logging Weave messages of various log levels from platform-specific code. */
36+
#define WDM_LOG_DEBUG(fmt, ...) _WDM_LOG(NLLogLevelDetail, fmt, ##__VA_ARGS__)
37+
#define WDM_LOG_INFO(fmt, ...) _WDM_LOG(NLLogLevelProgress, fmt, ##__VA_ARGS__)
38+
#define WDM_LOG_ERROR(fmt, ...) _WDM_LOG(NLLogLevelError, fmt, ##__VA_ARGS__)
39+
40+
/** Macro for logging a method signature (must be called from an Objective-C method). */
41+
#define WDM_LOG_METHOD_SIG() WDM_LOG_INFO(@"<%@: %p>", NSStringFromClass([self class]), self)
42+
43+
/**
44+
* @def _WDM_LOG(logLevel, fmt, ...)
45+
*
46+
* @brief
47+
* Helper macro for formatting Weave logs and delegating log handling to @c NLWeaveLogging.
48+
*
49+
* @param logLevel An @c NLLogLevel specifying the level of the log message.
50+
* @param fmt The log message format.
51+
* @param ... A variable number of arguments to be added to the log message format.
52+
*/
53+
#define _WDM_LOG(logLevel, fmt, ...) \
54+
do { \
55+
NSString * formattedMessage = ([NSString \
56+
stringWithFormat:@"%s:%d %@", __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:fmt, ##__VA_ARGS__]]); \
57+
\
58+
[NLWeaveLogging handleWeaveLogFromModule:NLLogModuleCocoa \
59+
moduleName:kNLWeaveDeviceManagerCocoaModuleName \
60+
level:logLevel \
61+
formattedMessage:formattedMessage]; \
62+
} while (0);
63+
64+
#else // DEBUG
65+
66+
// Do nothing – strip logs from release builds.
3267
#define WDM_LOG_DEBUG(...)
68+
#define WDM_LOG_INFO(...)
3369
#define WDM_LOG_ERROR(...)
3470
#define WDM_LOG_METHOD_SIG() ({})
3571

36-
#endif
72+
#endif // DEBUG
73+
74+
NS_ASSUME_NONNULL_END
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
*
3+
* Copyright (c) 2020 Google LLC
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* @file
21+
* This file defines the @c NLWeaveLogWriter protocol for an external Weave log writer.
22+
*
23+
*/
24+
25+
#import <Foundation/Foundation.h>
26+
#import "NLWeaveLogging.h"
27+
28+
NS_ASSUME_NONNULL_BEGIN
29+
30+
/**
31+
* Defines an interface for an external log writer to register to receive Weave logs.
32+
*
33+
* External clients can implement this protocol to receive Weave log messages for custom handling –
34+
* the shared @c NLWeaveLogWriter can be set using +[NLWeaveLogging setSharedLogWriter:].
35+
*/
36+
@protocol NLWeaveLogWriter <NSObject>
37+
38+
/**
39+
* Method called with individual Weave log messages as they are logged.
40+
*
41+
* @param logModule The Weave module in which the log was created.
42+
* @param logLevel The logging level of the log.
43+
* @param logMessage The formatted log message.
44+
*/
45+
- (void)writeLogFromModule:(NLLogModule)logModule
46+
level:(NLLogLevel)logLevel
47+
message:(NSString *)logMessage NS_SWIFT_NAME(writeLog(from:level:message:));
48+
49+
@end
50+
51+
NS_ASSUME_NONNULL_END
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
*
3+
* Copyright (c) 2020 Google LLC
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* @file
21+
* This file implements a platform-specific Weave log interface.
22+
*
23+
*/
24+
25+
#import <Foundation/Foundation.h>
26+
27+
NS_ASSUME_NONNULL_BEGIN
28+
29+
@protocol NLWeaveLogWriter;
30+
31+
/**
32+
* Weave logging modules – for indicating which component created a log.
33+
*
34+
* @note
35+
* Must align with the order of nl::Weave::Logging::LogModule values.
36+
*/
37+
typedef NS_ENUM(NSInteger, NLLogModule) {
38+
NLLogModuleNotSpecified = 0,
39+
40+
NLLogModuleInet,
41+
NLLogModuleBle,
42+
NLLogModuleMessageLayer,
43+
NLLogModuleSecurityManager,
44+
NLLogModuleExchangeManager,
45+
NLLogModuleTLV,
46+
NLLogModuleASN1,
47+
NLLogModuleCrypto,
48+
NLLogModuleDeviceManager,
49+
NLLogModuleAlarm,
50+
NLLogModuleBDX,
51+
NLLogModuleDataManagement,
52+
NLLogModuleDeviceControl,
53+
NLLogModuleDeviceDescription,
54+
NLLogModuleEcho,
55+
NLLogModuleFabricProvisioning,
56+
NLLogModuleNetworkProvisioning,
57+
NLLogModuleServiceDirectory,
58+
NLLogModuleServiceProvisioning,
59+
NLLogModuleSoftwareUpdate,
60+
NLLogModuleTokenPairing,
61+
NLLogModuleHeatLink,
62+
NLLogModuleTimeService,
63+
NLLogModuleWeaveTunnel,
64+
NLLogModuleHeartbeat,
65+
NLLogModuleWeaveSystemLayer,
66+
NLLogModuleDropcamLegacyPairing,
67+
NLLogModuleEventLogging,
68+
NLLogModuleSupport,
69+
70+
// Module for logs originating from the Objective-C @c NLLogging macros.
71+
//
72+
// Must NOT overlap with the values mapped from nl::Weave::Logging::LogModule.
73+
NLLogModuleCocoa = 100,
74+
};
75+
76+
/**
77+
* Logging levels – for indicating the relative importance of a log message.
78+
*
79+
* @note
80+
* Must align with the order of nl::Weave::Logging::LogCategory values.
81+
*/
82+
typedef NS_ENUM(NSInteger, NLLogLevel) {
83+
NLLogLevelNone = 0,
84+
NLLogLevelError,
85+
NLLogLevelProgress,
86+
NLLogLevelDetail,
87+
NLLogLevelRetain,
88+
};
89+
90+
/**
91+
* Platform-specific component for managing Weave log messages.
92+
*
93+
* Exposes an interface for external clients to configure the shared @c NLWeaveLogWriter – the log
94+
* writer will receive Weave logs from both the shared C++ source code and the platform-specific
95+
* Objective-C source code. This allows clients to connect Weave logs to their own logging system.
96+
*/
97+
@interface NLWeaveLogging : NSObject
98+
99+
#pragma Logging Configuration
100+
101+
/**
102+
* Sets the shared @c NLWeaveLogWriter to start receiving Weave logs.
103+
*
104+
* @note
105+
* This should be called prior to any Weave operations to ensure the log writer has been
106+
* configured before any logs are written. The log writer will only receive messages logged after
107+
* it has been set as the shared writer.
108+
*/
109+
+ (void)setSharedLogWriter:(nullable id<NLWeaveLogWriter>)logWriter;
110+
111+
#pragma Log Methods
112+
113+
/**
114+
* Internal handler method for logging a message to the console and notifying the shared log writer.
115+
*
116+
* @param logModule The logging module to which the log belongs.
117+
* @param logModuleName The name of the logging module.
118+
* @param logLevel The level of the log message.
119+
* @param formattedLogMessage The formatted log message.
120+
*/
121+
+ (void)handleWeaveLogFromModule:(NLLogModule)logModule
122+
moduleName:(NSString *)logModuleName
123+
level:(NLLogLevel)logLevel
124+
formattedMessage:(NSString *)formattedLogMessage;
125+
126+
@end
127+
128+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)