1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-25 23:02:35 +03:00

Use OSLog in NetUtils

This commit is contained in:
Tae Won Ha 2019-03-08 17:42:45 +01:00
parent 67ffdfbb2a
commit 36ba30d7b5
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
2 changed files with 24 additions and 13 deletions

View File

@ -1,7 +1,8 @@
// /**
// Created by Tae Won Ha on 1/13/17. * Greg Omelaenko - http://omelaen.co
// Copyright (c) 2017 Tae Won Ha. All rights reserved. * Tae Won Ha - http://taewon.de - @hataewon
// * See LICENSE
*/
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>

View File

@ -1,21 +1,31 @@
// /**
// Created by Tae Won Ha on 1/13/17. * Greg Omelaenko - http://omelaen.co
// Copyright (c) 2017 Tae Won Ha. All rights reserved. * Tae Won Ha - http://taewon.de - @hataewon
// * See LICENSE
*/
#import "NetUtils.h" #import "NetUtils.h"
#import <os/log.h>
#import <sys/socket.h> #import <sys/socket.h>
#import <netinet/in.h> #import <netinet/in.h>
static os_log_t logger;
@implementation NetUtils @implementation NetUtils
// from https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/UsingSocketsandSocketStreams.html#//apple_ref/doc/uid/CH73-SW9 // 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 // and http://stackoverflow.com/a/20850182/6939513
// slightly modified // slightly modified
+ (in_port_t)openPort { + (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); int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0) { if(sock < 0) {
NSLog(@"ERROR Could not open a socket"); os_log_error(logger, "Could not open socket");
return 0; return 0;
} }
@ -28,24 +38,24 @@
if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) { if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
if(errno == EADDRINUSE) { 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; return 0;
} else { } 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; return 0;
} }
} }
socklen_t len = sizeof(sin); socklen_t len = sizeof(sin);
if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1) { if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1) {
NSLog(@"ERROR getsockname"); os_log_error(logger, "getsockname failed.");
return 0; return 0;
} }
in_port_t result = ntohs(sin.sin_port); in_port_t result = ntohs(sin.sin_port);
if (close (sock) < 0 ) { 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; return 0;
} }