Skip to content

Commit

Permalink
Merge pull request #1457 from asmagill/loggingUpdates
Browse files Browse the repository at this point in the history
Logging updates to LuaSkin
  • Loading branch information
asmagill authored Aug 23, 2017
2 parents 9c7d11e + 1c0c8c5 commit 1aed38f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 154 deletions.
13 changes: 0 additions & 13 deletions LuaSkin/LuaSkin/Skin.h
Original file line number Diff line number Diff line change
Expand Up @@ -757,18 +757,5 @@ NSString *specMaskToString(int spec);
*/
- (NSString *)tracebackWithTag:(NSString *)theTag fromStackPos:(int)level ;

/*!
@abstract Log the specified message with at the specified level with the traceback position prepended.
@discussion Logs the specified message, prepended with the lua chunk name and line number at the specified traceback level, for the specified level. The log level and combined message is logged with @link logAtLevel:withMessage: @/link.
@warning This method is primarily for testing and may be removed in a future release.
@param level The message log level as an integer. Predefined levels are defined and used within LuaSkin itself as (in decreasing level of severity) @link LS_LOG_ERROR @/link, @link LS_LOG_WARN @/link, @link LS_LOG_INFO @/link, @link LS_LOG_DEBUG @/link, and @link LS_LOG_VERBOSE @/link.
@param theMessage the message to log
@param pos the lua traceback level to attempt to retrieve the chunk name and line number from
*/
- (void)logAtLevel:(int)level withMessage:(NSString *)theMessage fromStackPos:(int)pos ;

@end

