環(huán)球觀熱點(diǎn):【NDK】封裝日志庫
2022-12-02 18:39:48 來源:程序員客棧
(資料圖片僅供參考)
點(diǎn)擊關(guān)注,與你共同成長!
【NDK】封裝日志庫0x1需求 供C++、Java調(diào)用控制臺(tái)輸出文件輸出(文件大?。┰O(shè)置日志等級(jí)0x2 C++ 0x21 LogUtils.h
////Createdby后端碼匠on2022/11/30.//#ifndefNDKPRACTICE_LOGUTILS_H#defineNDKPRACTICE_LOGUTILS_H#include0x22 LogUtils.cpp#include #include #defineLOG_TAG"km_media_log"#defineLOGD(...)__android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)#defineLOGI(...)__android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)#defineLOGW(...)__android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)#defineLOGE(...)__android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)#defineLOG_TEXT_MAX_LENGTH(1024)//單條日志大小#defineLOG_FILE_MAX_SIZE(1024*1024*3)//文件最大為3MBenum{LOG_LEVEL_NONE=0,LOG_LEVEL_ERR=1,LOG_LEVEL_WARNING=2,LOG_LEVEL_INFO=3,LOG_LEVEL_DEBUG=4};#ifdef__cplusplusextern"C"{#endif/***初始化日志選項(xiàng)*@parampFile*@paramfilename*@paramlogLevel*@paramprintScreen*@return*/intLogInit(constchar*pFile,constchar*filename,intlogLevel,intprintScreen);/***日志處理*@paramlevel*@paramstrFormat*@param...*/voidWriteTextLog(intlevel,constchar*strFormat,...);/***向文件中寫入日志*@paramlevel*@paramlog*/voidWriteTextLogBottom(intlevel,constchar*log);/***關(guān)閉日志庫*/voidLogClose();#ifdef__cplusplus}#endif#endif//NDKPRACTICE_LOGUTILS_H
////Createdby后端碼匠on2022/11/30.//#include0x3 Java 0x31 LogUtils#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include"LogUtils.h"charLOG_FILE_NAME[100]="km_media_log.txt";//日志默認(rèn)名稱//日志級(jí)別intg_log_file_level=LOG_LEVEL_NONE;intg_log_screen_level=LOG_LEVEL_NONE;longg_RollingPtr=0;//文件路徑staticstd::stringg_logFilePath;intLogInit(constchar*pFile,constchar*filename,intlogLevel,intprintScreen){g_RollingPtr=0;g_log_file_level=logLevel;g_log_screen_level=printScreen;if(filename!=nullptr){strcpy(LOG_FILE_NAME,filename);}if(pFile!=nullptr){g_logFilePath=std::string(pFile)+"/"+LOG_FILE_NAME;}else{g_logFilePath=LOG_FILE_NAME;}return0;}charg_log_info[LOG_TEXT_MAX_LENGTH+100];voidWriteTextLog(intlevel,constchar*strFormat,...){if(level>g_log_file_level&&level>g_log_screen_level){return;}time_tnow;chartimeStr[20];chartemBuf[LOG_TEXT_MAX_LENGTH];time(&now);strftime(timeStr,sizeof(timeStr),"%Y-%m-%d%H:%M:%S",localtime(&now));va_listargs;va_start(args,strFormat);vsnprintf(temBuf,sizeof(temBuf)-1,strFormat,args);va_end(args);switch(level){caseLOG_LEVEL_DEBUG:LOGD("%s",g_log_info);sprintf(g_log_info,"%s[DEBUG]%s\n",timeStr,temBuf);break;caseLOG_LEVEL_INFO:LOGI("%s",g_log_info);sprintf(g_log_info,"%s[INFO]%s\n",timeStr,temBuf);break;caseLOG_LEVEL_WARNING:LOGW("%s",g_log_info);sprintf(g_log_info,"%s[WARN]%s\n",timeStr,temBuf);break;caseLOG_LEVEL_ERR:LOGE("%s",g_log_info);sprintf(g_log_info,"%s[ERROR]%s\n",timeStr,temBuf);break;default:LOGI("%s",g_log_info);sprintf(g_log_info,"%s[NONE]%s\n",timeStr,temBuf);break;}if(level<=g_log_file_level&&!g_logFilePath.empty()){WriteTextLogBottom(level,g_log_info);}}voidWriteTextLogBottom(intlevel,constchar*log){if(level<=g_log_file_level){FILE*fp;structstatinfo{};if(stat(g_logFilePath.c_str(),&info)!=0){g_RollingPtr=0;fp=fopen(g_logFilePath.c_str(),"we");//createfileif(fp==nullptr){LOGE("%s,fopen(w)%sfail,err:%d",__func__,g_logFilePath.c_str(),errno);return;}fprintf(fp,"%s,statfailcreatelogfile,errno:%d\n",__func__,errno);fprintf(fp,"%s",log);fclose(fp);return;}if(info.st_size>=LOG_FILE_MAX_SIZE)//loopwrite{//這里使用復(fù)寫的方式,保證日志文件不會(huì)超過LOG_FILE_MAX_SIZEfp=fopen(g_logFilePath.c_str(),"r+");if(nullptr==fp){LOGE("%s,fopen(r+)%sfail,size:%ld,err:%d",__func__,g_logFilePath.c_str(),info.st_size,errno);return;}if(fseek(fp,g_RollingPtr,SEEK_SET)<0){fclose(fp);return;}g_RollingPtr+=strlen(log);if(g_RollingPtr>info.st_size){g_RollingPtr=0;}}else{fp=fopen(g_logFilePath.c_str(),"a");if(fp==nullptr){LOGE("%s,fopen(a)%sfail,size:%ld,err:%d",__func__,g_logFilePath.c_str(),info.st_size,errno);return;}}fprintf(fp,"%s",log);fclose(fp);}}voidLogClose(){g_log_file_level=LOG_LEVEL_NONE;g_log_screen_level=LOG_LEVEL_NONE;}
////Createdby后端碼匠on2022/11/30.//packagecn.com.codingce.ndkpractice.utils;importandroid.content.Context;importandroid.os.Environment;importandroid.util.Log;importjava.io.File;publicclassLogUtils{privatestaticContextglobalAplicationContext=null;privatestaticStringPATH_LOGCAT=null;publicenumLogLevel{LOG_LEVEL_NONE,LOG_LEVEL_ERR,LOG_LEVEL_WARNING,LOG_LEVEL_INFO,LOG_LEVEL_DEBUG}publicstaticvoidinit(){if(globalAplicationContext==null)return;booleanobtainSDcardAccess=false;try{obtainSDcardAccess=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);}catch(Exceptione){Log.e("LogUtils","Exception:"+e.getMessage());}if(obtainSDcardAccess){//優(yōu)先保存到SD卡中finalFileexternalFilesDir=globalAplicationContext.getExternalFilesDir(null);if(externalFilesDir!=null){PATH_LOGCAT=externalFilesDir.getAbsolutePath()+File.separator+"kmsdk";}else{//如果SD卡不存在,就保存到本應(yīng)用的目錄下PATH_LOGCAT=globalAplicationContext.getFilesDir().getAbsolutePath()+File.separator+"kmsdk";}}else{//如果SD卡不存在,就保存到本應(yīng)用的目錄下PATH_LOGCAT=globalAplicationContext.getFilesDir().getAbsolutePath()+File.separator+"kmsdk";}Filefile=newFile(PATH_LOGCAT);if(!file.exists()){file.mkdirs();}LogInit(PATH_LOGCAT,"km_media_log.txt",4,4);Log.e("LogUtils","curfilediris:"+file.toString());}publicsynchronizedstaticvoidsetApplicationContext(ContextaplicationContext){globalAplicationContext=aplicationContext.getApplicationContext();}//日志類初始化publicstaticnativevoidLogInit(StringlogFilePath,StringlogName,intlogfileLevel,intlogScreenLevel);publicstaticnativevoidlogJni(intlogLevel,Stringcontent);publicstaticnativevoidlogClose();}0x32 Native
////Createdby后端碼匠on2022/11/30.//#include#include #include"LogUtils.h"#definediagnosis_assert(...)assert(__VA_ARGS__)intret=-1;staticvoidnativeLogUtilsRegisterNatives(JNIEnv*jniEnv);JNIEXPORTjintJNICALLJNI_OnLoad(JavaVM*vm,void*reserved){JNIEnv*jniEnv{nullptr};if(vm->GetEnv((void**)&jniEnv,JNI_VERSION_1_6)!=JNI_OK){diagnosis_assert(!"JNIversionerror!");returnJNI_EVERSION;}nativeLogUtilsRegisterNatives(jniEnv);returnJNI_VERSION_1_6;}staticvoidLocalLogInit(JNIEnv*env,jclassclazz,jstringlogFilePath,jstringlogName,jintlogfile_level,jintlog_screen_level){if(ret!=0){constchar*path=env->GetStringUTFChars(logFilePath,JNI_FALSE);constchar*name=env->GetStringUTFChars(logName,JNI_FALSE);intfileLevel=logfile_level;intscreenLevel=log_screen_level;ret=LogInit(path,name,fileLevel,screenLevel);env->ReleaseStringUTFChars(logFilePath,path);env->ReleaseStringUTFChars(logName,name);}}staticvoidlogJni(JNIEnv*env,jclassclazz,jint_level,jstring_str){if(ret!=0){LOGE("logerror!LogInitneed");return;}constchar*str=env->GetStringUTFChars(_str,JNI_FALSE);WriteTextLog(_level,str);env->ReleaseStringUTFChars(_str,str);}staticvoidlogClose(JNIEnv*env,jclassclazz){LogClose();ret=-1;}staticJNINativeMethodnativeUtilsMethods[]={{"LogInit","(Ljava/lang/String;Ljava/lang/String;II)V",(void*)LocalLogInit},{"logJni","(ILjava/lang/String;)V",(void*)logJni},{"logClose","()V",(void*)logClose},};staticvoidnativeLogUtilsRegisterNatives(JNIEnv*jniEnv){if(jniEnv==nullptr){return;}jclassclazz=nullptr;do{clazz=jniEnv->FindClass("cn/com/codingce/ndkpractice/utils/LogUtils");if(clazz==nullptr){diagnosis_assert(!"FindClassLogUtilserror!");break;}if(jniEnv->RegisterNatives(clazz,nativeUtilsMethods,std::extent ::value)!=0){diagnosis_assert(!"RegisterNativeserror!");break;}}while(false);if(jniEnv->ExceptionCheck()==JNI_TRUE){jniEnv->ExceptionClear();}if(clazz!=nullptr){jniEnv->DeleteLocalRef(clazz);}}
【C++】STL梳理
【Android】NDK開發(fā)Crash分析
【C++】PK游戲(玩轉(zhuǎn)多態(tài))
以上,便是今天的分享,希望大家喜歡,覺得內(nèi)容不錯(cuò)的,歡迎「分享」「贊」或者點(diǎn)擊「在看」支持,謝謝各位。
相關(guān)閱讀
-
環(huán)球觀熱點(diǎn):【NDK】封裝日志庫
點(diǎn)擊關(guān)注,與你共同成長!【NDK】封裝日志庫0x1需求供C++、Java調(diào)用... -
【Android】JNI靜態(tài)與動(dòng)態(tài)注冊(cè)介紹_環(huán)球看熱訊
點(diǎn)擊關(guān)注,與你共同成長!【Android】JNI靜態(tài)與動(dòng)態(tài)注冊(cè)介紹JNI的兩... -
世界熱推薦:今晚7:00直播丨下一個(gè)突破...
今晚19:00,Cocos視頻號(hào)直播馬上點(diǎn)擊【預(yù)約】啦↓↓↓在運(yùn)營了三年... -
NFT周刊|Magic Eden宣布支持Polygon網(wǎng)...
Block-986在NFT這樣的市場(chǎng),每周都會(huì)有相當(dāng)多項(xiàng)目起起伏伏。在過去... -
環(huán)球今亮點(diǎn)!頭條觀察 | DeFi的興衰與...
在比特幣得到機(jī)構(gòu)關(guān)注之后,許多財(cái)務(wù)專家預(yù)測(cè)世界將因?yàn)榧用茇泿诺?.. -
重新審視合作,體育Crypto的可靠關(guān)系才能雙贏
Block-987即使在體育Crypto領(lǐng)域,人們的目光仍然集中在FTX上。隨著...