UPDATE FOR THE READERS: 这里讨论的代码现在可以在这个Codename One库中找到(它也在CN1 Extensions Manager中......):https://github.com/jsfan3/CN1Libs-NativeLogsReader
Short question:
我编写了一个工作的原生Android代码,以便在执行我的Codename One应用程序时获取设备的本机日志 . 我用了native interface functionality provided by Codename One . 我还需要帮助来实现iOS的这个界面 .
Long question...
首先,当我在我的应用程序中出现奇怪的行为时,我试图获取本机日志(例如Android Studio和XCode提供的日志)来寻求帮助...因为标准的Codename One日志在几种情况下是不够的(例如,我遇到了使用Google Maps CN1Lib等本机组件的问题 .
我是Android Studio的新手,我很难获得我的Codename One应用程序的Logcat,我也很难使用构建服务器提供的本机源 . 而且,我没有Mac,所以我不能使用XCode .
我的语言技能仅限于Codename One Java 8,我不会“说”Android原生Java,我觉得iOS原生Objective-C是不可读的......
这就是为什么,当我需要在Codename One的Github存储库中提交一些问题时,为了帮助自己提供准确的日志,我尝试编写本机代码来获取String中的本机日志(我可以通过几种简单的方式管理,比如我可以在表单中显示它,我可以通过电子邮件发送给自己) .
所以......我能够实现Android的代码:它完美无缺 . 我测试了几个Android版本 .
CallNativeCode.java
package ...;
import com.codename1.system.NativeInterface;
/**
* Native Code interface
*/
public interface CallNativeCode extends NativeInterface {
public void clearAndRestartLog();
public String readLog();
public String getFilePath();
}
CallNativeCodeImpl.java
package ...;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CallNativeCodeImpl {
public String getFilePath() {
return null;
}
public void clearAndRestartLog() {
// https://developer.android.com/studio/command-line/logcat
try {
Runtime.getRuntime().exec("logcat -b all -c");
Runtime.getRuntime().exec("logcat");
} catch (IOException ex) {
// logcat non available?
}
}
// original code: https://stackoverflow.com/q/12692103
public String readLog() {
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
return log.toString();
} catch (IOException e) {
return "Log is not available.";
}
}
public boolean isSupported() {
return true;
}
}
它的工作原理如下:在Codename One应用程序的主类的init()中,我添加了:
// Test of Native code
CallNativeCode callNativeCode = NativeLookup.create(CallNativeCode.class);
if (callNativeCode != null && callNativeCode.isSupported()) {
Log.p("Native code can be executed");
callNativeCode.clearAndRestartLog();
Log.p("Native LOG cleared and restarted");
} else {
Log.p("Native code cannot be executed");
}
然后,当我想获得应用程序的本机日志时,我可以执行:
String nativeLog = callNativeCode.readLog();
通过这种方式,我可以获得Android Studio Logcat的相同输出,而无需使用Android Studio,也无需连接到计算机的设备 .
我试图为iOS复制这个功能......但是我遇到了麻烦,因为我不知道Objective-C . 我试图将本机输出日志重定向到一个文件,然后读取该文件(调整我找到的一些代码,并试图猜测它是如何工作的)......但我不知道该怎么做,我的代码没有'在iOS构建服务器上编译 .
以下代码是我尝试做的 . 怎么修好?谢谢
myPackageName_CallNativeCodeImpl.h
#import <Foundation/Foundation.h>
@interface cool_teammate_registration_CallNativeCodeImpl : NSObject {
}
-(NSString*)readLog;
-(NSString*)getFilePath;
-(void)clearAndRestartLog;
-(BOOL)isSupported;
@end
myPackageName_CallNativeCodeImpl.m
#import "myPackageName_CallNativeCodeImpl.h"
@implementation myPackageName_CallNativeCodeImpl
-(NSString*)getFilePath{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]];
NSString* logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
return logFilePath;
}
-(NSString)readLog{
NSString* logFilePath = [self getFilePath];
NSString* content = [NSString stringWithContentsOfFile:logFilePath encoding:NSUTF8StringEncoding error:nil];
return content;
}
-(void)clearAndRestartLog{
// https://stackoverflow.com/questions/5179108/iphone-how-to-read-application-logs-from-device
NSString* logFilePath = [self getFilePath];
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}
-(BOOL)isSupported{
return YES;
}
@end
1 回答
我的iOS代码中出现了拼写错误(
NSString
而不是NSStrings*
) .我在这篇文章中找到了有用的信息:Log iOS Errors to file . 我使用该信息对代码进行了一些小优化 .
最后,这是我的工作代码(在iOS 8,iOS 9,iOS 10,iOS 11上成功测试) . 通过这种方式,我可以获得控制台日志,而无需将设备连接到安装了XCode的Mac :)
myPackageName_CallNativeCodeImpl.m