android notes :C++ file add log method share

  • 2020-05-19 05:42:07
  • OfStack

1. Add header files

#include <utils/Log.h>   
// or   
#include <cutils/Log.h>  

At this point, log can be printed using ALOGE/ALOGI/ALOGW, etc
However, there are some places where you can't use this because you rely on the libutils libctuils library
In the mk file see add the following dependencies to compile

#LOCAL_MODULE := ... ...   
#base_intermediates := $(call local-intermediates-dir)  // Add under that line , This looks like a lookup dependency file   
LOCAL_SHARED_LIBRARIES += \  
        libutils libcutils  
    ... ...  
include $(BUILD_SHARED_LIBRARY) // Above that line, it's time to compile   

2. General stack

android::CallStack stack;  
stack.update(1);  
stack.dump("");  

This requires a header file, but one thing to note is that this header must be added at the end of all headers, otherwise it won't work, especially if you look at android source code webkit. The header file to be added is as follows:

#include <utils/CallStack.h>  

I have something to do today, and I will complete it later. If you have a better or special case, please recommend it
3.C++ alternative stack
This method is file - controlled. The purpose of file control is achieved by reading and writing the files in the android device, and then assigning the null pointer
As long as the C, C++ files can be added to this log, no dependency, convenient! There is also a disadvantage, log can only run to the place you added, because the library crashed, the later can not run.
The implementation code is as follows:

FILE *fp = NULL; // Need to pay attention to    
fp = fopen("data/test", "r");  
if(NULL == fp)  
{  
    return false; // When you need to return a value    
} esle {  
    fclose(fp);  
    int *fp = NULL;  
    *fp = 100;  // Null pointer assignment error, demo is wrong    
    return false; // When you need to return a value    
}  

1 library collapse is not seen stack, we can disassemble to see the specific stack.
There are two sets of libraries compiled in android: one set is unsigned, which is unviewable, and the other set is signed, which is what we are looking at.
The signed compiler is compiled under the path (\out\target\product\generic\symbols\system\lib)
android comes with the disassembler tool arm, which can be normally used after setting up the compilation environment. The command is as follows:

arm-eabi-addr2line -f -e **.so  address 1  address 2 ... ...  

Related articles: