2016-07-26 9 views
0

Zum Beispiel gibt es eine ALOGI-Funktion ("es gibt ein Protokoll") in dalvik/vm/native/dalvik_system_DexFile.cpp, und ich möchte dieses Protokoll in logcat oder DDMS sehen.Wie drucke ich die ALOGI (...) in Android C++ Datei?

Wie kann ich das tun? Bitte erläutern. Vielen Dank.

+0

Sind Sie eine eigene Android ROM bauen? Wenn nicht, bin ich mir nicht sicher, ob Sie ändern können, was Dalvik aufzeichnen wird. Wenn Sie Ihr eigenes ROM erstellen, versuchen Sie, die CFLAGS für das entsprechende Modul in der entsprechenden Android.mk-Datei zu ändern. Zum Beispiel das Hinzufügen von -UNDEBUG -DDEBUG = 1 -DLOG_NDEBUG = 1. – Michael

Antwort

1

Die beste Möglichkeit zum Drucken von Protokollen besteht darin, eine Kopf-Kreuzplattform für diese Aufgabe zu erstellen. Im Beispiel mit dem folgenden logger.h:

#pragma once 

#define TMB_DEBUG_LOGS 1 

#ifdef TMB_DEBUG_LOGS 
// YES LOGS 
# ifdef ANDROID 
    // LOGS ANDROID 
#  include <android/log.h> 
#  define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,__VA_ARGS__) 
#  define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , LOG_TAG,__VA_ARGS__) 
#  define LOGI(...) __android_log_print(ANDROID_LOG_INFO , LOG_TAG,__VA_ARGS__) 
#  define LOGW(...) __android_log_print(ANDROID_LOG_WARN , LOG_TAG,__VA_ARGS__) 
#  define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , LOG_TAG,__VA_ARGS__) 
# else 
    // LOGS NO ANDROID (fprintf) 
     #include <stdio.h> 
     #include <time.h> 

     //Warning, multithreading problem in logs with this solution! 
     static char buff[100]; 
     static struct tm *sTm; 
     static time_t now; 
#  define PRINT_TIME {now = time(0); sTm = localtime(&now); strftime(buff, sizeof(buff), "[%Y-%m-%d %H:%M:%M]", sTm); fprintf(stderr, "%s ", buff); } 
#  define LOGV(...) {PRINT_TIME fprintf(stderr, "[V][%s] ", LOG_TAG); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n");} 
#  define LOGD(...) {PRINT_TIME fprintf(stderr, "[D][%s] ", LOG_TAG); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n");} 
#  define LOGI(...) {PRINT_TIME fprintf(stderr, "[I][%s] ", LOG_TAG); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n");} 
#  define LOGW(...) {PRINT_TIME fprintf(stderr, "[W][%s] ", LOG_TAG); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n");} 
#  define LOGE(...) {PRINT_TIME fprintf(stderr, "[E][%s] ", LOG_TAG); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n");} 
# endif // ANDROID 
#else 
// NO LOGS 
# define LOGV(...) 
# define LOGD(...) 
# define LOGI(...) 
# define LOGW(...) 
# define LOGE(...) 
#endif // TMB_DEBUG_LOGS 

in Ihrer Quelle, in Beispiel main.c

//... your includes 
#define LOG_TAG "MainProgram" 
#include "logger.h" 

main() 
{ 
    // My code 
    LOGI("The program is creating a info log"); 
    LOGD("My foo: %d", foo_variable); 
}