50 changes: 13 additions & 37 deletions LuaSkin/LuaSkin/Skin.m
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,7 @@ - (BOOL)registerPushNSHelper:(pushNSHelperFunction)helperFN forClass:(const char
[self logAtLevel:LS_LOG_WARN
withMessage:[NSString stringWithFormat:@"registerPushNSHelper:forClass:%s already defined at %@",
className,
self.registeredNSHelperLocations[@(className)]]
fromStackPos:level] ;
self.registeredNSHelperLocations[@(className)]]] ;
} else {
luaL_where(self.L, level) ;
NSString *locationString = @(lua_tostring(self.L, -1)) ;
Expand All @@ -502,8 +501,7 @@ - (BOOL)registerPushNSHelper:(pushNSHelperFunction)helperFN forClass:(const char
}
} else {
[self logAtLevel:LS_LOG_WARN
withMessage:@"registerPushNSHelper:forClass: requires both helperFN and className"
fromStackPos:level] ;
withMessage:@"registerPushNSHelper:forClass: requires both helperFN and className"] ;
}
return allGood ;
}
Expand Down Expand Up @@ -572,8 +570,7 @@ - (BOOL)registerLuaObjectHelper:(luaObjectHelperFunction)helperFN forClass:(cons
[self logAtLevel:LS_LOG_WARN
withMessage:[NSString stringWithFormat:@"registerLuaObjectHelper:forClass:%s already defined at %@",
className,
self.registeredLuaObjectHelperFunctions[@(className)]]
fromStackPos:level] ;
self.registeredLuaObjectHelperFunctions[@(className)]]] ;
} else {
luaL_where(self.L, level) ;
NSString *locationString = @(lua_tostring(self.L, -1)) ;
Expand All @@ -584,8 +581,7 @@ - (BOOL)registerLuaObjectHelper:(luaObjectHelperFunction)helperFN forClass:(cons
}
} else {
[self logAtLevel:LS_LOG_WARN
withMessage:@"registerLuaObjectHelper:forClass: requires both helperFN and className"
fromStackPos:level] ;
withMessage:@"registerLuaObjectHelper:forClass: requires both helperFN and className"] ;
}
return allGood ;
}
Expand Down Expand Up @@ -624,8 +620,7 @@ - (NSRect)tableToRectAtIndex:(int)idx {
return NSMakeRect(x, y, w, h) ;
} else {
[self logAtLevel:LS_LOG_WARN
withMessage:[NSString stringWithFormat:@"returning NSZeroRect: can't make NSRect from %s.", lua_typename(self.L, lua_type(self.L, idx))]
fromStackPos:1] ;
withMessage:[NSString stringWithFormat:@"returning NSZeroRect: can't make NSRect from %s.", lua_typename(self.L, lua_type(self.L, idx))]] ;
return NSZeroRect ;
}
}
Expand All @@ -639,8 +634,7 @@ - (NSPoint)tableToPointAtIndex:(int)idx {
return NSMakePoint(x, y);
} else {
[self logAtLevel:LS_LOG_WARN
withMessage:[NSString stringWithFormat:@"returning NSZeroPoint: can't make NSPoint from %s.", lua_typename(self.L, lua_type(self.L, idx))]
fromStackPos:1] ;
withMessage:[NSString stringWithFormat:@"returning NSZeroPoint: can't make NSPoint from %s.", lua_typename(self.L, lua_type(self.L, idx))]] ;
return NSZeroPoint ;
}
}
Expand All @@ -654,8 +648,7 @@ - (NSSize)tableToSizeAtIndex:(int)idx {
return NSMakeSize(w, h);
} else {
[self logAtLevel:LS_LOG_WARN
withMessage:[NSString stringWithFormat:@"returning NSZeroSize: can't make NSSize from %s.", lua_typename(self.L, lua_type(self.L, idx))]
fromStackPos:1] ;
withMessage:[NSString stringWithFormat:@"returning NSZeroSize: can't make NSSize from %s.", lua_typename(self.L, lua_type(self.L, idx))]] ;
return NSZeroSize ;
}
}
Expand All @@ -677,8 +670,7 @@ - (lua_Integer)maxNatIndex:(int)idx {
}
} else {
[self logAtLevel:LS_LOG_ERROR
withMessage:[NSString stringWithFormat:@"table expected (found %s)", lua_typename(self.L, lua_type(self.L, idx))]
fromStackPos:0] ;
withMessage:[NSString stringWithFormat:@"table expected (found %s)", lua_typename(self.L, lua_type(self.L, idx))]] ;
}
return max ;
}
Expand All @@ -695,8 +687,7 @@ - (lua_Integer)countNatIndex:(int)idx {
}
} else {
[self logAtLevel:LS_LOG_ERROR
withMessage:[NSString stringWithFormat:@"table expected (found %s)", lua_typename(self.L, lua_type(self.L, idx))]
fromStackPos:0] ;
withMessage:[NSString stringWithFormat:@"table expected (found %s)", lua_typename(self.L, lua_type(self.L, idx))]] ;
}
return max ;
}
Expand Down Expand Up @@ -1026,8 +1017,7 @@ - (id)toNSObjectAtIndex:(int)idx withOptions:(NSUInteger)options alreadySeenObje
if (seenObject) {
if ([[seenObject lastObject] isEqualToNumber:@(NO)] && ((options & LS_NSAllowsSelfReference) != LS_NSAllowsSelfReference)) {
[self logAtLevel:LS_LOG_WARN
withMessage:@"lua table cannot contain self-references"
fromStackPos:1] ;
withMessage:@"lua table cannot contain self-references"] ;
// return [NSNull null] ;
return nil ;
} else {
Expand Down Expand Up @@ -1060,8 +1050,7 @@ - (id)toNSObjectAtIndex:(int)idx withOptions:(NSUInteger)options alreadySeenObje
} else {
if (stringOptions != LS_NSNone) {
[self logAtLevel:LS_LOG_DEBUG
withMessage:@"only one of LS_NSPreserveLuaStringExactly or LS_NSLuaStringAsDataOnly can be specified: using default behavior"
fromStackPos:0] ;
withMessage:@"only one of LS_NSPreserveLuaStringExactly or LS_NSLuaStringAsDataOnly can be specified: using default behavior"] ;
}
return [self getValidUTF8AtIndex:idx] ;
}
Expand Down Expand Up @@ -1190,8 +1179,7 @@ - (id)tableAtIndex:(int)idx withOptions:(NSUInteger)options alreadySeenObjects:(
} else {
[self logAtLevel:LS_LOG_ERROR
withMessage:[NSString stringWithFormat:@"array element (%s) cannot be converted into a proper NSObject",
luaL_tolstring(self.L, -1, NULL)]
fromStackPos:1] ;
luaL_tolstring(self.L, -1, NULL)]] ;
result = nil ;
lua_pop(self.L, 2) ; // luaL_tolstring result and lua_geti result
return nil ;
Expand All @@ -1209,8 +1197,7 @@ - (id)tableAtIndex:(int)idx withOptions:(NSUInteger)options alreadySeenObjects:(
[self logAtLevel:LS_LOG_ERROR
withMessage:[NSString stringWithFormat:@"dictionary %@ (%s) cannot be converted into a proper NSObject",
(key) ? @"key" : @"value",
luaL_tolstring(self.L, (key) ? -2 : lua_gettop(self.L), NULL)]
fromStackPos:1] ;
luaL_tolstring(self.L, (key) ? -2 : lua_gettop(self.L), NULL)]] ;
result = nil ;
lua_pop(self.L, 3) ; // luaL_tolstring result, lua_next value, and lua_next key
return nil ;
Expand All @@ -1236,17 +1223,6 @@ - (void) logAtLevel:(int)level withMessage:(NSString *)theMessage {
}
}

// Testing for: chunkname:currentline:theMessage
- (void) logAtLevel:(int)level withMessage:(NSString *)theMessage fromStackPos:(int)pos {
luaL_where(self.L, pos) ;
NSString *locationInfo = @(lua_tostring(self.L, -1)) ;
lua_pop(self.L, 1) ;
if (!locationInfo || [locationInfo isEqualToString:@""])
locationInfo = [NSString stringWithFormat:@"(no lua location info at depth %d)", pos] ;

[self logAtLevel:level withMessage:[NSString stringWithFormat:@"%@:%@", locationInfo, theMessage]] ;
}

// shorthand
- (void)logVerbose:(NSString *)theMessage { [self logAtLevel:LS_LOG_VERBOSE withMessage:theMessage] ; }
- (void)logDebug:(NSString *)theMessage { [self logAtLevel:LS_LOG_DEBUG withMessage:theMessage] ; }
Expand Down
4 changes: 1 addition & 3 deletions extensions/application/internal.m
Original file line number Diff line number Diff line change
Expand Up @@ -1174,9 +1174,7 @@ static int nsrunningapplication_tolua(lua_State *L, id obj) {

if (!new_application(L, [app processIdentifier])) {
lua_pop(L, 1) ; // removed aborted userdata
[[LuaSkin shared] logAtLevel:LS_LOG_WARN
withMessage:[NSString stringWithFormat:@"No Process ID for %@", obj]
fromStackPos:1] ;
[[LuaSkin shared] logWarn:[NSString stringWithFormat:@"No Process ID for %@", obj]] ;
lua_pushnil(L) ;
}
return 1 ;
Expand Down
18 changes: 9 additions & 9 deletions extensions/socket/internal.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,22 @@ - (id)init {
}

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
[LuaSkin logInfo:@"TCP socket connected"];
[LuaSkin logDebug:@"TCP socket connected"];
self.userData = DEFAULT;
if (self.connectCallback != LUA_NOREF)
connectCallback(self);
}

- (void)socket:(GCDAsyncSocket *)sock didConnectToUrl:(NSURL *)url {
[LuaSkin logInfo:@"TCP Unix domain socket connected"];
[LuaSkin logDebug:@"TCP Unix domain socket connected"];
self.userData = DEFAULT;
self.unixSocketPath = [url path];
if (self.connectCallback != LUA_NOREF)
connectCallback(self);
}

- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket {
[LuaSkin logInfo:@"TCP client connected"];
[LuaSkin logDebug:@"TCP client connected"];
newSocket.userData = CLIENT;

@synchronized(self.connectedSockets) {
Expand All @@ -104,12 +104,12 @@ - (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSo

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err {
if (sock.userData == CLIENT) {
[LuaSkin logInfo:[NSString stringWithFormat:@"TCP client disconnected: %@", [err localizedDescription]]];
[LuaSkin logDebug:[NSString stringWithFormat:@"TCP client disconnected: %@", [err localizedDescription]]];
@synchronized(self.connectedSockets) {
[self.connectedSockets removeObject:sock];
}
} else if (sock.userData == SERVER) {
[LuaSkin logInfo:[NSString stringWithFormat:@"TCP server disconnected: %@", [err localizedDescription]]];
[LuaSkin logDebug:[NSString stringWithFormat:@"TCP server disconnected: %@", [err localizedDescription]]];
@synchronized(self.connectedSockets) {
for (HSAsyncTcpSocket *client in self.connectedSockets)
[client disconnect];
Expand All @@ -123,19 +123,19 @@ - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err {
self.unixSocketPath = nil;
}
} else
[LuaSkin logInfo:[NSString stringWithFormat:@"TCP socket disconnected: %@", [err localizedDescription]]];
[LuaSkin logDebug:[NSString stringWithFormat:@"TCP socket disconnected: %@", [err localizedDescription]]];

sock.userData = nil;
}

- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag {
[LuaSkin logInfo:@"Data written to TCP socket"];
[LuaSkin logDebug:@"Data written to TCP socket"];
if (self.writeCallback != LUA_NOREF)
writeCallback(self, tag);
}

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
[LuaSkin logInfo:@"Data read from TCP socket"];
[LuaSkin logDebug:@"Data read from TCP socket"];
if (self.readCallback != LUA_NOREF)
readCallback(self, data, tag);
}
Expand All @@ -148,7 +148,7 @@ - (void)socket:(GCDAsyncSocket *)sock didReceiveTrust:(SecTrustRef)trust complet
}

- (void)socketDidSecure:(GCDAsyncSocket *)sock {
[LuaSkin logInfo:@"TCP socket secured"];
[LuaSkin logDebug:@"TCP socket secured"];
}

@end
Expand Down
8 changes: 4 additions & 4 deletions extensions/socket/udp.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ - (id)init {
}

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didConnectToAddress:(NSData *)address {
[LuaSkin logInfo:@"UDP socket connected"];
[LuaSkin logDebug:@"UDP socket connected"];
self.userData = DEFAULT;
if (self.connectCallback != LUA_NOREF)
connectCallback(self);
Expand All @@ -87,12 +87,12 @@ - (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotConnect:(NSError *)error {
}

- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error {
[LuaSkin logInfo:[NSString stringWithFormat:@"UDP socket closed: %@", [error localizedDescription]]];
[LuaSkin logDebug:[NSString stringWithFormat:@"UDP socket closed: %@", [error localizedDescription]]];
sock.userData = nil;
}

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag {
[LuaSkin logInfo:@"Data written to UDP socket"];
[LuaSkin logDebug:@"Data written to UDP socket"];
if (self.writeCallback != LUA_NOREF)
writeCallback(self, tag);
}
Expand All @@ -103,7 +103,7 @@ - (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueT
}

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext {
[LuaSkin logInfo:@"Data read from UDP socket"];
[LuaSkin logDebug:@"Data read from UDP socket"];
if (self.readCallback != LUA_NOREF)
readCallback(self, data, address);
}
Expand Down
Loading

0 comments on commit 1aed38f

Please sign in to comment.