linphone-iphone/Classes/Utils/Log.m
2016-01-18 13:33:07 +01:00

86 lines
3.1 KiB
Objective-C

/*
*
* Copyright (C) 2012 Belledonne Comunications, Grenoble, France
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "Log.h"
@implementation Log
+ (void)log:(OrtpLogLevel)severity file:(const char *)file line:(int)line format:(NSString *)format, ... {
va_list args;
va_start(args, format);
NSString *str = [[NSString alloc] initWithFormat:format arguments:args];
const char *utf8str = [str cStringUsingEncoding:NSString.defaultCStringEncoding];
int filesize = 20;
const char *filename = strchr(file, '/') ? strrchr(file, '/') + 1 : file;
char levelC = 'U'; // undefined
if ((severity & ORTP_FATAL) != 0) {
levelC = 'F';
} else if ((severity & ORTP_ERROR) != 0) {
levelC = 'E';
} else if ((severity & ORTP_WARNING) != 0) {
levelC = 'W';
} else if ((severity & ORTP_MESSAGE) != 0) {
levelC = 'I';
} else if (((severity & ORTP_TRACE) != 0) || ((severity & ORTP_DEBUG) != 0)) {
levelC = 'D';
}
// we want application logs to be always enabled so use | ORTP_ERROR extra mask
ortp_log(severity | ORTP_ERROR, "%c %*s:%3d - %s", levelC, filesize,
filename + MAX((int)strlen(filename) - filesize, 0), line, utf8str);
va_end(args);
}
+ (void)enableLogs:(BOOL)enabled {
[LinphoneManager.instance lpConfigSetBool:enabled forKey:@"debugenable_preference"];
linphone_core_enable_logs_with_cb((OrtpLogFunc)linphone_iphone_log_handler);
if (enabled) {
NSLog(@"Enabling debug logs");
linphone_core_set_log_level(ORTP_DEBUG);
} else {
NSLog(@"Disabling debug logs");
linphone_core_set_log_level(ORTP_ERROR);
}
linphone_core_enable_log_collection(enabled);
}
#pragma mark - Logs Functions callbacks
void linphone_iphone_log_handler(int lev, const char *fmt, va_list args) {
NSString *format = [[NSString alloc] initWithUTF8String:fmt];
NSString *formatedString = [[NSString alloc] initWithFormat:format arguments:args];
NSString *lvl = @"";
if ((lev & APP_LVL) == 0) {
if ((lev & ORTP_FATAL) != 0) {
lvl = @"F ";
} else if ((lev & ORTP_ERROR) != 0) {
lvl = @"E ";
} else if ((lev & ORTP_WARNING) != 0) {
lvl = @"W ";
} else if ((lev & ORTP_MESSAGE) != 0) {
lvl = @"I ";
} else if (((lev & ORTP_TRACE) != 0) || ((lev & ORTP_DEBUG) != 0)) {
lvl = @"D ";
}
}
// since \r are interpreted like \n, avoid double new lines when logging network packets (belle-sip)
NSLog(@"%@%@", lvl, [formatedString stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\n"]);
}
@end