mirror of
https://github.com/qvacua/vimr.git
synced 2024-12-24 22:33:52 +03:00
Use OSLog in NetUtils
This commit is contained in:
parent
67ffdfbb2a
commit
36ba30d7b5
@ -1,7 +1,8 @@
|
||||
//
|
||||
// Created by Tae Won Ha on 1/13/17.
|
||||
// Copyright (c) 2017 Tae Won Ha. All rights reserved.
|
||||
//
|
||||
/**
|
||||
* Greg Omelaenko - http://omelaen.co
|
||||
* Tae Won Ha - http://taewon.de - @hataewon
|
||||
* See LICENSE
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
|
@ -1,21 +1,31 @@
|
||||
//
|
||||
// Created by Tae Won Ha on 1/13/17.
|
||||
// Copyright (c) 2017 Tae Won Ha. All rights reserved.
|
||||
//
|
||||
/**
|
||||
* Greg Omelaenko - http://omelaen.co
|
||||
* Tae Won Ha - http://taewon.de - @hataewon
|
||||
* See LICENSE
|
||||
*/
|
||||
|
||||
#import "NetUtils.h"
|
||||
#import <os/log.h>
|
||||
#import <sys/socket.h>
|
||||
#import <netinet/in.h>
|
||||
|
||||
static os_log_t logger;
|
||||
|
||||
@implementation NetUtils
|
||||
|
||||
// from https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/UsingSocketsandSocketStreams.html#//apple_ref/doc/uid/CH73-SW9
|
||||
// and http://stackoverflow.com/a/20850182/6939513
|
||||
// slightly modified
|
||||
+ (in_port_t)openPort {
|
||||
static dispatch_once_t token;
|
||||
dispatch_once(&token, ^{
|
||||
// See Defs.swift
|
||||
logger = os_log_create("com.qvacua.VimR", "general");
|
||||
});
|
||||
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(sock < 0) {
|
||||
NSLog(@"ERROR Could not open a socket");
|
||||
os_log_error(logger, "Could not open socket");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -28,24 +38,24 @@
|
||||
|
||||
if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
|
||||
if(errno == EADDRINUSE) {
|
||||
NSLog(@"ERROR the port is not available. already to other process");
|
||||
os_log_error(logger, "the port is not available.");
|
||||
return 0;
|
||||
} else {
|
||||
NSLog(@"ERROR could not bind to process (%d) %s", errno, strerror(errno));
|
||||
os_log_error(logger, "could not bind to process (%{public}d) %{public}s", errno, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
socklen_t len = sizeof(sin);
|
||||
if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1) {
|
||||
NSLog(@"ERROR getsockname");
|
||||
os_log_error(logger, "getsockname failed.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
in_port_t result = ntohs(sin.sin_port);
|
||||
|
||||
if (close (sock) < 0 ) {
|
||||
NSLog(@"ERROR did not close: %s", strerror(errno));
|
||||
os_log_error(logger, "socket did not close: %{public}s", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user