Private
Public Access
1
0

logging: Use os log if running in launchd

This commit is contained in:
2024-01-07 18:12:17 -08:00
parent c65803845b
commit 72527088cc

View File

@@ -7,14 +7,49 @@
// //
#import "MBIMLogging.h" #import "MBIMLogging.h"
#import <os/log.h>
#define ESC(inner) "\033[" inner "m" #define ESC(inner) "\033[" inner "m"
#define CLR ESC("0") #define CLR ESC("0")
#define BLD "1;" #define BLD "1;"
#define RED "31;" #define RED "31;"
static os_log_t MBIMOSLog(void)
{
static dispatch_once_t onceToken;
static os_log_t customLog = NULL;
dispatch_once(&onceToken, ^{
customLog = os_log_create("net.buzzert.kordophoned", "General");
});
return customLog;
}
extern void __MBIMLogCommon(MBIMLogLevel level, NSString *format, ...) extern void __MBIMLogCommon(MBIMLogLevel level, NSString *format, ...)
{ {
va_list args;
va_start(args, format);
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
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 dispatch_once_t onceToken;
static NSDateFormatter *dateFormatter = nil; static NSDateFormatter *dateFormatter = nil;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
@@ -22,11 +57,6 @@ extern void __MBIMLogCommon(MBIMLogLevel level, NSString *format, ...)
dateFormatter.dateFormat = @"Y-MM-d HH:mm:ss"; dateFormatter.dateFormat = @"Y-MM-d HH:mm:ss";
}); });
va_list args;
va_start(args, format);
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
const char *c_fmt = "%s"; const char *c_fmt = "%s";
if (level == ML_NOTIFY) { if (level == ML_NOTIFY) {
// BOLD // BOLD
@@ -42,3 +72,4 @@ extern void __MBIMLogCommon(MBIMLogLevel level, NSString *format, ...)
fprintf(stdout, c_fmt, [message UTF8String]); fprintf(stdout, c_fmt, [message UTF8String]);
fprintf(stdout, CLR "\n"); fprintf(stdout, CLR "\n");
} }
}