Last active
March 11, 2016 09:23
-
-
Save lonetwin/2bfdd41da41dae326afb to your computer and use it in GitHub Desktop.
Minimal python ctypes based example of logging to GENIVI Diagnostic Log and Trace Daemon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Minimal example of writing logs to a dlt-daemon from a python app. The | |
# message will look like this when printed out using | |
# "dlt-receive -a localhost": | |
# 2016/03/10 10:24:42.772153 290716359 016 ECU1 DLTD INTM log info V 1 [ApplicationID 'APPY' registered for PID 29, Description=APPY service] | |
# 2016/03/10 10:24:42.772178 290716360 017 ECU1 DLTD INTM log info V 1 [ContextID 'APPY' registered for ApplicationID 'APPY', Description=Context for APPY] | |
# 2016/03/10 10:24:42.772182 290716359 000 ECU1 APPY APPY log info V 1 [This is a log message] | |
# 2016/03/10 10:24:42.772185 290716360 018 ECU1 DLTD INTM log info V 1 [Unregistered ContextID 'APPY' for ApplicationID 'APPY'] | |
# 2016/03/10 10:24:42.772190 290716360 019 ECU1 DLTD INTM log info V 1 [Unregistered ApplicationID 'APPY'] | |
import sys | |
from ctypes import * | |
class DltContext(Structure): | |
_fields_ = [ ('contextID', c_char * 4), | |
('log_level_pos', c_int32), | |
('log_level_ptr', POINTER(c_int8)), | |
('trace_status_ptr', POINTER(c_int8)), | |
('mcnt', c_uint8) | |
] | |
def main(message): | |
LOG_LEVEL_INFO = 0x04 | |
dlt = cdll.LoadLibrary('libdlt.so.2') | |
dlt.dlt_register_app('APPY', 'APPY service') # DLT_REGISTER_APP(..) | |
appy_ctx = DltContext() # DLT_DECLARE_CONTEXT(mein_ctx) | |
dlt.dlt_register_context(byref(appy_ctx), 'APPY', 'Context for APPY') # DLT_REGISTER_CONTEXT(..) | |
dlt.dlt_log_string(byref(appy_ctx), LOG_LEVEL_INFO, message) | |
dlt.dlt_unregister_context(byref(appy_ctx)) | |
dlt.dlt_unregister_app() | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print("Usage: ./%s <log message>" % sys.argv[0]) | |
else: | |
main(' '.join(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment