forked from jnordberg/irccloudapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSConsole.m
97 lines (78 loc) · 1.98 KB
/
JSConsole.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// JSConsole.m
// irccloudapp
//
// Created by Johan Nordberg on 2011-03-11.
// Copyright 2011 FFFF00 Agents AB. All rights reserved.
//
#import "JSConsole.h"
typedef enum {
JSLogLevelInfo,
JSLogLevelWarn,
JSLogLevelError,
} JSLogLevel;
@interface JSConsole (PrivateMethods)
- (NSString *)stringFromWebScriptObject:(WebScriptObject *)wsObj;
- (void)log:(id)value level:(JSLogLevel)level;
@end
@implementation JSConsole
- (NSString *)stringFromWebScriptObject:(WebScriptObject *)wsObj {
return [wsObj callWebScriptMethod:@"toString" withArguments:nil];
}
- (void)log:(id)value level:(JSLogLevel)level {
NSString *message;
if ([value isMemberOfClass:[WebScriptObject class]]) {
message = [self stringFromWebScriptObject:(WebScriptObject *)value];
} else {
message = [NSString stringWithString:value];
}
NSString *levelStr;
switch (level) {
case JSLogLevelInfo:
levelStr = @"INFO";
break;
case JSLogLevelWarn:
levelStr = @"WARNING";
break;
case JSLogLevelError:
levelStr = @"ERROR";
break;
default:
levelStr = @"UNKNOWN";
break;
}
NSLog(@"%@: %@", levelStr, message);
}
- (void)consoleLog:(id)value {
[self log:value level:JSLogLevelInfo];
}
- (void)consoleWarn:(id)value {
[self log:value level:JSLogLevelWarn];
}
- (void)consoleError:(id)value {
[self log:value level:JSLogLevelError];
}
#pragma mark -
+ (NSString *)webScriptNameForSelector:(SEL)sel {
if (sel == @selector(consoleLog:)) {
return @"log";
} else if (sel == @selector(consoleWarn:)) {
return @"warn";
} else if (sel == @selector(consoleError:)) {
return @"error";
}
return nil;
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel {
if (sel == @selector(consoleLog:) ||
sel == @selector(consoleWarn:) ||
sel == @selector(consoleError:)) {
return NO;
}
return YES;
}
#pragma mark -
- (void)dealloc {
[super dealloc];
}
@end