2019-01-22 23:31:36 -08:00
|
|
|
//
|
|
|
|
|
// MBIMLogging.m
|
|
|
|
|
// kordophoned
|
|
|
|
|
//
|
|
|
|
|
// Created by James Magahern on 1/22/19.
|
|
|
|
|
// Copyright © 2019 James Magahern. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "MBIMLogging.h"
|
2024-01-07 18:12:17 -08:00
|
|
|
#import <os/log.h>
|
2019-01-22 23:31:36 -08:00
|
|
|
|
|
|
|
|
#define ESC(inner) "\033[" inner "m"
|
|
|
|
|
#define CLR ESC("0")
|
|
|
|
|
#define BLD "1;"
|
|
|
|
|
#define RED "31;"
|
|
|
|
|
|
2024-01-07 18:12:17 -08:00
|
|
|
static os_log_t MBIMOSLog(void)
|
2019-01-22 23:31:36 -08:00
|
|
|
{
|
|
|
|
|
static dispatch_once_t onceToken;
|
2024-01-07 18:12:17 -08:00
|
|
|
static os_log_t customLog = NULL;
|
2019-01-22 23:31:36 -08:00
|
|
|
dispatch_once(&onceToken, ^{
|
2024-01-07 18:12:17 -08:00
|
|
|
customLog = os_log_create("net.buzzert.kordophoned", "General");
|
2019-01-22 23:31:36 -08:00
|
|
|
});
|
2024-01-07 18:12:17 -08:00
|
|
|
|
|
|
|
|
return customLog;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void __MBIMLogCommon(MBIMLogLevel level, NSString *format, ...)
|
|
|
|
|
{
|
2019-01-22 23:31:36 -08:00
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
|
|
|
|
|
va_end(args);
|
2024-01-07 18:12:17 -08:00
|
|
|
|
|
|
|
|
if (getenv("NSRunningFromLaunchd") != NULL) {
|
|
|
|
|
// Running with launchd, use oslog.
|
|
|
|
|
os_log_t mbimlog = MBIMOSLog();
|
|
|
|
|
|
|
|
|
|
switch (level) {
|
|
|
|
|
case ML_INFO:
|
|
|
|
|
os_log_debug(mbimlog, "%@", message);
|
|
|
|
|
break;
|
|
|
|
|
case ML_NOTIFY:
|
|
|
|
|
os_log_info(mbimlog, "%@", message);
|
|
|
|
|
break;
|
|
|
|
|
case ML_FATAL:
|
|
|
|
|
case ML_ERROR:
|
|
|
|
|
os_log_error(mbimlog, "%@", message);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Otherwise, write to stdout.
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
static NSDateFormatter *dateFormatter = nil;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
dateFormatter = [[NSDateFormatter alloc] init];
|
|
|
|
|
dateFormatter.dateFormat = @"Y-MM-d HH:mm:ss";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const char *c_fmt = "%s";
|
|
|
|
|
if (level == ML_NOTIFY) {
|
|
|
|
|
// BOLD
|
|
|
|
|
c_fmt = ESC(BLD) "%s";
|
|
|
|
|
} else if (level == ML_ERROR) {
|
|
|
|
|
c_fmt = ESC(RED) "%s";
|
|
|
|
|
} else if (level == ML_FATAL) {
|
|
|
|
|
c_fmt = ESC(BLD RED) "%s";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
|
|
|
|
|
fprintf(stdout, "%s: ", [dateStr UTF8String]);
|
|
|
|
|
fprintf(stdout, c_fmt, [message UTF8String]);
|
|
|
|
|
fprintf(stdout, CLR "\n");
|
2019-01-22 23:31:36 -08:00
|
|
|
}
|
|
|
|
|
}
|