-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWLog.m
More file actions
145 lines (125 loc) · 3.41 KB
/
WLog.m
File metadata and controls
145 lines (125 loc) · 3.41 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// WLog.m
// KleerControl
//
// Created by Steven Knudsen on 2010/05/06.
// 2010 TechConficio Inc.. No rights reserved, do what you want with this...
//
#import "WLog.h"
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
@implementation WLog
@synthesize enabled;
static WLog *sharedInstance = nil;
+ (void)initialize
{
if (sharedInstance == nil)
sharedInstance = [[self alloc] init];
}
+ (id)sharedWLog
{
//Already set by +initialize.
return sharedInstance;
}
+ (id)allocWithZone:(NSZone*)zone
{
//Usually already set by +initialize.
@synchronized(self) {
if (sharedInstance) {
//The caller expects to receive a new object, so implicitly retain it
//to balance out the eventual release message.
return [sharedInstance retain];
} else {
//When not already set, +initialize is our caller.
//It's creating the shared instance, let this go through.
return [super allocWithZone:zone];
}
}
}
- (id)init
{
//If sharedInstance is nil, +initialize is our caller, so initialize the instance.
//If it is not nil, simply return the instance without re-initializing it.
if (sharedInstance == nil) {
if ((self = [super init])) {
//Initialize the instance here.
#ifdef WLOG_ALT_IP_ADDR
ipAddress = WLOG_ALT_IP_ADDR;
#else
ipAddress = WLOG_DEFAULT_IP_ADDR;
#endif
#ifdef WLOG_ALT_PORT
port = WLOG_ALT_PORT;
#else
port = WLOG_DEFAULT_PORT;
#endif
enabled = NO;
// make a socket
// create socket
udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// init in broadcast mode
int broadcast = 1;
setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(int));
memset((char *) &targetAddress, 0, sizeof(targetAddress));
targetAddress.sin_family = AF_INET;
// broadcast mask is Fs
targetAddress.sin_addr.s_addr = htonl(0xFFFFFFFF);
targetAddress.sin_port = htons(port);
targetAddress.sin_len = sizeof(targetAddress);
// last, register for notifications to log stuff...
// listen to updates from AccelerometerViewControl
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(logIt:)
name:WLOG_NOTIFICATION
object:nil];
}
}
return self;
}
- (id)copyWithZone:(NSZone*)zone
{
return self;
}
- (id)retain
{
return self;
}
- (unsigned)retainCount
{
return UINT_MAX; // denotes an object that cannot be released
}
- (void)release
{
// do nothing
}
- (id)autorelease
{
return self;
}
- (void)logIt:(NSNotification *)notification
{
#pragma mark TODO - check for other objects to report (e.g., custom objs)
// check for NSString to log
NSMutableString *logString = [NSMutableString stringWithCapacity:20];
NSDate *now = [NSDate date];
[logString appendFormat:@"WLOG:%@|",[[now description] substringToIndex:19]];
NSDictionary *dict = [notification userInfo];
NSString *notificationString = [dict objectForKey:WLOG_STRING_KEY];
if (notificationString != nil) {
[logString appendFormat:@"%@\n",notificationString];
}
// only process is network is enabled and socket initialized OK
if( enabled && (udpSocket != -1) )
{
// create UDP packet as formatted string
// "ACC: <deviceid>,<timestamp>,<x>,<y>,<z>"
const char *msg = [logString UTF8String];
int error = sendto(udpSocket, msg, strlen(msg), 0, (struct sockaddr*)&targetAddress, sizeof(targetAddress));
if( error < 0 )
{
NSLog(@"Socket error %d", errno);
}
}
}
@end