Core code samples

The SDV Core services code samples in /system/software_defined_vehicle/samples enable you to start building your services. Each sample contains a README file describing the use case.

Use the logging API

To learn more about the instructions provided, see:

Basic log sample

logging.cpp presents writing to the main, system, and radio buffers. To read a short description of the available log buffers, see View alternative log buffers.

  • To run the sample without the conditional logs, such as ALOGI_IF:

    logging_sdv_sample
    
  • To run the sample and print the conditional logs, such as ALOGI_IF:

    LOGIF=1 logging_sdv_sample
    

Structured logging sample

Sample file: structured_logging.cpp

  • To run the sample, which writes logs to the events log buffer:

    structured_logging_sdv_sample
    

The event, security, and stats log buffers hold structured data rather than free text.

Structured data is logged using the event list API, which is always logged with INFO priority and the numeric tag ID.

A .logtags file can be defined with a logtags attribute in Android.bp. When provided, logcat uses this to derive the tag name from the numeric tag ID. The tag name can then be used for filtering structured logs. For details and a description of the file format, see the comment in the logcat event.logtags file.

During the build process, all logtags files are merged and placed into the /system/etc/event-log-tags file in the Android system image.

Compile a time log filtering sample

Sample file: compile_time_log_filtering.cpp

This sample contains macros you can adjust to remove some logs from the executable. See the source code for details.

Log filtering sample: log_filtering.cpp

  • To run the sample, setting the minimum log priority to WARN:

    LOGLEVEL=WARN log_filtering_sdv_sample
    

Log filtering is affected by system properties. For all logs with :

  • log.tag.<tag>
  • persist.log.tag.<tag>

For all logs in the system:

  • log.tag
  • persist.log.tag

To select the chosen log level, which are checked in order, each value can be set to V, D, I, W, or E.

The effective log level is the level set on the first system property that has a value, which may be lower or higher than the log level set by app with a __android_log_set_minimum_priority call. When none of the properties are set, the level configured with __android_log_set_minimum_priority applies.

For details, see the Logging system overview.

  • Run the sample, setting the default log tag to example_tag:

    LOGTAG=example_tag log_filtering_sdv_sample
    

The sample also contains a commented out LOG_TAG macro definition you can use to set the log tag for all log messages. The tag set in this way overrides the tag set at runtime with __android_log_set_default_tag.

If the LOG_TAG macro is not defined and the app does not call __android_log_set_default_tag, the default tag is the result of getprogname(). For example, the executable name.